【发布时间】:2018-02-13 06:06:06
【问题描述】:
下面是bookhomography-example-1.jpg,然后是bookhomography-example-2.jpg,来自popular OpenCV blogpost,关于单应性。
我可以进行单应性和扭曲图像,但是当我尝试使用 cv2.perspectiveTransform(pts, h) 或 cv2.perspectiveTransform(pts, h[0]) 时,h 或 h[0] 不起作用。我还尝试将二维数组h[0] 转换为元组的元组,但没有变化。这可能很简单,但我想不通。
错误信息:
Traceback(最近一次通话最后一次):
文件“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()
【问题讨论】:
-
我以为有一个问题:“我可以进行单应性和扭曲图像,但是当我尝试使用
cv2.perspectiveTransform(pts, h)或cv2.perspectiveTransform(pts, h[0])时,h或h[0]不起作用。” 注意: 将False设置为True以诱导失败。 -
当您在
perspectiveTransform()中使用h或h[0]时会出现不同的错误消息,因此您应该同时包含这两个。 -
@AlexanderReynolds 好的,我现在就检查一下,以及另一个问题;再给我大约 10 分钟,谢谢!
-
别担心,我已经回答了。我只是让你知道:)
标签: python-2.7 opencv cv2