Rotating back points from a rotated image in OpenCV

前端 未结 4 900
攒了一身酷
攒了一身酷 2020-12-17 16:43

I’m having troubles with rotation. What I want to do is this:

  • Rotate an image
  • Detect features on the rotated image (points)
  • Rotate back the p
4条回答
  •  既然无缘
    2020-12-17 17:05

    I had the same problem.

    For a transform M and point pp in the rotated image, we wish to find the point pp_org in the coordanates of the original image. Use the following lines:

    cv::Mat_ iM;
    cv::invertAffineTransform(M, iM);
    cv::Point2f pp_org = iM*pp;
    

    Where the operator * in the above line is defined as:

    cv::Point2f operator*(cv::Mat_ M, const cv::Point2f& p)
    { 
        cv::Mat_ src(3/*rows*/,1 /* cols */); 
    
        src(0,0)=p.x; 
        src(1,0)=p.y; 
        src(2,0)=1.0; 
    
        cv::Mat_ dst = M*src; //USE MATRIX ALGEBRA 
        return cv::Point2f(dst(0,0),dst(1,0)); 
    } 
    

    Note: M is the rotation matrix you used to go from the original to the rotated image

提交回复
热议问题