简体   繁体   English

在opencv中裁剪旋转的图像

[英]Cropping a rotated image in opencv

Ok so I am having problems cropping my rotated image in opencv. 好的,所以我在opencv中裁剪旋转的图像时遇到问题。 First off does it matter what order the points are in the array fed into minAreaRect? 首先,将点在数组中馈入minAreaRect的顺序有关系吗? If not then I don't know what is wrong with my code but as soon as I crop the rotated image the cropped image is just a black square. 如果不是,那么我不知道我的代码有什么问题,但是一旦我裁剪了旋转的图像,裁剪的图像就只是一个黑色正方形。 I assume because my box is wrong somehow. 我认为是因为我的盒子有误。 Anyway here is my code. 无论如何,这是我的代码。 Edit: my method was fine the problem is my points for the roi are wrong. 编辑:我的方法很好,问题是我对投资回报率的观点有误。

                int len = max(ImageAd.cols, ImageAd.rows);
            cv::Point2f pt(len/2., len/2.);
            cv:: Mat r = cv::getRotationMatrix2D(pt, -angled, 1.0);
            cv::warpAffine(ImageAd, ImageAd, r, ImageAd.size(), INTER_CUBIC);
            vector<cv::Point> points;
            points.push_back(Pt3);
            points.push_back(Pt4);
            points.push_back(Pt1);
            points.push_back(Pt2);

            cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
            cv::Size box_size = box.size;

            cv::getRectSubPix(ImageAd, box_size, box.center, ImageAd);
            cv::imshow("cropped", ImageAd);

Honestly I never used getRectSubPix so I don't know exactly what is wrong but.. why do just not take a roi? 老实说,我从来没有使用过getRectSubPix,所以我不知道到底是什么问题,但是..为什么不花钱呢? the C++ API with cv::Mat class are very handy for this.. 具有cv::Mat类的C ++ API对此非常方便。

Here is a documentation , you can take a ROI (that is a Region of Interest if you don't know it yet) with the overloaded function call operator of cv::Mat 这是一个文档 ,您可以使用cv::Mat的重载函数调用运算符获取ROI(如果您还不了解,则为感兴趣的区域)。

Anyway here is a snippet for take ROI 无论如何,这是获取ROI的代码段

cv::Mat img = …;
cv::Mat subImg = img(cv::Rect(0, 0, 100, 100));
cv::Mat subImg = img(cv::Range(0, 0), cv::Range(100, 100));

You have a cv::RotatedRect so you can directly use the RotatedRect::boundingRect property. 您有一个cv::RotatedRect因此您可以直接使用RotatedRect::boundingRect属性。

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

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