我的解决方案是计算结果图像的大小,然后进行翻译。
def warpTwoImages(img1, img2, H):
'''warp img2 to img1 with homograph H'''
h1,w1 = img1.shape[:2]
h2,w2 = img2.shape[:2]
pts1 = float32([[0,0],[0,h1],[w1,h1],[w1,0]]).reshape(-1,1,2)
pts2 = float32([[0,0],[0,h2],[w2,h2],[w2,0]]).reshape(-1,1,2)
pts2_ = cv2.perspectiveTransform(pts2, H)
pts = concatenate((pts1, pts2_), axis=0)
[xmin, ymin] = int32(pts.min(axis=0).ravel() - 0.5)
[xmax, ymax] = int32(pts.max(axis=0).ravel() + 0.5)
t = [-xmin,-ymin]
Ht = array([[1,0,t[0]],[0,1,t[1]],[0,0,1]]) # translate
result = cv2.warpPerspective(img2, Ht.dot(H), (xmax-xmin, ymax-ymin))
result[t[1]:h1+t[1],t[0]:w1+t[0]] = img1
return result
dst_pts = float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2)
src_pts = float32([kp2[m.trainIdx].pt for m in good]).reshape(-1,1,2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
result = warpTwoImages(img1_color, img2_color, M)
0
我这里有2张测试图像。我的问题是,如何在不裁剪图像的情况下将第一个图像中的正方形映射到第二个图像中的四边形。
图片1
图片2:
这是我当前使用openCV warpPerspective函数的代码。
结果:
如您所见,由于warpPerspective函数中的dsize =(800,800)参数,我无法获得图像1的完整视图。如果调整dsize,则正方形将无法正确映射。有什么方法可以调整输出图像的大小,以便获得图像1的全图?