通过warpAffine(opencv,c ++)在旋转图像后获取cv :: rect的新位置

Pou*_*and 1 c++ opencv image-processing

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

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));
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

fro*_*tto 5

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

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}
};
Run Code Online (Sandbox Code Playgroud)

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

vector<Point2f> rot_roi_points;
transform(roi_points, rot_roi_points, rot);
Run Code Online (Sandbox Code Playgroud)

这样,可以rot_roi_points保存变换后的矩形的点。

在此处输入图片说明 ==> 在此处输入图片说明