前言
VideoCapture:视频捕获相关类
代码实现
1、播放本地视频
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap("./image/1.mp4"); // 打开视频文件
if (!cap.isOpened()) {
cout << "open video fail!" << endl;
return -1;
}
cout << "widht =" << cap.get(CAP_PROP_FRAME_WIDTH) << endl;
cout << "heigt =" << cap.get(CAP_PROP_FRAME_HEIGHT) << endl;
cout << "FPS = " << cap.get(CAP_PROP_FPS) << endl;
for (;;) {
Mat frame;
cap >> frame;
if (frame.empty())
break;
imshow("MP4", frame);
if (waitKey(1000 / cap.get(CAP_PROP_FPS)) == 27)
break;
}
waitKey(0);
destroyAllWindows();
return 0;
}
2、调用摄像头
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap(0); // 打开摄像头
if (!cap.isOpened()) {
cout << "open video fail!" << endl;
return -1;
}
cout << "widht =" << cap.get(CAP_PROP_FRAME_WIDTH) << endl;
cout << "heigt =" << cap.get(CAP_PROP_FRAME_HEIGHT) << endl;
cout << "FPS = " << cap.get(CAP_PROP_FPS) << endl;
for (;;) {
Mat frame;
cap >> frame;
if (frame.empty())
break;
imshow("MP4", frame);
if (waitKey(1000 / cap.get(CAP_PROP_FPS)) == 27)
break;
}
waitKey(0);
destroyAllWindows();
return 0;
}
End
1
版权声明:本文为Daxiu128原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。