opencv3/C++實現(xiàn)視頻讀取、視頻寫入
視頻讀取
視頻讀取,主要利用VideoCapture類下的方法打開視頻并獲取視頻中的幀,具體示例如下:
#include<iostream> #include<opencv2/opencv.hpp> using namespace cv; int main() { VideoCapture capture; Mat frame; frame= capture.open("E:/image/a1.avi"); if(!capture.isOpened()) { printf("can not open ...\n"); return -1; } namedWindow("output", CV_WINDOW_AUTOSIZE); while (capture.read(frame)) { imshow("output", frame); waitKey(10); } capture.release(); return 0; }
capture.open()的參數(shù)為0時為讀取攝像頭:
frame= capture.open(0);
視頻寫入
通過攝像頭獲取視頻,然后通過capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)獲取當前幀的寬度和高度,創(chuàng)建一個VideoWriter類對象writer進行視頻的寫入。
寫入前可進行視頻的簡單處理。
#include<iostream> #include<opencv2/opencv.hpp> using namespace cv; int main() { VideoCapture capture; capture.open(0); if(!capture.isOpened()) { printf("can not open ...\n"); return -1; } Size size = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); VideoWriter writer; writer.open("E:/image/a2.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, size, true); Mat frame, gray; namedWindow("output", CV_WINDOW_AUTOSIZE); while (capture.read(frame)) { //轉換為黑白圖像 cvtColor(frame, gray, COLOR_BGR2GRAY); //二值化處理 threshold(gray, gray, 0, 255, THRESH_BINARY | THRESH_OTSU); cvtColor(gray, gray, COLOR_GRAY2BGR); imshow("output", gray); writer.write(gray); waitKey(10); } waitKey(0); capture.release(); return 0; }
以上這篇opencv3/C++實現(xiàn)視頻讀取、視頻寫入就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python 按照sheet合并多個Excel的示例代碼(多個sheet)
這篇文章主要介紹了python 按照sheet合并多個Excel的示例代碼(多個sheet),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Python Pygame實戰(zhàn)之超級炸彈人游戲的實現(xiàn)
如今的玩家們在無聊的時候會玩些什么游戲呢?王者還是吃雞是最多的選擇。但在80、90年代的時候多是一些很簡單的游戲:《超級瑪麗》、《魂斗羅》等。本文將利用Pygame制作另一個經(jīng)典游戲—炸彈人,感興趣的可以了解一下2022-03-03python tensorflow基于cnn實現(xiàn)手寫數(shù)字識別
這篇文章主要為大家詳細介紹了python tensorflow基于cnn實現(xiàn)手寫數(shù)字識別,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01