Hi all,
I implement Gabor filter in my c++ function (put it in Visual c++) . when I compiled it there was no error but when I clicked on a button for excution this function there was a failure report. I can't find out the mistake so I hope some one help me.

The Algorithm of Gabor filter can be found in Internet easily.

Here's my code :

// "image" is in Intel image format
// image is gray image
void Tien_xu_ly::Gabor(IplImage *image)
{
  #define R 300  // number of row        
  #define C 256  // number of colum
   double imag[R][C];
   double gabor[R][C];
    double gx[R][C],gy[R][C];
    double Vx[R][C],Vy[R][C];
    double thetaQ[R][C];
    int r,c,u,v,x,y,i,j;
    int stdev=4;
    double temple,x_theta,y_theta;
    double treten;

/* CONVERT IMAGE TO ARRAY*/
   for(i=0; i<image->height; i++)
    {
     for(j=0;j<image->width;j++)
     {
     imag[i][j]=(unsigned char)(image->imageData + image->
widthStep * i) [j] ; 
     }  
}

 /* LOCAL ORIENTATION ESTIMATION */
    for(r=0;r<R;r++)
    {
           for(c=0;c<C;c++)
      {
        //  gradients gx and gy (sobel)
       gx[r][c]=(imag[r-1][c-1]*1)+(imag[r-1][c]*0)+(imag[r-1][c+1]*-1)+(imag[r][c-1]*2)+(imag[r][c]*0)+(imag[r][c+1]*-2)+(imag[r+1][c-1]*1)+(imag[r+1][c]*0)+(imag[r+1][c+1]*-1);

       gy[r][c]=(imag[r-1][c-1]*1)+(imag[r-1][c]*2)+(imag[r-1][c+1]*1)+(imag[r][c-1]*0)+(imag[r][c]*0)+(imag[r][c+1]*0)+(imag[r+1][c-1]*-1)+(imag[r+1][c]*-2)+(imag[r+1][c+1]*-1);


       Vx[r][c]=0;
       Vy[r][c]=0;
       for(u=r-5;u<=r+5;u++) //10x10 window
        {
          for(v=c-5;v<=c+5;v++)
          {

            Vx[r][c]=Vx[r][c]+(2*(gx[u][v])*(gy[u][v]));

            Vy[r][c]=Vy[r][c]+((gx[u][v]*gx[u][v])*(gy[u][v]*gy[u][v]));
          }
        }  


        if(Vx[r][c]==0)
        {
          thetaQ[r][c]=90;
        }
        else
        {
           thetaQ[r][c]=(0.5*(atan(Vy[r][c])/(Vx[r][c]))); 
        }
     }  
  }  

/* RIDGE FREQUENCY ESTIMATION */
    //assume the average inter ridge distance to be 3, so 1/3 = 0.33 = f

/* 2D GABOR FILTERING IN SPATIAL SPACE */
  for(x=0;x<R;x++)
   {
     for(y=0;y<C;y++)
      {     
       x_theta=(x*cos(thetaQ[x][y]))+(y*sin(thetaQ[x][y]));
       y_theta=-(x*sin(thetaQ[x][y]))+(y*cos(thetaQ[x][y]));

       //stdev=4
       temple=(-1.0/2.0)*(((x_theta*x_theta)/(16))+((y_theta*y_theta)/(16)));

       //Gabor in the spatial domain
       gabor[x][y]=exp(temple)*cos(2*3.14*0.33*x_theta);
     }
   }

  //spatially convolve image with the filter to get the enhanced image

   for(x=0;x<R;x++)
   {
      for(y=0;y<C;y++)
      {
            treten=0;
            for(i=x-5;i<x+6;i++)
            {
                for(j=y-5;j<y+6;j++)
                {

                 treten+=(gabor[i][j]*imag[i][j]);

                }
            }

            (image->imageData + image->widthStep * y) [x]=treten;


      }
    }
}

Dani AI

Generated

