openCV视频操作(C++版本) VideoCapture类详解
opencv的视频操作主要用到
VideoCapture类,有三个构造函数:
VideoCapture::VideoCapture()//默认无参构造函数;
VideoCapture::VideoCapture(int device)//参数device指定要打开的摄像头设备,例如(0),(1);
VideoCapture::VideoCapture(const string& filename);
//构造函数中filename 是指要打开的视频文件路径以及名称;
【openCV视频操作(C++版本)】VideoWriter类:
VideoWriter::VideoWriter()
VideoWriter::VideoWriter(const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)
VideoWriter::VideoWriter(const String &filename, int apiPreference, int fourcc, double fps, Size frameSize, bool isColor=true)
filename:输出的路径
fourcc:文件格式
CV_FOURCC(‘P’, ‘I’, ‘M’, ‘1’) = MPEG-1 codec
CV_FOURCC(‘M’, ‘J’, ‘P’, ‘G’) = motion-jpeg codec
CV_FOURCC(‘M’, ‘P’, ‘4’, ‘2’) = MPEG-4.2 codec
CV_FOURCC(‘D’, ‘I’, ‘V’, ‘3’) = MPEG-4.3 codec
CV_FOURCC(‘D’, ‘I’, ‘V’, ‘X’) = MPEG-4 codec
CV_FOURCC(‘U’, ‘2’, ‘6’, ‘3’) = H263 codec
CV_FOURCC(‘I’, ‘2’, ‘6’, ‘3’) = H263I codec
CV_FOURCC(‘F’, ‘L’, ‘V’, ‘1’) = FLV1 codec
-1:会在运行的时候弹出选择框
fps帧率
framesize图像大小
iscolor:彩色图像\灰色图像
头函数
#include
#include
using namespace std;
using namespace cv;
调用摄像头
int main()
{ VideoCapture capture(0);
//打开笔记本自带摄像头(1)为外接摄像头
//或者
//VideoCapture capture;
//capture.open(0)
double rate = 25.0;
//视频的帧率
Size videoSize(1280,960);
VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), rate, videoSize);
while (1)
{
Mat frame;
capture >> frame;
//读取当前帧
writer << frame;
imshow("capture", frame);
if (waitKey(20) == 27)//27是键盘摁下esc时,计算机接收到的ascii码值
{
break;
}
}
return 0;
}
读文件中的视频 capture.get()参数详解
nt main()
{
//VideoCapture capture("C:/Users/dell/Desktop/Wl-1.m4v");
VideoCapture capture;
capture.open("C:/Users/dell/Desktop/Wl-1.m4v");
//检查是否打开成功
if (!capture.isOpened())
cout << "fail to open !" << endl;
//获取整个帧数
long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
//获取视频的帧数
cout << "整个视频共" << totalFrameNumber << "帧" << endl;
//从设置帧开始
long frameToStart = 300;
capture.set(CV_CAP_PROP_POS_FRAMES, frameToStart);
cout << "从第" << frameToStart << "帧开始读" << endl;
int frameTostop=400;
if (frameTostop < frameToStart)
{
cout << "结束帧小于开始帧,错误" << endl;
}
double rate = capture.get(CV_CAP_PROP_FPS);
//获取视频的帧率FPS
cout << "帧率为:" << rate << endl;
while (1)
{
Mat frame;
capture >> frame;
//读取当前帧
imshow("capture", frame);
waitKey(10);
//设置两帧之间的时间间隔。控制帧率,播放视频的时候,越小速度越快。
} return 0;
}
推荐阅读
- opencv|opencv-保存视频操作
- 深度学习|基于opencv的人脸检测
- c++|基于C++的OpenCV(八)图像处理
- Linux|Ubuntu20.4(安装OpenCV4,配置vscode+CMake作为基本开发环境)
- 均值滤波-----python
- OpenCV|【opencv】最近邻插值、双线性插值、双三次插值(三次样条插值)
- OpenCV|【opencv】边界模式 borderMode
- #|【人脸识别】face_recognition 库的使用
- SLAM|<<Slam十四讲>> ch13环境安装