【发布时间】:2017-11-18 14:51:15
【问题描述】:
我试图找到变换矩阵 H,以便我可以乘以 (x,y) 像素坐标并获得 (x,y) 真实世界坐标。这是我的代码:
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)坐标的地方,您将得到 homogenous 点,它们在缩放时是等效的——换句话说,它们可能会被缩放并且仍然是认为同一点。但是您需要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 是各自的像素图像平面上的坐标。
-
对不起。我没有完全理解缩放部分。
-
我想我明白你的意思了。让我检查一下我是否得到了正确的值。
-
快速试用代码,看看它是否能解决您的问题——如果能,我会添加更详细的答案。基本上,
[x', y', 1] = H*[x, y, 1]是不正确的;它是[sx', sy', s] = H*[x, y, 1]。但是你需要以1结尾的齐次点等于x,y坐标,所以你需要从齐次坐标中除以s。
标签: python opencv homography