Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用OpenCV+;播放捕获的视频时出错;python_Python 3.x_Opencv - Fatal编程技术网

Python 3.x 使用OpenCV+;播放捕获的视频时出错;python

Python 3.x 使用OpenCV+;播放捕获的视频时出错;python,python-3.x,opencv,Python 3.x,Opencv,我在Ubuntu 16.04 LTS操作系统上使用Opencv 3.2+Python。我正在尝试取消对从网络摄像头收集的鱼眼视频的干扰 下面是我的代码,我正在通过逐帧取消失真来取消鱼眼视频的失真。该程序执行正常,其未失真的鱼眼视频正确,但当我尝试播放存储的未失真视频(Underortedop.avi)时,它会给出一条错误消息,说明“无法解复用流”” 谁能帮我解决这个问题 由于VideoWriter中的帧大小不匹配,可能会发生此错误 out=cv2.VideoWriter('output.avi

我在Ubuntu 16.04 LTS操作系统上使用Opencv 3.2+Python。我正在尝试取消对从网络摄像头收集的鱼眼视频的干扰

下面是我的代码,我正在通过逐帧取消失真来取消鱼眼视频的失真。该程序执行正常,其未失真的鱼眼视频正确,但当我尝试播放存储的未失真视频(Underortedop.avi)时,它会给出一条错误消息,说明“无法解复用流”

谁能帮我解决这个问题


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


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

您确定您的
帧大小是
640x480
?错误已得到解决。框架大小不匹配存在问题。ThanksAlso图像应为RGB格式,因此确保
未失真\u img
为RGB格式
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()