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:错误:(-215)scn 1 == m.cols in function perspectiveTransform

任何帮助或建议将非常感激.

最佳答案 这个实现确实需要在将来的版本中进行更改.

来自perspectiveTransform()的OpenCV文档:

src – input two-channel (…) floating-point array

我强调了倾斜的重点.

>>> 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)

它并不直观,虽然它已被记录,但在这一点上并不是很明确.这就是你所需要的.我已回答了identical question before,但是对于cv2.transform()函数.

点赞