读取视频并将视频转换成图片




OpenCV视频读取播放,视频转换为图片








介绍几个有关视频读取的函数:

VideoCapture::VideoCapture

         VideoCapture可以从文件中或者摄像头中读取视频,这是提供给C++的接口的,C的接口是CvCapture结构。

      


 
 
  1. <span style= "white-space:pre"> </span>C++: VideoCapture::VideoCapture(conststring& filename)
  2. C++: VideoCapture::VideoCapture( int device)

         上面是初始化VideoCapture对象的,第一个是从文件中读取,第二个是从设备中读取,默认设备在这里是0;

例子:

         


 
 
  1. <span style= "white-space:pre"> </span>VideoCapturecapture(“sample.avi”); //读取sample.avi文件
  2. VideoCapturecapture( 0); //如果只有一个摄像头,capture会得到摄像头的视频。

VideoCapture::Open

         打开视频文件或者视频设备(例如摄像头)。

 


 
 
  1. C++: bool VideoCapture::open( const string& filename)
  2. C++: bool VideoCapture::open( int device)

例子:


 
 
  1. VideoCapturecapture;
  2. capture.open(“sample.avi”); //这里的两句等效于上面的VideoCapturecapture(“sample.avi”),capture.open(0);是一样的

注意:

         上面的视频文件打开,用完后,最好调用capture.release();来关闭刚刚打开的文件

 

VideoCapture::release

         C++: void VideoCapture::release()

         调用该方法关闭刚刚打开的文件。

 

VideoCapture::isOpened

         判断视频是否被打开。


 
 
  1. C++: bool VideoCapture::open( const string& filename)
  2. C++: bool VideoCapture::open( int device)

         成功打开,返回ture;否则false;

 

VideoCapture::grab

         从视频文件中或者设备中获取下一帧,

         C++:bool VideoCapture::grab()
 
 

         该方法成功调用返回ture;主要用于多摄像头情况下,特别当那些摄像头没有实现硬件同步。grab之后,需要调用retrive对获取的帧进行解码。


VideoCapture::retrieve

 <span style="white-space:pre">	</span>C++:bool VideoCapture::retrieve(Mat& image, int channel=0)
 
 

         对grab()得到的帧进行解码。

 

VideoCapture::read

         获取,解码,这个方法结合了grab和retrieve,这个方法比较方便,


 
 
  1. C++:VideoCapture& VideoCapture:: operator>>(Mat& image)
  2. C++: bool VideoCapture::read(Mat& image)


例子1:


 
 
  1. if(!capture.read(frame))
  2. {
  3. cout<< "读取视频失败" << endl;
  4. return -1;
  5. }

例子2:

        capture >>frame;
 
 

    这两个方法都可以,不过第一个能够判断,建议使用第一个,程序更健壮。

 

VideoCapture::get

         返回VideoCapture的一些属性

  C++: double VideoCapture::get(int propId)
 
 

         probId可以是下面的:

·        CV_CAP_PROP_POS_MSEC Currentposition of the video file in milliseconds or video capture timestamp.

·        CV_CAP_PROP_POS_FRAMES 0-basedindex of the frame to be decoded/captured next.

·        CV_CAP_PROP_POS_AVI_RATIO Relativeposition of the video file: 0 - start of the film, 1 - end of the film.

·        CV_CAP_PROP_FRAME_WIDTH Width of theframes in the video stream.

·        CV_CAP_PROP_FRAME_HEIGHT Height ofthe frames in the video stream.

·        CV_CAP_PROP_FPS Frame rate.

·        CV_CAP_PROP_FOURCC 4-charactercode of codec.

·        CV_CAP_PROP_FRAME_COUNT Number offrames in the video file.

·        CV_CAP_PROP_FORMAT Format ofthe Mat objects returned by retrieve() .

·        CV_CAP_PROP_MODE Backend-specificvalue indicating the current capture mode.

·        CV_CAP_PROP_BRIGHTNESS Brightnessof the image (only for cameras).

·        CV_CAP_PROP_CONTRAST Contrast ofthe image (only for cameras).

·        CV_CAP_PROP_SATURATION Saturationof the image (only for cameras).

·        CV_CAP_PROP_HUE Hue of theimage (only for cameras).

