OpenCV视频读写模块(videoio)

简介: OpenCV视频读写模块(videoio)

视频读写模块(videoio)

  ‍

  在视频处理中,常见的操作包括将视频写入磁盘和从磁盘读取视频,视频读/写后将其播放显示,videoio模块提供了视频读取和保存的功能

VideoCapture

构造函数

CV_WRAP VideoCapture();
CV_WRAP explicit VideoCapture(const String& filename, int apiPreference = CAP_ANY);
CV_WRAP explicit VideoCapture(const String& filename, int apiPreference, const std::vector<int>& params);
CV_WRAP explicit VideoCapture(int index, int apiPreference = CAP_ANY);

  会调用open函数对文件进行打开

open

  打开视频文件/设备, 如果当前对象已经有打开会调用release进行释放

  • apiPreference: 参数参考后端标识符[^1]
  • params: 参数参考通用属性标志[^2]
CV_WRAP virtual bool open(const String& filename, int apiPreference = CAP_ANY);
CV_WRAP virtual bool open(const String& filename, int apiPreference, const std::vector<int>& params);
CV_WRAP virtual bool open(int index, int apiPreference = CAP_ANY);
CV_WRAP virtual bool open(int index, int apiPreference, const std::vector<int>& params);

isOpened

  如果已经打开视频文件则返回true

CV_WRAP virtual bool isOpened() const;

release

  关闭视频文件/设备

CV_WRAP virtual void release();

grab

  从视频文件/设备抓取下一帧,一般在多个摄像机环境使用

CV_WRAP virtual bool grab();

retrieve

  解码并返回抓取的视频帧

CV_WRAP virtual bool retrieve(OutputArray image, int flag = 0);

read

virtual VideoCapture& operator >> (CV_OUT Mat& image);
virtual VideoCapture& operator >> (CV_OUT UMat& image); 
CV_WRAP virtual bool read(OutputArray image);

  read实际调用了grab和retrieve两个API,而>>实际调用了read

   if (grab())
    {
        retrieve(image);
    } else {
        image.release();
    }
    return !image.empty();

set

  设置属性

CV_WRAP virtual bool set(int propId, double value);

get

  获取属性

 CV_WRAP virtual double get(int propId) const;

getBackendName

  获取使用的后端名称

CV_WRAP String getBackendName() const;

setExceptionMode

  设置是否启用异常捕获模式

CV_WRAP void setExceptionMode(bool enable) { throwOnFail = enable; }

getExceptionMode

  获取是否启用异常捕获模式

CV_WRAP bool getExceptionMode() { return throwOnFail; }

waitAny

  等待就绪帧

 CV_WRAP static bool waitAny(const std::vector<VideoCapture>& streams,
            CV_OUT std::vector<int>& readyIndex,
            int64 timeoutNs = 0);

VideoWriter

  VideoWriter写入视频文件,API与VideoCapure类似

构造函数

  实际调用open函数来打开文件, 析构会调用release来释放

CV_WRAP VideoWriter();
CV_WRAP VideoWriter(const String& filename, int fourcc, double fps, 
        Size frameSize, bool isColor = true);
 CV_WRAP VideoWriter(const String& filename, int apiPreference, int fourcc, double fps,
                Size frameSize, bool isColor = true);
CV_WRAP VideoWriter(const String& filename, int fourcc, double fps, const Size& frameSize,
                        const std::vector<int>& params);
CV_WRAP VideoWriter(const String& filename, int apiPreference, int fourcc, double fps,
                        const Size& frameSize, const std::vector<int>& params);

open

  • filename: 文件名
  • apiPreference: 参数参考后端标识符[^1]
  • fourcc: 此参数一般调用fourcc静态函数来传递
  • fps: 帧率
  • framesize: 帧大小
  • isColor: 是否为彩色帧, 默认为true
  • params: 参数参考通用属性标志[^2]
 CV_WRAP virtual bool open(const String& filename, int fourcc, double fps,
                      Size frameSize, bool isColor = true);
CV_WRAP bool open(const String& filename, int apiPreference, int fourcc, double fps,
                      Size frameSize, bool isColor = true);
CV_WRAP bool open(const String& filename, int fourcc, double fps, const Size& frameSize,
                      const std::vector<int>& params);
 CV_WRAP bool open(const String& filename, int apiPreference, int fourcc, double fps,
                      const Size& frameSize, const std::vector<int>& params);

isOpened

  如果已经打开视频文件则返回true

CV_WRAP virtual bool isOpened() const;

release

  关闭视频文件

CV_WRAP virtual void release();

write

  写入帧

virtual VideoWriter& operator << (const Mat& image);
virtual VideoWriter& operator << (const UMat& image);
CV_WRAP virtual void write(InputArray image);

set

  设置属性值

CV_WRAP virtual bool set(int propId, double value);

get

  获取属性值

CV_WRAP virtual double get(int propId) const;

fourcc

  返回fourcc编码, 常见Video FOURCCs - Win32 apps | Microsoft Learn

CV_WRAP static int fourcc(char c1, char c2, char c3, char c4);

getBackendName

  获取后端名称

CV_WRAP String getBackendName() const;

枚举

后端标识符

enum VideoCaptureAPIs {
       CAP_ANY          = 0,            //自动检测
       CAP_VFW          = 200,          //windows视频
       CAP_V4L          = 200,          //V4L/V4L2支持
       CAP_V4L2         = CAP_V4L,      //通CAP_V4L
       CAP_FIREWIRE     = 300,          // IEEE 1394 驱动
       CAP_FIREWARE     = CAP_FIREWIRE, //IEEE 1394 驱动
       CAP_IEEE1394     = CAP_FIREWIRE, //IEEE 1394 驱动
       CAP_DC1394       = CAP_FIREWIRE, //IEEE 1394 驱动
       CAP_CMU1394      = CAP_FIREWIRE, //IEEE 1394 驱动
       CAP_QT           = 500,          // QuickTime 
       CAP_UNICAP       = 600,          // Unicap 驱动
       CAP_DSHOW        = 700,          // DirectShow
       CAP_PVAPI        = 800,          // PvAPI
       CAP_OPENNI       = 900,          // Kinect中应用的OpenNI
       CAP_OPENNI_ASUS  = 910,          //Asus Xtion中应用的OpenNI 
       CAP_ANDROID      = 1000,         // Android 系统
       CAP_XIAPI        = 1100,         // XIMEA相机API
       CAP_AVFOUNDATION = 1200,         // IOS的AVFoundation框架
       CAP_GIGANETIX    = 1300,         // Smartek Giganetix GigEVisionSDK
       CAP_MSMF         = 1400,         //微软Media Foundation视频模块
       CAP_WINRT        = 1410,         //微软Windows Runtime视频模块
       CAP_INTELPERC    = 1500,         // RealSense
       CAP_REALSENSE    = 1500,         //RealSense
       CAP_OPENNI2      = 1600,         // Kinect OpenNI2 
       CAP_OPENNI2_ASUS = 1610,         // Asus Xtion OpenNI2 
       CAP_OPENNI2_ASTRA= 1620,         //Orbbec Astra OpenNI2 
       CAP_GPHOTO2      = 1700,         //  gPhoto2 连接
       CAP_GSTREAMER    = 1800,         // GStreamer
       CAP_FFMPEG       = 1900,         // ffmpeg打开或保存视频文件
       CAP_IMAGES       = 2000,         // OpenCV图像序列
       CAP_ARAVIS       = 2100,         // Aravis SDK
       CAP_OPENCV_MJPEG = 2200,         // OpenCV 内置MotionJPEG编解码
       CAP_INTEL_MFX    = 2300,         // Intel MediaSDK
       CAP_XINE         = 2400,         // XINE engine (Linux)
       CAP_UEYE         = 2500,         // uEye 相机API
       CAP_OBSENSOR     = 2600,         // Orbbec 3D-Sensor device/module (Astra+, Femto)
     };

通用属性标志

