У меня есть два изображения, которые я загружаю в функцию 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 строк, гдепоявляются операции с указателями.
Спасибо за помощь в решении моей проблемы.