《随堂小记》 OPENCV+获取视频中某一帧的图像并保存

来源:互联网 发布:阿里云禁止ip访问网站 编辑:程序博客网 时间:2024/06/05 11:34

配置:
opencv3.2
VS2017
Windows10

  1. 下载opencv 并添加path路径之后在VS上配置具体网上很多例子写的都不错
    可以看下http://blog.csdn.net/poem_qianmo/article/details/19809337/

2.代码部分

// GetApictureOfVideos.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "string"#include <iostream>//io流  cout#include <opencv2/highgui/highgui.hpp>//添加此句不出错说明安装配置成功#include <time.h>using namespace std;using namespace cv;void Video_to_Image(string filename){    VideoCapture capture(filename);//获取VideoCapture 对象    if (!capture.isOpened())    {        cout << "open video error";    }    //capture.get十分强大 可以获得帧的很多信息    int frame_width = (int)capture.get(CAP_PROP_FRAME_WIDTH);    int frame_height = (int)capture.get(CAP_PROP_FRAME_HEIGHT);    int frame_number = capture.get(CAP_PROP_FRAME_COUNT);    cout << "frame_width is " << frame_width << endl;    cout << "frame_height is " << frame_height << endl;    cout << "frame_number is " << frame_number << endl;    srand((unsigned)time(NULL));#时间点    long frameToStart = rand() % frame_number;#取  最大帧数之内的 随机数    cout <<"帧开始的地方"<< frameToStart << endl;    capture.set(CAP_PROP_POS_FRAMES, frameToStart);//从此时的帧数开始获取帧    Mat frame; #Mat对象  其实就是图像对象    char image_name[20];    if (!capture.read(frame))    {        cout << "读取视频失败" << endl;     }    imshow("che", frame);//显示    sprintf(image_name, "%s%s", "image", ".jpg");//保存的图片名    imwrite(image_name, frame);#写入  前面是  path+name不要忘了后缀 后面是 帧    capture.release();#释放对象}int main(){    string filename = "F:/1.mp4";    Video_to_Image(filename);    waitKey(0);    return 0;}
阅读全文
0 0
原创粉丝点击