我是靠谱客的博主 冷傲小懒虫,这篇文章主要介绍opencv笔记(三十三)——Video Capture使用示例。读取视频1、打开摄像头并存储视频demo2、打开视频文件demo,现在分享给大家,希望可以做个参考。

  这次通过一个读取视频的综合例子来介绍VideoCapture类的一些基本操作:

复制代码
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> using namespace std; using namespace cv; int main() { //打开视频文件:其实就是建立一个VideoCapture结构 VideoCapture capture("D:/videos/PetsD2TeC2.avi"); //检测是否正常打开:成功打开时,isOpened返回ture if(!capture.isOpened()) cout<<"fail to open!"<<endl; //获取整个帧数 long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT); cout<<"整个视频共"<<totalFrameNumber<<"帧"<<endl; //设置开始帧() long frameToStart = 300; capture.set( CV_CAP_PROP_POS_FRAMES,frameToStart); cout<<"从第"<<frameToStart<<"帧开始读"<<endl; //设置结束帧 int frameToStop = 400; if(frameToStop < frameToStart) { cout<<"结束帧小于开始帧,程序错误,即将退出!"<<endl; return -1; } else { cout<<"结束帧为:第"<<frameToStop<<"帧"<<endl; } //获取帧率 double rate = capture.get(CV_CAP_PROP_FPS); cout<<"帧率为:"<<rate<<endl; //定义一个用来控制读取视频循环结束的变量 bool stop = false; //承载每一帧的图像 Mat frame; //显示每一帧的窗口 namedWindow("Extracted frame"); //两帧间的间隔时间: //int delay = 1000/rate; int delay = 1000/rate; //利用while循环读取帧 //currentFrame是在循环体中控制读取到指定的帧后循环结束的变量 long currentFrame = frameToStart; //滤波器的核 int kernel_size = 3; Mat kernel = Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size); while(!stop) { //读取下一帧 if(!capture.read(frame)) { cout<<"读取视频失败"<<endl; return -1; } //这里加滤波程序 imshow("Extracted frame",frame); filter2D(frame,frame,-1,kernel); imshow("after filter",frame); cout<<"正在读取第"<<currentFrame<<"帧"<<endl; //waitKey(int delay=0)当delay ≤ 0时会永远等待;当delay>0时会等待delay毫秒 //当时间结束前没有按键按下时,返回值为-1;否则返回按键 int c = waitKey(delay); //按下ESC或者到达指定的结束帧后退出读取视频 if((char) c == 27 || currentFrame > frameToStop) { stop = true; } //按下按键后会停留在当前帧,等待下一次按键 if( c >= 0) { waitKey(0); } currentFrame++; } //关闭视频文件 capture.release(); waitKey(0); return 0; }

注释比较详尽,相信大家都能看得懂,这里再做几点补充:

1.由于原视频是网络摄像头采集的,所以有很多雪花点,在这里进行了简单的均值滤波处理。

2.虽然VideoCapture类中有grab(捕获下一帧)和retrieve(对该帧进行解码)操作,但是直接用read比较简单。

3.get函数的功能很强大,可以获取关于视频的大部分信息,具体内容可以查看帮助手册。

4.为了保证视频播放的流畅性,帧与帧之间加入了时延。这个时延是通过帧率算出来的。

      影片(video)是由連續的影像(image)組成,組成影片的影像稱為影格(frame),影片播放時會不斷呈現新的影格,影格間的時間稱作更新頻率(frame rate)。由於人類眼睛的結構,通常頻率高於每秒約12個影格的時候,就會認為是連貫的,而電影的拍攝及播放影格率通常為每秒24個影格,對一般人而言算可以接受,當然如果有更快的更新頻率,看起來會更流暢。

        OpenCV有兩種影片來源,分別是讀取硬碟裡的影像檔,或者是用電腦鏡頭讀取的即時影像,兩者使用方式類似,皆使用OpenCV的VideoCapture物件進行這類型的操作,這邊介紹如何使用VideoCapture物件。

VideoCapture建構式

VideoCapture::VideoCapture()

VideoCapture::VideoCapture(const string& filename)

VideoCapture::VideoCapture(int device)

  • filename:影像檔名。
  • device:裝置(像攝影機)的編號。
  • 透過建構式不同的輸入參數,指定VideoCapture()的來源為影片檔或攝影機。

VideoCapture初始化

bool VideoCapture::open(const string& filename)//开视频文件

bool VideoCapture::open(int device) //开摄像头

  • 可以在建構式就指定來源,也可以先用VideoCapture()這個建構式,接著用open()設定來源。

bool VideoWriter::isOpened()

  • 檢查是否初始化成功,如果成功返回true,否則返回false。

VideoCapture讀取影像

讀取影像:VideoCapture& VideoCapture::operator>>(Mat& image)

讀取影像:bool VideoCapture::read(Mat& image)

  • 透過這個函式不斷讀取來源影格,把資訊寫進image。

VideoCapture影像格式

double VideoCapture::get(int propId)

  • 得到影像設定,propId代表返回哪個設定。

bool VideoCapture::set(int propId, double value)

  • 進行影像設定,propId代表針對哪個設定。

1、打开摄像头并存储视频demo

       demo1: 以下程式碼使用電腦的攝影機讀取影像,接著及時秀出影像,用VideoCapture::get()讀取影像的尺寸,waitKey(33)模擬每秒30個frame的效果:

复制代码
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
#include <cstdio> #include <opencv2/opencv.hpp> using namespace cv; int main() { //打开视频文件:其实就是建立一个VideoCapture结构 VideoCapture video(0); //检测是否正常打开:成功打开时,isOpened返回ture if (!video.isOpened()) { return -1; } //检测视频的大小 Size videoSize = Size((int)video.get(CV_CAP_PROP_FRAME_WIDTH), (int)video.get(CV_CAP_PROP_FRAME_HEIGHT)); //显示没一帧的窗口 namedWindow("video demo", CV_WINDOW_AUTOSIZE); //承载每一帧的矩阵图像 Mat videoFrame; while(true){ video >> videoFrame; if(videoFrame.empty()){ break; } imshow("video demo", videoFrame); waitKey(33); } return 0; }

demo 2:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; void main() { VideoCapture capture(0);//如果是笔记本,0打开的是自带的摄像头,1 打开外接的相机 double rate = 25.0;//视频的帧率 Size videoSize(1280,960); VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), rate, videoSize); Mat frame; while (capture.isOpened()) { capture >> frame; writer << frame; imshow("video", frame); if (waitKey(20) == 27)//27是键盘摁下esc时,计算机接收到的ascii码值 { break; } } }

demo 3:用过这个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
#include <opencv2/opencv.hpp> #include <iostream> int main() { cv::VideoCapture capture; capture.open(0);//open 根据编号打开摄像头 std::cout<<"-------------"<<std::endl; if (!capture.isOpened()) { std::cout << "Read video Failed !" << std::endl; return 0; } cv::Mat frame; cv::namedWindow("video test"); int frame_num = 800; for (int i = 0; i < frame_num - 1; ++i) { capture >> frame; //capture.read(frame); imshow("video test", frame); if (cv::waitKey(30) == 'q') { break; } } cv::destroyWindow("video test"); capture.release(); return 0; }

2、打开视频文件demo

demo1:以下程式碼改為讀取avi檔播放,VideoCapture的建構式改為要讀取的avi檔的檔名:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio> #include <opencv2/opencv.hpp> using namespace cv; int main(){ VideoCapture video("VideoTest.avi"); if (!video.isOpened()){ return -1; } Size videoSize = Size((int)video.get(CV_CAP_PROP_FRAME_WIDTH),(int)video.get(CV_CAP_PROP_FRAME_HEIGHT)); namedWindow("video demo", CV_WINDOW_AUTOSIZE); Mat videoFrame; while(true){ video >> videoFrame; if( videoFrame.empty()){ break; } imshow("video demo", videoFrame); waitKey(33); } return 0; }

WaitKey(33)

        当前帧被显示后,等待 33毫秒。如果用户触发了一个按键, c会被设置成这个按键的 ASCII码,否则会被设置成 -1。WaitKey(33) 在此处的另外一个作用是,控制帧率。

demo2:打开视频文件并处理

复制代码
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
#include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; void processiamge(Mat &frame) { circle(frame, Point(cvRound(frame.cols / 2), cvRound(frame.rows / 2)), 150, Scalar(0, 0, 255), 2, 8); } int main() { string filename = "1.avi";//打开的视频文件 VideoCapture capture; capture.open(filename); double rate = capture.get(CV_CAP_PROP_FPS);//获取视频文件的帧率 int delay = cvRound(1000.000 / rate); if (!capture.isOpened())//判断是否打开视频文件 { return -1; } else { while (true) { Mat frame; capture >> frame;//读出每一帧的图像 if (frame.empty()) break; imshow("处理前视频", frame); processiamge(frame); imshow("处理后视频", frame); waitKey(delay); } } return 0; }

demo 3:跟调摄像头的demo3一个套路,容易理解

复制代码
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
#include <opencv2/opencv.hpp> #include <iostream> int main() { cv::VideoCapture capture; capture.open("test.mp4"); std::cout<<"-------------"<<std::endl; if (!capture.isOpened()) { std::cout << "Read video Failed !" << std::endl; return 0; } cv::Mat frame; cv::namedWindow("video test"); capture.get(cv::CAP_PROP_FRAME_COUNT); std::cout << "total frame number is: " << frame_num << std::endl; for (int i = 0; i < frame_num - 1; ++i) { capture >> frame; //capture.read(frame); imshow("video test", frame); if (cv::waitKey(30) == 'q') { break; } } cv::destroyWindow("video test"); capture.release(); return 0; }

结构体 CvCapture

      CvCapture 是一个结构体,用来保存图像捕获所需要的信息。 opencv提供两种方式从外部捕获图像:

        一种是从摄像头中,

        一种是通过解码视频得到图像。

     两种方式都必须从第一帧开始一帧一帧的按顺序获取,因此每获取一帧后都要保存相应的状态和参数。

     比如从视频文件中获取,需要保存视频文件的文件名,相应的解码器类型,下一次如果要获取将需要解码哪一帧等。 这些信息都保存在CvCapture结构中,每获取一帧后,这些信息都将被更新,获取下一帧需要将新信息传给获取的 api接口

 

参考文档:

http://monkeycoding.com/?p=649

http://blog.sina.com.cn/s/blog_68ed8b21010165j3.html

https://blog.csdn.net/hust_bochu_xuchao/article/details/52221865

带进度条demo

http://lib.csdn.net/article/opencv/30181

最后

以上就是冷傲小懒虫最近收集整理的关于opencv笔记(三十三)——Video Capture使用示例。读取视频1、打开摄像头并存储视频demo2、打开视频文件demo的全部内容,更多相关opencv笔记(三十三)——Video内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部