2

I get that when I apply warpPerspective() again with inverse of transformation matrix, I obtain first image:

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))

However, I apply some operations to warped and I get some locations on the image (like a rectangle). How am I going to find corresponding coordinates of the rectangle on the restored image?

Answers in both python and C++ are appreciated.

Ekrem Doğan
  • 674
  • 6
  • 13

2 Answers2

2
M = cv2.getPerspectiveTransform(pts1,pts2)

With this matrix you obtain 3x3 transformation matrix, which includes rotation, scaling, translation, projection vector.

By applying warpPerspective you are moving from source coordinate system (frame {A}) to destination coordinate system (frame {B}).

To move back from frame {B} to frame {A}, you need to multiply inverse of the matrix M with point P in frame {B}. As matrix is 3x3 matrix, you need to add z = 1 to your point P to make it 3x1 matrix.

_, 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
CVRP_2021
  • 31
  • 3
0

You should be able to simply pass the coordinates of your rectangle as input array to the warpPerspective function, to get the corresponding transformed points. For instance (sample code in C++ with std::vector):

//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);

Observe that if you need to apply the transformation M on the src image to get the dst image, you need to set the flag WARP_INVERSE_MAP.

Hope this can help you

Marco Ferro
  • 386
  • 4
  • 18