使用OpenCV + Python播放捕获的视频时出错

时间:2017-11-29 18:17:56

标签: python-3.x opencv

我在Ubuntu 16.04 LTS OS上使用Opencv 3.2 + Python。我试图取消从网络摄像头收集的鱼眼视频。

下面是我的代码,我通过逐帧失真来解决鱼眼视频的问题。该程序正在执行正常及其无失真的鱼眼视频,但当我尝试播放存储的未失真视频(undistortedop.avi)时,它会显示错误消息“无法解复用流”。

有人可以帮我解决这个问题吗?

Error

import cv2

import numpy as np

import os

import glob

import sys

assert float(cv2.__version__.rsplit('.', 1)[0]) >= 3, 'OpenCV version 3 or newer required.'

DIM=(1280, 720)

K=np.array([[517.7167401534203, 0.0, 641.312338873659], [0.0, 518.0410707880329, 361.1273127787553], [0.0, 0.0, 1.0]])

D=np.array([[-0.00428080929837007], [-0.14786471866085527], [0.07941291495275071], [-0.025649243686649097]])

balance=0.95

dim2=None

dim3=None

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object

fourcc = cv2.VideoWriter_fourcc(*'DIVX')

out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

out1 = cv2.VideoWriter('undistortedop.avi',fourcc, 20.0, (640,480))

while True:

   ret, frame = cap.read()

   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

   # img = cv2.imread(img_path)

   # dim1 = img.shape[:2][::-1]  #dim1 is the dimension of input image to un-distort

   dim1=(1280, 720)

   assert dim1[0]/dim1[1] == DIM[0]/DIM[1], "Image to undistort needs to have same aspect ratio as the ones used in calibration"

   if not dim2:

       dim2 = dim1

   if not dim3:

       dim3 = dim1

   dim3=(630, 480)

   scaled_K = K * dim1[0] / DIM[0]  # The values of K is to scale with image dimension.

   scaled_K[2][2] = 1.0  # Except that K[2][2] is always 1.0

   # This is how scaled_K, dim2 and balance are used to determine the final K used to un-distort image. OpenCV document failed to make this clear!

   new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(scaled_K, D, dim2, np.eye(3), balance=balance)

   map1, map2 = cv2.fisheye.initUndistortRectifyMap(scaled_K, D, np.eye(3), new_K, dim3, cv2.CV_16SC2)

   undistorted_img = cv2.remap(frame, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)

   # cv2.imwrite('Testpic3_undistorted.jpg', undistorted_img)

   out1.write(undistorted_img)

   cv2.imshow("undistorted", undistorted_img)


   # Write the frame

   out.write(frame)

   cv2.imshow('frame',frame)

   # cv2.imshow('gray', gray)

   if cv2.waitKey(1) & 0xFF == ord('q'):

       break

cap.release()

out.release()

out1.release()

cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

由于VideoWriter中的帧大小不匹配,可能会发生此错误。

out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))