opencv中的访问冲突

时间:2012-02-13 15:22:07

标签: pointers opencv access-violation

我有两个图像,我在main()函数中加载,其指针传递给crossCor()函数。

我面临的问题是代码一旦进入crossCor函数就会翻转,表示遇到了访问冲突。我相信这是我如何使用corrCros函数中的指针操作的问题,即我转移输入图像以计算一些相似性度量。

请好好评论一下代码。从代码中可以看出我是新手。

int main(void)
{
...

IplImage *tempCut=NULL, *resTemplate=NULL, *searchCut=NULL, *resSearch = NULL;

        CvBox2D newBoundingBox = frame1_featBB[i2];
        /* BoundingBox of search image is larger by the offset 
         * therefore an update of the values for resampling is needed*/
        newBoundingBox.size.height = newBoundingBox.size.height+2*offset;
        newBoundingBox.size.width =  newBoundingBox.size.width +2*offset;

        CvSize tempSize = cvSize(frame1_featBB[i2].size.width, frame1_featBB[i2].size.height);
        CvSize newSizeT = cvSize(resamplingFactor*frame1_featBB[i2].size.width, resamplingFactor*frame1_featBB[i2].size.height);
        CvSize searchSize = cvSize(newBoundingBox.size.width, newBoundingBox.size.height);
        CvSize newSizeS = cvSize(resamplingFactor*newBoundingBox.size.width, resamplingFactor*newBoundingBox.size.height);

        /* Memory allocation */
        allocateOnDemand( &tempCut, tempSize, IPL_DEPTH_8U, 1 );    
        allocateOnDemand( &resTemplate, newSizeT, IPL_DEPTH_8U, 1 );
        allocateOnDemand( &searchCut, searchSize, IPL_DEPTH_8U, 1 );
        allocateOnDemand( &resSearch, newSizeS, IPL_DEPTH_8U, 1 );

        /* Cut ROI around the current target */
        templateCut( frame1, tempCut, &frame1_featBB[i2] );
        templateCut( frame2, searchCut, &newBoundingBox );                      

        /* Resample the images for subpixel accuracy */
        resampleIm(resTemplate, tempCut,resamplingFactor);
        resampleIm(resSearch, searchCut, resamplingFactor); 





        /* CORRELATION TASK */
        PointCorr corResampled = crossCor(resSearch, resTemplate);

...
cvReleaseImage(&tempCut);
cvReleaseImage(&resTemplate);
cvReleaseImage(&searchCut);
cvReleaseImage(&resSearch);
}



PointCorr crossCor(IplImage* resSearch, IplImage* resTemplate)
{   

/* Template mean gray values */
uchar* ptr2grayT = (uchar*)(resTemplate->imageData);
float gT_mean;
gT_mean = windowMeanGray(resTemplate, ptr2grayT); 

/* Def of variable search window mean gray values */        
float gS_mean=0;

/* Def of sigmas */
float cor_coeff=0, sigmaTS=0, sigmaT=0, sigmaS=0;


/************ MAIN LOOP ******************************** */
/************ CORRELATION TASK ***************************/

/* Initialization of correlation peaks register */
PointCorr corrPeak;
corrPeak.x = 0;
corrPeak.y = 0;
corrPeak.corrCoeff = -1;

/* Times to loop through */
int loopHeight = resSearch->height - resTemplate->height;
int loopWidth = resSearch->width - resTemplate->width;

/* Pointers ini */
uchar* ptr2grayS = NULL;
uchar* ptrSearch = NULL;  
uchar* ptrTemplate = NULL;


/* These loops work as following:
 *                               resTemplate image is slided over each pixel of resSearch and for each position a correlation coefficient is computed; 
 *                               the first two loops (i & i2) shift over the search image 
 *                               the next two loops (i5 & i6) shift over the template i.e. for each i & i2, i5 & i6 make full loops over resTemplate */


for(int i=0; i<loopHeight; i++)
{
    cout << "row: " << i << endl;
    for(int i2=0; i2<loopWidth; i2++)
    {

        //cout << "next col: " << i2 <<endl;
        sigmaTS=0; sigmaT=0; sigmaS=0;
        ptr2grayS = (uchar*)(resSearch->imageData);
        gS_mean = windowMeanGray(resSearch, ptr2grayS); //mean gray value of resSearch image pixels that coincide with the template (resTemplate); the mean changes as we shift the template to other locations of resSearch

        /* This is a loop computation of corr coeff 
         * for each pix within the search window */
        for(int i5=0; i5<resTemplate->width; i5++)
        {
            ptrSearch = (uchar*)(resSearch->imageData + (i+i5)*resSearch->widthStep)[i2]; //i2 - cols 
            ptrTemplate = (uchar*)(resTemplate->imageData + i5*resTemplate->widthStep);

            for(int i6=0; i6<resTemplate->height; i6++)
            {

            sigmaTS = sigmaTS + (float)(*ptrTemplate - gT_mean)*(float)(*ptrSearch - gS_mean);
            sigmaT  = sigmaT + (float)(*ptrTemplate - gT_mean)*(float)(*ptrTemplate - gT_mean);
            sigmaS  = sigmaS + (float)(*ptrSearch  - gS_mean)*(float)(*ptrSearch  - gS_mean);


            ptrTemplate++;
            ptrSearch++;
            }
        }

        /* Finally the correlation coefficient for a given pixel in resSearch*/
        cor_coeff = sigmaTS/sqrt(sigmaT*sigmaS);
        //cout << "Corr coeff " << cor_coeff << endl;

        /* Save the current highest correlation coefficient */
        if(cor_coeff>corrPeak.corrCoeff)
        {
            corrPeak.corrCoeff = cor_coeff;
            corrPeak.x         = i2;
            corrPeak.y         = i;
            cout << "MAX! " << endl;
        }   
    }       
}

return(corrPeak);

}


我遇到了被阻止的JavaScript问题因此我在这里做出回应。不方便。

我注释掉了指针移位和调试的部分代码。那是代码:

uchar* ptrSearch = NULL;
uchar* ptrTemplate = NULL;

for(int i=0; i<loopHeight; i++)
{
    for(int i2=0; i2<loopWidth; i2++)
    {


        /* This is a loop computation of corr coeff 
         * for each pix within the search window */
        for(int i5=0; i5<resTemplate->width; i5++)
        {
            ptrSearch = (uchar*)(resSearch->imageData + (i+i5)*resSearch->widthStep +i2 ); //i2 - cols 
            ptrTemplate = (uchar*)(resTemplate->imageData + i5*resTemplate->widthStep);

            for(int i6=0; i6<resTemplate->height; i6++)
            {

            ptrTemplate++;
            ptrSearch++;
            }
        }

    }       
}

现在只测试了ptrTemplate和ptrSearch。它抗议的循环内部似乎是计算机情绪的问题。它会循环4次,有时甚至不会循环。如果我让它继续而不是打破它将继续进行另外几个循环/线并再次翻转。

:(

我应该补充一点,它会在指针操作出现的4行中的任何一行停止。

感谢您为解决我的问题做出贡献。

0 个答案:

没有答案