Ask Your Question

Revision history [back]

That is quite straightforward if you have the outer points of your line, you can do it this way:

double angle = atan( (point1.y – point2.y) / (point1.x – point2.x) );
Point2f pt(face_region.cols/2, face_region.rows/2);
Mat rotation = getRotationMatrix2D(pt, angle, 1.0);
Mat rotated_face;
warpAffine(face_region, rotated_face, rotation, Size(face_region.cols, face_region.rows));

That is quite straightforward if you have the outer points of your line, you can do it this way:

double angle = atan( (point1.y – point2.y) / (point1.x – point2.x) );
Point2f pt(face_region.cols/2, face_region.rows/2);
Mat rotation = getRotationMatrix2D(pt, angle, 1.0);
Mat rotated_face;
warpAffine(face_region, rotated_face, rotation, Size(face_region.cols, face_region.rows));

Let me update with something I found on SO, right here.

If M is the rotation matrix you get from cv::getRotationMatrix2D, to rotate a cv::Point p with this matrix you can do this:

cv::Point result;
result.x = M.at<double>(0,0)*p.x + M.at<double>(0,1)*p.y + M.at<double>(0,2);
result.y = M.at<double>(1,0)*p.x + M.at<double>(1,1)*p.y + M.at<double>(1,2);

If you want to rotate a point back, generate the inverse matrix of M or use cv::getRotationMatrix2D(center, -rotateAngle, scale) to generate a matrix for reverse rotation.

--> for me that means that you can just replace M with the rotation matrix that we grabbed and then put in the original location of the points! Good luck!