c++ - opencv waitkey 没有响应?

标签 c++ opencv

我是 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/

相关文章:

c++ - 重载、可变参数函数和 bool 类型

c# - 如何为我的坐标系获取 "thinner"图?

c - 移植到较新的 OpenCV 'C' 接口(interface);未找到 cv.h 文件

opencv - opencv warpaffine崩溃

c++ - 使用类型为 'array' 的类成员

c++ - "class has no constructors"类模板错误

c++ - 为避免有符号/无符号比较而进行的 C 转换是否有意义?

python - 运行 .detector (openCV) 时内核死机

opencv - OpenCV中霍夫圆和minEnclosed圆检测圆的区别?

opencv - OpenCV 中使用 ANN 的 OCR 示例