通过warpAffine(opencv,c ++)在旋转图像后获取cv :: rect的新位置 [英] get new location of cv::rect after rotation image by warpAffine (opencv , c++)

查看:823
本文介绍了通过warpAffine(opencv,c ++)在旋转图像后获取cv :: rect的新位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用以下代码旋转图像后,我想获得cv :: rect(ROI)的新位置:

I want to get new location of a cv::rect (ROI) after rotate the image by using the following code :

cv::Point2f center(image.cols/2.0, image.rows/2.0);

cv::Rect ROI = cv::Rect(100,200,50,100);

cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1.0);
cv::Rect bbox = cv::RotatedRect(center,image.size(), angle).boundingRect();

rot.at<double>(0,2) += bbox.width/2.0 - center.x;
rot.at<double>(1,2) += bbox.height/2.0 - center.y;


cv::warpAffine(image, image, rot, bbox.size(),cv::INTER_LINEAR,cv::BORDER_CONSTANT,
               cv::Scalar(255, 255, 255));

我该怎么做?

推荐答案

由于具有旋转矩阵,因此可以使用cv::transform函数旋转ROI矩形.首先,您需要该矩形的点数组.

Since you have the rotation matrix, you can rotate the ROI rectangle using cv::transform function. First of all, you would need an array of points of that rectangle.

vector<Point2f> roi_points = {
        {roi.x,             roi.y},
        {roi.x + roi.width, roi.y},
        {roi.x + roi.width, roi.y + roi.height},
        {roi.x,             roi.y + roi.height}
};

然后,您可以使用cv::transform:

vector<Point2f> rot_roi_points;
transform(roi_points, rot_roi_points, rot);

这样,rot_roi_points保留了变换后的矩形的点.

This way, rot_roi_points holds points of the transformed rectangle.

==>

这篇关于通过warpAffine(opencv,c ++)在旋转图像后获取cv :: rect的新位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