The image copied to ROI does not follow the C ++ camera. How to fix it?

I am working on Windows7 x64 with opencv and Visual Studio 2010 in C ++ language.

I have created a project in which I show a rectangular area to my camera ( squared_surface call ). This area is recognized by tracing rectangle c findSquare ()

and drawSquares ()

opencv squares.cpp

. On this rectangle I create an ROI and there I copy the image (call copied_image)

My problem is that when I rotate the squared_surface (in front of the camera) the copied_image doesn't follow it.

I think I need to use functions getPerpective ()

and warpPerspective ()

, but I don't know how to do it. 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 hereenter image description here

Thank!

EDIT. I was thinking about rotating Copied_image so it follows Squared_surface , but I need to calculate the rotation angle 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 make Copied_image follow the Squared_surface when I rotate the square_surface?

Help me please!

+3


source to share


1 answer


I think I found a bug. Rect rectangle = boundingRect(Mat(squares[i]));

Here's the problem. You create a variable rectangle

as a bounding rectangle of coordinates in squares[i]

. Therefore your code is always trying to find the bounding rectangle and not the actual rectangle.

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



A rotating rectangle RotatedRect (const Point2f &_center, const Size2f &_size, float _angle)

requires a center point, a floating point angle, and a size. Since you have all the coordinates, I think you can use basic math and trigonometry to calculate the center and angle on how your rectangle should rotate / orientate. Let me know if it helps.

+2


source







All Articles