【问题标题】:get new location of cv::rect after rotation image by warpAffine (opencv , c++)通过warpAffine(opencv,c ++)旋转图像后获取cv :: rect的新位置
【发布时间】:2017-02-13 16:41:44
【问题描述】:

我想在使用以下代码旋转图像后获取 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));

我该怎么做?

【问题讨论】:

    标签: c++ opencv image-processing


    【解决方案1】:

    由于您有旋转矩阵,您可以使用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 保存了转换后矩形的点。

    ==>

    【讨论】:

      【解决方案2】:

      为了获得 cv::rect (ROI) 的新位置,您必须使用以下函数转换其每个角:

      cv::Point2f Convert(const cv::Point2f & p, const cv::Mat & t)
      {
          float x = p.x*t.at<double>((0, 0) + p.y*t.at<double>((0, 1) + t.at<double>((0, 2); 
          float y = p.x*t.at<double>((1, 0) + p.y*t.at<double>((1, 1) + t.at<double>((1, 2); 
          return cv::Point2f(x, y);
      }
      

      变换矩阵与您用于图像旋转的相同。

      【讨论】:

      • 谢谢我把你的函数改成如下: cv::Point2f Convert(const cv::Point2f & p, const cv::Mat & t) { float x = p.xt.at(0, 0) + p.yt.at(0, 1) + t.at(0, 2);浮动 y = p.xt.at(1, 0) + p.yt.at(1, 1) + t.at(1, 2);返回 cv::Point2f(x, y);并调用如下函数: cv::Point2f c1 = Convert(cv::Point2f(ROI.x,ROI.y),rot) ;但我得到非常大的浮点值(exp:-3.99357e+15)。怎么了?
      • 因为转换矩阵有double类型。
      猜你喜欢
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多