【OpenCV-Python】教程:1-2 视频读取显示保存

文章目录

  • 目标
    • 代码
    • VideoCapture
      • 接口
      • 参数
    • get
      • 接口
      • 参数
    • set
      • 原型
    • VideoWriter
    • 扩展信息
      • 运行时选择后端API
      • 使能后端API
      • 第三方驱动和相机
      • FFmpeg
    • 【参考】

目标

  1. 视频读入
  2. 视频显示
  3. 视频保存

代码

# 导入库
import numpy as np
import cv2 

# 从摄像头读取视频
cap = cv2.VideoCapture(0)
# 从文件读取视频
# cap = cv2.VideoCapture('testvideo.avi')

# 打开失败则退出
if not cap.isOpened():
    print("Cannot open camera")
    exit()

# 定义保存视频的编码格式、分辨率和帧率等信息
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,  480))

# 循环读取视频并显示视频
while True:  
    # 获取每一帧
    ret, frame = cap.read()
  
    # 获得帧图像,则返回 True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
  
    # 显示每一帧
    cv2.imshow('frame', frame)
  
    # 保存视频
    out.write(frame)
  
    # 按 q 键退出
    if cv2.waitKey(1) == ord('q'):
        break

# 释放句柄和销毁窗口
cap.release()
out.release()
cv2.destroyAllWindows()

cap = cv2.VideoCapture(0) 创建一个 VideoCapture 的对象,参数可以是设备的编号(相机编号,一般如果只有一个摄像头,设置为0即可,如果有多个摄像头,可以选择编号),也可以是视频文件。读取完后,可以一帧一帧的抓取视频帧,最后别忘了释放定义的 VideoCapture 的对象 cap

VideoCapture

接口


cv::VideoCapture::VideoCapture(); // 默认构造
cv::VideoCapture::VideoCapture(const String & filename, int apiPreference=CAP_ANY); // 
cv::VideoCapture::VideoCapture(const String & filename, int apiPreference, const std::vector<int>& params);
cv::VideoCapture::VideoCapture(int index, int apiPreference=CAP_ANY);
cv::VideoCapture::VideoCapture(int index, int apiPreference, const std::vector<int>& params);
cv.VideoCapture() ->	<VideoCapture object>
cv.VideoCapture(filename[, apiPreference]) ->	<VideoCapture object>
cv.VideoCapture(filename, apiPreference, params) ->	<VideoCapture object>
cv.VideoCapture(index[, apiPreference]) ->	<VideoCapture object>
cv.VideoCapture(index, apiPreference, params) ->	<VideoCapture object>

参数

filename:
- 视频名: video.avi, etc…
- 图像序列:img_%02d.jpg
- 视频流的URL:protocol://host:port/script_name?script_params|auth
- IP相机的URL,

index: 视频设备序列号

apiPreference: 首选实现 capture 的API方式,如:cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW.

