OpenCV中如何通过键盘终止一个正在运行的视频

    OpenCV读取视频极为简单,但是如何在显示过程中终止掉这个视频呢。

    答案就是使用waitKey这个函数,这个函数的定义如下:

CV_EXPORTS_W int waitKey(int delay = 0);
    在delay不为0时将会得到键盘上的信息,那么我们只需要将waitKey读取到的信息进行判定就好了。

    示例代码为:

#include "stdafx.h"
#include <opencv2\opencv.hpp>

int main()
{
	cv::VideoCapture capture_watcher(0);
	cv::Mat Image;
	int flag = 1;
	char inputC = 0;
	while(flag)
	{
 		if (!(capture_watcher.read(Image)))
		{
		}
		cv::imshow("Watcher", Image);
		inputC = cv::waitKey(100);
		if (inputC == 112)
		{
			flag = 0;
		}
	}
    return 0;
}




猜你喜欢

转载自blog.csdn.net/coulson_zhao/article/details/79253458