1. 程式人生 > >【OpenCV】VideoCapture類解析

【OpenCV】VideoCapture類解析

VideoCaputre 是OpenCV 中用來從攝像頭或視訊檔案或影象序列獲取/處理視訊的類.
Class for video capturing from video files, image sequences or cameras. The class provides C++ API for capturing video from cameras or for reading video files and image sequences.

Read video file

輸入檔案路徑

VideoCapture (const String &filename)

Read from Camera

輸入相機的 device id, 通常內建攝像頭是 0, 外接的從 1 開始增加.

VideoCapture (int index)

比如, 如果外接了camera, 就用外接的. 如果沒有外接的, 就用內建的. 可以這樣寫.

VideoCapture cap(1); // try to open the USB camera
if (!cap.isOpened())  {
	cap.open(0);// try to open the default camera if USB camera failed
	if (!cap.isOpened()) {
		cout << "No Camera Found!"
<< endl; return; } } // do normal bussiness

A full example: Read video, Capture image, Process image, Display image

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return
-1; Mat edges; namedWindow("edges",1); for(;;) { Mat frame; cap >> frame; // get a new frame from camera cvtColor(frame, edges, COLOR_BGR2GRAY); GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5); Canny(edges, edges, 0, 30, 3); imshow("edges", edges); if(waitKey(30) >= 0) break; } // the camera will be deinitialized automatically in VideoCapture destructor return 0; }
  • 就是不需要釋放操作, VideoCapture類的解構函式會自動完成。

相機設定/視訊引數: set()函式

virtual bool cv::VideoCapture::set(int 	propId, double value )	
  • propId: Property identifier
  • value: Value of the property.

Video files related

  • CAP_PROP_POS_MSEC : Current position of the video file in milliseconds.
  • CAP_PROP_POS_FRAMES: 0-based index of the frame to be decoded/captured next.
  • CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
  • CAP_PROP_FRAME_WIDTH: Width of the frames in the video stream.
  • CAP_PROP_FRAME_HEIGHT: Height of the frames in the video stream.
  • CAP_PROP_FPS: Frame rate.
  • CAP_PROP_FOURCC: 4-character code of codec.
  • CAP_PROP_FRAME_COUNT: Number of frames in the video file.
  • CAP_PROP_FORMAT: Format of the Mat objects returned by retrieve() .
  • CAP_PROP_MODE: Backend-specific value indicating the current capture mode.

Camera settings related

  • CAP_PROP_BRIGHTNESS: Brightness of the image (only for cameras).
  • CAP_PROP_CONTRAST: Contrast of the image (only for cameras).
  • CAP_PROP_SATURATION: Saturation of the image (only for cameras).
  • CAP_PROP_HUE: Hue of the image (only for cameras).
  • CAP_PROP_GAIN: Gain of the image (only for cameras).
  • CAP_PROP_EXPOSURE: Exposure (only for cameras).
  • CAP_PROP_CONVERT_RGB: Boolean flags indicating whether images should be converted to RGB.
  • CAP_PROP_WHITE_BALANCE: Currently unsupported
  • CAP_PROP_RECTIFICATION: Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

examples

比如說有些攝像頭會自動開啟自動對焦功能(AutoFocus), 但是實際場景中我們需要禁掉這個功能. 比如, Logitech 的攝像頭, 又沒有官方的SDK可以使用. 這時候, 就可以通過 OpenCV中 VideoCaputure 的 set 函式, 對相機引數進行設定.

	cv::VideoCapture cap(0);
	if (cap.isOpened())
		cap.set(cv::CAP_PROP_AUTOFOCUS, 0); // turn off autofocus

但在實際使用中, 發現這個總是失效, 可能涉及到相機底層有關. 可以換另一個方法,

	cv::VideoCapture cap(0);
	if (cap.isOpened())
		cap.set(cv::CAP_PROP_SETTINGS, 1); // open setting panel

這樣就會彈出一個完整的相機配置框, 選擇想要的引數. 在實際測試中, 這個 100% 成功.

Ref