【问题标题】:How to reverse warpPerspective()如何反转 warpPerspective()
【发布时间】:2016-01-25 09:51:33
【问题描述】:

当我再次使用变换矩阵的逆矩阵应用 warpPerspective() 时,我得到了第一张图像:

M = cv2.getPerspectiveTransform(pts1,pts2)
warped = cv2.warpPerspective(img, M, (cols,rows))

# ... some operations, obtain a rectangle

ret, IM = cv2.invert(M)
restored = cv2.warpPerspective(warped, IM, (cols,rows))

但是,我对warped 应用了一些操作,并在图像上得到了一些位置(如矩形)。如何在restored图像上找到矩形的对应坐标?

感谢 Python 和 C++ 的答案。

【问题讨论】:

    标签: python c++ opencv matrix linear-algebra


    【解决方案1】:
    M = cv2.getPerspectiveTransform(pts1,pts2)
    

    有了这个矩阵,你就得到了3x3的变换矩阵,包括旋转、缩放、平移、投影向量。

    通过应用warpPerspective,您正在从源坐标系({A} 框架)移动到目标坐标系({B} 框架)。

    要从帧 {B} 移回帧 {A},您需要将矩阵 M 的逆矩阵与帧 {B} 中的点 P 相乘。由于矩阵是 3x3 矩阵,您需要将 z = 1 添加到您的点 P 以使其成为 3x1 矩阵。

    _, IM = cv2.invert(M)
    x1 = 159
    y1 = 99
    coord = [x1, y1] + [1]
    P = np.float32(coord)
    
    x, y, z = np.dot(IM, P)
    #Divide x and y by z to get 2D in frame {A}
    new_x = x/z
    new_y = y/z
    

    【讨论】:

      【解决方案2】:

      您应该能够简单地将矩形的坐标作为输入数组传递给warpPerspective 函数,以获取相应的转换点。例如(带有 std::vector 的 C++ 示例代码):

      //r1,r2,r3,r4 are the four points of your rectangle in the source image
      std::vector<Point2f> rectangle_vertexes;
      rectangle_vertexes.push_back(r1);
      rectangle_vertexes.push_back(r2);
      rectangle_vertexes.push_back(r3);
      rectangle_vertexes.push_back(r4);
      Mat transformed_vertexes;
      warpPerspective(Mat(rectangle_vertexes),transformed_vertexes,IM,Size(1,4),WARP_INVERSE_MAP);
      

      请注意,如果您需要对src 图像应用转换M 以获得dst 图像,则需要设置标志WARP_INVERSE_MAP

      希望对你有帮助

      【讨论】:

      • 我得到 [0, 0; 0, 0; 0, 0; 0, 0] 代表transformed_vertexes
      • 你的矩形的原始坐标值是多少?你的矩阵 IM 的价值是多少?
      猜你喜欢
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2017-03-24
      • 1970-01-01
      • 2013-01-19
      • 2014-03-26
      相关资源
      最近更新 更多