我目前正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
它到达时会出现以下错误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)
Run Code Online (Sandbox Code Playgroud)
所以我们从这里看到A的只是一个单通道矩阵,即二维矩阵.一排,两排.您需要一个双通道图像,即三维矩阵,其中第三维的长度为2或3,具体取决于您是在2D或3D点发送.
简而言之,你需要再添加一组括号来制作你在三维中发送的点集,其中x值在第一个通道中,y值在第二个通道中.
>>> A = np.array([[[0, 0]]], dtype=np.float32)
>>> A.shape
(1, 1, 2)
Run Code Online (Sandbox Code Playgroud)
它并不直观,虽然它已被记录,但在这一点上并不是很明确.这就是你所需要的.我以前回答了一个相同的问题,但是对于这个cv2.transform()功能.