OpenCV Python cv2.perspectiveTransform
问题内容:
我目前正在尝试使用OpenCV和Python进行视频稳定化。我使用以下函数来计算旋转:
def accumulate_rotation(src, theta_x, theta_y, theta_z, timestamps, prev, current, f, gyro_delay=None, gyro_drift=None, shutter_duration=None):
if prev == current:
return src
pts = []
pts_transformed = []
for x in range(10):
current_row = []
current_row_transformed = []
pixel_x = x * (src.shape[1] / 10)
for y in range(10):
pixel_y = y * (src.shape[0] / 10)
current_row.append([pixel_x, pixel_y])
if shutter_duration:
y_timestamp = current + shutter_duration * (pixel_y - src.shape[0] / 2)
else:
y_timestamp = current
transform = getAccumulatedRotation(src.shape[1], src.shape[0], theta_x, theta_y, theta_z, timestamps, prev,
current, f, gyro_delay, gyro_drift)
output = cv2.perspectiveTransform(np.array([[pixel_x, pixel_y]], dtype="float32"), transform)
current_row_transformed.append(output)
pts.append(current_row)
pts_transformed.append(current_row_transformed)
o = utilities.meshwarp(src, pts_transformed)
return o
我遇到以下错误output = cv2.perspectiveTransform(np.array([[pixel_x, pixel_y]], dtype="float32"), transform)
:
cv2.error: /Users/travis/build/skvark/opencv- python/opencv/modules/core/src/matmul.cpp:2271: error: (-215) scn + 1 == m.cols in function perspectiveTransform
任何帮助或建议,将不胜感激。
问题答案:
确实需要在将来的版本中更改此实现。
从OpenCV文档中获得perspectiveTransform()
:
src –输入 两通道 (…)浮点数组
我偏重强调。
>>> A = np.array([[0, 0]], dtype=np.float32)
>>> A.shape
(1, 2)
因此,从这里我们看到的A
只是一个单通道矩阵,即二维矩阵。一排,两列。相反,您需要一个 两通道
图像,即一个三维矩阵,其中三维尺寸的长度为2或3,具体取决于您是以2D还是3D点进行发送。
简而言之,您需要再添加一组括号,以三维方式发送要发送的点集,其中x
值在第一个通道中,而y
值在第二个通道中。
>>> A = np.array([[[0, 0]]], dtype=np.float32)
>>> A.shape
(1, 1, 2)
它不是直观的,尽管有文档记录,但在这一点上不是很明确。这就是您所需要的。之前,我已经回答了一个相同的问题,但cv2.transform()
功能方面。