How to remember a previous image with OpenCV in a loop?
I have about 300 images which I want to process in a loop. In every iteration I want to compare an image with an image from the previous loop.
I am trying to hold the current image in:
fIplImageHeader
The image from the previous iteration is stored in:
lastF开发者_如何学PythonIplImageHeader
This is how I am doing it and it is giving me an exception:
for ( int i = 0; i < 300; i++ )
{
// get the path to an image (images are named 0.jpg, 1.jpg etc)
// filePath.c_str() contains the path to an image
// load the current image to an IplImage object
IplImage* fIplImageHeader = cvLoadImage( filePath.c_str() );
// create a header for te previous image
IplImage* lastFIplImageHeader = cvCreateImageHeader(
cvSize( fIplImageHeader->width, fIplImageHeader->height ),
fIplImageHeader->depth, fIplImageHeader->nChannels
);
if ( NULL != lastFIplImageHeader->imageData )
{
// COMPARE IMAGES HERE
// only if we are already in 2nd + iteration
}
// copy the current image to the lastFIplImageHeader
// which will hold it in the next iteration
if ( lastFIplImageHeader )
{
cvReleaseImage( &lastFIplImageHeader );
}
cvCopy(fIplImageHeader , lastFIplImageHeader);
// do stuff
if ( fIplImageHeader )
{
cvReleaseImage( &fIplImageHeader );
}
}
The exception:
First-chance exception at 0x753cb727 in Client.exe: Microsoft C++ exception: cv::Exception at memory location 0x0036ef5c..
Unhandled exception at 0x753cb727 in Client.exe: Microsoft C++ exception: cv::Exception at memory location 0x0036ef5c..
I suggest you load the first image outside the loop and iterate from i=1.
You did not show how (if at all) you initialized lastFIplImageHeader and fIplImageHeader outside the loop.
In the line if ( NULL != lastFIplImageHeader->imageData )
there is no test that lastFIplImageHeader
itself is not null.
I suggest you add a whole bunch of assert()
s to verify that the data you use is actually valid when you try to operate on it.
精彩评论