加快OpenCV的输入响应速度

时间:2016-01-21 00:11:16

标签: c++ opencv

我正在编写我的第一个0程序,只是在我的Macbook上使用相机进行一些图像处理。下面的代码只显示了相机,并允许我按1进行普通视图,234更改GRB和#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; } 将其改为黑白色。

不幸的是,我必须按住键才能做出回应。导致此延迟的原因是什么?我如何让代码对输入更敏感?

<table id="tblLayout" style ="width:100%"  cellpadding="0" cellspacing="0" >
            <thead>
                <tr>
                    <th style="width:200px"></th>
                    <th style="width:100px">col1</th>
                    <th style="width:100px">col2</th>
                    <th style="width:500px">col3</th>
                    <th style="width:100px">col4</th>
                    <th style="width:100px">col5</th>
                    <th style="width:1200px">col6</th>                        
                    <td></td>
                </tr>
            </thead>

            <tbody>

1 个答案:

答案 0 :(得分:4)

问题是由您对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
  • 您无需初始化#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; } OpenCV函数的矩阵。

所以这里的完整代码有一些改进:

default