user1002079
user1002079

Reputation: 23

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

Upvotes: 2

Views: 4299

Answers (3)

morynicz
morynicz

Reputation: 2332

Try to use XML/YAML persistence mechanisms of OpenCV. Write a list of image paths for loading and use a loop to read those paths and load images. In the example of stereo calibration such a list is used (there is a directory with examples installed with OpenCV).

Upvotes: 0

user784435
user784435

Reputation:

@Chris code works, I want to add an even simpler routine

void loadImage()
{
int nImages = 6;
for (int i = 0; i < nImages; ++i) 
{
IplImage *image;
char filename[100];
strcpy(filename, "images/");

char frameNo[10];
sprintf(frameNo, "%03i", i);

strcat(filename, frameNo);
strcat(filename, ".jpg");

image = cvLoadImage(filename);
cvNamedWindow("pic");
cvShowImage("pic",image);
cvWaitKey(1000);
}
}

Upvotes: 1

Chris
Chris

Reputation: 8170

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.

Upvotes: 1

Related Questions