opencv之採集攝像頭資料
阿新 • • 發佈:2019-01-22
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/core/core.hpp>
- usingnamespace cv;
- int main()
- {
- VideoCapture cap(0);
- if(!cap.isOpened())
- {
- return -1;
- }
- Mat frame;
- Mat edges;
- bool stop = false;
- while
- {
- cap>>frame;
- cvtColor(frame, edges, CV_BGR2GRAY);
- GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
- Canny(edges, edges, 0, 30, 3);
- 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類的解構函式會自動幫你完成。