简体   繁体   English

OpenCV如何旋转cv :: RotatedRect?

[英]OpenCV How to rotate cv::RotatedRect?

How do I apply some transformation (eg rotation) to a cv::rotatedRect ? 如何将一些转换(例如旋转)应用于cv::rotatedRect Tried using cv::warpAffine but won't work, as it is supposed to be applied to cv::Mat... 尝试使用cv::warpAffine但不起作用,因为它应该应用于cv :: Mat ...

You can control rotation translation and scale directly using the internal variables angle , center & size see documentation . 您可以使用内部变量anglecentersize直接控制旋转平移和缩放,请参阅文档

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) 更一般的转换需要使用points()来获取顶点并使用例如cv::warpAffine来操纵它们,但是一旦这样做,你将不再拥有cv::rotatedRect (根据定义)

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. 如果您正在计划执行affineperspective等复杂操作,则应该处理rotated rect的点,结果可能是四边形而不是矩形。

cv::warpAffine works for images. cv :: warpAffine适用于图像。 you should use cv::Transform and cv::Perspectivetransform 你应该使用cv :: Transformcv :: 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. TLDR:创建一个新矩形。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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