This implementation is crashing for a few very concrete reasons. As observed, many loops read pixels outside the image bounds (r==0, r==R-1, etc.), and the function also allocates many large double[R][C] arrays on the stack — those two together commonly produce memory corruption or a stack overflow. Other errors to fix before anything else: the orientation math is wrong (you want 0.5 * atan2(sum(2 GxGy), sum(Gx^2 − Gy^2)), not atan(Vy)/Vx), angles must be in radians (setting 90 is wrong unless you convert), the Gabor kernel must be built with coordinates centered on the kernel (not raw image x,y), and the write-back to image pixels must use the correct row/column order and clamped/saturated values.

A safer, shorter pipeline using OpenCV (recommended) — compute gradients with Sobel, accumulate local second-moment terms with a box filter, compute orientation with atan2, then build/apply Gabor kernels (or use cv::getGaborKernel). Example sketch:

// convert IplImage* to cv::Mat, float normalized
cv::Mat src = cv::cvarrToMat(image), imgf;
src.convertTo(imgf, CV_32F, 1.0/255.0);

// gradients
cv::Mat gx, gy;
cv::Sobel(imgf, gx, CV_32F, 1, 0, 3);
cv::Sobel(imgf, gy, CV_32F, 0, 1, 3);

// local sums over window (11x11 here)
cv::Mat Gxx = gx.mul(gx), Gyy = gy.mul(gy), Gxy = gx.mul(gy);
cv::Mat Sx, Sy;
cv::boxFilter(Gxx - Gyy, Sx, CV_32F, cv::Size(11,11));
cv::boxFilter(2.0f * Gxy, Sy, CV_32F, cv::Size(11,11));

// orientation (radians)
cv::Mat ang;
cv::phase(Sx, Sy, ang);        // ang = atan2(Sy, Sx)
cv::Mat theta = 0.5f * ang;

If you cannot use OpenCV, avoid large stack arrays: use std::vector or new/delete and flatten 2D into 1D. Always clamp window loops to [0,width-1] and [0,height-1]. When generating a spatial Gabor kernel, loop kernel coordinates around a center (u = -k..k, v = -k..k) and use cos(2*M_PI*freq*u_theta) with u_theta in kernel-local coordinates. When writing pixels back use saturate_cast<uchar>(value) and the correct row pointer: uchar* row = (uchar*)(image->imageData + rowIdx*image->widthStep); row[col] = ...;.

Start by fixing bounds and switching large arrays to heap storage — that alone will likely stop the crash. Once stable, correct the orientation formula and radians/degree mix, then verify the kernel centering and convolution indices. This addresses the main failure modes raised by and will make the filter behavior predictable for and others.

Recommended Answers

All 4 Replies

For one thing the arrays go out of bounds when r = 0 or c = 0 or r = R or c = C.

for( r = 0 ; r < R; r++ )
{
	for(c=0;c<C;c++)
	{
		// gradients gx and gy (sobel)
		gx[r][c]=(imag[r-1][c-1]*1)+(imag[r-1][c]*0)+(imag[r-1][c+1]*-1)+(imag[r][c-1]*2)+(imag[r][c]*0)+(imag[r][c+1]*-2)+(imag[r+1][c-1]*1)+(imag[r+1][c]*0)+(imag[r+1][c+1]*-1);

		gy[r][c]=(imag[r-1][c-1]*1)+(imag[r-1][c]*2)+(imag[r-1][c+1]*1)+(imag[r][c-1]*0)+(imag[r][c]*0)+(imag[r][c+1]*0)+(imag[r+1][c-1]*-1)+(imag[r+1][c]*-2)+(imag[r+1][c+1]*-1);


		Vx[r][c]=0;
		Vy[r][c]=0;
		for(u=r-5;u<=r+5;u++) //10x10 window
		{
			for(v=c-5;v<=c+5;v++)
			{

				Vx[r][c]=Vx[r][c]+(2*(gx[u][v])*(gy[u][v]));

				Vy[r][c]=Vy[r][c]+((gx[u][v]*gx[u][v])*(gy[u][v]*gy[u][v]));
			}
		}


		if(Vx[r][c]==0)
		{
			thetaQ[r][c]=90;
		}
		else
		{
			thetaQ[r][c]=(0.5*(atan(Vy[r][c])/(Vx[r][c])));
		}
	}
}

Does this code for gabor filter work?

Try it and find out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.