VideoCapture打开摄像头接口讲解
- VideoCapture
- bool open (int index)
- VideoCapture cap(index)
- open(int cameraNum, int apiPreference)
源码
cap_ffmpeg_impl.hpp
VideoCapture打开视频流文件
- bool open (const String& filename)
- VideoCapture cap(const String & filename)
- bool open(const String & filename, int apiPreference)
VideoCapture 关闭和空间释放
- 关闭和空间释放
- ~VideoCapture
- release()
读取一帧视频
read(OutputArray image)//cap.cpp
bool grab() //读取并解码
virtual bool retrieve(OutputArray image, int flag = 0);//图像色彩转化
vc >> mat
bool VideoCapture::read(OutputArray image)
{
CV_INSTRUMENT_REGION()
if(grab())
retrieve(image);
else
image.release();
return !image.empty();
}
#include<iostream>
#include<stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace cv;
using namespace std;
int main(int argc, char *argv)
{
VideoCapture video;
video.open("1.mp4");
if (!video.isOpened())
{
cout << "open video failed!" << endl;
getchar();
return -1;
}
cout << "open video success!" << endl;
namedWindow("video");
Mat frame;
for (;;)
{
if (!video.read(frame))
{
break;
}
if (frame.empty()) break;
imshow("video", frame);
waitKey(5);
}
getchar();
return 0;
}
#include<iostream>
#include<stdio.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
using namespace cv;
using namespace std;
int main(int argc, char *argv)
{
VideoCapture video;
video.open("1.mp4");
if (!video.isOpened())
{
cout << "open video failed!" << endl;
getchar();
return -1;
}
cout << "open video success!" << endl;
namedWindow("video");
Mat frame;
for (;;)
{
//if (!video.read(frame))
//{
// break;
//}
//读帧与解码
if (!video.grab())
{
break;
}
//转换颜色格式
if (!video.retrieve(frame))
{
break;
}
if (frame.empty()) break;
imshow("video", frame);
waitKey(5);
}
getchar();
return 0;
}