如何在循环中使用OpenCV记住以前的图像?

时间:2010-12-29 21:13:20

标签: c++ opencv

我想要在循环中处理大约300张图像。在每次迭代中,我都想将图像与前一个循环中的图像进行比较。

我试图将当前图像保存在:

fIplImageHeader 

上一次迭代的图像存储在:

lastFIplImageHeader 

这就是我这样做的原因,它给了我一个例外:

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

}

例外:

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..

1 个答案:

答案 0 :(得分:1)

我建议你在循环外加载第一个图像并从i = 1迭代。 您没有说明如何(如果有的话)在循环外初始化lastFIplImageHeader和fIplImageHeader。
if ( NULL != lastFIplImageHeader->imageData )行中,没有测试lastFIplImageHeader本身不为空 我建议您添加一大堆assert()来验证您使用的数据在尝试操作时是否真正有效。