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

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

我该怎么做?

最佳答案

因为您有旋转矩阵,您可以使用 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 保存变换矩形的点。

enter image description here ==> enter image description here

关于c++ - 通过 warpAffine(opencv,c++)在旋转图像后获取 cv::rect 的新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42197648/

相关文章:

c++ - Lua C API : what's the difference between lua_gettop() and -1?

c++ - Pthread 模板参数错误

opencv - BeagleBone、OpenCV 和网络摄像头问题

python - Anaconda OpenCV Arch Linux libselinux.so 错误

image-processing - 图像处理: How to get luminance weighting value?

image-processing - 使用 VIPS 命令行将图像切割成图 block

C++ 参数更改为垃圾,即使使用常量调用也是如此

c++ - 这个预定义函数降低了我程序的性能

android - 在 OpenCV Android 中访问相机帧像素

perl - ImageMagick:寻找一种快速模糊图像的方法