从 image.ppm c++ 创建视频 [英] Create video from image.ppm c++
问题描述
我不知道如何使用图像序列创建视频.
我的图像保存在文件中(扩展名是 .ppm),文件的类型是 FILE* 不像我在别人想要的其他主题中找到的垫子也可以创建视频,但他使用的是 Mat 类型.
例如,我将 5 个图像保存在 5 个文件中(每个文件一个图像),我想按照图像的顺序(图像 0 到图像 4)创建一个视频,视频中的一秒与一张图像匹配.
这是我的代码:
I don't how to create a video with image sequences.
My image are save in file (the extension is .ppm), the type of the file is so FILE* not a Mat like I can found in others topics who someone want to create a video too but he uses Mat type.
For example, i save 5 images in 5 files (one image per file so) and i want to create a video with the order of the image (image0 to image4) and one sec in the video match with one image.
Here my code :
#include <fstream>
#include <iostream>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace cv;
using namespace std;
void colortemp(float temp, FILE* tempfile)
{
float temp_min = -138.0;
//float temp_max = -37.0;
float color_min = 240.0;
//float color_max = 0;
if(temp < 135.0f)
{
temp = 135.0f;
}
if(temp > 310.0f)
{
temp = 310.0f;
}
float a = ( 0.0f - 240.0f) / ( 310.0f - 135.0f);
float b = 240.0f - (a * 135.0f);
float h = (temp * a ) + b;
float S = 1.0f, V = 1.0f; //HSV
float P, Q, T, fract;
unsigned char pix[3];
(h == 360.0f)?(h = 0.0f):(h /= 60.0f);
fract = h - floor(h);
P = (V*(1. - S))*255;
Q = (V*(1. - S*fract))*255;
T = (V*(1. - S*(1. - fract)))*255;
V*=255;
S*=255;
if (0. <= h && h < 1.)
{
pix[0] = (int)V;
pix[1] = (int)T;
pix[2] = (int)P;
}
else if (1. <= h && h < 2.)
{
pix[0] = (int)Q;
pix[1] = (int)V;
pix[2] = (int)P;
}
else if (2. <= h && h < 3.)
{
pix[0] = (int)P;
pix[1] = (int)V;
pix[2] = (int)T;
}
else if (3. <= h && h < 4.)
{
pix[0] = (int)P;
pix[1] = (int)Q;
pix[2] = (int)V;
}
else if (4. <= h && h < 5.)
{
pix[0] = (int)T;
pix[1] = (int)P;
pix[2] = (int)V;
}
else if (5. <= h && h < 6.)
{
pix[0] = (int)V;
pix[1] = (int)P;
pix[2] = (int)Q;
}
else
{
pix[0] = 0;
pix[1] = 0;
pix[2] = 0;
}
fwrite(pix,1,3,tempfile);
}
int main()
{
// TRANSFORM 3D MATRIX INTO 2D MATRIX
vector<vector<vector<float>>>vec3d(10,vector<vector<float>>(10,vector<float>(10)));
vector<vector<float>>vec2d(10,vector<float>(10));
FILE *imageFile;
int height=1000,width=1000;
//int height=10,width=10;
vector<Mat> images;
//srand((unsigned int) time(0));
int cpt = 0;
for(int temps = 0; temps < 5; temps ++)
{
srand(temps);
//Modify the 3d matrix -> add cpt
for(int x = 0; x < 10; x++)
for(int y = 0; y < 10; y++)
for(int z = 0; z < 10; z++)
vec3d[x][y][z] = rand()%300;
//vec3d[x][y][z] = x + cpt;
cpt++;
//Fix z to get a same slice
int z = 8;
//3D matrix to 2D matrix
for(int x = 0; x < 10; x++)
for(int y = 0; y < 10; y++)
vec2d[x][y] = vec3d[x][y][z];
//name of the image
char filename[50]="";
const char* filename_tmp = "image";
string s = to_string(temps);
char const *numberFile = s.c_str();//use char const* as target type
const char* extension = ".ppm";
strcat(filename,filename_tmp);
strcat(filename,numberFile);
strcat(filename,extension);
cout << filename << " = name_file" << endl;
//open the file
imageFile=fopen(filename,"wb");
if(imageFile==NULL)
{
perror("ERROR: Cannot open output file");
exit(EXIT_FAILURE);
}
fprintf(imageFile,"P6\n"); // P6 filetype
fprintf(imageFile,"%d %d\n",width,height); // dimensions
fprintf(imageFile,"255\n"); // Max pixel
for(int x = 0; x < 10; x++)
for(int i = 0; i < 100; i++)
for(int y = 0; y < 10; y++)
for(int k = 0; k <100 ; k++)
colortemp(vec2d[x][y],imageFile);
fclose(imageFile);
}
return 0;
}
我阅读了这个主题:从图像序列创建视频在开放简历中
但是答案使用 Mat 类型(如果可以转换 FILE* -> Mat,我不知道如何进行,我尝试对此进行编码但它不起作用)并且某些变量未知,未定义.
此外,我阅读了主题答案的链接和教程使用参数,但是当我执行不带参数时,程序需要创建视频.
I read this topic : create a video from image sequence in open cv
But the answer use Mat type (I don't how if it's possible to convert FILE* -> Mat, I try to code this but it doesn't work) and some of the variable are unknown, not defined.
Furthermore, I read the link of the answer of the topic and the tutorial use arguments but the program need to create the video when I execute without arguments.
感谢帮助
我试过了,但没有用.
I try that but it doesn't work.
在循环结束时我添加: Mat image = imread(filename,cv::IMREAD_COLOR);
和 Mat image = imread(filename,cv)::IMREAD_COLOR);
和 images.push_back(image);
我创建了视频:
at the end of the loop for the time of my program i add : Mat image = imread(filename,cv::IMREAD_COLOR);
and Mat image = imread(filename,cv::IMREAD_COLOR);
and images.push_back(image);
And i create the video :
VideoWriter outputVideo("outcpp.avi",cv::VideoWriter::fourcc('M','J','P','G'),10,Size(CAP_PROP_FRAME_WIDTH,CAP_PROP_FRAME_HEIGHT));
for(int i=0; i<images.size(); i++){
outputVideo.write(images[i]);
}
outputVideo.release();
cout << "Finished writing" << endl;
代码编译成功了,但是视频打不开...
The code compile but I can't open the video...
推荐答案
此链接有助于了解如何初始化对象 VideoWriter.
我使用常量作为宽度和高度,但我应该输入矩阵的大小,这里是代码:
This link can help to understand how to initialize an object VideoWriter.
I use the constant for the width and height but I should put the size of my matrix , here the code :
sizzz = Size(image.cols, image.rows);
VideoWriter outputVideo("outcpp.avi",cv::VideoWriter::fourcc('M','J','P','G'),2,sizzz);
if(outputVideo.isOpened() == false)
{
cout << "error" << endl;
return -1;
}
for(int i=0; i<images.size(); i++)
{
outputVideo.write(images[i]);
if (images[i].empty())
{
cout << "problem" << endl;
break;
}
}
outputVideo.release();
这篇关于从 image.ppm c++ 创建视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!