Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv Homography matrix H and Inverse H to transform a point is not getting expected results

I am using Opencv python interface and got the homography matrix H. It seems to work properly as I can use warp perspective to get warped image from the source image. I now tried to use H and Inverse H to transform a point (not image) back and forth between the two coordinates and is not getting the expected results.

To get the matrix, I did this:

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 do a forward and inverse transform:

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 was expecting c=100 and d=200, or at least something close.

This is the matrix and it's 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]]

I tried to do a dot product, and it seems to generate an identity matrix 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]]

Any help is appreciated.

like image 482
Fuithecat Avatar asked Oct 19 '22 03:10

Fuithecat


1 Answers

You have a typo in second line

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

h4 should be multiplied with y coordinate

like image 139
alexisrozhkov Avatar answered Oct 22 '22 00:10

alexisrozhkov