enum VideoCaptureProperties {
       CAP_PROP_POS_MSEC       =0, // 当前视频的毫秒数
       CAP_PROP_POS_FRAMES     =1, //下一个要解码/捕获的帧索引
       CAP_PROP_POS_AVI_RATIO  =2, //视频文件的相对位置 0表示开始 1表示结束
       CAP_PROP_FRAME_WIDTH    =3, //视频流中帧的宽度
       CAP_PROP_FRAME_HEIGHT   =4, //视频流中帧的高度
       CAP_PROP_FPS            =5, //帧率
       CAP_PROP_FOURCC         =6, //编解码器
       CAP_PROP_FRAME_COUNT    =7, //帧数
       CAP_PROP_FORMAT         =8, //Math格式对象
       CAP_PROP_MODE           =9, // 当前捕获模式
       CAP_PROP_BRIGHTNESS    =10, //图像亮度(仅支持相机)
       CAP_PROP_CONTRAST      =11, //图像对比度(仅支持相机)
       CAP_PROP_SATURATION    =12, //图像饱和度(仅支持相机)
       CAP_PROP_HUE           =13, //图像色调(仅支持相机)
       CAP_PROP_GAIN          =14, //图像增益(仅支持相机)
       CAP_PROP_EXPOSURE      =15, //曝光(仅支持相机)
       CAP_PROP_CONVERT_RGB   =16, //是否将图像转化为RGB格式
       CAP_PROP_WHITE_BALANCE_BLUE_U =17, //暂不支持
       CAP_PROP_RECTIFICATION =18, //立体相机校正标志
       CAP_PROP_MONOCHROME    =19,
       CAP_PROP_SHARPNESS     =20,
       CAP_PROP_AUTO_EXPOSURE =21, //!< DC1394:曝光控制由相机完成
       CAP_PROP_GAMMA         =22,
       CAP_PROP_TEMPERATURE   =23,
       CAP_PROP_TRIGGER       =24,
       CAP_PROP_TRIGGER_DELAY =25,
       CAP_PROP_WHITE_BALANCE_RED_V =26,
       CAP_PROP_ZOOM          =27,
       CAP_PROP_FOCUS         =28,
       CAP_PROP_GUID          =29,
       CAP_PROP_ISO_SPEED     =30,
       CAP_PROP_BACKLIGHT     =32,
       CAP_PROP_PAN           =33,
       CAP_PROP_TILT          =34,
       CAP_PROP_ROLL          =35,
       CAP_PROP_IRIS          =36,
       CAP_PROP_SETTINGS      =37, // 弹出视频/相机过滤器对话框
       CAP_PROP_BUFFERSIZE    =38,
       CAP_PROP_AUTOFOCUS     =39,
       CAP_PROP_SAR_NUM       =40, // 样本纵横比: num/den(num)
       CAP_PROP_SAR_DEN       =41, // 样本纵横比: num/den(den)
       CAP_PROP_BACKEND       =42, //当前后端
       CAP_PROP_CHANNEL       =43, //视频输出或通道数(仅相机)
       CAP_PROP_AUTO_WB       =44, //开启/关闭自动白平衡功能
       CAP_PROP_WB_TEMPERATURE=45, //白平衡色温
       CAP_PROP_CODEC_PIXEL_FORMAT =46,    //编解码器格式
       CAP_PROP_BITRATE       =47, //视频比特率(kbit/s) (只读)
       CAP_PROP_ORIENTATION_META=48, // 由stream meta定义的帧选择,仅使用与ffmpeg后端
       CAP_PROP_ORIENTATION_AUTO=49, //若为true,则根据视频元数据旋转输出帧
       CAP_PROP_HW_ACCELERATION=50, //!< (**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      =51, //!< (**open-only**) Hardware device index (select GPU if multiple available). Device enumeration is acceleration type specific.
       CAP_PROP_HW_ACCELERATION_USE_OPENCL=52, //!< (**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=53, //!< (**open-only**) timeout in milliseconds for opening a video capture (applicable for FFmpeg and GStreamer back-ends only)
       CAP_PROP_READ_TIMEOUT_MSEC=54, //!< (**open-only**) timeout in milliseconds for reading from a video capture (applicable for FFmpeg and GStreamer back-ends only)
       CAP_PROP_STREAM_OPEN_TIME_USEC =55, //<! (read-only) time in microseconds since Jan 1 1970 when stream was opened. Applicable for FFmpeg backend only. Useful for RTSP and other live streams
       CAP_PROP_VIDEO_TOTAL_CHANNELS = 56, //!< (read-only) Number of video channels
       CAP_PROP_VIDEO_STREAM = 57, //!< (**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 = 58, //!< (**open-only**) Specify stream in multi-language media files, -1 - disable audio processing or microphone. Default value is -1.
       CAP_PROP_AUDIO_POS = 59, //!< (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 = 60, //!< (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 = 61, //!< (open, read) Alternative definition to bits-per-sample, but with clear handling of 32F / 32S
       CAP_PROP_AUDIO_SAMPLES_PER_SECOND = 62, //!< (open, read) determined from file/codec input. If not specified, then selected audio sample rate is 44100
       CAP_PROP_AUDIO_BASE_INDEX = 63, //!< (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 = 64, //!< (read-only) Number of audio channels in the selected audio stream (mono, stereo, etc)
       CAP_PROP_AUDIO_TOTAL_STREAMS = 65, //!< (read-only) Number of audio streams.
       CAP_PROP_AUDIO_SYNCHRONIZE = 66, //!< (open, read) Enables audio synchronization.
       CAP_PROP_LRF_HAS_KEY_FRAME = 67, //!< FFmpeg back-end only - Indicates whether the Last Raw Frame (LRF), output from VideoCapture::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 = 68, //!< Positive index indicates that returning extra data is supported by the video back end.  This can be retrieved as cap.retrieve(data, <returned index>).  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, <returned index>).
       CAP_PROP_FRAME_TYPE = 69, //!< (read-only) FFmpeg back-end only - Frame type ascii code (73 = 'I', 80 = 'P', 66 = 'B' or 63 = '?' if unknown) of the most recently read frame.
       CAP_PROP_N_THREADS = 70, //!< (**open-only**) Set the maximum number of threads to use. Use 0 to use as many threads as CPU cores (applicable for FFmpeg back-end only).
#ifndef CV_DOXYGEN
       CV__CAP_PROP_LATEST
#endif
     };

写通用属性标识符

enum VideoWriterProperties {
  // 编码视频流的当前质量,可以在某些编解码器中动态调整
  VIDEOWRITER_PROP_QUALITY = 1,  
  // 编码的视频帧大小
  VIDEOWRITER_PROP_FRAMEBYTES = 2, 
  // 并行编码的条纹数,若将其设置为-1,则用于自动检测
  VIDEOWRITER_PROP_NSTRIPES = 3,   //!< Number of stripes for parallel encoding. -1 for auto detection.
  // 如果不为0,则编码器将期望并编码彩色帧,否则将处理灰度帧
  VIDEOWRITER_PROP_IS_COLOR = 4,
  //!< Defaults to CV_8U.   
  VIDEOWRITER_PROP_DEPTH = 5,  
  //硬件加速类型,只支持VideoWriter构造函数或open方法中的参数,默认值是特定于后端的
  VIDEOWRITER_PROP_HW_ACCELERATION = 6, 
  // 硬件设备索引(如果多个可用,选择GPU) 设备枚举特定于加速类型
  VIDEOWRITER_PROP_HW_DEVICE       = 7,
  // 如果不为0,则创建新的OpenCL上下文并将其绑定到当前线程
   // 使用视频加速上下文创建的OpenCL上下文附加了它(如果还没有附加),以便在cv::UMat和HW加速编码器之间优化GPU数据拷贝。
  VIDEOWRITER_PROP_HW_ACCELERATION_USE_OPENCL= 8, 
#ifndef CV_DOXYGEN
  CV__VIDEOWRITER_PROP_LATEST
#endif
};

硬件加速标识符

