溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Opencv實現(xiàn)讀取攝像頭和視頻數(shù)據(jù)

發(fā)布時間:2020-10-01 17:12:21 來源:腳本之家 閱讀:205 作者:沉淪的夏天 欄目:編程語言

實際上,按一定速度讀取攝像頭視頻圖像后,便可以對圖像進(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í)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI