加快OpenCV的输入响应
Speeding up OpenCV's input response
我正在编写我的第一个OpenCV
程序,只是使用Macbook上的相机进行一些图像处理。下面的代码只显示了相机,并允许我按0
进行正常视图,1
、2
或3
从GRB更改为黑白,4
将其更改为黑白。
不幸的是,我必须按住键才能响应。是什么导致了这种延迟,如何使代码对输入做出更快的响应?
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
using namespace cv;
using namespace std;
Mat channel(Mat A, int ich) {
Mat Channel[3];
Mat B = A.clone();
split(B, Channel);
for(int i = 0; i < 3; i++) {
if( ich-1 != i ) Channel[i] = Mat::zeros(B.rows, B.cols, CV_8UC1);
}
merge(Channel, 3, B);
return B;
}
Mat BW(Mat A) {
Mat B;
B = A.clone();
cvtColor( A, B, CV_BGR2GRAY );
return B;
}
int main() {
int waitCount = 1; // wait for this many milliseconds to check for input
VideoCapture stream1(0);
namedWindow("cam", CV_WINDOW_NORMAL);
if( !stream1.isOpened() ) {
cout << "Cannot open camera!" << endl;
}
int showKind = 0;
Mat cameraFrame; // showKind = 0
Mat grey; // showkind = 4
while( true ) {
/// read the cameraFrame
stream1.read(cameraFrame);
/// show the cameraFrame
if( showKind == 0 ) imshow("cam", cameraFrame);
else if( showKind > 0 && showKind < 4 ) imshow("cam", channel(cameraFrame, showKind));
else if( showKind == 4 ) imshow("cam", BW(cameraFrame) );
else {
cout << "ERROR: Unknown showKind = " << showKind << endl;
}
////////////////////////////////////////////////////////////
/// check for input
////////////////////////////////////////////////////////////
// close down
if( waitKey(waitCount) == 27 ) {
cout << "ESC pressed ... exiting" << endl;
break;
}
// convert showKind
else if( waitKey(waitCount) == 48 ) {
cout << "Showkind changed to 0" << endl;
showKind = 0;
}
else if( waitKey(waitCount) == 49 ){
cout << "Showkind changed to 1" << endl;
showKind = 1;
}
else if( waitKey(waitCount) == 50 ){
cout << "Showkind changed to 2" << endl;
showKind = 2;
}
else if( waitKey(waitCount) == 51 ){
cout << "Showkind changed to 3" << endl;
showKind = 3;
}
else if( waitKey(waitCount) == 52 ){
cout << "Showkind changed to 4" << endl;
showKind = 4;
}
}
return 0;
}
该问题是由对waitKey
的级联调用引起的。您只能调用waitKey
一次,并存储按下key
,例如:
int key = waitKey(waitCount);
if (key == 27) {
cout << "ESC pressed ... exiting" << endl;
break;
}
// convert showKind
else if (key == 48) {
cout << "Showkind changed to 0" << endl;
showKind = 0;
}
else if (key == 49){
cout << "Showkind changed to 1" << endl;
showKind = 1;
}
else if (key == 50){
cout << "Showkind changed to 2" << endl;
showKind = 2;
}
else if (key == 51){
cout << "Showkind changed to 3" << endl;
showKind = 3;
}
else if (key == 52){
cout << "Showkind changed to 4" << endl;
showKind = 4;
}
- 使用
switch
语句也可以更清楚。 - 您可以简单地使用
#include <opencv2/opencv.hpp>
而不是所有#include
- 要将矩阵设置为给定值,可以使用
setTo
,因此Channel[i].setTo(0);
- 您不需要初始化
OutputArray
OpenCV 函数的矩阵。
所以这里的完整代码有一些改进:
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat channel(const Mat& A, int ich) {
Mat Channel[3];
Mat B;
split(A, Channel);
for (int i = 0; i < 3; i++) {
if (ich - 1 != i) Channel[i].setTo(0);
}
merge(Channel, 3, B);
return B;
}
Mat BW(const Mat& A) {
Mat B;
cvtColor(A, B, CV_BGR2GRAY);
return B;
}
int main() {
int waitCount = 1; // wait for this many milliseconds to check for input
VideoCapture stream1(0);
namedWindow("cam", CV_WINDOW_NORMAL);
if (!stream1.isOpened()) {
cout << "Cannot open camera!" << endl;
}
int showKind = 0;
Mat cameraFrame; // showKind = 0
Mat grey; // showkind = 4
while (true) {
/// read the cameraFrame
stream1 >> cameraFrame;
/// show the cameraFrame
if (showKind == 0) imshow("cam", cameraFrame);
else if (showKind > 0 && showKind < 4) imshow("cam", channel(cameraFrame, showKind));
else if (showKind == 4) imshow("cam", BW(cameraFrame));
else {
cout << "ERROR: Unknown showKind = " << showKind << endl;
}
////////////////////////////////////////////////////////////
/// check for input1
////////////////////////////////////////////////////////////
// close down
int key = waitKey(waitCount);
if (key == 27) {
cout << "ESC pressed ... exiting" << endl;
break;
}
// convert showKind
else if (key == 48) {
cout << "Showkind changed to 0" << endl;
showKind = 0;
}
else if (key == 49){
cout << "Showkind changed to 1" << endl;
showKind = 1;
}
else if (key == 50){
cout << "Showkind changed to 2" << endl;
showKind = 2;
}
else if (key == 51){
cout << "Showkind changed to 3" << endl;
showKind = 3;
}
else if (key == 52){
cout << "Showkind changed to 4" << endl;
showKind = 4;
}
}
return 0;
}
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在指定的响应之前C++用户输入两次
- 确定QProcess是否已完成执行或正在等待用户输入(或响应)
- 应用程序的响应时间与给定时间段内输入触发器的数量不经意地相关
- 加快OpenCV的输入响应