enum VideoAccelerationType
{
    VIDEO_ACCELERATION_NONE     =  0,  //!< Do not require any specific H/W acceleration, prefer software processing.
                                       //!< Reading of this value means that special H/W accelerated handling is not added or not detected by OpenCV.

    VIDEO_ACCELERATION_ANY      =  1,  //!< Prefer to use H/W acceleration. If no one supported, then fallback to software processing.
                                       //!< @note H/W acceleration may require special configuration of used environment.
                                       //!< @note Results in encoding scenario may differ between software and hardware accelerated encoders.
    //!< DirectX 11
    VIDEO_ACCELERATION_D3D11    =  2,  
    VIDEO_ACCELERATION_VAAPI    =  3,  //!< VAAPI
    VIDEO_ACCELERATION_MFX      =  4,  //!< libmfx (Intel MediaSDK/oneVPL)
};

IEEE 1394 驱动

enum { 
    // 关闭该功能(不是手动控制,也不是自动控制)。
       CAP_PROP_DC1394_OFF                = -4, 
    // 当用户设置某个特征值时自动设置。
       CAP_PROP_DC1394_MODE_MANUAL        = -3, 
       CAP_PROP_DC1394_MODE_AUTO          = -2,
       CAP_PROP_DC1394_MODE_ONE_PUSH_AUTO = -1,
       CAP_PROP_DC1394_MAX                = 31
     };

OpenNI后端相机属性

enum { 
       CAP_PROP_OPENNI_OUTPUT_MODE       = 100,
       // 帧最大深度,单位为mm
       CAP_PROP_OPENNI_FRAME_MAX_DEPTH   = 101, 
    // 基准, 单位mm
       CAP_PROP_OPENNI_BASELINE          = 102, 
    // 焦距,单位为像素
       CAP_PROP_OPENNI_FOCAL_LENGTH      = 103,
    // 将重映射深度map与图像map同步的标志
       CAP_PROP_OPENNI_REGISTRATION      = 104, 
       CAP_PROP_OPENNI_REGISTRATION_ON   = CAP_PROP_OPENNI_REGISTRATION,
       CAP_PROP_OPENNI_APPROX_FRAME_SYNC = 105,
       CAP_PROP_OPENNI_MAX_BUFFER_SIZE   = 106,
       CAP_PROP_OPENNI_CIRCLE_BUFFER     = 107,
       CAP_PROP_OPENNI_MAX_TIME_DURATION = 108,
       CAP_PROP_OPENNI_GENERATOR_PRESENT = 109,
       CAP_PROP_OPENNI2_SYNC             = 110,
       CAP_PROP_OPENNI2_MIRROR           = 111
     };

OpenNI shortcuts

enum { 
       CAP_OPENNI_IMAGE_GENERATOR_PRESENT         = CAP_OPENNI_IMAGE_GENERATOR + CAP_PROP_OPENNI_GENERATOR_PRESENT,
       CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE     = CAP_OPENNI_IMAGE_GENERATOR + CAP_PROP_OPENNI_OUTPUT_MODE,
       CAP_OPENNI_DEPTH_GENERATOR_PRESENT         = CAP_OPENNI_DEPTH_GENERATOR + CAP_PROP_OPENNI_GENERATOR_PRESENT,
       CAP_OPENNI_DEPTH_GENERATOR_BASELINE        = CAP_OPENNI_DEPTH_GENERATOR + CAP_PROP_OPENNI_BASELINE,
       CAP_OPENNI_DEPTH_GENERATOR_FOCAL_LENGTH    = CAP_OPENNI_DEPTH_GENERATOR + CAP_PROP_OPENNI_FOCAL_LENGTH,
       CAP_OPENNI_DEPTH_GENERATOR_REGISTRATION    = CAP_OPENNI_DEPTH_GENERATOR + CAP_PROP_OPENNI_REGISTRATION,
       CAP_OPENNI_DEPTH_GENERATOR_REGISTRATION_ON = CAP_OPENNI_DEPTH_GENERATOR_REGISTRATION,
       CAP_OPENNI_IR_GENERATOR_PRESENT            = CAP_OPENNI_IR_GENERATOR + CAP_PROP_OPENNI_GENERATOR_PRESENT,
     };

深度生成器提供OpenNI数据

enum { 
       // 深度数据, 单位为mm(CV_16UC1格式)
       CAP_OPENNI_DEPTH_MAP         = 0, 
    // XYZ数据,单位为米(CV_32FC3格式)
       CAP_OPENNI_POINT_CLOUD_MAP   = 1, 
    // 差异值,单位为像素(CV_8UC1格式)
       CAP_OPENNI_DISPARITY_MAP     = 2, 
    // 差异值,单位为像素(CV_32FC1格式)
       CAP_OPENNI_DISPARITY_MAP_32F = 3, 
    // CV_8UC1
       CAP_OPENNI_VALID_DEPTH_MASK  = 4, 
    //RGB图像生成器提供的数据
       CAP_OPENNI_BGR_IMAGE         = 5, 
    // 灰度图像生成器提供的数据
       CAP_OPENNI_GRAY_IMAGE        = 6, 
    // IR图像生成器提供的数据
       CAP_OPENNI_IR_IMAGE          = 7  
     };

OpenNI图像生成器支持的输出格式

enum { CAP_OPENNI_VGA_30HZ  = 0,
       CAP_OPENNI_SXGA_15HZ = 1,
       CAP_OPENNI_SXGA_30HZ = 2,
       CAP_OPENNI_QVGA_30HZ = 3,
       CAP_OPENNI_QVGA_60HZ = 4
     };

Gstream

enum { 
     // 默认为1
    CAP_PROP_GSTREAMER_QUEUE_LENGTH = 200
     };

PvAPI

相机SDK

  • CAP_PROP_PVAPI_MULTICASTIP:启用组播主模式的IP。禁用组播为0。
  • CAP_PROP_PVAPI_FRAMESTARTTRIGGERMODE:确定如何启动帧。
  • CAP_PROP_PVAPI_DECIMATIONHORIZONTAL:图像的水平子采样。
  • CAP_PROP_PVAPI_DECIMATIONVERTICAL:图像的垂直子采样。
  • CAP_PROP_PVAPI_BINNINGX:水平合并因子。
  • CAP_PROP_PVAPI_BINNINGY:垂直合并因子。
  • CAP_PROP_PVAPI_PIXELFORMAT:像素格式。

帧启动触发模式

  • CAP_PVAPI_FSTRIGMODE_FREERUN: Freerun模式
  • CAP_PVAPI_FSTRIGMODE_SYNCIN1:同步输入1
  • CAP_PVAPI_FSTRIGMODE_SYNCIN2:同步输入2
  • CAP_PVAPI_FSTRIGMODE_FIXEDRATE:固定速率
  • CAP_PVAPI_FSTRIGMODE_SOFTWARE:软件

水平和垂直子采样

  • CAP_PVAPI_DECIMATION_OFF:关闭
  • CAP_PVAPI_DECIMATION_2OUTOF4:4分之2的2次采样
  • CAP_PVAPI_DECIMATION_2OUTOF8:8分之2的2次采样
  • CAP_PVAPI_DECIMATION_2OUTOF16:16分之2的2次采样

像素格式

  • CAP_PVAPI_PIXELFORMAT_MONO8:单色8位
  • CAP_PVAPI_PIXELFORMAT_MONO16:单色16位
  • CAP_PVAPI_PIXELFORMAT_BAYER8:Bayer8
  • CAP_PVAPI_PIXELFORMAT_BAYER16:Bayer16
  • CAP_PVAPI_PIXELFORMAT_RGB24:RGB24
  • CAP_PVAPI_PIXELFORMAT_BGR24:BGR24
  • CAP_PVAPI_PIXELFORMAT_RGBA32:RGBA32
  • CAP_PVAPI_PIXELFORMAT_BGRA32:BGRA32

XIMEA相机SDK后端

  ​image

  ​image

  ​image

  ​image

  ​image

  ​image

