Ask Your Question

Revision history [back]

A piece of code I am using for backprojecting detection rectangles can be seen below. I think you will be able to apply this to your needs.

// Use a rectangle representation on the frame but warp back the coordinates
// Retrieve the 4 corners detected in the rotation image
Point p1 ( temp[j].x, temp[j].y ); // Top left
Point p2 ( (temp[j].x + temp[j].width), temp[j].y ); // Top right
Point p3 ( (temp[j].x + temp[j].width), (temp[j].y + temp[j].height) ); // Down right
Point p4 ( temp[j].x, (temp[j].y + temp[j].height) ); // Down left

// Add the 4 points to a matrix structure
Mat coordinates = (Mat_<double>(3,4) << p1.x, p2.x, p3.x, p4.x,\
                                        p1.y, p2.y, p3.y, p4.y,\
                                        1   , 1  ,  1   , 1    );

// Apply a new inverse tranformation matrix
Point2f pt(frame.cols/2., frame.rows/2.);
Mat r = getRotationMatrix2D(pt, -(degree_step*(i+1)), 1.0);
Mat result = r * coordinates;

// Retrieve the ew coordinates from the tranformed matrix
Point p1_back, p2_back, p3_back, p4_back;
p1_back.x=(int)result.at<double>(0,0);
p1_back.y=(int)result.at<double>(1,0);

p2_back.x=(int)result.at<double>(0,1);
p2_back.y=(int)result.at<double>(1,1);

p3_back.x=(int)result.at<double>(0,2);
p3_back.y=(int)result.at<double>(1,2);

p4_back.x=(int)result.at<double>(0,3);
p4_back.y=(int)result.at<double>(1,3);

// Draw a rotated rectangle by lines, using the reverse warped points
line(frame, p1_back, p2_back, color, 2);
line(frame, p2_back, p3_back, color, 2);
line(frame, p3_back, p4_back, color, 2);
line(frame, p4_back, p1_back, color, 2);

This works for detecting rotated faces for me and then transforming them back to the original, non-rotated image.

Good luck!