opencv 讀取視訊、開啟攝像頭、寫入視訊檔案
阿新 • • 發佈:2019-02-15
1、開啟視訊檔案
#include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; void processiamge(Mat &frame) { circle(frame, Point(cvRound(frame.cols / 2), cvRound(frame.rows / 2)), 150, Scalar(0, 0, 255), 2, 8); } int main() { string filename = "1.avi";//開啟的視訊檔案 VideoCapture capture; capture.open(filename); double rate = capture.get(CV_CAP_PROP_FPS);//獲取視訊檔案的幀率 int delay = cvRound(1000.000 / rate); if (!capture.isOpened())//判斷是否開啟視訊檔案 { return -1; } else { while (true) { Mat frame; capture >> frame;//讀出每一幀的影象 if (frame.empty()) break; imshow("處理前視訊", frame); processiamge(frame); imshow("處理後視訊", frame); waitKey(delay); } } return 0; }
2、開啟攝像頭,採集圖片,並儲存到視訊
主要用到兩個類
VideoCapture 開啟攝像頭
VideoWriter 儲存為視訊檔案
具體引數,可參考 http://blog.csdn.net/yang_xian521/article/details/7440190#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; void main() { VideoCapture capture(0);//如果是筆記本,0開啟的是自帶的攝像頭,1 開啟外接的相機 double rate = 25.0;//視訊的幀率 Size videoSize(1280,960); VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), rate, videoSize); Mat frame; while (capture.isOpened()) { capture >> frame; writer << frame; imshow("video", frame); if (waitKey(20) == 27)//27是鍵盤摁下esc時,計算機接收到的ascii碼值 { break; } } }