enum { CAP_PROP_XI_DOWNSAMPLING                                 = 400, //!< Change image resolution by binning or skipping.
       CAP_PROP_XI_DATA_FORMAT                                  = 401, //!< Output data format.
       CAP_PROP_XI_OFFSET_X                                     = 402, //!< Horizontal offset from the origin to the area of interest (in pixels).
       CAP_PROP_XI_OFFSET_Y                                     = 403, //!< Vertical offset from the origin to the area of interest (in pixels).
       CAP_PROP_XI_TRG_SOURCE                                   = 404, //!< Defines source of trigger.
       CAP_PROP_XI_TRG_SOFTWARE                                 = 405, //!< Generates an internal trigger. PRM_TRG_SOURCE must be set to TRG_SOFTWARE.
       CAP_PROP_XI_GPI_SELECTOR                                 = 406, //!< Selects general purpose input.
       CAP_PROP_XI_GPI_MODE                                     = 407, //!< Set general purpose input mode.
       CAP_PROP_XI_GPI_LEVEL                                    = 408, //!< Get general purpose level.
       CAP_PROP_XI_GPO_SELECTOR                                 = 409, //!< Selects general purpose output.
       CAP_PROP_XI_GPO_MODE                                     = 410, //!< Set general purpose output mode.
       CAP_PROP_XI_LED_SELECTOR                                 = 411, //!< Selects camera signalling LED.
       CAP_PROP_XI_LED_MODE                                     = 412, //!< Define camera signalling LED functionality.
       CAP_PROP_XI_MANUAL_WB                                    = 413, //!< Calculates White Balance(must be called during acquisition).
       CAP_PROP_XI_AUTO_WB                                      = 414, //!< Automatic white balance.
       CAP_PROP_XI_AEAG                                         = 415, //!< Automatic exposure/gain.
       CAP_PROP_XI_EXP_PRIORITY                                 = 416, //!< Exposure priority (0.5 - exposure 50%, gain 50%).
       CAP_PROP_XI_AE_MAX_LIMIT                                 = 417, //!< Maximum limit of exposure in AEAG procedure.
       CAP_PROP_XI_AG_MAX_LIMIT                                 = 418, //!< Maximum limit of gain in AEAG procedure.
       CAP_PROP_XI_AEAG_LEVEL                                   = 419, //!< Average intensity of output signal AEAG should achieve(in %).
       CAP_PROP_XI_TIMEOUT                                      = 420, //!< Image capture timeout in milliseconds.
       CAP_PROP_XI_EXPOSURE                                     = 421, //!< Exposure time in microseconds.
       CAP_PROP_XI_EXPOSURE_BURST_COUNT                         = 422, //!< Sets the number of times of exposure in one frame.
       CAP_PROP_XI_GAIN_SELECTOR                                = 423, //!< Gain selector for parameter Gain allows to select different type of gains.
       CAP_PROP_XI_GAIN                                         = 424, //!< Gain in dB.
       CAP_PROP_XI_DOWNSAMPLING_TYPE                            = 426, //!< Change image downsampling type.
       CAP_PROP_XI_BINNING_SELECTOR                             = 427, //!< Binning engine selector.
       CAP_PROP_XI_BINNING_VERTICAL                             = 428, //!< Vertical Binning - number of vertical photo-sensitive cells to combine together.
       CAP_PROP_XI_BINNING_HORIZONTAL                           = 429, //!< Horizontal Binning - number of horizontal photo-sensitive cells to combine together.
       CAP_PROP_XI_BINNING_PATTERN                              = 430, //!< Binning pattern type.
       CAP_PROP_XI_DECIMATION_SELECTOR                          = 431, //!< Decimation engine selector.
       CAP_PROP_XI_DECIMATION_VERTICAL                          = 432, //!< Vertical Decimation - vertical sub-sampling of the image - reduces the vertical resolution of the image by the specified vertical decimation factor.
       CAP_PROP_XI_DECIMATION_HORIZONTAL                        = 433, //!< Horizontal Decimation - horizontal sub-sampling of the image - reduces the horizontal resolution of the image by the specified vertical decimation factor.
       CAP_PROP_XI_DECIMATION_PATTERN                           = 434, //!< Decimation pattern type.
       CAP_PROP_XI_TEST_PATTERN_GENERATOR_SELECTOR              = 587, //!< Selects which test pattern generator is controlled by the TestPattern feature.
       CAP_PROP_XI_TEST_PATTERN                                 = 588, //!< Selects which test pattern type is generated by the selected generator.
       CAP_PROP_XI_IMAGE_DATA_FORMAT                            = 435, //!< Output data format.
       CAP_PROP_XI_SHUTTER_TYPE                                 = 436, //!< Change sensor shutter type(CMOS sensor).
       CAP_PROP_XI_SENSOR_TAPS                                  = 437, //!< Number of taps.
       CAP_PROP_XI_AEAG_ROI_OFFSET_X                            = 439, //!< Automatic exposure/gain ROI offset X.
       CAP_PROP_XI_AEAG_ROI_OFFSET_Y                            = 440, //!< Automatic exposure/gain ROI offset Y.
       CAP_PROP_XI_AEAG_ROI_WIDTH                               = 441, //!< Automatic exposure/gain ROI Width.
       CAP_PROP_XI_AEAG_ROI_HEIGHT                              = 442, //!< Automatic exposure/gain ROI Height.
       CAP_PROP_XI_BPC                                          = 445, //!< Correction of bad pixels.
       CAP_PROP_XI_WB_KR                                        = 448, //!< White balance red coefficient.
       CAP_PROP_XI_WB_KG                                        = 449, //!< White balance green coefficient.
       CAP_PROP_XI_WB_KB                                        = 450, //!< White balance blue coefficient.
       CAP_PROP_XI_WIDTH                                        = 451, //!< Width of the Image provided by the device (in pixels).
       CAP_PROP_XI_HEIGHT                                       = 452, //!< Height of the Image provided by the device (in pixels).
       CAP_PROP_XI_REGION_SELECTOR                              = 589, //!< Selects Region in Multiple ROI which parameters are set by width, height, ... ,region mode.
       CAP_PROP_XI_REGION_MODE                                  = 595, //!< Activates/deactivates Region selected by Region Selector.
       CAP_PROP_XI_LIMIT_BANDWIDTH                              = 459, //!< Set/get bandwidth(datarate)(in Megabits).
       CAP_PROP_XI_SENSOR_DATA_BIT_DEPTH                        = 460, //!< Sensor output data bit depth.
       CAP_PROP_XI_OUTPUT_DATA_BIT_DEPTH                        = 461, //!< Device output data bit depth.
       CAP_PROP_XI_IMAGE_DATA_BIT_DEPTH                         = 462, //!< bitdepth of data returned by function xiGetImage.
       CAP_PROP_XI_OUTPUT_DATA_PACKING                          = 463, //!< Device output data packing (or grouping) enabled. Packing could be enabled if output_data_bit_depth > 8 and packing capability is available.
       CAP_PROP_XI_OUTPUT_DATA_PACKING_TYPE                     = 464, //!< Data packing type. Some cameras supports only specific packing type.
       CAP_PROP_XI_IS_COOLED                                    = 465, //!< Returns 1 for cameras that support cooling.
       CAP_PROP_XI_COOLING                                      = 466, //!< Start camera cooling.
       CAP_PROP_XI_TARGET_TEMP                                  = 467, //!< Set sensor target temperature for cooling.
       CAP_PROP_XI_CHIP_TEMP                                    = 468, //!< Camera sensor temperature.
       CAP_PROP_XI_HOUS_TEMP                                    = 469, //!< Camera housing temperature.
       CAP_PROP_XI_HOUS_BACK_SIDE_TEMP                          = 590, //!< Camera housing back side temperature.
       CAP_PROP_XI_SENSOR_BOARD_TEMP                            = 596, //!< Camera sensor board temperature.
       CAP_PROP_XI_CMS                                          = 470, //!< Mode of color management system.
       CAP_PROP_XI_APPLY_CMS                                    = 471, //!< Enable applying of CMS profiles to xiGetImage (see XI_PRM_INPUT_CMS_PROFILE, XI_PRM_OUTPUT_CMS_PROFILE).
       CAP_PROP_XI_IMAGE_IS_COLOR                               = 474, //!< Returns 1 for color cameras.
       CAP_PROP_XI_COLOR_FILTER_ARRAY                           = 475, //!< Returns color filter array type of RAW data.
       CAP_PROP_XI_GAMMAY                                       = 476, //!< Luminosity gamma.
       CAP_PROP_XI_GAMMAC                                       = 477, //!< Chromaticity gamma.
       CAP_PROP_XI_SHARPNESS                                    = 478, //!< Sharpness Strength.
       CAP_PROP_XI_CC_MATRIX_00                                 = 479, //!< Color Correction Matrix element [0][0].
       CAP_PROP_XI_CC_MATRIX_01                                 = 480, //!< Color Correction Matrix element [0][1].
       CAP_PROP_XI_CC_MATRIX_02                                 = 481, //!< Color Correction Matrix element [0][2].
       CAP_PROP_XI_CC_MATRIX_03                                 = 482, //!< Color Correction Matrix element [0][3].
       CAP_PROP_XI_CC_MATRIX_10                                 = 483, //!< Color Correction Matrix element [1][0].
       CAP_PROP_XI_CC_MATRIX_11                                 = 484, //!< Color Correction Matrix element [1][1].
       CAP_PROP_XI_CC_MATRIX_12                                 = 485, //!< Color Correction Matrix element [1][2].
       CAP_PROP_XI_CC_MATRIX_13                                 = 486, //!< Color Correction Matrix element [1][3].
       CAP_PROP_XI_CC_MATRIX_20                                 = 487, //!< Color Correction Matrix element [2][0].
       CAP_PROP_XI_CC_MATRIX_21                                 = 488, //!< Color Correction Matrix element [2][1].
       CAP_PROP_XI_CC_MATRIX_22                                 = 489, //!< Color Correction Matrix element [2][2].
       CAP_PROP_XI_CC_MATRIX_23                                 = 490, //!< Color Correction Matrix element [2][3].
       CAP_PROP_XI_CC_MATRIX_30                                 = 491, //!< Color Correction Matrix element [3][0].
       CAP_PROP_XI_CC_MATRIX_31                                 = 492, //!< Color Correction Matrix element [3][1].
       CAP_PROP_XI_CC_MATRIX_32                                 = 493, //!< Color Correction Matrix element [3][2].
       CAP_PROP_XI_CC_MATRIX_33                                 = 494, //!< Color Correction Matrix element [3][3].
       CAP_PROP_XI_DEFAULT_CC_MATRIX                            = 495, //!< Set default Color Correction Matrix.
       CAP_PROP_XI_TRG_SELECTOR                                 = 498, //!< Selects the type of trigger.
       CAP_PROP_XI_ACQ_FRAME_BURST_COUNT                        = 499, //!< Sets number of frames acquired by burst. This burst is used only if trigger is set to FrameBurstStart.
       CAP_PROP_XI_DEBOUNCE_EN                                  = 507, //!< Enable/Disable debounce to selected GPI.
       CAP_PROP_XI_DEBOUNCE_T0                                  = 508, //!< Debounce time (x * 10us).
       CAP_PROP_XI_DEBOUNCE_T1                                  = 509, //!< Debounce time (x * 10us).
       CAP_PROP_XI_DEBOUNCE_POL                                 = 510, //!< Debounce polarity (pol = 1 t0 - falling edge, t1 - rising edge).
       CAP_PROP_XI_LENS_MODE                                    = 511, //!< Status of lens control interface. This shall be set to XI_ON before any Lens operations.
       CAP_PROP_XI_LENS_APERTURE_VALUE                          = 512, //!< Current lens aperture value in stops. Examples: 2.8, 4, 5.6, 8, 11.
       CAP_PROP_XI_LENS_FOCUS_MOVEMENT_VALUE                    = 513, //!< Lens current focus movement value to be used by XI_PRM_LENS_FOCUS_MOVE in motor steps.
       CAP_PROP_XI_LENS_FOCUS_MOVE                              = 514, //!< Moves lens focus motor by steps set in XI_PRM_LENS_FOCUS_MOVEMENT_VALUE.
       CAP_PROP_XI_LENS_FOCUS_DISTANCE                          = 515, //!< Lens focus distance in cm.
       CAP_PROP_XI_LENS_FOCAL_LENGTH                            = 516, //!< Lens focal distance in mm.
       CAP_PROP_XI_LENS_FEATURE_SELECTOR                        = 517, //!< Selects the current feature which is accessible by XI_PRM_LENS_FEATURE.
       CAP_PROP_XI_LENS_FEATURE                                 = 518, //!< Allows access to lens feature value currently selected by XI_PRM_LENS_FEATURE_SELECTOR.
       CAP_PROP_XI_DEVICE_MODEL_ID                              = 521, //!< Returns device model id.
       CAP_PROP_XI_DEVICE_SN                                    = 522, //!< Returns device serial number.
       CAP_PROP_XI_IMAGE_DATA_FORMAT_RGB32_ALPHA                = 529, //!< The alpha channel of RGB32 output image format.
       CAP_PROP_XI_IMAGE_PAYLOAD_SIZE                           = 530, //!< Buffer size in bytes sufficient for output image returned by xiGetImage.
       CAP_PROP_XI_TRANSPORT_PIXEL_FORMAT                       = 531, //!< Current format of pixels on transport layer.
       CAP_PROP_XI_SENSOR_CLOCK_FREQ_HZ                         = 532, //!< Sensor clock frequency in Hz.
       CAP_PROP_XI_SENSOR_CLOCK_FREQ_INDEX                      = 533, //!< Sensor clock frequency index. Sensor with selected frequencies have possibility to set the frequency only by this index.
       CAP_PROP_XI_SENSOR_OUTPUT_CHANNEL_COUNT                  = 534, //!< Number of output channels from sensor used for data transfer.
       CAP_PROP_XI_FRAMERATE                                    = 535, //!< Define framerate in Hz.
       CAP_PROP_XI_COUNTER_SELECTOR                             = 536, //!< Select counter.
       CAP_PROP_XI_COUNTER_VALUE                                = 537, //!< Counter status.
       CAP_PROP_XI_ACQ_TIMING_MODE                              = 538, //!< Type of sensor frames timing.
       CAP_PROP_XI_AVAILABLE_BANDWIDTH                          = 539, //!< Calculate and returns available interface bandwidth(int Megabits).
       CAP_PROP_XI_BUFFER_POLICY                                = 540, //!< Data move policy.
       CAP_PROP_XI_LUT_EN                                       = 541, //!< Activates LUT.
       CAP_PROP_XI_LUT_INDEX                                    = 542, //!< Control the index (offset) of the coefficient to access in the LUT.
       CAP_PROP_XI_LUT_VALUE                                    = 543, //!< Value at entry LUTIndex of the LUT.
       CAP_PROP_XI_TRG_DELAY                                    = 544, //!< Specifies the delay in microseconds (us) to apply after the trigger reception before activating it.
       CAP_PROP_XI_TS_RST_MODE                                  = 545, //!< Defines how time stamp reset engine will be armed.
       CAP_PROP_XI_TS_RST_SOURCE                                = 546, //!< Defines which source will be used for timestamp reset. Writing this parameter will trigger settings of engine (arming).
       CAP_PROP_XI_IS_DEVICE_EXIST                              = 547, //!< Returns 1 if camera connected and works properly.
       CAP_PROP_XI_ACQ_BUFFER_SIZE                              = 548, //!< Acquisition buffer size in buffer_size_unit. Default bytes.
       CAP_PROP_XI_ACQ_BUFFER_SIZE_UNIT                         = 549, //!< Acquisition buffer size unit in bytes. Default 1. E.g. Value 1024 means that buffer_size is in KiBytes.
       CAP_PROP_XI_ACQ_TRANSPORT_BUFFER_SIZE                    = 550, //!< Acquisition transport buffer size in bytes.
       CAP_PROP_XI_BUFFERS_QUEUE_SIZE                           = 551, //!< Queue of field/frame buffers.
       CAP_PROP_XI_ACQ_TRANSPORT_BUFFER_COMMIT                  = 552, //!< Number of buffers to commit to low level.
       CAP_PROP_XI_RECENT_FRAME                                 = 553, //!< GetImage returns most recent frame.
       CAP_PROP_XI_DEVICE_RESET                                 = 554, //!< Resets the camera to default state.
       CAP_PROP_XI_COLUMN_FPN_CORRECTION                        = 555, //!< Correction of column FPN.
       CAP_PROP_XI_ROW_FPN_CORRECTION                           = 591, //!< Correction of row FPN.
       CAP_PROP_XI_SENSOR_MODE                                  = 558, //!< Current sensor mode. Allows to select sensor mode by one integer. Setting of this parameter affects: image dimensions and downsampling.
       CAP_PROP_XI_HDR                                          = 559, //!< Enable High Dynamic Range feature.
       CAP_PROP_XI_HDR_KNEEPOINT_COUNT                          = 560, //!< The number of kneepoints in the PWLR.
       CAP_PROP_XI_HDR_T1                                       = 561, //!< Position of first kneepoint(in % of XI_PRM_EXPOSURE).
       CAP_PROP_XI_HDR_T2                                       = 562, //!< Position of second kneepoint (in % of XI_PRM_EXPOSURE).
       CAP_PROP_XI_KNEEPOINT1                                   = 563, //!< Value of first kneepoint (% of sensor saturation).
       CAP_PROP_XI_KNEEPOINT2                                   = 564, //!< Value of second kneepoint (% of sensor saturation).
       CAP_PROP_XI_IMAGE_BLACK_LEVEL                            = 565, //!< Last image black level counts. Can be used for Offline processing to recall it.
       CAP_PROP_XI_HW_REVISION                                  = 571, //!< Returns hardware revision number.
       CAP_PROP_XI_DEBUG_LEVEL                                  = 572, //!< Set debug level.
       CAP_PROP_XI_AUTO_BANDWIDTH_CALCULATION                   = 573, //!< Automatic bandwidth calculation.
       CAP_PROP_XI_FFS_FILE_ID                                  = 594, //!< File number.
       CAP_PROP_XI_FFS_FILE_SIZE                                = 580, //!< Size of file.
       CAP_PROP_XI_FREE_FFS_SIZE                                = 581, //!< Size of free camera FFS.
       CAP_PROP_XI_USED_FFS_SIZE                                = 582, //!< Size of used camera FFS.
       CAP_PROP_XI_FFS_ACCESS_KEY                               = 583, //!< Setting of key enables file operations on some cameras.
       CAP_PROP_XI_SENSOR_FEATURE_SELECTOR                      = 585, //!< Selects the current feature which is accessible by XI_PRM_SENSOR_FEATURE_VALUE.
       CAP_PROP_XI_SENSOR_FEATURE_VALUE                         = 586, //!< Allows access to sensor feature value currently selected by XI_PRM_SENSOR_FEATURE_SELECTOR.
     };

