cv::VideoCapture
Class Reference
cv::VideoCapture
Class Reference
Class for video capturing from video files, image sequences or cameras.
1. Detailed Description
The class provides C++ API for capturing video from cameras or for reading video files and image sequences.
//============================================================================
// Name : cv::VideoCapture
// Author : Yongqiang Cheng
// Version : Feb 22, 2020
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
//--- INITIALIZE VIDEOCAPTURE
VideoCapture cap;
// open the default camera using default API
// cap.open(0);
// OR advance usage: select any API backend
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open selected camera using selected API
cap.open(deviceID + apiID);
// check if we succeeded
if (!cap.isOpened())
{
cerr << "ERROR! Unable to open camera\n";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl << "Press any key to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty())
{
cerr << "ERROR! blank frame grabbed\n";
break;
}
// show live and wait for a key with timeout long enough to show images
imshow("yongqiang", frame);
if (waitKey(5) >= 0)
{
break;
}
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
16:20:11 **** Build of configuration Debug for project DisplayImage ****
make all
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
Finished building: ../src/DisplayImage.cpp
Building target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "DisplayImage" ./src/DisplayImage.o -lopencv_core -lopencv_video -lopencv_ml -lopencv_imgproc -lopencv_img_hash -lopencv_flann -lopencv_features2d -lopencv_calib3d -lopencv_dnn -lopencv_dnn_objdetect -lopencv_cvv -lopencv_text -lopencv_datasets -lopencv_aruco -lopencv_bgsegm -lopencv_shape -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_bioinspired
Finished building target: DisplayImage
16:20:13 Build Finished (took 1s.739ms)
2. Note
In C API the black-box structure CvCapture
is used instead of VideoCapture
.
- (C++) A basic sample on using the
VideoCapture
interface can be found atOPENCV_SOURCE_CODE/samples/cpp/videocapture_starter.cpp
- (Python) A basic sample on using the
VideoCapture
interface can be found atOPENCV_SOURCE_CODE/samples/python/video.py
- (Python) A multi threaded video processing sample can be found at
OPENCV_SOURCE_CODE/samples/python/video_threaded.py
- (Python)
VideoCapture
sample showcasing some features of theVideo4Linux2
backendOPENCV_SOURCE_CODE/samples/python/video_v4l2.py
backend ['bæk,ɛnd]:n. 后端
3. Enumeration
https://docs.opencv.org/4.2.0/d4/d15/group__videoio__flags__base.html
#include <opencv2/videoio.hpp>
VideoCapture API backends identifier.
VideoCapture API 后端标识符。
enum cv::VideoCaptureAPIs {
cv::CAP_ANY = 0,
cv::CAP_VFW = 200,
cv::CAP_V4L = 200,
cv::CAP_V4L2 = CAP_V4L,
cv::CAP_FIREWIRE = 300,
cv::CAP_FIREWARE = CAP_FIREWIRE,
cv::CAP_IEEE1394 = CAP_FIREWIRE,
cv::CAP_DC1394 = CAP_FIREWIRE,
cv::CAP_CMU1394 = CAP_FIREWIRE,
cv::CAP_QT = 500,
cv::CAP_UNICAP = 600,
cv::CAP_DSHOW = 700,
cv::CAP_PVAPI = 800,
cv::CAP_OPENNI = 900,
cv::CAP_OPENNI_ASUS = 910,
cv::CAP_ANDROID = 1000,
cv::CAP_XIAPI = 1100,
cv::CAP_AVFOUNDATION = 1200,
cv::CAP_GIGANETIX = 1300,
cv::CAP_MSMF = 1400,
cv::CAP_WINRT = 1410,
cv::CAP_INTELPERC = 1500,
cv::CAP_REALSENSE = 1500,
cv::CAP_OPENNI2 = 1600,
cv::CAP_OPENNI2_ASUS = 1610,
cv::CAP_GPHOTO2 = 1700,
cv::CAP_GSTREAMER = 1800,
cv::CAP_FFMPEG = 1900,
cv::CAP_IMAGES = 2000,
cv::CAP_ARAVIS = 2100,
cv::CAP_OPENCV_MJPEG = 2200,
cv::CAP_INTEL_MFX = 2300,
cv::CAP_XINE = 2400
}