·        CV_CAP_PROP_GAIN Gain of theimage (only for cameras).

·        CV_CAP_PROP_EXPOSURE Exposure(only for cameras).

·        CV_CAP_PROP_CONVERT_RGB Booleanflags indicating whether images should be converted to RGB.

·        CV_CAP_PROP_WHITE_BALANCE Currentlynot supported

·        CV_CAP_PROP_RECTIFICATION Rectificationflag for stereo cameras (note: only supported by DC1394 v 2.x backendcurrently)

 

 

VideoCapture::set

         设置VideoCapture的属性:

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

         设置的probId和上面的get一样。

 

下面的是视频读取和将视频内的画面转化为图片的代码,里面的注释应该足够解释了。

 


 
 
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3. #include <opencv2/imgproc/imgproc.hpp>
  4. #include <iostream>
  5. using namespace std;
  6. using namespace cv;
  7. int main()
  8. {
  9. //打开视频文件:其实就是建立一个VideoCapture结构
  10. VideoCapture capture("G:\\视频分析入门练习\\视频分析入门练习 - 附件\\sample.avi");
  11. //检测是否正常打开:成功打开时,isOpened返回ture
  12. if (!capture.isOpened())
  13. cout << "fail toopen!"<< endl;
  14. //获取整个帧数
  15. long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
  16. cout << "整个视频共" << totalFrameNumber << "帧" << endl;
  17. //设置开始帧()
  18. long frameToStart = 1;
  19. capture. set(CV_CAP_PROP_POS_FRAMES, frameToStart);
  20. cout << "从第" <<frameToStart << "帧开始读" << endl;
  21. //设置结束帧
  22. int frameToStop = 30;
  23. if (frameToStop < frameToStart)
  24. {
  25. cout << "结束帧小于开始帧,程序错误,即将退出!" << endl;
  26. return -1;
  27. }
  28. else
  29. {
  30. cout << "结束帧为:第" << frameToStop << "帧" << endl;
  31. }
  32. //获取帧率
  33. double rate = capture.get(CV_CAP_PROP_FPS);
  34. cout << "帧率为:" << rate<< endl;
  35. //定义一个用来控制读取视频循环结束的变量
  36. bool stop = false;
  37. //承载每一帧的图像
  38. Mat frame;
  39. //显示每一帧的窗口
  40. namedWindow( "Extractedframe");
  41. //两帧间的间隔时间:
  42. //int delay = 1000/rate;
  43. double delay = 1000 / rate;
  44. //利用while循环读取帧
  45. //currentFrame是在循环体中控制读取到指定的帧后循环结束的变量
  46. long currentFrame = frameToStart;
  47. //滤波器的核
  48. int kernel_size = 3;
  49. Mat kernel = Mat::ones(kernel_size, kernel_size, CV_32F) / ( float)(kernel_size*kernel_size);
  50. while (!stop)
  51. {
  52. //读取下一帧
  53. if(!capture.read(frame))
  54. {
  55. cout << "读取视频失败" << endl;
  56. return -1;
  57. }
  58. cout << "正在读取第" << currentFrame << "帧" << endl;
  59. imshow( "Extractedframe",frame);
  60. cout << "正在写第" << currentFrame << "帧" << endl;
  61. stringstream str;
  62. str<< "sample" <<currentFrame << ".png";
  63. cout << str.str() << endl;
  64. imwrite(str.str() , frame);
  65. //waitKey(intdelay=0)当delay≤ 0时会永远等待;当delay>0时会等待delay毫秒
  66. //当时间结束前没有按键按下时,返回值为-1;否则返回按键
  67. int c = waitKey(delay);
  68. //按下ESC或者到达指定的结束帧后退出读取视频
  69. if (( char)c == 27 ||currentFrame > frameToStop)
  70. {
  71. stop = true;
  72. }
  73. //按下按键后会停留在当前帧,等待下一次按键
  74. if (c >= 0)
  75. {
  76. waitKey( 0);
  77. }
  78. currentFrame++;
  79. }
  80. //关闭视频文件
  81. capture.release();
  82. waitKey( 0);
  83. return 0;
  84. }


结果:

         读取后的图片,因为图片很多,上面代码在设置的时候,只读取了30帧


        

转自:https://blog.csdn.net/zhonghuan1992/article/details/38469315
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值