我是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;
}
我怎样才能得到&#39; q&#39;退出程序的关键?
答案 0 :(得分:3)
您只需使用一个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;
}
答案 1 :(得分:1)
函数waitKey无限地等待键事件(当延迟&lt; = 0时)或者等待延迟毫秒,当它为正时。
因此,如果您将0(或负值)传递给waitKey,它将永远等待直到按键。
答案 2 :(得分:0)
您正在使用Visual Studio吗?代码没有错。就我而言,我只是将Release
更改为x <- rnorm(100, 0, 1)
。就这样。