c++ - 从image.ppm c++创建视频

标签 c++ opencv

我不怎么用图像序列创建视频。
我的图像保存在文件中(扩展名为 .ppm),文件的类型是如此 FILE *不是Mat,就像我在其他人也想创建视频但也使用Mat类型的主题中找到的那样。
例如,我将5个图像保存在5个文件中(每个文件一个图像),并且我想创建一个具有图像顺序(从image0到image4)的视频,并且视频中的一秒与一个图像匹配。
这是我的代码:

#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;
}
我读了这个话题:create a video from image sequence in open cv
但是答案使用Mat类型(我不知道是否可以转换FILE *-> Mat,我尝试对此进行编码,但不起作用),并且某些变量是未知的,未定义。
此外,我阅读了主题答案的链接,并且教程使用了参数,但是当我执行不带参数的时,程序需要创建视频。
感谢帮助
编辑:我尝试了,但它不起作用。
在循环时间结束时,我添加了: Mat image = imread(filename,cv::IMREAD_COLOR);Mat image = imread(filename,cv::IMREAD_COLOR);images.push_back(image);然后我创建视频:
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;
代码可以编译,但是我无法打开视频...

最佳答案

Writing video with openCV - no key frame set for track 0
此链接可以帮助您了解如何初始化对象VideoWriter。
我将常量用于宽度和高度,但是我应该将矩阵的大小放入代码中:

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();

关于c++ - 从image.ppm c++创建视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63035489/

相关文章:

c++ - 在表达式模板中嵌套子表达式

python - opencv getImage() 错误

c++ - 如何在OpenCV中实现一个等价于bwmorph Matlab函数的函数

python - OpenCV - Otsu 二值化后 RETR_EXTERNAL 不工作

python - 使用cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)时出错

java - 准备位图 byte[] 数据以传递给 jni

c++ - X Window 系统上的顶级窗口

c++ - Kiss FFT 有多快?

c++ - 标准库标识符与用户标识符冲突

c++ - 有9次破坏怎么解释?