2016-09-01 57 views
1

首先,我應該說我是OpenCV的初學者。我試圖將從網絡攝像頭的實時視頻流從RGB轉換爲灰度。轉換實時視頻幀爲灰度(OpenCV)

我在我的函數如下代碼:

VideoCapture cap(0); 

while (true) 
{ 
    Mat frame; 
    Mat grayscale; 
    cvtColor(frame, grayscale, CV_RGB2GRAY); 
    imshow("Debug Window", grayscale); 
    if (waitKey(30) >=0) 
    { 
     cout << "End of Stream"; 
     break; 
    } 
} 

我知道這是不完整的。我試圖找到一種方法來拍攝視頻幀,並將其發送到,使用cvtColor操縱它,然後將其輸出回灰度,以便我可以將其顯示在屏幕上。

如果任何人都可以提供幫助,將不勝感激。

回答

3

請參見本例中,這裏的完整代碼存在,希望這會爲你工作:

#include "opencv2/highgui/highgui.hpp" 
#include <iostream> 

using namespace cv; 
using namespace std; 

int main(int argc, char* argv[]) 
{ 
    VideoCapture cap(0); // open the video camera no. 0 

    if (!cap.isOpened()) // if not success, exit program 
    { 
     cout << "Cannot open the video cam" << endl; 
     return -1; 
    } 


    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); 

    while (1) 
    { 
     Mat frame; 

     bool bSuccess = cap.read(frame); // read a new frame from video 

     if (!bSuccess) 
     { 
      cout << "Cannot read a frame from video stream" << endl; 
      break; 
     } 

     Mat grayscale; 
     cvtColor(frame, grayscale, CV_RGB2GRAY); 

     imshow("MyVideo", grayscale); 

     if (waitKey(30) == 27) 
     { 
      cout << "esc key is pressed by user" << endl; 
      break; 
     } 
    } 
    return 0; 

} 
+0

#include

0

你只是初始化變量「框架」,忘了將圖像分配給它。由於變量「框架」是空的,你不會得到輸出。抓取圖像並從視頻序列「上限」複製到幀。這段代碼將爲你完成這項工作。

Mat frame; 
    bool bSuccess = cap.read(frame); // read a frame from the video 
    if (!bSuccess) 
    { 
     cout << "Cannot read a frame from video stream" << endl; 
     break; 
    }