我想找到變換矩陣H,這樣我就可以乘以(x,y)像素座標並獲得(x,y)真實世界座標。這是我的代碼:Opencv單應性從像素xy座標找到全局xy座標
import cv2
import numpy as np
from numpy.linalg import inv
if __name__ == '__main__' :
D=[159.1,34.2]
I=[497.3,37.5]
G=[639.3,479.7]
A=[0,478.2]
# Read source image.
im_src = cv2.imread('/home/vivek/june_14.png')
# Four corners of the book in source image
pts_src = np.array([D,I,G,A])
# Read destination image.
im_dst = cv2.imread('/home/vivek/june_14.png')
# Four corners of the book in destination image.
print "img1 shape:",im_dst.shape
scale=1
O=[0.0,0.0]
X=[134.0*scale,0]
Y=[0.0,184.0*scale]
P=[134.0*scale,184.0*scale]
# lx = 75.5 * scale
# ly = 154.0 * scale
pts_dst = np.array([O,X,P,Y])
# Calculate Homography
h, status = cv2.findHomography(pts_src, pts_dst)
print "homography:",h
print "inv of H:",inv(h)
print "position of the blob on the ground xy plane:",np.dot(np.dot(h,np.array([[323.0],[120.0],[1.0]])),scale)
# Warp source image to destination based on homography
im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))
# Display images
cv2.imshow("Source Image", im_src)
cv2.imshow("Destination Image", im_dst)
cv2.imshow("Warped Source Image", im_out)
cv2.imwrite("im_out.jpg", im_out)
cv2.waitKey(0)
我得到的全球xy是非常關閉。我在某處做錯了什麼?
對不起,但是變量'D,I,G,A,O,X,P,Y'是什麼?這些應該代表什麼?無論如何,在你計算「真實世界」(x,y)座標的地方,你將得到*均勻*點,它們在縮放時是等價的 - 換句話說,它們可以被縮放並且仍然會被考慮同樣的觀點。但是你需要'x,y'點,而不是縮放的點,所以你需要按比例分割。三矢量全部按照相同的量進行縮放,因此您可以使用縮放因子的最後一個條目。你應該做'pts = scale * np.dot(h,np.array([[323.0],[120.0],[1.0]]))'然後'pts = pts/pts [-1]'。 –
OXPY是真實的單詞點(O原點,右側X-134英寸,右側P-134英寸,向下184英寸,Y-184英寸)和DIGA是圖像平面上的相應像素座標。 –
對不起。我沒有得到比例縮放部分。 –