001-Opencv3之图像显示及视频显示、创建进度条

1、图像显示


using namespace std;
using namespace cv;

//显示图像
void main()
{
	Mat img = imread("E:/004-C++Opencv/OpencvLearning/public/123.jpg");
	if (img.empty()) return;
	namedWindow("ShowImg", WINDOW_AUTOSIZE);
	imshow("ShowImg", img);
	waitKey(0);
}

2、视频读取

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;


void main()
{
	VideoCapture cap;
	cap.open("E:/004-C++Opencv/OpencvLearning/public/123.mp4");
	int fps = cap.get(CV_CAP_PROP_FPS);
	int framenum = cap.get(CV_CAP_PROP_FRAME_COUNT);
	cout << "帧率:" << fps << endl;
	cout << "总帧数:" << framenum << endl;
	Mat frame;
	while(1)/*for (;;)*/
	{
		cap >> frame;
		if (frame.empty())
		{
			break;
		}
		imshow("frame", frame);
		waitKey(1);//waitKey里面的数字表示毫秒,等于也就是如果值为1的话,相当于隔一毫秒显示一帧
	}
	
}

3、读写视频文件

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void main()
{
	VideoCapture cap("../public/123.mp4");
	double fps = cap.get(CAP_PROP_FPS);
	Size size((int)cap.get(CAP_PROP_FRAME_WIDTH), (int)cap.get(CAP_PROP_FRAME_HEIGHT));

	VideoWriter writer;
	writer.open("../public/polar.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, size);
	
	Mat frame;
	for (;;)
	{
		cap >> frame;
		if (frame.empty()) break;
		writer << frame;
	}
	writer.release();
}

4、创建进度条

int g_slider_position = 0;
int g_run = 1, g_donset = 0;
VideoCapture g_cap;

void onTrackbarSlide(int pos, void *)
{
	g_cap.set(CAP_PROP_POS_FRAMES, pos);
	if (!g_donset)
		g_run = 1;
	g_donset = 0;
}

void main()
{
	namedWindow("VI", WINDOW_AUTOSIZE);
    resizeWindow("VI", 500, 500);//设置窗口大小,前提是nameWindow第二个参数为WINDOW_NORMAL
	g_cap.open("E:/004-C++Opencv/OpencvLearning/public/123.mp4");
	int frames = (int)g_cap.get(CAP_PROP_FRAME_COUNT);
	createTrackbar("POS", "VI", &g_slider_position, frames, onTrackbarSlide);
	Mat frame;
	while (true)
	{
		if (g_run != 0)
		{
			g_cap >> frame;
			if (frame.empty()) break;
			int current_pos = (int)g_cap.get(CAP_PROP_POS_FRAMES);
			g_donset = 1;

			setTrackbarPos("POS", "VI", current_pos);
			imshow("VI", frame);
			g_run -= 1;//作用:起到单步运行的效果,如果注释则会直接播放视频
		}
		char c = (char)waitKey(10);//10表示每10毫秒显示一帧
		if (c == 's')
		{
			g_run = 1;// single step
		}
		if (c == 'r')
		{
			g_run = -1;
		}
		if (c == 27)
			break;
		
	}

}

 

 

发布了168 篇原创文章 · 获赞 200 · 访问量 57万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览