一、设备连接
检查开启状态; win+r键并输入services.msc 检查VMware USB Arbitration Service是否开启; ②打开摄像头
二、播放视频
创建一个test2.cpp文件 并加入以下代码:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
VideoCapture capture("1.mp4");
while(1){
Mat frame;
capture >> frame;
if(frame.empty())
break;
imshow("读取视频帧",frame);
waitKey(30);
}
system("pause");
return 0;
}
编译 test2.cpp 文件; 问题: 这里输出时会报错: 这是因为linux不认识 system(“pause”); 这条语句, 改为: 添加:
#include <unistd.h>
并将
system("pause");
改为
pause();
即可在linux系统下实现保留控制台的效果。 输出结果:
三、录制视频
添加文件test3.cpp 添加以下代码:
#include<iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "error" << endl;
waitKey(0);
return 0;
}
int w = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH));
int h = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
Size videoSize(w, h);
VideoWriter writer("RecordVideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, videoSize);
Mat frame;
int key;
char startOrStop = 1;
char flag = 0;
while (1)
{
cap >> frame;
key = waitKey(100);
if (key == 32)
{
startOrStop = 1 - startOrStop;
if (startOrStop == 0)
{
flag = 1;
}
}
if (key == 27)
{
break;
}
if (startOrStop == 0 && flag==1)
{
writer << frame;
cout << "recording" << endl;
}
else if (startOrStop == 1)
{
flag = 0;
cout << "end recording" << endl;
}
imshow("picture", frame);
}
cap.release();
writer.release();
destroyAllWindows();
return 0;
}
编译: 输出结果:
参考博客
https://blog.csdn.net/ssj925319/article/details/109231145
|