how to convert sequence of images in to video? [closed]

asked 2019-01-03 09:37:02 -0500

arti gravatar image

updated 2019-01-03 09:40:25 -0500

berak gravatar image

Hello all, i want to convert sequence of images into video. i written the code it is not giving any error but the created .avi file is only 5.51 kb and nothing is playing with it. my code is

#include "opencv2/opencv.hpp"
#include <sstream>
#include <iostream>
#include "stdafx.h"

using namespace cv;
using namespace std;

Mat frame, img;
int i = 0;

int main(int, char**)

    Size frame_size(256, 256);

    //Size frame_size(frame_width, frame_height);
    int frames_per_second = 30;

    //Create and initialize the VideoWriter object 

    VideoWriter oVideoWriter("BIKINGSub.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'),
        frames_per_second, frame_size, true);

    int z = 1;
    char filename[80];

    while (1)

        sprintf_s(filename, "SubtractionResultsStatic/BIKING/biking_%d.png", z);
        Mat frame = imread(filename, 1);

        //If the VideoWriter object is not initialized successfully, exit the program

        //resize(frame, frame, Size(320, 180));

        oVideoWriter.write(frame); //writer the frame into the file

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
            cout << "esc key is pressed by user" << endl;


    return 0;

please suggest what i'm doing wrong?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by LBerger
close date 2019-01-03 23:18:03.629386


Mat frame = imread(filename, 1); what does it mean 1 ? use IMREAD_COLOR it will give you an image with three planes (not four as in png file)

check if rame is empty or not

 if (frame.empty())
       cout << "check file path"

a good idea is to close avi file too

LBerger gravatar imageLBerger ( 2019-01-03 09:43:02 -0500 )edit

are the images you read 256x256 ? (that's what you gave to the VideoWriter)

berak gravatar imageberak ( 2019-01-03 09:43:28 -0500 )edit

Use an ostringstream instead of the C sprintf.

For example:

#include <sstream>
#include <iostream>
using namespace std;

int main(void)
    ostringstream filename;

    int x = 123456;

    filename << "Some text " << x;

    cout << filename.str().c_str() << endl;

    return 0;
sjhalayka gravatar imagesjhalayka ( 2019-01-03 13:33:37 -0500 )edit

Thanks a lot sir...problem resolved.

arti gravatar imagearti ( 2019-01-03 23:16:59 -0500 )edit