Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ opencv等待键没有响应?_C++_Opencv - Fatal编程技术网

C++ opencv等待键没有响应?

C++ opencv等待键没有响应?,c++,opencv,C++,Opencv,我是opencv新手,也许有些东西我就是不明白。 我有一个等待键,等待字母a,还有一个应该断开,并导致退出。 一个或另一个似乎很好,但不是两个都好。我没有收到编译器错误或警告。包含的代码将为列举的图片拍摄一系列,但当我按下键盘上的字母“q”时不会关闭。 我做错了什么 #include <stdio.h> #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using na

我是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;
}
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv){
视频捕捉帽;
//打开默认相机,使用与0不同的东西,否则;
如果(!cap.open(0))
返回0;
//使用alpha通道创建mat
垫(480、640、CV_8UC4);
int i=0;
为了(;){//永远
垫架;
cap>>框架;
if(frame.empty())中断;//视频流结束
imshow(“这就是你,微笑!:)”,框架);
如果(waitKey(1)=97){//a
字符串名称=格式(“img%04d.png”,i++);//新建!
imwrite(名称、帧);
}
如果(waitKey(1)=113)中断;//按q停止捕获
}
返回0;
}
如何获取退出程序的“q”键?

来源:


waitKey函数无限期地等待按键事件(当延迟时,您只需使用一个
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;
}
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv){
视频捕捉帽;
//打开默认相机,使用与0不同的东西,否则;
如果(!cap.open(0))
返回0;
//使用alpha通道创建mat
垫(480、640、CV_8UC4);
int i=0;
为了(;){//永远
垫架;
cap>>框架;
if(frame.empty())中断;//视频流结束
imshow(“这就是你,微笑!:)”,框架);
//获取按下的值
int key=(waitKey(0)和0xFF);
如果(键=='a'){//a
字符串名称=格式(“img%04d.png”,i++);//新建!
imwrite(名称、帧);
}
else if(key=='q')break;//按q停止捕获
否则{
//按下无效键…继续下一帧
}
}
返回0;
}

您正在使用Visual Studio吗?代码没有问题。对于我的情况,我只是将
调试
更改为
发布
。就这些


waitKey(1)==113
更改为
waitKey(0)==113
。这样,它将等待击键,而不仅仅是1毫秒。该程序将有效退出,但不再更新帧。当我按下“a”键时,它会更新,但不会在之前更新。现在你可以通过按下除a和q之外的任何键进入下一帧。如果要自动转到下一帧,请在waitKey中输入大于0(毫秒)的值。您可以使用
int key=(waitKey(1)&0xFF)