Opencv Homography matrix H and inverse H to transform points do not get the expected results

I use the Opencv python interface and got the homography matrix H. It seems to work correctly, since I can use the warp perspective to get the distorted image from the original image. Now I tried to use H and Inverse H to convert a point (not an image) back and forth between two coordinates and not getting the expected results.

To get the matrix, I did the following:

pts1 = np.float32(corners)
pts2 = np.float32([[0,0], [400,0], [400,400], [0,400]])
self.transform_matrix = cv2.getPerspectiveTransform(pts1, pts2)

Given this matrix, I use the following to convert back and forth:

def transformPoints(self, x, y, reverse=False, integer=True):

        if reverse == False:
            H = self.transform_matrix
        else:
            val, H = cv2.invert(self.transform_matrix)

        # get the elements in the transform matrix
        h0 = H[0,0]
        h1 = H[0,1]
        h2 = H[0,2]
        h3 = H[1,0]
        h4 = H[1,1]
        h5 = H[1,2]
        h6 = H[2,0]
        h7 = H[2,1]
        h8 = H[2,2]

        tx = (h0*x + h1*y + h2)
        ty = (h3*x + h4*x + h5)
        tz = (h6*x + h7*y + h8)

        if integer==True:
            px = int(tx/tz)
            py = int(ty/tz)
            Z = int(1/tz)
        else:
            px = tx/tz
            py = ty/tz
            Z = 1/tz

        return (px, py)

Now if I do this:

s, t = 100,200
print "s=%d, t=%d" % (s,t)
a, b = pt.transformPoints(s,t)
print "a=%d, b=%d" % (a,b)

c, d = pt.transformPoints(a, b, True)
print "c=%d, d=%d" % (c,d)

This is what it prints: a = 395, b = 169 c = 91, d = 226

I expected c = 100 and d = 200, or at least something close.

This is a matrix and it is inverse. H matrix

[[ -1.01486350e-01  -1.99156329e+01   8.44058060e+02]
 [  1.82486862e+00   3.62765073e-01  -1.49259809e+03]
 [ -4.43678849e-03  -4.28012674e-02   1.00000000e+00]]

Inverse:

[[  4.13378829e-01   1.05495739e-01  -1.91452995e+02]
 [ -3.12201095e-02  -2.37099792e-02  -9.03788455e+00]
 [  4.97814178e-04  -5.46754880e-04  -2.36269358e-01]]

, , , ok:

[[  1.00000000e+00   1.77635684e-15  -5.68434189e-14]
 [ -6.93889390e-18   1.00000000e+00   5.32907052e-15]
 [ -2.16840434e-19   1.73472348e-18   1.00000000e+00]]

.

+4
1

tx = (h0*x + h1*y + h2) 
ty = (h3*x + h4*x + h5) 
tz = (h6*x + h7*y + h8)

h4 y

+3

Source: https://habr.com/ru/post/1605670/


All Articles