“OpenCV保存视频失败,只有6KB”原因分析和解决方法

1. 问题描述:

学习OpenCV tutorials 时,根据用例中的内容,保存视频文件,只获取到一个6KB大小的文件。

代码如下:

# from camera
import cv2 as cv
cap = cv.VideoCapture(0)

# define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID')

out = cv.VideoWriter('output.avi', fourcc, 20.0, (640,480))
while(1):
    ret, frame = cap.read()

    if ret ==True:
        out.write(frame.astype('uint8'))
        cv.namedWindow('frame', cv.WINDOW_NORMAL)
        cv.imshow('frame', frame)
        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
        
cap.release()
out.release()
cv.destroyAllWindows()

2. 问题分析

调用VideoWriter函数时,保存的视频文件大小修改了,和原大小不一致,导致保存时评失败。

3. 解决方法

保持原视频大小,即可以解决该问题。 原视频文件的尺寸可以通过cap.get函数进行获取。

解决方法为:将上面的cv.VideoWriter函数中最后一个参数由(640,480)修改为(int(cap.get(3)),int(cap.get(4)))。即:out = cv.VideoWriter('output.avi', fourcc, 20.0, (int(cap.get(3)),int(cap.get(4))))

更改后,完整代码为:

# from camera
import cv2 as cv
cap = cv.VideoCapture(0)

# define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID')

out = cv.VideoWriter('output.avi', fourcc, 20.0, (int(cap.get(3)),int(cap.get(4))))
while(1):
    ret, frame = cap.read()

    if ret ==True:
        out.write(frame.astype('uint8'))
        cv.namedWindow('frame', cv.WINDOW_NORMAL)
        cv.imshow('frame', frame)
        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
        
cap.release()
out.release()
cv.destroyAllWindows()

上面代码中,get(3),get(4)分别代表原视频流文件的WidthHeight,详细可以见下面的cap.get函数。

4. other

4.1 cap.get函数

函数原型为:retval = cv.VideoCapture.get( propId ),其中propId参数是枚举值cv::VideoCaptureProperties中的一个

其中各枚举值的含义为:

enum cv::VideoCaptureProperties {
  cv::CAP_PROP_POS_MSEC =0,
  cv::CAP_PROP_POS_FRAMES =1,
  cv::CAP_PROP_POS_AVI_RATIO =2,
  cv::CAP_PROP_FRAME_WIDTH =3,
  cv::CAP_PROP_FRAME_HEIGHT =4,
  cv::CAP_PROP_FPS =5,
  cv::CAP_PROP_FOURCC =6,
  cv::CAP_PROP_FRAME_COUNT =7,
  cv::CAP_PROP_FORMAT =8,
  cv::CAP_PROP_MODE =9,
  cv::CAP_PROP_BRIGHTNESS =10,
  cv::CAP_PROP_CONTRAST =11,
  cv::CAP_PROP_SATURATION =12,
  cv::CAP_PROP_HUE =13,
  cv::CAP_PROP_GAIN =14,
  cv::CAP_PROP_EXPOSURE =15,
  cv::CAP_PROP_CONVERT_RGB =16,
  cv::CAP_PROP_WHITE_BALANCE_BLUE_U =17,
  cv::CAP_PROP_RECTIFICATION =18,
  cv::CAP_PROP_MONOCHROME =19,
  cv::CAP_PROP_SHARPNESS =20,
  cv::CAP_PROP_AUTO_EXPOSURE =21,
  cv::CAP_PROP_GAMMA =22,
  cv::CAP_PROP_TEMPERATURE =23,
  cv::CAP_PROP_TRIGGER =24,
  cv::CAP_PROP_TRIGGER_DELAY =25,
  cv::CAP_PROP_WHITE_BALANCE_RED_V =26,
  cv::CAP_PROP_ZOOM =27,
  cv::CAP_PROP_FOCUS =28,
  cv::CAP_PROP_GUID =29,
  cv::CAP_PROP_ISO_SPEED =30,
  cv::CAP_PROP_BACKLIGHT =32,
  cv::CAP_PROP_PAN =33,
  cv::CAP_PROP_TILT =34,
  cv::CAP_PROP_ROLL =35,
  cv::CAP_PROP_IRIS =36,
  cv::CAP_PROP_SETTINGS =37,
  cv::CAP_PROP_BUFFERSIZE =38,
  cv::CAP_PROP_AUTOFOCUS =39,
  cv::CAP_PROP_SAR_NUM =40,
  cv::CAP_PROP_SAR_DEN =41,
  cv::CAP_PROP_BACKEND =42,
  cv::CAP_PROP_CHANNEL =43,
  cv::CAP_PROP_AUTO_WB =44,
  cv::CAP_PROP_WB_TEMPERATURE =45
}

其中,3、4的含义为:
3 - Width of the frames in the video stream.
4 - Height of the frames in the video stream.

4.2 其他问题

使用cv.imshow('frame', frame)显示视频流中的一帧帧图像,有如下错误提示:

cv2.error: OpenCV(4.0.0) /Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

找到了一些原因:路径不全、路径中含有中文,等都进行尝试,未能解决问题

你可能感兴趣的:(Python,OpenCV)