提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
基本介绍
代码具有以下功能
1.显示图片,以及图片的基操
2.显示视频, 暂停/继续, 进度条
3.摄像头监控
重要内容
1.获取视频参数
video.get(CAP_PROP_FRAME_COUNT);
其中CAP_PROP_FRAME_COUNT代表总帧数,还有这些
CAP_PROP_POS_FRAMES 当前帧
CAP_PROP_FRAME_WIDTH 视频宽度
CAP_PROP_FRAME_HEIGHT 视频长度
CAP_PROP_FPS 视频帧率
2.创建进度条滑块
createTrackbar("进度条", "播放视频", 0, total, fun);
void fun(int num, void*)
{
video.set(CAP_PROP_POS_FRAMES, num);
cont = num;
}
setTrackbarPos("进度条", "播放视频", cont);
3. 保存摄像头视频
VideoWriter video_out;
video_out.open("text.mp4", VideoWriter::fourcc('D', 'I', 'V', 'X'), 7.3, Size(w, h));
video_out.open("text.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'),20.0, Size(width, hight));
video_out.write(img);
代码
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
VideoCapture video;
int cont;
void img_show(const string ph)
{
Mat img = imread(ph), img1;
if (img.empty()) {
cout << "open img error";
exit(0);
}
imshow("原图片", img);
//resize(img, img1, Size(500, 500));
resize(img, img1, Size(), 0.5, 0.5);
imshow("修改后的图片", img1);
while (1)
{
int key = waitKey(0); //指定按键退出" 回车键 "
cout << "key=" << key << endl;
if (key == 13)
{
imwrite("3.jpg", img1);
exit(0);
}
}
}
void fun(int num, void*)
{
video.set(CAP_PROP_POS_FRAMES, num);
cont = num;
}
void video_show(const string ph)
{
if (!video.open(ph))
{
cout << "open video error\n";
exit(0);
}
// 帧率 == 一秒种播放的图片数量 10 20
double fps = video.get(CAP_PROP_FPS);
cout << "fps = " << fps << endl;
int t = 1000 / fps;
Mat img, img1;
cont = video.get(CAP_PROP_POS_FRAMES);
int total = video.get(CAP_PROP_FRAME_COUNT);
//创建滑块
namedWindow("播放视频");
createTrackbar("进度条", "播放视频", 0, total, fun);
while (1) {
video >> img;
if (img.empty()) {
cout << "视频播放结束\n";
break;
}
resize(img, img1, Size(), 0.5, 0.5);
cont = video.get(cv::CAP_PROP_POS_FRAMES); //当前帧数
setTrackbarPos("进度条", "播放视频", cont);
imshow("播放视频", img1);
int key = waitKey(t);
if (key == 13)
break;
else if (key == 32)
{
while (waitKey(0) != 32)
waitKey(0);
}
}
}
void camera_show()
{
VideoWriter video_out;
if (!video.open(0))
{
cout << "open canera error\n";
exit(0);
}
int h = video.get(CAP_PROP_FRAME_HEIGHT);
int w = video.get(CAP_PROP_FRAME_WIDTH);
video_out.open("text.mp4", VideoWriter::fourcc('D', 'I', 'V', 'X'), 7.3, Size(w, h));
Mat img;
while (1)
{
video >> img;
imshow("摄像头监控",img);
video_out.write(img);
int key = waitKey(100);
if ( key == 13)
break;
else if (key == 32) {
imwrite("img.jpg", img);
}
}
video.release();
}
int main(void)
{
camera_show();
return 0;
}