cv2.perspectiveTransform()不接受单应性矩阵

uho*_*hoh 2 opencv python-2.7 cv2

以下是bookhomography-example-1.jpgbookhomography-example-2.jpg流行的OpenCV博文约单应。

我可以做单应性和扭曲图像,但是当我尝试使用或时,hh[0]不起作用。我也尝试过将2D数组转换为元组的元组,但是没有任何变化。这可能很简单,但我无法弄清楚。cv2.perspectiveTransform(pts, h)cv2.perspectiveTransform(pts, h[0])h[0]

错误信息:

追溯(最近一次通话):

T_dst = cv2.perspectiveTransform(pts_dst,h)中的文件“ bookhomography stackexchange v00.py”,第36行,TypeError:m不是数字元组

注:设置FalseTrue诱发失败。两条变换线之一是方向错误,但均会失败。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

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()
Run Code Online (Sandbox Code Playgroud)

alk*_*asm 5

在此处查看我的答案以获取快速修复。TL:DR; OpenCV函数perspectiveTransform()采用奇数格式指定的点,而findHomography()可以使用您拥有的格式。


首先注意findHomography()返回两个值;单应矩阵,以及一个mask。从文档

cv2.findHomography(srcPoints, dstPoints[, method[, ransacReprojThreshold[, mask]]]) ? retval, mask

第二个返回值不是单应性,因此h[0]应使用为什么。或者,您可以编写:

h, mask = cv2.findHomography(srcPoints, dstPoints)
Run Code Online (Sandbox Code Playgroud)

要么

h = cv2.findHomography(srcPoints, dstPoints)[0]
Run Code Online (Sandbox Code Playgroud)

因此h 保留单应性以减少混乱。并请注意,使用hh[0]按您指定的方式会给您带来不同的错误消息:

使用h

>>> T_dst = cv2.perspectiveTransform(pts_dst, h)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: m is not a numerical tuple
Run Code Online (Sandbox Code Playgroud)

使用h[0]

>>> T_dst = cv2.perspectiveTransform(pts_dst, h[0])
OpenCV Error: Assertion failed (scn + 1 == m.cols) in perspectiveTransform, file .../opencv/modules/core/src/matmul.cpp, line 2299
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: .../opencv/modules/core/src/matmul.cpp:2299: error: (-215) scn + 1 == m.cols in function perspectiveTransform
Run Code Online (Sandbox Code Playgroud)

不幸的是,错误消息对您并没有真正的帮助,因为实际的问题是指定点的方式。从技术上讲,这是用户错误,但是文档(以及函数本身)可能应该更改。


无论如何,解决方法:将通道添加到引导程序pts。看到不同:

>>> np.float32([52, 376, 240, 528, 413, 291, 217, 266]).reshape(4, -1)
array([[  52.,  376.],
       [ 240.,  528.],
       [ 413.,  291.],
       [ 217.,  266.]], dtype=float32)
>>> np.float32([52, 376, 240, 528, 413, 291, 217, 266]).reshape(4, 1, -1)
array([[[  52.,  376.]],

       [[ 240.,  528.]],

       [[ 413.,  291.]],

       [[ 217.,  266.]]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)

幸运的是,findHomography()该格式也可以使用,因此您不必根据所使用的功能使用两种不同的格式。为了安全起见,请始终为OpenCV函数以这种格式放置点。

>>> pts_src = np.float32([52, 376, 240, 528, 413, 291, 217, 266]).reshape(4, 1, -1)
>>> pts_dst = np.float32([56, 478, 387, 497, 376, 124, 148, 218]).reshape(4, 1, -1)
>>> h = cv2.findHomography(pts_src, pts_dst)[0]
>>> T_dst = cv2.perspectiveTransform(pts_dst, h)
>>> T_src = cv2.perspectiveTransform(pts_src, h)
>>> T_src
array([[[  56.,  478.]],

       [[ 387.,  497.]],

       [[ 376.,  124.]],

       [[ 148.,  218.]]], dtype=float32)
>>> T_dst
array([[[ 157.78089905,  588.9598999 ]],

       [[ 495.96539307,  365.68994141]],

       [[ 200.45231628,  -69.54611206]],

       [[  15.72697926,  204.0632019 ]]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)

上面没有错误。