我正在使用Opencv python接口并获得单应矩阵H.它似乎正常工作,因为我可以使用warp透视来从源图像中获取扭曲图像.我现在尝试使用H和In H来在两个坐标之间来回转换点(而不是图像)并且没有得到预期的结果.
为了得到矩阵,我这样做了:
pts1 = np.float32(corners)
pts2 = np.float32([[0,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]]
任何帮助表示赞赏.
最佳答案
你在第二行有一个拼写错误
tx = (h0*x + h1*y + h2)
ty = (h3*x + h4*x + h5)
tz = (h6*x + h7*y + h8)
h4应与y坐标相乘