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;


      }
    }
}

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.