class gabor: from math import pi from numpy import linspace freqs = [ (.1, 1.), # (.2, .3), # (.2, .4), (.2, .5), (.3, .3), # (.3, .4), # (.3, .5), (.5, .5), ] NUM_ANGLES = 8 thetas = arange( float(NUM_ANGLES) ) / NUM_ANGLES * pi # real part of Gabor depends on theta mod pi params = [{ 'frequency': f, 'theta': theta, 'bandwidth': b } for theta in thetas for (f, b) in freqs] function = staticmethod( lambda *args, **kwargs: gabor_filter(*args, **kwargs)[0])
def angle_pass_filter(img, frequency, theta, bandwidth): """ returns the magnitude of a gabor filter response for a certain angle args: img: m x n ndarray """ real, imag = gabor_filter(img, frequency, theta, bandwidth) mag = np.sqrt(np.square(real) + np.square(imag)) return mag
plt.grid(False) # <codecell> X = Es.copy()#transform.rescale(E, 0.25) scales = 1. / np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20]) # 2 to 20 pixels orientations = np.linspace(0, np.pi * 17./18., 18) # 0 to 180 degrees in 10 degree increments # Results array gabor = np.zeros((len(orientations), len(scales))) # Perform Gabor filtering for i, iv in enumerate(orientations) : for j, jv in enumerate(scales) : Y, Z = filter.gabor_filter(ski.img_as_float(X), jv, iv) gabor[i, j] = np.sqrt(np.sum(np.abs(Y)**2) + np.sum(np.abs(Z)**2)) # Return energy print i # <codecell> px = Y.ravel() py = 1 - px # <codecell> # <codecell>
### GABOR FILTERING # Define scales and orientations to compute over pixelscales = np.arange(15, 55, 2) gaborscales = 4. / pixelscales # 2 to 20 pixels orientations = np.linspace(0, np.pi * 11./12., 12) # 0 to 180 degrees in 15 degree increments # Results array gabor = np.zeros((len(orientations), len(gaborscales))) # Perform Gabor filtering for i, iv in enumerate(orientations) : for j, jv in enumerate(gaborscales) : gaborReal, gaborImag = filter.gabor_filter(Es, jv, iv) gabor[i, j] = np.sqrt(np.sum(np.abs(gaborReal) ** 2) + np.sum(np.abs(gaborImag) ** 2)) # Return energy print "Thread %s. Gabor filtering. Completion : %f" % (sys.argv[1], (i / float(len(orientations)))) # Determine orientation-independent scale which fits best optimalscale = np.argmax(np.sum(gabor, axis = 0)) # At this scale, calculate directionality coefficient g = gabor[:, optimalscale] directionality = (g.max() - g.min()) / g.max() # Results scale.append(pixelscales[optimalscale]) direc.append(directionality)
def _gabor(self, *params): real, imag = gabor_filter(numpy.array(self.image), *params) return real + 1j * imag