Opencv實現(xiàn)讀取攝像頭和視頻數(shù)據(jù)
更新時間:2018年01月23日 09:09:03 作者:沉淪的夏天
這篇文章主要為大家詳細(xì)介紹了Opencv實現(xiàn)讀取攝像頭和視頻數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
實際上,按一定速度讀取攝像頭視頻圖像后,便可以對圖像進(jìn)行各種處理了。
那么獲取主要用到的是VideoCapture類,一個demo如下:
//如果有外接攝像頭,則ID為0,內(nèi)置為1,否則用0就可以表示內(nèi)置攝像頭
cv::VideoCapture cap(0);
//判斷攝像頭是否打開
if(!cap.isOpened())
{
return -1;
}
cv::Mat myframe;
cv::Mat edges;
bool stop = false;
while(!stop)
{
//獲取當(dāng)前幀
cap>>myframe;
//轉(zhuǎn)化為灰度圖
cv::cvtColor(myframe, edges, CV_BGR2GRAY);
//高斯濾波器
cv::GaussianBlur(edges, edges, cv::Size(7,7), 1.5, 1.5);
//Canny算子檢測邊緣
cv::Canny(edges, edges, 0, 30, 3);
//顯示邊緣
cv::imshow("current frame",edges);
if(cv::waitKey(30) >=0)
stop = true;
}
cv::waitKey(0);
同樣的,如果要讀取一段視頻文件,視頻文件可以看做是一連串的視頻幀組成,而顯示時設(shè)置一定延時,便可以按一定速度顯示,一個demo如下:
// Open the video file
cv::VideoCapture capture("../images/bike.avi");
// check if video successfully opened
if (!capture.isOpened())
return 1;
// Get the frame rate
double rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame");
// Delay between each frame
// corresponds to video frame rate
int delay= 1000/rate;
//用于設(shè)置幀的移動位置。
input_video.set(CV_CAP_PROP_POS_FRAMES,100);
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
}
// Close the video file
capture.release();
cv::waitKey();
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VSCode 搭建 Arm 遠(yuǎn)程調(diào)試環(huán)境的步驟詳解
這篇文章主要介紹了VSCode 搭建 Arm 遠(yuǎn)程調(diào)試環(huán)境的步驟詳解,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
c語言鏈表基本操作(帶有創(chuàng)建鏈表 刪除 打印 插入)
這篇文章主要介紹了c語言鏈表基本操作,大家參考使用吧2013-12-12
C++數(shù)據(jù)結(jié)構(gòu)深入探究棧與隊列
棧和隊列,嚴(yán)格意義上來說,也屬于線性表,因為它們也都用于存儲邏輯關(guān)系為 "一對一" 的數(shù)據(jù),但由于它們比較特殊,本章講解分別用隊列實現(xiàn)棧與用棧實現(xiàn)隊列2022-05-05

