3

I work on Windows7 x64 with opencv and Visual Studio 2010 on c++ language.

I created a project in which I show to my camera a rectangular area (call squared_surface). This area is recognized by tracing a rectangle with findSquare () and drawSquares () of opencv file squares.cpp. On this rectangle I create a ROI and there I copy an image (let's call copied_image)

My problem is that when I rotate squared_surface (in front of camera), copied_image does not follow it.

I think I need to use the functions getPerpective () and warpPerspective (), but I do not know how. Can anyone help me?

Here's the code:

int main(){
 vector<vector<Point> > squares;

 cv::VideoCapture cap(0);
 for (;;) {
    cv::Mat image;
    cap >> image;
    findSquares(image, squares);
    for (size_t i = 0; i < squares.size(); i++) {
    Rect rectangle = boundingRect(Mat(squares[i]));
                
    if((rectangle.width<=630)&& (rectangle.width >= 420) && (rectangle.height<= 490) &&(rectangle.height >= 250 )) {
        cv::Size dsize = Size(rectangle.width, rectangle.height);
        Mat img1 = imread("scacchiera.jpg");
    cv::resize(img1,img1,dsize,0,0, INTER_LINEAR);
        Rect roi (rectangle.x, rectangle.y,rectangle.width, rectangle.height);
        
    Mat imageRoi(image, roi);
       img1.copyTo(imageRoi);
       }
    } 
  drawSquares(image, squares);
  imshow("camera",image);
  if(waitKey(30) >= 0) break;
}
return 0;
}

enter image description here enter image description here

Thanks!

EDIT. I was thinking of rotating Copied_image, so it follows Squared_surface, but I need to calculate the angle of rotation of the rectangle identified by the camera (drawn in red in the above images). Is there a way to calculate this angle?

Or how can I do so that Copied_image follows Squared_surface when I rotate squared_surface?

Help me, please!

Community
  • 1
  • 1
Cristina1986
  • 505
  • 1
  • 8
  • 21
  • A Region of Interest is generally a left, right, top and bottom image co-ordinate -- not a rotated or skewed rectangle. I don't know OpenCV, but you should be able to just take the corners you detected and create a textured rectangle. – paddy Jan 20 '13 at 22:54
  • How can I create a textured rectangle? – Cristina1986 Jan 20 '13 at 23:02

1 Answers1

2

I think I found the bug. Rect rectangle = boundingRect(Mat(squares[i])); This is where the problem is. You are creating the variable rectangle as a bounding rectangle of the coordinates in squares[i]. So your code always tries to find out the bounding rectangle and not the actual rectangle.

Instead of using a bounding rectangle, try using a rotated rectangle. Here is how to use it: http://www710.univ-lyon1.fr/~eguillou/documentation/opencv2/classcv_1_1_rotated_rect.html

The rotated rectangle RotatedRect (const Point2f &_center, const Size2f &_size, float _angle) requires the center point location, floating point angle and the size. Since you have all the coordinates I think you can use basic math and trigonometry to calculate the center and the angle on how your rectangle should be rotated/orientated. Let me know if this helps.

Ashwath
  • 56
  • 3
  • Just to make things clear, these are few examples of bounding rectangles: http://www.austincc.edu/cchrist1/GAME1343/TransformedCollision/BoundingRectangles.png , http://edn.embarcadero.com/article/images/10277/D15PX16.jpg ... you are looking for the actual rectangle. Otherwise your code works perfectly! – Ashwath Jan 22 '13 at 01:47
  • Thanks! I thought I could use RotatedRect, but I don't know how to calculate the angle. Any suggestions? – Cristina1986 Jan 22 '13 at 11:54
  • From trigonometry, for any line between 2 points A and B whose coordinates are (x1,y1) and (x2,y2) respectively, angle of that line from the horizontal, is atan((y2-y1)/(x2-x1)); you can use this link as a reference for atan http://www.cplusplus.com/reference/cmath/atan/ – Ashwath Jan 22 '13 at 15:32
  • In your case, the angle for the rotatedrect will be the angle between line joining the bottom 2 points (or the top 2 points) and the horizontal. I believe you have all the points' coordinates in the squares[i] vector. – Ashwath Jan 22 '13 at 15:58