本人菜鳥一枚,很多代碼到現在讀起來也挺費勁的,前段時間在http://blog.csdn.net/thefutureisour/article/details/7530177上看到一篇關于攝像頭啟動的博文,學到很多。作為知識儲備,是以寫此博文以友善自己今後的資料查找和學習,話不多說,直接上代碼。
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/core/core.hpp>
- using namespace cv;
- int main()
- {
- VideoCapture cap(1);
- if(!cap.isOpened())
- {
- return -1;
- }
- Mat frame;
- Mat edges;
- bool stop = false;
- while(!stop)
- {
- cap>>frame;
- cvtColor(frame, edges, CV_BGR2GRAY);
- imshow("目前視訊",edges);
- if(waitKey(30) >=0)
- stop = true;
- }
- return 0;
- }
1. VideoCapture類有兩種用法,一種是VideoCapture(const string& filename)用來打開視訊檔案,一種是VideoCapture(int device)用來打開裝置。
2. isOpened函數用來檢測VideoCapture類是否打開成功。
3. C++版本的OpenCV有一個明顯的好處,就是不需要釋放操作(不論是視訊還是圖檔),VideoCapture類的析構函數會自動幫你完成。