opencv 保存的视频大小总为6KB
灵玲菱 2019-08-12 02:39:16 //#include "stdAfx.h"
#include <opencv2/core/core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <direct.h>//for mk_dir
#include <io.h>//for _acess()
using namespace cv;
using namespace std;
void Image2Video(string path_scr, string path_save);
//图片转视频
void Image2Video(string path_scr, string path_save)
{
char image_name[15];
string s_image_name;
VideoWriter writer;
int isColor = 0;
int frame_fps = 24;
int frame_width = 900;
int frame_height = 800;
string video_name = path_save + "out.avi";
writer = VideoWriter(video_name, CV_FOURCC('X', 'V', 'I', 'D'), frame_fps, Size(frame_width, frame_height), isColor);
cout << "frame_width is " << frame_width << endl;
cout << "frame_height is " << frame_height << endl;
cout << "frame_fps is " << frame_fps << endl;
namedWindow("image to video", 1);
int num = 167;//输入的图片总张数
int i = 1;
Mat img;
while (i <= num)
{
sprintf(image_name, "%d%s", ++i, ".jpg");
s_image_name = path_scr + image_name;
img = imread(s_image_name);//读入图片
if (!img.data)//判断图片调入是否成功
{
cout << "Could not load image file...\n" << endl;
}
//写入
writer<<img;
imshow("image to video", img);
if (waitKey(30) == 27 || i > 166)
{
cout << "touch ESC" << endl;
break;
}
}
}
int main(int argc, char** argv)
{
// string videoFromfile = "E:\\face.mp4"; //读取视频
string Imagespath = "F:\\Visual Studio 2013\\12\\"; // 保存图片的文件夹路径一定要有,因为OpenCV不会自动创建文件夹
string videoTosave = "F:\\Visual Studio 2013\\"; //保存视频路径
//video2image(videoFromfile,Imagespath);
Image2Video(Imagespath, videoTosave);
return 0;
}