我是 opencv 的新手,也许有些地方我只是不明白。 我有一个 waitkey,等待字母 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
,获取按下的键,并采取相应的 Action 。
#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;
}
关于c++ - opencv waitkey 没有响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089304/