实际上,按一定速度读取摄像头视频图像后,便可以对图像进行各种处理了。
那么获取主要用到的是VideoCapture类,一个demo如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//如果有外接摄像头,则ID为0,内置为1,否则用0就可以表示内置摄像头
cv::VideoCapture cap(0);
//判断摄像头是否打开
if (!cap.isOpened())
{
return -1;
}
cv::Mat myframe;
cv::Mat edges;
bool stop = false ;
while (!stop)
{
//获取当前帧
cap>>myframe;
//转化为灰度图
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);
|
同样的,如果要读取一段视频文件,视频文件可以看做是一连串的视频帧组成,而显示时设置一定延时,便可以按一定速度显示,一个demo如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
// 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;
//用于设置帧的移动位置。
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();
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/u014679313/article/details/48953495