copencv

How to display successive frames with opencv


I'm looking for a solution to display successive frames in one window using OpenCV. I have a sequence of images (001.jpg, 002.jpg, 003.jpg, etc.), but not a video. I have to display them within a loop.

The standard code to display an image is:

IplImage* src = cvLoadImage("001.jpg");
cvNamedWindow("My pic"); 
cvShowImage("My pic",src); 
cvWaitKey();

Solution

  • As Link suggests, one option would be to write a function that automatically generates the correct filename, for example:

    void loadImage(IplImage *image, int number)
    {
        // Store path to directory
        char filename[100];
        strcpy(filename, "/path/to/files");
    
        // Convert integer to char    
        char frameNo[10];
        sprintf(frame, "%03i", number);
    
        // Combine to generate path
        strcat(filename, frameNo);
        strcat(filename, ".bmp");
    
        // Use path to load image
        image = cvLoadImage(filename);
    }
    

    This could then be used in a loop for a known number of images

    IplImage *im;
    for (int i = 0; i < nImages; ++i)
    {
        loadImage(im, i);
    
        /*
            Do stuff with im
        */
    }
    

    An alternative option would be to investigate the boost directory iterator.