我想在旋转图像后使用以下代码获取 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));
我该怎么做?
最佳答案
因为您有旋转矩阵,您可以使用 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}
};
然后,您可以使用cv::transform
:
vector<Point2f> rot_roi_points;
transform(roi_points, rot_roi_points, rot);
这样,rot_roi_points
保存变换矩形的点。
==>
关于c++ - 通过 warpAffine(opencv,c++)在旋转图像后获取 cv::rect 的新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42197648/