[英]opencv waitkey not responding?
我是opencv的新手,也许我只是不了解某些内容。 我有一个等待键,它等待字母a,另一个应该中断,并导致退出。 一个或另一个似乎工作正常,但不能同时工作。 我没有得到编译器错误或警告。 所包含的代码将采用一系列用于枚举的图片,但是当我按键盘上的字母“ q”时不会关闭。 我究竟做错了什么?
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv){
VideoCapture cap;
// open the default camera, use something different from 0 otherwise;
if(!cap.open(0))
return 0;
// Create mat with alpha channel
Mat mat(480, 640, CV_8UC4);
int i = 0;
for(;;){ //forever
Mat frame;
cap >> frame;
if( frame.empty() ) break; // end of video stream
imshow("this is you, smile! :)", frame);
if( waitKey(1) == 97 ){ //a
String name = format("img%04d.png", i++); // NEW !
imwrite(name, frame);
}
if( waitKey(1) == 113 ) break; // stop capturing by pressing q
}
return 0;
}
如何获得“ q”键以退出程序?
您只需要使用一个waitKey
,获取按下的键,然后执行相应的操作即可。
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv){
VideoCapture cap;
// open the default camera, use something different from 0 otherwise;
if (!cap.open(0))
return 0;
// Create mat with alpha channel
Mat mat(480, 640, CV_8UC4);
int i = 0;
for (;;){ //forever
Mat frame;
cap >> frame;
if (frame.empty()) break; // end of video stream
imshow("this is you, smile! :)", frame);
// Get the pressed value
int key = (waitKey(0) & 0xFF);
if (key == 'a'){ //a
String name = format("img%04d.png", i++); // NEW !
imwrite(name, frame);
}
else if (key == 'q') break; // stop capturing by pressing q
else {
// Pressed an invalid key... continue with next frame
}
}
return 0;
}
您正在使用Visual Studio吗? 代码没有错。 就我而言,我只是将Debug
更改为Release
。 就这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.