1. 程式人生 > >opencv之採集攝像頭資料

opencv之採集攝像頭資料

  1. #include <opencv2/highgui/highgui.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <opencv2/core/core.hpp>
  4. usingnamespace cv;  
  5. int main()  
  6. {  
  7.     VideoCapture cap(0);  
  8.     if(!cap.isOpened())  
  9.     {  
  10.         return -1;  
  11.     }  
  12.     Mat frame;  
  13.     Mat edges;  
  14.     bool stop = false;  
  15.     while
    (!stop)  
  16.     {  
  17.         cap>>frame;  
  18.         cvtColor(frame, edges, CV_BGR2GRAY);  
  19.         GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);  
  20.         Canny(edges, edges, 0, 30, 3);  
  21.         imshow("當前視訊",edges);  
  22.         if(waitKey(30) >=0)  
  23.             stop = true;  
  24.     }  
  25.     return 0;  
  26. }  

對程式碼的幾點說明:

1. VideoCapture類有兩種用法,一種是VideoCapture(const string& filename)用來開啟視訊檔案,一種是VideoCapture(int device)用來開啟裝置。

2. isOpened函式用來檢測VideoCapture類是否開啟成功。

3. C++版本的OpenCV有一個明顯的好處,就是不需要釋放操作(不論是視訊還是圖片),VideoCapture類的解構函式會自動幫你完成。