天天看點

opencv學習筆記(1):基于balser相機的視訊流顯示通過opencv實作

本人菜鳥一枚,很多代碼到現在讀起來也挺費勁的,前段時間在http://blog.csdn.net/thefutureisour/article/details/7530177上看到一篇關于攝像頭啟動的博文,學到很多。作為知識儲備,是以寫此博文以友善自己今後的資料查找和學習,話不多說,直接上代碼。

  1. #include <opencv2/highgui/highgui.hpp>  
  2. #include <opencv2/imgproc/imgproc.hpp>  
  3. #include <opencv2/core/core.hpp>  
  4. using namespace cv;  
  5. int main()  
  6. {  
  7.     VideoCapture cap(1);  
  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.         imshow("目前視訊",edges);  
  20.         if(waitKey(30) >=0)  
  21.             stop = true;  
  22.     }  
  23.     return 0;  
  24. }  

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

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

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