ARAVIS 相机API

  • CAP_PROP_ARAVIS_AUTOTRIGGER: 如果相机配置了触发开关,则自动触发帧捕获

IOS AVFoundation框架属性

  • CAP_PROP_IOS_DEVICE_FOCUS: 表示自动对焦模式。
  • CAP_PROP_IOS_DEVICE_EXPOSURE表示曝光模式。
  • CAP_PROP_IOS_DEVICE_FLASH表示闪光灯模式。
  • CAP_PROP_IOS_DEVICE_WHITEBALANCE表示白平衡模式。
  • CAP_PROP_IOS_DEVICE_TORCH表示手电筒模式。

GigE Vision相机属性

  • CAP_PROP_GIGA_FRAME_OFFSET_X: 用于控制图像的偏移量
  • CAP_PROP_GIGA_FRAME_OFFSET_Y: 用于控制图像的偏移量
  • CAP_PROP_GIGA_FRAME_WIDTH_MAX: 用于控制图像的最大宽度
  • CAP_PROP_GIGA_FRAME_HEIGH_MAX: 用于控制图像的最大高度
  • CAP_PROP_GIGA_FRAME_SENS_WIDTH: 用于控制传感器的宽度
  • CAP_PROP_GIGA_FRAME_SENS_HEIGH: 是用于控制传感器的高度

