12

I get an H matrix using findHomography function.

H = findHomography(points_src, points_dst);

Then, I use H with warpPerspective to get a perspective projection of the image

warpPerspective(im_src, im_dst, H, Size(100, 100));

and from here I get a set of points of interest

vector<Point2f> points = some_function(im_dst)

Now I want to get back only the set of "points" to the original image and in this way I will now the set of points of interest in the original image.

For this task, I guess that I need to use again warpPerspective with the flag WARP_INVERSE_MAP but this does not work.

2 Answers2

20

You could indeed use findHomography() once again, but thanks to the composition properties of such matrix, a much more elegant, accurate and fast way to reverse the transformation is to simply inverse the homography matrix.

Here is a little demonstration using Python:

import cv2
import numpy as np

source = np.float32([[0, 0], [100, 0], [100, 100], [0, 100]])
dest = np.float32([[0, 0], [-1000, 0], [-1000, -1000], [0, -1000]])

points = np.float32([[[50, 50]]])

homography, _ = cv2.findHomography(source, dest)

transformed = cv2.perspectiveTransform(points, homography)

print(transformed)
# => [[[-500. -500.]]]

homography_inverse = np.linalg.inv(homography)

detransformed = cv2.perspectiveTransform(transformed, homography_inverse)

print(detransformed)
# => [[[50. 50.]]]
Delgan
  • 18,571
  • 11
  • 90
  • 141
7

To find the inverse homography, simply switch points_dst and points_src:

H_inv= findHomography(points_dst, points_src);

Then if you apply the H_inv to the points trandformed by H, you will obtain the original points.

cerebrou
  • 5,353
  • 15
  • 48
  • 80