简体   繁体   中英

OpenCV How to rotate cv::RotatedRect?

How do I apply some transformation (eg rotation) to a cv::rotatedRect ? Tried using cv::warpAffine but won't work, as it is supposed to be applied to cv::Mat...

You can control rotation translation and scale directly using the internal variables angle , center & size see documentation .

More general transformations requires getting the vertices using points() and manipulating them using for example cv::warpAffine but once doing that you will no longer have a cv::rotatedRect (by definition)

If you are planing to do complex operations like affine or perspective , you should deal with the points of the rotated rect and the result may be quad shape not a rectangle.

cv::warpAffine works for images. you should use cv::Transform and cv::Perspectivetransform

They take array of points and produced array of points.

Example:

cv::RotatedRect rect;
//fill rect somehow
cv::Point2f rect_corners[4];
rect.points(rect_corners);
std::vector<cv::Point2f> rect_corners_transformed(4);
cv::Mat M;
//fill M with affine transformation matrix
cv::transform(std::vector<cv::Point2f>(std::begin(rect_corners), std::end(rect_corners)), rect_corners_transformed, M);
// your transformed points are in rect_corners_transformed

TLDR: Create a new rectangle.

I don't know if it will help you, but I solved a similar problem by creating a new rectangle and ignoring the old one. In other words, I calculated the new angle, and then assigned it and the values of the old rectangle (the center point and the size) to the new rectangle:

RotatedRect newRotatedRectangle(oldRectangle.center, oldRectangle.size, newAngle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM