FacetsPythonCourse2008: gabor.py

File gabor.py, 1.0 kB (added by goodman, 7 months ago)
Line 
1 from numpy import *
2
3 __all__ = ['gabor_fn']
4
5 def gabor_fn(sigma_x,sigma_y,theta,lambda_,psi,gamma,width=None, height=None):
6     '''
7     Gabor function, adapted from Wikipedia's matlab code
8     '''
9     if width is None:
10         sz_x = 6*sigma_x
11         width = sz_x
12     else:
13         sz_x = width
14     if sz_x%2==0:
15         sz_x = sz_x+1
16
17     if height is None:
18         sz_y= 6*sigma_y
19         height = sz_y
20     else:
21         sz_y = height
22     if sz_y%2==0:
23         sz_y = sz_y+1
24      
25     x,y = mgrid[-int(sz_x/2):int(sz_x/2)+1,int(-sz_y/2):int(sz_y/2)+1]
26      
27     # Rotation
28     x_theta = x*cos(theta)+y*sin(theta)
29     y_theta = -x*sin(theta)+y*cos(theta)
30      
31     gb = exp(-.5*(x_theta**2/sigma_x**2+gamma**2*y_theta**2/sigma_y**2))*cos(2*pi/lambda_*x_theta+psi)
32     return gb[0:width,0:height]
33
34 if __name__=='__main__':
35     import pylab
36     theta = pi/4
37     gb = gabor_fn(100.,100.,theta,300.,0.,0.8,500,520)
38     print gb.shape
39     pylab.gray()
40     pylab.imshow(gb)
41     pylab.colorbar()
42     pylab.show()