I am trying to use a gabor filter on an image using a gabor function and parameters from a published object recognition paper. Instead of outputting the filtered image, the output image seems to be a portion of a white/gray circular gabor gradient in the top left of the image and the rest of the image is just black pixels. I have tried troubleshooting and I am not sure where things are going wrong. Please help. Thank you in advance.
Initially in the for loop I had:
output = gabor_output
if output > 0:
output = output
else:
output = 0
That was resulting in all black pixels so I made the changes below in part of my attempt to troubleshoot. Ideally I would like the output = output (as above) rather than output = 1 (what I have below). Here is the code I am trying:
# Gabor Function from S1 of "Robust Object Recognition" paper
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import math
orientation_1 = 0.0
orientation_2 = 45.0
orientation_3 = 90.0
orientation_4 = 135.0
def compute_x0(x, y, orientation):
x0 = (x * math.cos(orientation)) + (y * math.sin(orientation))
return x0
def compute_y0(x, y, orientation):
y0 = (-x * math.sin(orientation)) + (y * math.cos(orientation))
return y0
def gabor_function(x, y, orientation, wavelength, width, aspect_ratio=0.3):
x0 = compute_x0(x, y, orientation)
y0 = compute_y0(x, y, orientation)
term_1 = np.exp(-(x0**2 + aspect_ratio**2 * y0**2) / (2 * width**2))
term_2 = math.cos(((2 * np.pi) / wavelength) * x0)
output = term_1 * term_2
return output
# Trying it out
# Upload Image, convert to grayscale, convert to np.array
image = Image.open('/Users/damondtrowbridge/Desktop/MATLAB/Face.png')
bw = image.convert('L')
bw_array = np.array(bw) / 255.0
# Crop to fit different images and adjust for pooling
og_size = np.shape(bw_array)
Xdim = (og_size[0]//4)*4
Ydim = (og_size[1]//4)*4
cropped = bw_array[0:Xdim, 0:Ydim]
# Set up output array
out_1 = np.zeros((Xdim-3, Ydim-3))
for x in range(3, Xdim - 3):
for y in range(3, Ydim - 3):
gabor_output = gabor_function(x, y, orientation_1, 3.5, 2.8, aspect_ratio=0.3)
output = gabor_output * cropped[x, y]
if output > 0:
output = 1
else:
output = 0
out_1[x, y] = output
plt.imshow(out_1[:, :], cmap='gray', aspect='auto', vmin=0, vmax=1)
plt.suptitle('Filtered Image')
plt.show()