Opencv仿射和透射变换的坐标变换

OpenCV里面通过getAffineTransform或者findTransformECC获取变形矩阵的饿时候会得到一个2x3或者3x3的变形矩阵,分别对应仿射矩阵和透视矩阵.

变形的时候如果想要算出原始图像坐标到目标图像的坐标的时候,可以使用下面方法计算

  1. if (matrixSize == 3)
  2. {
  3. //计算透视变换坐标
  4. double w= warp_matrix.at<float>(2, 0)*orgX + warp_matrix.at<float>(2, 1)*orgY + warp_matrix.at<float>(2, 2);
  5.  
  6. targetX = (warp_matrix.at<float>(0, 0)*orgX + warp_matrix.at<float>(0, 1)*orgY + warp_matrix.at<float>(0, 2))/w;
  7. targetY = (warp_matrix.at<float>(1, 0)*orgX + warp_matrix.at<float>(1, 1)*orgY + warp_matrix.at<float>(1, 2))/w;
  8. }
  9. else {
  10. //计算仿射变换坐标
  11. targetX = warp_matrix.at<float>(0, 0)*orgX + warp_matrix.at<float>(0, 1)*orgY + warp_matrix.at<float>(0, 2);
  12. targetY = warp_matrix.at<float>(1, 0)*orgX + warp_matrix.at<float>(1, 1)*orgY + warp_matrix.at<float>(1, 2);
  13. }