转载请注明出处!!!

http://blog.csdn.net/zhonghuan1992







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








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

VideoCapture::VideoCapture

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

      

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

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

样例:

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

VideoCapture::Open

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

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

样例:

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

注意:

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

VideoCapture::release

C++: void VideoCapture::release()

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

VideoCapture::isOpened

推断视频是否被打开。

         C++:bool VideoCapture::open(const string& filename)
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。这种方法比較方便,

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

样例1:

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

样例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一样。

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

#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("G:\\视频分析入门练习\\视频分析入门练习 - 附件\\sample.avi");
//检測是否正常打开:成功打开时,isOpened返回ture
if (!capture.isOpened())
cout << "fail toopen!"<< endl; //获取整个帧数
long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
cout << "整个视频共" << totalFrameNumber << "帧" << endl; //设置開始帧()
long frameToStart = 1;
capture.set(CV_CAP_PROP_POS_FRAMES, frameToStart);
cout << "从第" <<frameToStart << "帧開始读" << endl; //设置结束帧
int frameToStop = 30; 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("Extractedframe"); //两帧间的间隔时间:
//int delay = 1000/rate;
double 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;
} cout << "正在读取第" << currentFrame << "帧" << endl;
imshow("Extractedframe",frame); cout << "正在写第" << currentFrame << "帧" << endl;
stringstream str;
str<<"sample" <<currentFrame << ".png";
cout << str.str() << endl;
imwrite(str.str() , frame); //waitKey(intdelay=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;
}

结果:

读取后的图片,由于图片非常多。上面代码在设置的时候,仅仅读取了30帧

OpenCV视频读取播放,视频转换为图片的更多相关文章

  1. OpenCV录制视频,播放视频

    一.录制视频 获取摄像头操作 camObj = cv2.VideoCapture(0) :0为默认计算机默认摄像头,多个摄像头依次后面1.2.3 检查是否成功初始化: camObj.isOpen() ...

  2. 使用OpenCV把二进制mnist数据集转换为图片

    mnist数据集是以二进制形式保存的,这里借助OpenCV把mnist数据集转换成图片格式.转换程序如下: #include <iostream> #include <fstream ...

  3. ios——MPMoviePlayerController截取视频缩略图 播放视频又可以截取视频缩略图

    #import <AVKit/AVKit.h>#import <MediaPlayer/MediaPlayer.h>#import "ViewController.h ...

  4. C++视频读取与视频保存

    VideoCapture cap("E:\\122.avi"); //计算视频帧数 int VedioFPS = cap.get(CV_CAP_PROP_FPS); //cout ...

  5. AVAssetReader+AVAssetReaderTrackOutput播放视频

    该文章引用自:http://www.jianshu.com/p/3d5ccbde0de1 IOS 微信聊天发送小视频的秘密(AVAssetReader+AVAssetReaderTrackOutput ...

  6. [转载]matlab视频读取函数VideoReader

    看到以前matlab中读取视频多 使用mmreader等(参考<matlab读取/播放视频的函数>),而现在matlab有一个专门的视频读取类VideoReader完成视频读取的功能. 相 ...

  7. ffmpeg和opencv 播放视频文件和显示器

    ffmpeg它是基于最新版本,在官网下载http://ffmpeg.zeranoe.com/builds/.编译时VS2010配置相关头文件及库的路径就可以.opencv的搭建參考上一个博客. 首先简 ...

  8. OpenCV的视频读取

    现在找一个能拍摄视频的设备真是太容易了.结果大家都用视频来代替以前的序列图像.视频可能由两种形式得到,一个是像网络摄像头那样实时视频流,或者由其他设备产生的压缩编码后的视频文件.幸运的是,OpenCV ...

  9. OpenCV计算机视觉学习(1)——图像基本操作(图像视频读取,ROI区域截取,常用cv函数解释)

    1,计算机眼中的图像 我们打开经典的 Lena图片,看看计算机是如何看待图片的: 我们点击图中的一个小格子,发现计算机会将其分为R,G,B三种通道.每个通道分别由一堆0~256之间的数字组成,那Ope ...

随机推荐

  1. 预编译scss以及scss和less px 转rem

    预编译scss步骤: 1 搜索ruby并安装,点击 2 安装sass: 3 在hubuilder工具中设置预编译: 触发命令地址为ruby安装地址 命令参数为 %FileName% %FileBase ...

  2. mongoDB最新版安装

    转载自:http://www.higis.org/2012/04/25/ubuntu-install-mongodb/ ubuntu上安装mongodb本可以直接通过sudo apt-get inst ...

  3. .NET and php

    原文发布时间为:2011-12-29 -- 来源于本人的百度文章 [由搬家工具导入] http://www.php-compiler.net/blog/2011/phalanger-3-0

  4. 生成静态页面方法 .NET

    原文发布时间为:2009-09-30 -- 来源于本人的百度文章 [由搬家工具导入] 采用模板法:【例子中的两个页面以及生成的页面均在同一个目录,自己可以去改】 模板Template.htm: < ...

  5. 改变querystring值,然后重定向

    原文发布时间为:2009-11-13 -- 来源于本人的百度文章 [由搬家工具导入] 本页面改变querystring值,然后重定向 本页面,避免出现重复querystring。。 如避免出现 www ...

  6. hdu 2654(欧拉函数)

    Become A Hero Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. AC日记——换教室 洛谷 P1850

    题目描述 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 i ( 1≤ i≤n)个时同段上, 两节内容相同的课 ...

  8. EOJ 3.30 A. 打工时不可能打工的【贪心】

    [链接]:https://acm.ecnu.edu.cn/contest/59/problem/A/ A. 打工时不可能打工的 Time limit per test: 2.0 seconds Mem ...

  9. 【spring boot logback】日志logback格式解析

    日志logback格式解析 logback官网 格式解析 https://logback.qos.ch/manual/layouts.html#ClassicPatternLayout 官网格式解析有 ...

  10. hibernate多对一单向配置

    查看:http://blog.csdn.net/u010702229/article/details/13170263