Opencv单应矩阵H和逆H变换一个点没有得到预期的结果

2024-05-19 10:22:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Opencv python接口,得到了单应矩阵H,它似乎工作正常,因为我可以使用warp透视图从源图像中获取扭曲图像。我现在尝试使用H和逆H在两个坐标之间来回变换一个点(不是图像),但没有得到预期的结果。

为了得到矩阵,我这样做了:

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

给定这个矩阵,我使用以下方法进行正向和反向变换:

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)

现在,如果我这样做:

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)

这就是它的指纹: a=395,b=169 c=91,d=226

我期望c=100和d=200,或者至少接近。

这是矩阵,它是逆矩阵。
H矩阵

[[ -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]]

反向:

[[  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]]

我试着做一个点积,它似乎生成了一个身份矩阵:

[[  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]]

如有任何帮助,我们将不胜感激。


Tags: py图像selftruetransform矩阵matrixtz

热门问题