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时)或等待延迟毫秒(当delay <= 0时)。
所以如果你传递0(或负值)给waitKey,它将永远等待直到按下一个键。
您正在使用Visual Studio吗?代码没有任何问题。在我的例子中,我把Debug
换成了Release
。这是所有。
输入图片描述
相关文章:
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- C++ 信号和插槽不工作:插槽不响应事件
- NodeJs 服务器充斥着 UDP 广播,不发送响应
- C++关于ENUM的问题。我得到的响应比枚举列表大
- 应用程序在打开的简历中捕获视频后没有响应
- 使用 cpprest (Casablanca) 返回 PDF 响应
- 为什么当通过 TCP 发送的消息速率增加时,请求-响应消息对的延迟会降低?
- 如何使用从处理程序调度的最终回调将响应异步返回给调用方on_read?
- 获取加密的正文响应WinHttp HTTPS
- QtConcurrent - 在发布到 UI 线程的数千个结果中保持 GUI 响应
- 同步读取多个 TCP 响应
- HTTP 响应格式不正确?
- 如何使用软化工具包从 OPC UA 服务器异步读取操作回调中的数据值响应中获取 NodeId 详细信息
- HTTP帖子,无需等待响应
- 问题导致程序停止响应,并且姓氏未正确打印
- AT 命令响应解析器
- 将静态 IP 分配给我的 ESP32 服务器后不再响应
- 用于C++的 TCP/IP 解析器和响应器
- 我会导致太多内存泄漏,以至于我的计算机无响应吗?
- Opencv waitkey没有响应