Intel Perceptual Computing SDK

  • CAP_PROP_INTELPERC_PROFILE_COUNT是用于控制Intel Perceptual Computing SDK的配置文件数
  • CAP_PROP_INTELPERC_PROFILE_IDX是用于控制Intel Perceptual Computing SDK的配置文件索引
  • CAP_PROP_INTELPERC_DEPTH_LOW_CONFIDENCE_VALUE是用于控制深度相机低置信度值
  • CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE是用于控制深度相机饱和值
  • CAP_PROP_INTELPERC_DEPTH_CONFIDENCE_THRESHOLD是用于控制深度相机置信度阈值
  • CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_HORZ是用于控制深度相机水平
  • CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_VERT是用于控制深度相机垂直焦距

Intel Perceptual Streams

  • CAP_INTELPERC_DEPTH_GENERATOR: 控制Intel Perceptual Computing SDK的深度
  • CAP_INTELPERC_IMAGE_GENERATOR: 控制Intel Perceptual Computing SDK的图像
  • CAP_INTELPERC_IR_GENERATOR: 控制Intel Perceptual Computing SDK的红外发生器
  • CAP_INTELPERC_GENERATORS_MASK: 控制Intel Perceptual Computing SDK的发生器掩码

  ‍

  • CAP_INTELPERC_DEPTH_MAP表示每个像素都是一个16位整数,该值表示物体到相机的XY平面或笛卡尔深度的距离
  • CAP_INTELPERC_UVDEPTH_MAP表示每个像素都包含两个32位浮点值,范围为0-1,表示深度坐标到颜色坐标的映射;
  • CAP_INTELPERC_IR_MAP表示每个像素都是一个16位整数,该值表示反射激光束的强度;
  • CAP_INTELPERC_IMAGE表示普通图像

gPhoto2属性

  • CAP_PROP_GPHOTO2_PREVIEW表示仅从实时预览模式捕获预览;
  • CAP_PROP_GPHOTO2_WIDGET_ENUMERATE表示只读
  • CAP_PROP_GPHOTO2_RELOAD_CONFIG表示触发器,仅由set触发。重新加载相机设置;
  • CAP_PROP_GPHOTO2_RELOAD_ON_CHANGE表示在设置时重新加载所有设置;
  • CAP_PROP_GPHOTO2_COLLECT_MSGS表示收集带有详细信息的消息;
  • CAP_PROP_GPHOTO2_FLUSH_MSGS表示只读,返回(const char *);
  • CAP_PROP_SPEED表示曝光速度。可以是只读的,取决于相机程序;
  • CAP_PROP_APERTURE表示光圈。可以是只读的,取决于相机程序;
  • CAP_PROP_EXPOSUREPROGRAM表示相机曝光程序;
  • CAP_PROP_VIEWFINDER表示进入实时预览模式。

图像序列属性

  • CAP_PROP_IMAGES_BASE表示图像序列的基础
  • CAP_PROP_IMAGES_LAST表示图像序列的最后一个

VideoCaptureOBSensorDataType

  OBSensor视频捕获的数据类型。

  • CAP_OBSENSOR_DEPTH_MAP用于深度值(以毫米为单位)
  • CAP_OBSENSOR_BGR_IMAGE用于从BGR流生成器中获取的数据,
  • CAP_OBSENSOR_IR_IMAGE用于从IR流生成器中获取的数据(CV_16UC1)。

VideoCaptureOBSensorGenerators

  OBSensor视频捕获的生成器。

  • CAP_OBSENSOR_DEPTH_GENERATOR用于深度生成器
  • CAP_OBSENSOR_IMAGE_GENERATOR用于图像生成器
  • CAP_OBSENSOR_IR_GENERATOR用于IR生成器

VideoCaptureOBSensorProperties

  OBSensor视频捕获的内部属性。

  • CAP_PROP_OBSENSOR_INTRINSIC_FX用于x方向的内部焦距
  • CAP_PROP_OBSENSOR_INTRINSIC_FY用于y方向的内部焦距
  • CAP_PROP_OBSENSOR_INTRINSIC_CX用于x方向的内部光学中心
  • CAP_PROP_OBSENSOR_INTRINSIC_CY用于y方向的内部光学中心

官方示例

video-input-psnr-ssim

  OpenCV: Video Input with OpenCV and similarity measurement

  知识点:

  • 读视频
  • 图像质量评价PSNR(峰值信噪比)和SSIM(结构相似性)
#include <iostream> // for standard I/O
#include <string>   // for strings
#include <iomanip>  // for controlling float print precision
#include <sstream>  // string to number conversion

#include <opencv2/core.hpp>     // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/imgproc.hpp>  // Gaussian Blur
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>  // OpenCV window I/O

using namespace std;
using namespace cv;

double getPSNR ( const Mat& I1, const Mat& I2);
Scalar getMSSIM( const Mat& I1, const Mat& I2);

static void help()
{
    cout
        << "------------------------------------------------------------------------------" << endl
        << "This program shows how to read a video file with OpenCV. In addition, it "
        << "tests the similarity of two input videos first with PSNR, and for the frames "
        << "below a PSNR trigger value, also with MSSIM."                                   << endl
        << "Usage:"                                                                         << endl
        << "./video-input-psnr-ssim <referenceVideo> <useCaseTestVideo> <PSNR_Trigger_Value> <Wait_Between_Frames> " << endl
        << "--------------------------------------------------------------------------"     << endl
        << endl;
}

int main(int argc, char *argv[])
{
    help();
    // 接受4个参数
    // 第一个参数为参考视频
    // 第二个参数为比较视频
    // 第三个参数为相对值,我们用PSNR算法去计算每一帧图像,而仅当PSNR算法计算出的结果低于输入值的时候,用SSIM算法去验证。
    // 第四个参数为延迟等待的时间
    if (argc != 5)
    {
        cout << "没有足够的参数" << endl;
        return -1;
    }

    stringstream conv;

    const string sourceReference = argv[1], sourceCompareWith = argv[2];
    int psnrTriggerValue, delay;
    conv << argv[3] << endl << argv[4];       // put in the strings
    conv >> psnrTriggerValue >> delay;        // take out the numbers

    int frameNum = -1;          // Frame counter
    // 读取视频文件
    VideoCapture captRefrnc(sourceReference), captUndTst(sourceCompareWith);

    if (!captRefrnc.isOpened())
    {
        cout  << "打开参考视频失败 " << sourceReference << endl;
        return -1;
    }

    if (!captUndTst.isOpened())
    {
        cout  << "打开比较视频失败 " << sourceCompareWith << endl;
        return -1;
    }
    // 获取视频的分辨率
    Size refS = Size((int) captRefrnc.get(CAP_PROP_FRAME_WIDTH),
                     (int) captRefrnc.get(CAP_PROP_FRAME_HEIGHT)),
         uTSi = Size((int) captUndTst.get(CAP_PROP_FRAME_WIDTH),
                     (int) captUndTst.get(CAP_PROP_FRAME_HEIGHT));

    if (refS != uTSi)
    {
        cout << "输入分辨率不同!" << endl;
        return -1;
    }

    const char* WIN_UT = "压缩后的视频";
    const char* WIN_RF = "原视频";

    // Windows
    namedWindow(WIN_RF, WINDOW_AUTOSIZE);
    namedWindow(WIN_UT, WINDOW_AUTOSIZE);
    moveWindow(WIN_RF, 400       , 0);         //750,  2 (bernat =0)
    moveWindow(WIN_UT, refS.width, 0);         //1500, 2

    cout << "源视频宽:" << refS.width << "  高:" << refS.height
         << " 帧数:" << captRefrnc.get(CAP_PROP_FRAME_COUNT) << endl;

    cout << "PSNR 值 " << setiosflags(ios::fixed) << setprecision(3)
         << psnrTriggerValue << endl;

    Mat frameReference, frameUnderTest;
    double psnrV;
    Scalar mssimV;

    for(;;) //Show the image captured in the window and repeat
    {
        captRefrnc >> frameReference;
        captUndTst >> frameUnderTest;

        if (frameReference.empty() || frameUnderTest.empty())
        {
            cout << " < < <  Game over!  > > > ";
            break;
        }

        ++frameNum;
        cout << "Frame: " << frameNum << "# ";

        ///////////////////////////////// PSNR ////////////////////////////////////////////////////
        psnrV = getPSNR(frameReference,frameUnderTest);
        cout << setiosflags(ios::fixed) << setprecision(3) << psnrV << "dB";

        //////////////////////////////////// MSSIM /////////////////////////////////////////////////
        if (psnrV < psnrTriggerValue && psnrV)
        {
            // 当psnr值小于输入值时进行MSSIM
            mssimV = getMSSIM(frameReference, frameUnderTest);

            cout << " MSSIM: "
                << " R " << setiosflags(ios::fixed) << setprecision(2) << mssimV.val[2] * 100 << "%"
                << " G " << setiosflags(ios::fixed) << setprecision(2) << mssimV.val[1] * 100 << "%"
                << " B " << setiosflags(ios::fixed) << setprecision(2) << mssimV.val[0] * 100 << "%";
        }

        cout << endl;

        ////////////////////////////////// Show Image /////////////////////////////////////////////
        imshow(WIN_RF, frameReference);
        imshow(WIN_UT, frameUnderTest);

        char c = (char)waitKey(delay);
        if (c == 27) break;
    }

    return 0;
}

// ![get-psnr]
double getPSNR(const Mat& I1, const Mat& I2)
{
    Mat s1;
    absdiff(I1, I2, s1);       // |I1 - I2|
    s1.convertTo(s1, CV_32F);  // cannot make a square on 8 bits
    s1 = s1.mul(s1);           // |I1 - I2|^2

    Scalar s = sum(s1);        // sum elements per channel

    double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels

    if( sse <= 1e-10) // for small values return zero
        return 0;
    else
    {
        double mse  = sse / (double)(I1.channels() * I1.total());
        double psnr = 10.0 * log10((255 * 255) / mse);
        return psnr;
    }
}
// ![get-psnr]

// ![get-mssim]

Scalar getMSSIM( const Mat& i1, const Mat& i2)
{
    const double C1 = 6.5025, C2 = 58.5225;
    /***************************** INITS **********************************/
    int d = CV_32F;

    Mat I1, I2;
    i1.convertTo(I1, d);            // cannot calculate on one byte large values
    i2.convertTo(I2, d);

    Mat I2_2   = I2.mul(I2);        // I2^2
    Mat I1_2   = I1.mul(I1);        // I1^2
    Mat I1_I2  = I1.mul(I2);        // I1 * I2

    /*************************** END INITS **********************************/

    Mat mu1, mu2;                   // PRELIMINARY COMPUTING
    GaussianBlur(I1, mu1, Size(11, 11), 1.5);
    GaussianBlur(I2, mu2, Size(11, 11), 1.5);

    Mat mu1_2   =   mu1.mul(mu1);
    Mat mu2_2   =   mu2.mul(mu2);
    Mat mu1_mu2 =   mu1.mul(mu2);

    Mat sigma1_2, sigma2_2, sigma12;

    GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);
    sigma1_2 -= mu1_2;

    GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);
    sigma2_2 -= mu2_2;

    GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);
    sigma12 -= mu1_mu2;

    ///////////////////////////////// FORMULA ////////////////////////////////
    Mat t1, t2, t3;

    t1 = 2 * mu1_mu2 + C1;
    t2 = 2 * sigma12 + C2;
    t3 = t1.mul(t2);                 // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))

    t1 = mu1_2 + mu2_2 + C1;
    t2 = sigma1_2 + sigma2_2 + C2;
    t1 = t1.mul(t2);                 // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))

    Mat ssim_map;
    divide(t3, t1, ssim_map);        // ssim_map =  t3./t1;

    Scalar mssim = mean(ssim_map);   // mssim = average of ssim map
    return mssim;
}
// ![get-mssim]

