以下是关于单应bookhomography-example-1.jpg
性bookhomography-example-2.jpg
的流行 OpenCV 博客文章。
我可以进行单应性和扭曲图像,但是当我尝试使用or时h
orh[0]
不起作用。我也尝试将二维数组转换为元组的元组,但没有变化。这可能很简单,但我无法弄清楚。cv2.perspectiveTransform(pts, h)
cv2.perspectiveTransform(pts, h[0])
h[0]
错误信息:
回溯(最近一次通话最后):
文件“bookhomography stackexchange v00.py”,第 36 行,在 T_dst = cv2.perspectiveTransform(pts_dst, h) TypeError: m is not a numeric tuple
注意:设置False
为True
诱导失败。两条变换线之一是错误的方向,但都失败了。
import numpy as np
import matplotlib.pyplot as plt
import cv2
im_src = cv2.imread("bookhomography-example-2.jpg")
im_dst = cv2.imread("bookhomography-example-1.jpg")
im_srcrgb = cv2.cvtColor(im_src, cv2.COLOR_BGR2RGB)
im_dstrgb = cv2.cvtColor(im_dst, cv2.COLOR_BGR2RGB)
pts_src = np.float32([52, 376, 240, 528, 413, 291, 217, 266]).reshape(4, -1)
pts_dst = np.float32([56, 478, 387, 497, 376, 124, 148, 218]).reshape(4, -1)
h = cv2.findHomography(pts_src, pts_dst)
print "type(h): ", type(h)
print "len(h): ", len(h)
print "type(h[0]): ", type(h[0])
print "len(h[0]): ", len(h[0])
print "h[0].shape: ", h[0].shape
shape = im_src.shape[:2][::-1]
print h[0]
print "pts_src:"
print pts_src
print "pts_dst:"
print pts_dst
if False:
T_dst = cv2.perspectiveTransform(pts_dst, h)
T_src = cv2.perspectiveTransform(pts_src, h)
print "T_src:"
print T_src
print "T_dst:"
print T_dst
im_fin = cv2.warpPerspective(im_src, h[0], shape)
im_finrgb = cv2.cvtColor(im_fin, cv2.COLOR_BGR2RGB)
plt.figure()
plt.subplot(1, 3, 1)
plt.imshow(im_srcrgb)
x, y = pts_src.T
plt.plot(x, y, 'or', ms=8)
plt.subplot(1, 3, 2)
plt.imshow(im_dstrgb)
x, y = pts_dst.T
plt.plot(x, y, 'or', ms=8)
plt.subplot(1, 3, 3)
plt.imshow(im_finrgb)
x, y = pts_dst.T
plt.plot(x, y, 'or', ms=8)
plt.show()