-2

我想在不使用 cvcanny 函数的情况下将 canny 边缘检测应用于图像,因此所需步骤的一部分是应用高斯蒙版,我有 2 个蒙版用于 x 和 y 方向。现在的问题是,每当我尝试将掩码卷积到我的图像上时,由于“访问冲突”而导致执行中断。为什么会发生这种情况,我该如何克服?

//DECLARATIONS..
double maskx[3][3];
double masky[3][3];
double convx[1000][1000]={0};
double convy[1000][1000]={0};
double M[1000][1000]={0};  //magnitude
double slope[1000][1000];


int gaussian_mask()
{
int MaskRadius=SIGMA*3;
double eq1;
double exp=2.71828183;

for(int p=-MaskRadius; p<=MaskRadius; p++)
{
for(int q=-MaskRadius; q<=MaskRadius; q++)
{
    eq1=-1*(p*p + q*q)/(2*SIGMA);
    maskx[p+MaskRadius][q+MaskRadius]=-1*q*(pow(exp,eq1));
    masky[p+MaskRadius][q+MaskRadius]=-1*p*(pow(exp,eq1));
}
}

return MaskRadius;
}

IplImage* convolve(IplImage *im)
{
int MaskRadius=gaussian_mask();
int row=im->width;
int col=im->height;
printf("row: %d, col= %d",row,col);

//-----------------------------------------------------------------------------------//

IplImage *pix=cvCreateImage(cvGetSize(im), im->depth, 1);      //converting 3 channel   to 1 channel
cvSetImageCOI(im,1);
cvCopy(im,pix);
cout<<endl<<"No. of channels = "<<pix->nChannels;
//-----------------------------------------------------------------------------------------------//

for(int i=MaskRadius; i<=row-MaskRadius; i++)             //convolving the image
{
uchar* ptr1 = (uchar*) (pix->imageData + i * pix->widthStep);
uchar* ptr0 = (uchar*) (pix->imageData + (i-1) * pix->widthStep);
uchar* ptr2 = (uchar*) (pix->imageData + (i+1) * pix->widthStep);
for(int j=MaskRadius; j<=col-MaskRadius; j++)
 {
     cout<<endl<<i<<" , "<<j;
     convx[i][j]=(double)ptr1[j-1]*maskx[1][0]+ptr1[j]*maskx[1][1]+ptr1[j+1]*maskx[1][2] + (ptr2[j-1]*maskx[0][0]+ptr2[j]*maskx[0][1]+ptr2[j+1]*maskx[0] + ptr0[j-1]*maskx[2][0]+ptr0[j]*maskx[2][1]+ptr0[j+1]*maskx[2][2]);

     convy[i][j]=(double)ptr1[j-1]*masky[1][0]+ptr1[j]*masky[1][1]+ptr1[j+1]*masky[1][2] + (ptr2[j-1]*masky[0][0]+ptr2[j]*masky[0][1]+ptr2[j+1]*masky[0] + ptr0[j-1]*masky[2][0]+ptr0[j]*masky[2][1]+ptr0[j+1]*masky[2][2]);

    double eq2=pow(convx[i][j],2)+pow(convy[i][j],2);
    M[i][j]=(double)sqrt(eq2);
}
}
4

1 回答 1

-1

The access violation can happen when you are creating the mask, when you are converting the image, or when you are doing the convolution. You can start by commenting out all code and then uncommenting it from top, while observing which line/block gives you the error. Also use the debugger of the IDE to see the values of indexes, and check for those going out of range.

于 2013-07-08T02:49:53.937 回答