PSNR

定义

  参考: 图像处理之图像质量评价指标PSNR(峰值信噪比)_Hard Coder的博客-CSDN博客

  PSNR全称为“Peak Signal-to-Noise Ratio”,中文意思即为峰值信噪比,是衡量图像质量的指标之一。PSNR是基于MSE(均方误差)定义,对给定一个大小为m*n的原始图像I和对其添加噪声后的噪声图像K,其MSE可定义为:

  ​image

  则PSNR可定义为:

  ​image

评价标准

  PSNR值越大,表示图像的质量越好,一般来说:

   (1)高于40dB:说明图像质量极好(即非常接近原始图像)

   (2)30—40dB:通常表示图像质量是好的(即失真可以察觉但可以接受)

   (3)20—30dB:说明图像质量差

   (4)低于20dB:图像质量不可接受

SSIM

  图像处理之图像质量评价指标SSIM(结构相似性)_Hard Coder的博客-CSDN博客

Video-writer

知识点

  • 视频写入

视频结构

  每一个视频文件本质上都是一个容器,文件的扩展名只是表示容器格式(例如 avimov ,或者 mkv )而不是视频和音频的压缩格式。容器里可能会有很多元素,例如视频流,音频流和一些字幕流等等。这些流的储存方式是由每一个流对应的编解码器(codec)决定的。通常来说,视频流很可能使用 mp3aac 格式来储存。而视频格式就更多些,通常是 XVIDDIVXH264LAGS (Lagarith Lossless Codec)等等。具体你能够使用的编码器种类可以在操作系统的编解码器列表里找到。

  限制:

  • 开发者们试图将这个部分尽可能地精简,结果就是OpenCV能够处理的视频只剩下 avi 扩展名的了。
  • 不能创建超过2GB的单个视频,还有就是每个文件里只能支持一个视频流,不能将音频流和字幕流等其他数据放在里面。

源码

#include <iostream>    // for standard I/O
#include <string>   // for strings

#include <opencv2/core.hpp>     // Basic OpenCV structures (cv::Mat)
#include <opencv2/videoio.hpp>  // Video write

using namespace std;
using namespace cv;

static void help()
{
    cout
        << "------------------------------------------------------------------------------" << endl
        << "This program shows how to write video files."                                   << endl
        << "You can extract the R or G or B color channel of the input video."              << endl
        << "Usage:"                                                                         << endl
        << "./video-write <input_video_name> [ R | G | B] [Y | N]"                          << endl
        << "------------------------------------------------------------------------------" << endl
        << endl;
}

int main(int argc, char *argv[])
{
    help();

    // 三个参数
    // 第一个参数为输入文件名
    // 第二个参数为需要转化
    if (argc != 4)
    {
        cout << "Not enough parameters" << endl;
        return -1;
    }

    const string source      = argv[1];           // 原视频文件
    const bool askOutputType = argv[3][0] =='Y';  // If false it will use the inputs codec type

    VideoCapture inputVideo(source);              // Open input
    if (!inputVideo.isOpened())
    {
        cout  << "Could not open the input video: " << source << endl;
        return -1;
    }

    string::size_type pAt = source.find_last_of('.');                  // 找到扩展名的位置
    const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // 新的视频文件名
    int ex = static_cast<int>(inputVideo.get(CAP_PROP_FOURCC));     // 获取编解码器fourcc标识

    // Transform from int to char via Bitwise operators
    char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};

    Size S = Size((int) inputVideo.get(CAP_PROP_FRAME_WIDTH),    // Acquire input size
                  (int) inputVideo.get(CAP_PROP_FRAME_HEIGHT));

    // 打开输出
    VideoWriter outputVideo;
    if (askOutputType)
        outputVideo.open(NAME, ex=-1, inputVideo.get(CAP_PROP_FPS), S, true);
    else
        outputVideo.open(NAME, ex, inputVideo.get(CAP_PROP_FPS), S, true);

    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write: " << source << endl;
        return -1;
    }

    cout << "输入视频宽度:" << S.width << "  高度:" << S.height
         << " 帧数:" << inputVideo.get(CAP_PROP_FRAME_COUNT) << endl;
    cout << "编解码器类型: " << EXT << endl;

    // 选择要保存的通道
    int channel = 2;
    switch(argv[2][0])
    {
    case 'R' : channel = 2; break;
    case 'G' : channel = 1; break;
    case 'B' : channel = 0; break;
    }
    Mat src, res;
    vector<Mat> spl;

    for(;;) //Show the image captured in the window and repeat
    {
        inputVideo >> src;              // read
        if (src.empty()) break;         // check if at end

        // 置空该通道
        split(src, spl);
        for (int i =0; i < 3; ++i)
            if (i != channel)
                spl[i] = Mat::zeros(S, spl[0].type());
       merge(spl, res);

       //outputVideo.write(res); //save or
       outputVideo << res;
    }

    cout << "写入完成" << endl;
    return 0;
}

[^1]: ## 后端标识符

[^2]: ## 通用属性标志

相关实践学习
基于阿里云DeepGPU实例,用AI画唯美国风少女
本实验基于阿里云DeepGPU实例,使用aiacctorch加速stable-diffusion-webui,用AI画唯美国风少女,可提升性能至高至原性能的2.6倍。
相关文章
|
5天前
|
机器学习/深度学习 算法 数据可视化
计算机视觉+深度学习+机器学习+opencv+目标检测跟踪+一站式学习(代码+视频+PPT)-2
计算机视觉+深度学习+机器学习+opencv+目标检测跟踪+一站式学习(代码+视频+PPT)
41 0
|
5天前
|
机器学习/深度学习 Ubuntu Linux
计算机视觉+深度学习+机器学习+opencv+目标检测跟踪+一站式学习(代码+视频+PPT)-1
计算机视觉+深度学习+机器学习+opencv+目标检测跟踪+一站式学习(代码+视频+PPT)
26 1
|
2月前
|
存储 监控 开发工具
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C++)
Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C++)
28 0
|
2月前
|
算法 API 开发工具
Baumer工业相机堡盟工业相机如何使用BGAPISDK和OpenCV生成视频(C++)
Baumer工业相机堡盟工业相机如何使用BGAPISDK和OpenCV生成视频(C++)
23 0
|
2月前
|
计算机视觉 Python 索引
【Python Opencv】图片与视频的操作
【Python Opencv】图片与视频的操作
【Python Opencv】图片与视频的操作
|
3月前
|
机器学习/深度学习 算法 计算机视觉
python入门(六) opencv的安装,图片操作,绘制文字图形,视频操作
python入门(六) opencv的安装,图片操作,绘制文字图形,视频操作
|
3月前
|
编解码 计算机视觉 Python
OpenCV保存摄像头视频和视频文件操作实战(附Python源码)
OpenCV保存摄像头视频和视频文件操作实战(附Python源码)
101 0
|
3月前
|
计算机视觉 Python
OpenCV获取视频文件的属性并动态显示实战(附Python源码)
OpenCV获取视频文件的属性并动态显示实战(附Python源码)
36 0
|
3月前
|
计算机视觉 Python
OpenCV实现视频的暂停播放和继续播放功能实战(附Python源码)
OpenCV实现视频的暂停播放和继续播放功能实战(附Python源码)
44 0
|
3月前
|
计算机视觉 Python
Opencv播放视频文件、将视频转换为灰度视频实战(附Python源码)
Opencv播放视频文件、将视频转换为灰度视频实战(附Python源码)
32 0

相关产品