繁体   English   中英

opencv的waitkey没有响应?

[英]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;
}

文档中

函数waitKey无限等待键事件(当delay <= 0时),或者等待正数时等待的延迟毫秒。

因此,如果将0(或负值)传递给waitKey,它将永远等待直到按键。

您正在使用Visual Studio吗? 代码没有错。 就我而言,我只是将Debug更改为Release 就这样。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM