c++ - Speeding up OpenCV's input response -


i writing first opencv program , playing around image manipulation using camera on macbook. code below shows camera , allows me press 0 normal view, 1, 2, or 3 change grb , 4 change black , white.

unfortunately, have hold down key respond. causes delay , how can code more responsive input?

#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 = 0; < 3; i++) {         if( ich-1 != ) 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 many milliseconds check 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 cameraframe         stream1.read(cameraframe);          /// show 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 input         ////////////////////////////////////////////////////////////         // close down         if( waitkey(waitcount) == 27 ) {              cout << "esc pressed ... exiting" << endl;             break;         }            // convert showkind         else if( waitkey(waitcount) == 48 ) {              cout << "showkind changed 0" << endl;             showkind = 0;         }            else if( waitkey(waitcount) == 49 ){             cout << "showkind changed 1" << endl;             showkind = 1;         }            else if( waitkey(waitcount) == 50 ){             cout << "showkind changed 2" << endl;             showkind = 2;         }            else if( waitkey(waitcount) == 51 ){             cout << "showkind changed 3" << endl;             showkind = 3;         }            else if( waitkey(waitcount) == 52 ){             cout << "showkind changed 4" << endl;             showkind = 4;         }        }         return 0; } 

the issue caused cascade calls waitkey. can call waitkey once, , store key pressed, like:

int key = waitkey(waitcount); if (key == 27) {     cout << "esc pressed ... exiting" << endl;     break; } // convert showkind else if (key == 48) {     cout << "showkind changed 0" << endl;     showkind = 0; } else if (key == 49){     cout << "showkind changed 1" << endl;     showkind = 1; } else if (key == 50){     cout << "showkind changed 2" << endl;     showkind = 2; } else if (key == 51){     cout << "showkind changed 3" << endl;     showkind = 3; } else if (key == 52){     cout << "showkind changed 4" << endl;     showkind = 4; } 

  • using switch statement clearer.
  • you can use #include <opencv2/opencv.hpp> instead of #include
  • to set matrix given value, can use setto, channel[i].setto(0);
  • you not need initialize matrices outputarray of opencv's functions.

so here full code few improvements:

#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);     (int = 0; < 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 many milliseconds check 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 cameraframe         stream1 >> cameraframe;           /// show 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 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 0" << endl;             showkind = 0;         }         else if (key == 49){             cout << "showkind changed 1" << endl;             showkind = 1;         }         else if (key == 50){             cout << "showkind changed 2" << endl;             showkind = 2;         }         else if (key == 51){             cout << "showkind changed 3" << endl;             showkind = 3;         }         else if (key == 52){             cout << "showkind changed 4" << endl;             showkind = 4;         }     }     return 0; } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -