歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV中讀取視頻/camera代碼

OpenCV中讀取視頻/camera代碼

日期:2017/3/1 10:07:15   编辑:Linux編程

發現OpenCV中讀取視頻或者usb camera的方法如此簡單,下面是opencv2.31中實現的讀取攝像頭的代碼:

int main()
{
//打開視頻文件
//cv::VideoCapture capture("bike.avi");
//0 open default camera
cv::VideoCapture capture(0);
//檢查視頻是否打開
if(!capture.isOpened())
return 1;

// 得到幀率
double rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // 現在的視頻幀
cv::namedWindow("Extracted Frame");

// 兩幀之間的間隔時間
int delay= 1000/rate;
// 循環播放所有的幀
while (!stop) {
// 讀下一幀
if (!capture.read(frame))
break;
//在窗口中顯示圖像
cv::imshow("Extracted Frame",frame);
// 按任意鍵停止視頻播放
//if (cv::waitKey(delay)>=0)
// stop= true;
cv::waitKey(20);
}
// 關閉視頻文件
capture.release();
return 0;
}

Copyright © Linux教程網 All Rights Reserved