Gabor filter ... Please assist

Status
Not open for further replies.

darelet

Newbie level 1
Joined
May 28, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,289
Hi everyone,
I am new to this forum, and to image processing as well. I am working on the Gabor filter, and have read previous threads on the same from this forum as well. My problem is a bit unique though, while I tried implementing my 2D Gabor according to sources on the internet, the results are not exactly the same for the filtered image, and are even worse for the Gabor image itself. I have worked myself to my individual limits unsuccessfully, and don't see any obvious failing in my implementation. Please assist...
Here's my code (i'll post the necessary parts only for readability)

Code:
gx_r = new float[y_filterWidth*x_filterWidth]; gx_i = new float[y_filterWidth*x_filterWidth];
	
	for (int y=-y_filterHalfWidth; y<=y_filterHalfWidth; y++){
		for (int x=-x_filterHalfWidth; x<=x_filterHalfWidth; x++){
			float x1 = (x-x0)*cos(theta) + (y-y0)*sin(theta),
				  y1 = -(x-x0)*sin(theta) + (y-y0)*cos(theta);
			float exponential = exp(-((x1*x1) + ((gamma * gamma)*(y1*y1))) / (2*(sigma_x*sigma_y))); 
			Gaussian = factor * exponential;
			gx_r[(y+y_filterHalfWidth) * x_filterWidth + (x+x_filterHalfWidth)] = 
				Gaussian * cos(2*PI*(x1/lambda) + phase);
			gx_i[(y+y_filterHalfWidth) * x_filterWidth + (x+x_filterHalfWidth)] = 
				Gaussian * sin(2*PI*(x1/lambda) + phase);
		}
	}

	// Take away the mean
		float mean_r = 0.0, mean_i = 0.0;
		for (int f = 0; f < (y_filterWidth*x_filterWidth); f++){
			mean_r += gx_r[f];	mean_i += gx_i[f];
		}
		mean_r = mean_r / (y_filterWidth*x_filterWidth);
		mean_i = mean_i / (y_filterWidth*x_filterWidth);
		for (int f = 0; f < (y_filterWidth*x_filterWidth); f++){
			gx_r[f] -= mean_r; // Remove DC bias
			gx_i[f] -= mean_i;
		}

Next, I try to display the Gabor image...

Code:
IplImage *imagee = cvCreateImage(cvSize(y_filterWidth,x_filterWidth),8,1);
		float max=-0.0, val=0;
		for (int my = 0; my <= y_filterWidth; ++my){
			for (int mx=0; mx<=x_filterWidth; mx++){
				val = gx_r[my * x_filterWidth + mx]*gx_r[my * x_filterWidth + mx];
				val+= (gx_i[my * x_filterWidth + mx]*gx_i[my * x_filterWidth + mx]);
				if (val > max) max=val;
			}
		}
		for (int my = 0; my <= y_filterWidth; ++my){
			for (int mx=0; mx<=x_filterWidth; mx++){
				val = gx_r[my * x_filterWidth + mx]*gx_r[my * x_filterWidth + mx];
				val += (gx_i[my * x_filterWidth + mx]*gx_i[my * x_filterWidth + mx]);
				 val *= (255/max);
				val = (float) System::Math::Round(val);
				imagee->imageData[my*x_filterWidth+mx] = (uchar) val;
			}
		}
		cvNamedWindow("temp", 1); cvShowImage("temp",imagee); cvResizeWindow("temp", 120, 120);
		cvSaveImage("c:\\tit\\gaussian.pgm",imagee);

Next is the convolution

Code:
float newValue, newValue_i, maxvalue=0.0, minValue=0;
	float *tmp = new float[height*width]; 

	for(int i=0; i<(height*width);++i) { tmp[i] = (float)0; }

		for (int y = y_filterHalfWidth; y < height-y_filterHalfWidth; ++y){
		for (int x = x_filterHalfWidth; x < width-x_filterHalfWidth; ++x){
			newValue=0; newValue_i=0;
			for (int my = -y_filterHalfWidth; my <= y_filterHalfWidth; ++my){
				for (int mx=-x_filterHalfWidth; mx<=x_filterHalfWidth; mx++){
					newValue	+= data->data[(y-my)*width+(x-mx)]* //data->data[(y)*width+(x)]* 
						gx_r[(my+y_filterHalfWidth) * x_filterWidth + (mx+x_filterHalfWidth)];
					newValue_i	+= data->data[(y-my)*width+(x-mx)]* //data->data[(y)*width+(x)]* 
						gx_i[(my+y_filterHalfWidth) * x_filterWidth + (mx+x_filterHalfWidth)];
				}
				// Get Energy
			tmp[y*width+x] = (newValue*newValue)+(newValue_i*newValue_i); 
			//tmp[y*width+x] = sqrt((newValue*newValue)+(newValue_i*newValue_i)); // Magnitude
			maxvalue = tmp[y*width+x]>maxvalue?tmp[y*width+x]:maxvalue;
			minValue = tmp[y*width+x]<minValue?tmp[y*width+x]:minValue;
			}
		} 
		}
		outImage->data = new unsigned char[height*width];
		for (int i=0; i<(height*width);++i)
			outImage->data[i] = (uchar) System::Math::Round( (1 + (tmp[i]-minValue))*(255-0)/maxvalue-minValue );
		// Above normalizes using: xnew = (1 + (x_old - x_old_min)) * (x_new_max - x_new_min / x_old_max - x_old_min)

	return outImage;
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…