OpenCV4 keyboard monitoring function cv2::waitKey( delay )

1. Function prototype:cv::waitKey( delay )

delay:等待时间(毫秒)
作用:通过 waitKey() 可以获取键盘输入

2. Example:

实现键盘输入1,将图像转为灰度图输出;
键盘输入2,将图像转为HSV图输出;
实现键盘输入3,将图像增加亮度输出;
// 键盘响应操作
void QuickDemo::keyBoard(Mat &image) {
    
    
	// 初始化一个空白图像,大小和类型与输入的image图像一样;
	Mat dst = Mat::zeros( image.size(), image.type() );
	while (true){
    
    
		int c = waitKey(100);		// 每次循环等待100毫秒
		if (c==27) {
    
    	// 按Esc退出整个应用程序
			break;
		}
		if (c==49){
    
    
			// 按 1 将图像转换成灰度图像
			std::cout << "you enter key #1" << std::endl;
			cvtColor(image, dst, COLOR_BGR2GRAY);
		}
		if (c == 50) {
    
    
			// 按 2 将图像转换成HSV图像
			std::cout << "you enter key #2" << std::endl;
			cvtColor(image, dst, COLOR_BGR2HSV);
		}
		if (c == 51) {
    
    
			// 按 3 将图像像素进行加法运算
			std::cout << "you enter key #3" << std::endl;
			cvtColor(image, dst, COLOR_BGR2HSV);
			dst = Scalar(60, 60, 60);
			// 求和,dst = dst + image
			add( dst, image, dst );
		}
		namedWindow("键盘响应", WINDOW_NORMAL);
		imshow("键盘响应", dst);

	}
}

Show results:
insert image description here

Guess you like

Origin blog.csdn.net/qq_33867131/article/details/131531758