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))
{
//轉(zhuǎn)換為黑白圖像
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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何在pycharm中配置pyqt5設(shè)計GUI操作教程
這篇文章主要介紹了如何在pycharm中配置pyqt5設(shè)計GUI的操作教程,有需要的朋友可以借鑒參考下,希望大家可以多多交流,討論相關(guān)問題共同提升2021-08-08
解決運行django程序出錯問題 ''str''object has no attribute''_meta''
這篇文章主要介紹了解決運行django程序出錯問題 'str'object has no attribute'_meta',具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
python3.6 +tkinter GUI編程 實現(xiàn)界面化的文本處理工具(推薦)
這篇文章主要介紹了python3.6 +tkinter GUI編程 實現(xiàn)界面化的文本處理工具(推薦)的相關(guān)資料,需要的朋友可以參考下2017-12-12

