Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse Homography

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.

like image 220
Pavel Angel Mendoza Villafane Avatar asked Feb 22 '17 16:02

Pavel Angel Mendoza Villafane


Video Answer


1 Answers

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.]]]
like image 182
Delgan Avatar answered Sep 16 '22 22:09

Delgan