【问题标题】:Error while playing captured Video using OpenCV + Python使用 OpenCV + Python 播放捕获的视频时出错
【发布时间】:2017-11-29 18:17:55
【问题描述】:

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

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

谁能帮我解决这个问题?

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

【问题讨论】:

  • 您确定您的frame 大小是640x480
  • 错误已解决。框架大小不匹配存在问题。谢谢
  • 图片也应该是RGB的,所以要确保undistorted_img是rgb

标签: python-3.x opencv


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2016-10-14
    • 1970-01-01
    • 2013-02-07
    • 2020-10-19
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    相关资源
    最近更新 更多