learning opencv3: 一:overview 打开自己的视频文件加上暂停快进按钮

霸气de小男生 提交于 2020-11-08 11:59:33

 

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

#include <iostream>
using namespace std;

int g_slider_position = 0;
int g_run = 1, g_dontset = 0; //start out in single step mode
cv::VideoCapture g_cap;

void onTrackbarSlide(int pos, void *) {
	g_cap.set(CV_CAP_PROP_POS_FRAMES, pos);  //opencv 2410
	//g_cap.set(CV::CAP_PROP_POS_FRAMES, pos);
	if (!g_dontset)
		g_run = 1;

	g_dontset = 0;
}

int main(int argc, char** argv) {
	cv::namedWindow("Example2_4", CV_WINDOW_AUTOSIZE);
	g_cap.open(string(argv[1]));
	int frames = (int)g_cap.get(CV_CAP_PROP_FRAME_COUNT);
	int tmpw = (int)g_cap.get(CV_CAP_PROP_FRAME_WIDTH);
	int tmph = (int)g_cap.get(CV_CAP_PROP_FRAME_HEIGHT);

	cout << "Video has " << frames << " frames of dimensions("
		<< tmpw << ", " << tmph << ")." << endl;
	cv::createTrackbar("Position", "Example2_4", &g_slider_position, frames,
		onTrackbarSlide);

	cv::Mat frame;
	for (;;) {
		if (g_run != 0) {
			g_cap >> frame; if (frame.empty()) break;

			int current_pos = (int)g_cap.get(CV_CAP_PROP_POS_FRAMES);
			g_dontset = 1;

			cv::setTrackbarPos("Position", "Example2_4", current_pos);
			cv::imshow("Example2_4", frame);
			g_run -= 1;
		}

		char c = (char)cv::waitKey(10);
		if (c == 's') // single step
		{
			g_run = 1; cout << "Single step, run = " << g_run << endl;
		}
		if (c == 'r') // run mode
		{
			g_run = -1; cout << "Run mode, run = " << g_run << endl;
		}
		if (c == 27)
			break;
	}
	return(0);
}
//
//just read a video from our disk

//int main(int argc, char** argv) {
//
//	cv::namedWindow("Example3", cv::WINDOW_AUTOSIZE);
//	cv::VideoCapture cap;
//	cap.open(argv[1]);
//
//	cv::Mat frame;
//	for (;;) {
//		cap >> frame;
//		if (frame.empty()) break; // Ran out of film
//		cv::imshow("Example3", frame);
//		if (cv::waitKey(33) >= 0) break;
//	}
//	return 0;
//}

 

 

 

 

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!