2016-01-21 14 views
2

我正在編寫我的第一個OpenCV程序,並在我的Macbook上使用相機進行一些圖像處理。下面的代碼只是顯示相機,並允許我按正常顯示的01,23將GRB和4更改爲黑白。我不得不按下鍵來響應。什麼原因導致了這種延遲,以及如何讓代碼更加響應輸入?加速OpenCV的輸入響應

#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; 
} 

回答

4

該問題是由您的級聯調用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);
  • 你並不需要初始化是OpenCV的職能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; 
}