后端Capture方式 描述
CAP_ANY Python: cv2.CAP_ANY Auto detect == 0.
CAP_VFW Python: cv2.CAP_VFW Video For Windows (obsolete, removed)
CAP_V4L Python: cv2.CAP_V4L V4L/V4L2 capturing support.
CAP_V4L2 Python: cv2.CAP_V4L2 Same as CAP_V4L.
CAP_FIREWIRE Python: cv2.CAP_FIREWIRE IEEE 1394 drivers.
CAP_FIREWARE Python: cv2.CAP_FIREWARE Same value as CAP_FIREWIRE.
CAP_IEEE1394 Python: cv2.CAP_IEEE1394 Same value as CAP_FIREWIRE.
CAP_DC1394 Python: cv2.CAP_DC1394 Same value as CAP_FIREWIRE.
CAP_CMU1394 Python: cv2.CAP_CMU1394 Same value as CAP_FIREWIRE.
CAP_QT Python: cv2.CAP_QT QuickTime (obsolete, removed)
CAP_UNICAP Python: cv2.CAP_UNICAP Unicap drivers (obsolete, removed)
CAP_DSHOW Python: cv2.CAP_DSHOW DirectShow (via videoInput)
CAP_PVAPI Python: cv2.CAP_PVAPI PvAPI, Prosilica GigE SDK.
CAP_OPENNI Python: cv2.CAP_OPENNI OpenNI (for Kinect)
CAP_OPENNI_ASUS Python: cv2.CAP_OPENNI_ASUS OpenNI (for Asus Xtion)
CAP_ANDROID Python: cv2.CAP_ANDROID Android - not used.
CAP_XIAPI Python: cv2.CAP_XIAPI XIMEA Camera API.
CAP_AVFOUNDATION Python: cv2.CAP_AVFOUNDATION AVFoundation framework for iOS (OS X Lion will have the same API)
CAP_GIGANETIX Python: cv2.CAP_GIGANETIX Smartek Giganetix GigEVisionSDK.
CAP_MSMF Python: cv2.CAP_MSMF Microsoft Media Foundation (via videoInput)
CAP_WINRT Python: cv2.CAP_WINRT Microsoft Windows Runtime using Media Foundation.
CAP_INTELPERC Python: cv2.CAP_INTELPERC Synonym for CAP_INTELPERC.
CAP_REALSENSE Python: cv2.CAP_REALSENSE RealSense (former Intel Perceptual Computing SDK)
CAP_OPENNI2 Python: cv2.CAP_OPENNI2 OpenNI2 (for Kinect)
CAP_OPENNI2_ASUS Python: cv2.CAP_OPENNI2_ASUS OpenNI2 (for Asus Xtion and Occipital Structure sensors)
CAP_OPENNI2_ASTRA Python: cv2.CAP_OPENNI2_ASTRA OpenNI2 (for Orbbec Astra)
CAP_GPHOTO2 Python: cv2.CAP_GPHOTO2 gPhoto2 connection
CAP_GSTREAMER Python: cv2.CAP_GSTREAMER GStreamer.
CAP_FFMPEG Python: cv2.CAP_FFMPEG Open and record video file or stream using the FFMPEG library.
CAP_IMAGES Python: cv2.CAP_IMAGES OpenCV Image Sequence (e.g. img_%02d.jpg)
CAP_ARAVIS Python: cv2.CAP_ARAVIS Aravis SDK.
CAP_OPENCV_MJPEG Python: cv2.CAP_OPENCV_MJPEG Built-in OpenCV MotionJPEG codec.
CAP_INTEL_MFX Python: cv2.CAP_INTEL_MFX Intel MediaSDK.
CAP_XINE Python: cv2.CAP_XINE XINE engine (Linux)
CAP_UEYE Python: cv2.CAP_UEYE uEye Camera API

get

接口

virtual double cv::VideoCapture::get(int propId);
cv.VideoCapture.get(propId) ->	retval

参数

propId: 属性ID号

set

原型

virtual bool cv::VideoCapture::set(int propId, double value);
cv.VideoCapture.set(	propId, value	) ->	retval

ID列表如下

属性ID号 描述
CAP_PROP_POS_MSEC Python: cv2.CAP_PROP_POS_MSEC 视频文件当前的位置(ms)
CAP_PROP_POS_FRAMES Python: cv2.CAP_PROP_POS_FRAMES 从0开始的视频帧号
CAP_PROP_POS_AVI_RATIO Python: cv2.CAP_PROP_POS_AVI_RATIO 视频文件的相对位置 0开始,1结束
CAP_PROP_FRAME_WIDTH Python: cv2.CAP_PROP_FRAME_WIDTH 视频帧的宽
CAP_PROP_FRAME_HEIGHT Python: cv2.CAP_PROP_FRAME_HEIGHT 视频帧的高
CAP_PROP_FPS Python: cv2.CAP_PROP_FPS 帧率
CAP_PROP_FOURCC Python: cv2.CAP_PROP_FOURCC 4个字符的编码代号. seeVideoWriter::fourcc .
CAP_PROP_FRAME_COUNT Python: cv2.CAP_PROP_FRAME_COUNT 帧的数量
CAP_PROP_FORMAT Python: cv2.CAP_PROP_FORMAT Mat对象的格式 (seeMat::type()) returned by VideoCapture::retrieve(). Set value -1 to fetch undecoded RAW video streams (as Mat 8UC1).
CAP_PROP_MODE Python: cv2.CAP_PROP_MODE 后端特定实现值指示当前捕获模式
CAP_PROP_BRIGHTNESS Python: cv2.CAP_PROP_BRIGHTNESS 图像的亮度值 (only for those cameras that support).
CAP_PROP_CONTRAST Python: cv2.CAP_PROP_CONTRAST 图像的对比度 (only for cameras).
CAP_PROP_SATURATION Python: cv2.CAP_PROP_SATURATION 图像的饱和度 (only for cameras).
CAP_PROP_HUE Python: cv2.CAP_PROP_HUE 图像的色调 (only for cameras).
CAP_PROP_GAIN Python: cv2.CAP_PROP_GAIN 图像的增益 (only for those cameras that support).
CAP_PROP_EXPOSURE Python: cv2.CAP_PROP_EXPOSURE 曝光时间 (only for those cameras that support).
CAP_PROP_CONVERT_RGB Python: cv2.CAP_PROP_CONVERT_RGB 布尔值表明是否必须转换RGB ``GStreamer note : The flag is ignored in case if custom pipeline is used. It’s user responsibility to interpret pipeline output.
CAP_PROP_WHITE_BALANCE_BLUE_U Python: cv2.CAP_PROP_WHITE_BALANCE_BLUE_U Currently unsupported.
CAP_PROP_RECTIFICATION Python: cv2.CAP_PROP_RECTIFICATION 立体相机的矫正标志 (note: only supported by DC1394 v 2.x backend currently).
CAP_PROP_MONOCHROME Python: cv2.CAP_PROP_MONOCHROME
CAP_PROP_SHARPNESS Python: cv2.CAP_PROP_SHARPNESS
CAP_PROP_AUTO_EXPOSURE Python: cv2.CAP_PROP_AUTO_EXPOSURE DC1394: exposure control done by camera, user can adjust reference level using this feature.
CAP_PROP_GAMMA Python: cv2.CAP_PROP_GAMMA
CAP_PROP_TEMPERATURE Python: cv2.CAP_PROP_TEMPERATURE
CAP_PROP_TRIGGER Python: cv2.CAP_PROP_TRIGGER
CAP_PROP_TRIGGER_DELAY Python: cv2.CAP_PROP_TRIGGER_DELAY
CAP_PROP_WHITE_BALANCE_RED_V Python: cv2.CAP_PROP_WHITE_BALANCE_RED_V
CAP_PROP_ZOOM Python: cv2.CAP_PROP_ZOOM
CAP_PROP_FOCUS Python: cv2.CAP_PROP_FOCUS
CAP_PROP_GUID Python: cv2.CAP_PROP_GUID
CAP_PROP_ISO_SPEED Python: cv2.CAP_PROP_ISO_SPEED
CAP_PROP_BACKLIGHT Python: cv2.CAP_PROP_BACKLIGHT
CAP_PROP_PAN Python: cv2.CAP_PROP_PAN
CAP_PROP_TILT Python: cv2.CAP_PROP_TILT
CAP_PROP_ROLL Python: cv2.CAP_PROP_ROLL
CAP_PROP_IRIS Python: cv2.CAP_PROP_IRIS
CAP_PROP_SETTINGS Python: cv2.CAP_PROP_SETTINGS Pop up video/camera filter dialog (note: only supported by DSHOW backend currently. The property value is ignored)
CAP_PROP_BUFFERSIZE Python: cv2.CAP_PROP_BUFFERSIZE
CAP_PROP_AUTOFOCUS Python: cv2.CAP_PROP_AUTOFOCUS
CAP_PROP_SAR_NUM Python: cv2.CAP_PROP_SAR_NUM Sample aspect ratio: num/den (num)
CAP_PROP_SAR_DEN Python: cv2.CAP_PROP_SAR_DEN Sample aspect ratio: num/den (den)
CAP_PROP_BACKEND Python: cv2.CAP_PROP_BACKEND Current backend (enum VideoCaptureAPIs). Read-only property.
CAP_PROP_CHANNEL Python: cv2.CAP_PROP_CHANNEL Video input or Channel Number (only for those cameras that support)
CAP_PROP_AUTO_WB Python: cv2.CAP_PROP_AUTO_WB enable/ disable auto white-balance
CAP_PROP_WB_TEMPERATURE Python: cv2.CAP_PROP_WB_TEMPERATURE white-balance color temperature
CAP_PROP_CODEC_PIXEL_FORMAT Python: cv2.CAP_PROP_CODEC_PIXEL_FORMAT (read-only) codec’s pixel format. 4-character code - seeVideoWriter::fourcc . Subset of AV_PIX_FMT_* or -1 if unknown
CAP_PROP_BITRATE Python: cv2.CAP_PROP_BITRATE (read-only) Video bitrate in kbits/s
CAP_PROP_ORIENTATION_META Python: cv2.CAP_PROP_ORIENTATION_META (read-only) Frame rotation defined by stream meta (applicable for FFmpeg back-end only)
CAP_PROP_ORIENTATION_AUTO Python: cv2.CAP_PROP_ORIENTATION_AUTO if true - rotates output frames of CvCapture considering video file’s metadata (applicable for FFmpeg back-end only) (https://github.com/opencv/opencv/issues/15499)
CAP_PROP_HW_ACCELERATION Python: cv2.CAP_PROP_HW_ACCELERATION (open-only ) Hardware acceleration type (see VideoAccelerationType). Setting supported only via params parameter in cv::VideoCapture constructor / .open() method. Default value is backend-specific.
CAP_PROP_HW_DEVICE Python: cv2.CAP_PROP_HW_DEVICE (open-only ) Hardware device index (select GPU if multiple available). Device enumeration is acceleration type specific.
CAP_PROP_HW_ACCELERATION_USE_OPENCL Python: cv2.CAP_PROP_HW_ACCELERATION_USE_OPENCL (open-only ) If non-zero, create new OpenCL context and bind it to current thread. The OpenCL context created with Video Acceleration context attached it (if not attached yet) for optimized GPU data copy between HW accelerated decoder and cv::UMat.
CAP_PROP_OPEN_TIMEOUT_MSEC Python: cv2.CAP_PROP_OPEN_TIMEOUT_MSEC (open-only ) timeout in milliseconds for opening a video capture (applicable for FFmpeg back-end only)
CAP_PROP_READ_TIMEOUT_MSEC Python: cv2.CAP_PROP_READ_TIMEOUT_MSEC (open-only ) timeout in milliseconds for reading from a video capture (applicable for FFmpeg back-end only)
CAP_PROP_STREAM_OPEN_TIME_USEC Python: cv2.CAP_PROP_STREAM_OPEN_TIME_USEC
CAP_PROP_VIDEO_TOTAL_CHANNELS Python: cv2.CAP_PROP_VIDEO_TOTAL_CHANNELS (read-only) Number of video channels
CAP_PROP_VIDEO_STREAM Python: cv2.CAP_PROP_VIDEO_STREAM (open-only ) Specify video stream, 0-based index. Use -1 to disable video stream from file or IP cameras. Default value is 0.
CAP_PROP_AUDIO_STREAM Python: cv2.CAP_PROP_AUDIO_STREAM (open-only ) Specify stream in multi-language media files, -1 - disable audio processing or microphone. Default value is -1.
CAP_PROP_AUDIO_POS Python: cv2.CAP_PROP_AUDIO_POS (read-only) Audio position is measured in samples. Accurate audio sample timestamp of previous grabbed fragment. See CAP_PROP_AUDIO_SAMPLES_PER_SECOND and CAP_PROP_AUDIO_SHIFT_NSEC.
CAP_PROP_AUDIO_SHIFT_NSEC Python: cv2.CAP_PROP_AUDIO_SHIFT_NSEC (read only) Contains the time difference between the start of the audio stream and the video stream in nanoseconds. Positive value means that audio is started after the first video frame. Negative value means that audio is started before the first video frame.
CAP_PROP_AUDIO_DATA_DEPTH Python: cv2.CAP_PROP_AUDIO_DATA_DEPTH (open, read) Alternative definition to bits-per-sample, but with clear handling of 32F / 32S
CAP_PROP_AUDIO_SAMPLES_PER_SECOND Python: cv2.CAP_PROP_AUDIO_SAMPLES_PER_SECOND (open, read) determined from file/codec input. If not specified, then selected audio sample rate is 44100
CAP_PROP_AUDIO_BASE_INDEX Python: cv2.CAP_PROP_AUDIO_BASE_INDEX (read-only) Index of the first audio channel for .retrieve() calls. That audio channel number continues enumeration after video channels.
CAP_PROP_AUDIO_TOTAL_CHANNELS Python: cv2.CAP_PROP_AUDIO_TOTAL_CHANNELS (read-only) Number of audio channels in the selected audio stream (mono, stereo, etc)
CAP_PROP_AUDIO_TOTAL_STREAMS Python: cv2.CAP_PROP_AUDIO_TOTAL_STREAMS (read-only) Number of audio streams.
CAP_PROP_AUDIO_SYNCHRONIZE Python: cv2.CAP_PROP_AUDIO_SYNCHRONIZE (open, read) Enables audio synchronization.
CAP_PROP_LRF_HAS_KEY_FRAME Python: cv2.CAP_PROP_LRF_HAS_KEY_FRAME FFmpeg back-end only - Indicates whether the Last Raw Frame (LRF), output fromVideoCapture::read() when VideoCapture is initialized with VideoCapture::open(CAP_FFMPEG, {CAP_PROP_FORMAT, -1}) or VideoCapture::set(CAP_PROP_FORMAT,-1) is called before the first call to VideoCapture::read(), contains encoded data for a key frame.
CAP_PROP_CODEC_EXTRADATA_INDEX Python: cv2.CAP_PROP_CODEC_EXTRADATA_INDEX Positive index indicates that returning extra data is supported by the video back end. This can be retrieved as cap.retrieve(data,). E.g. When reading from a h264 encoded RTSP stream, the FFmpeg backend could return the SPS and/or PPS if available (if sent in reply to a DESCRIBE request), from calls to cap.retrieve(data, ).

VideoWriter

视频保存的属性 描述
VIDEOWRITER_PROP_QUALITY Python: cv2.VIDEOWRITER_PROP_QUALITY Current quality (0…100%) of the encoded videostream. Can be adjusted dynamically in some codecs.
VIDEOWRITER_PROP_FRAMEBYTES Python: cv2.VIDEOWRITER_PROP_FRAMEBYTES (Read-only): Size of just encoded video frame. Note that the encoding order may be different from representation order.
VIDEOWRITER_PROP_NSTRIPES Python: cv2.VIDEOWRITER_PROP_NSTRIPES Number of stripes for parallel encoding. -1 for auto detection.
VIDEOWRITER_PROP_IS_COLOR Python: cv2.VIDEOWRITER_PROP_IS_COLOR If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames.
VIDEOWRITER_PROP_DEPTH Python: cv2.VIDEOWRITER_PROP_DEPTH Defaults to CV_8U.
VIDEOWRITER_PROP_HW_ACCELERATION Python: cv2.VIDEOWRITER_PROP_HW_ACCELERATION (open-only ) Hardware acceleration type (see VideoAccelerationType). Setting supported only via params parameter in VideoWriter constructor / .open() method. Default value is backend-specific.
VIDEOWRITER_PROP_HW_DEVICE Python: cv2.VIDEOWRITER_PROP_HW_DEVICE (open-only ) Hardware device index (select GPU if multiple available). Device enumeration is acceleration type specific.
VIDEOWRITER_PROP_HW_ACCELERATION_USE_OPENCL Python: cv2.VIDEOWRITER_PROP_HW_ACCELERATION_USE_OPENCL (open-only ) If non-zero, create new OpenCL context and bind it to current thread. The OpenCL context created with Video Acceleration context attached it (if not attached yet) for optimized GPU data copy between cv::UMat and HW accelerated encoder.

扩展信息

其他信息请参考 OpenCV 官网教程

【OpenCV-Python】教程:1-2 视频读取显示保存_第1张图片

有些后端是系统库,有些是OpenCV自带的,也有的是外部的。

运行时选择后端API

OpenCV 会自动选择第一个可用的 API(apiPreference=cv::CAP_ANY) 也可以选择API,如

//declare a capture object
cv::VideoCapture cap(filename, cv::CAP_MSMF);

//or specify the apiPreference with open
cap.open(filename, cv::CAP_MSMF);

使能后端API

  1. 打开对应的 CMake选项, e.g. -DWITH_GSTREAMER=ON
  2. 重新编译 OpenCV

第三方驱动和相机

大部分的工业场景下,有很多厂商有自己的 API接口,并没有集成到 OpenCV里,我们有一个折中的方案是将他们的数据放到内存中,整理成 MAT格式,然后用 OpenCV读取和使用。

FFmpeg

OpenCV 可以使用 FFmpeg 库进行录制和转换音视频。如果需要运行时使用 FFmpeg, 那么发布的时候要一起发布 FFmpeg 库。

【参考】

  1. https://docs.opencv.org/4.5.5/d8/dfe/classcv_1_1VideoCapture.html#a4576e03f447abfdbd602c0809824ec03
  2. https://docs.opencv.org/4.5.5/d0/da7/videoio_overview.html

你可能感兴趣的:(OpenCV-Python,教程,opencv,python,音视频)