Question

I want to apply canny edge detection to an image without using the cvcanny function, so a part of the required steps is to apply the gaussian mask for which i have 2 masks for x and y direction. Now the problem is that whenever i try the convolution of the mask onto my image, the execution breaks due to "access violation". Why does this happen and how can i overcome this?

//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);
}
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top