forked from cbh123/gabors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgabor_final.py
111 lines (75 loc) · 3.22 KB
/
gabor_final.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import scipy as sp
from scipy import arange, sqrt, array, amax, ceil, io, fliplr, flipud
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import ipdb
import time
from tqdm import tqdm
# rot = np.array([90,-67.5,-45,-22.5,0,45,67.5])
# RF_siz = sp.arange(33,67,2)
# Div = sp.arange(4,3.15,-.05)
rot = np.array([90,-67.5,-45,-22.5,0,22.5,45,67.5])
RF_siz = sp.arange(33,67,2)
Div = sp.arange(4,3.15,-.05)
def create_gabor(rot,RF_siz,Div,plot,num=10):
count = 0
numFilterSizes = len(RF_siz)
numSimpleFilters = len(rot)
lamb = (RF_siz * 2.)/Div
sigma = lamb * 0.8
G = 0.3
phases = [0, np.pi/2]
# Initialize Filterbank
alt_fb = np.zeros((65,65,1,272),dtype=np.float32)
# for k in tqdm(range(0,numFilterSizes-1)):
for k in tqdm(range(1,numFilterSizes+1)):
for r in tqdm(range(1,numSimpleFilters+1)):
f = np.zeros([RF_siz[numFilterSizes-1],RF_siz[numFilterSizes-1]])
fx = np.zeros([RF_siz[numFilterSizes-1],RF_siz[numFilterSizes-1]])
## Parameters
theta = rot[r-1]*(np.pi/180)
filtSize = RF_siz[k-1]
img_center = ceil(33.0) ## New center for padding with zeros
center = ceil(filtSize/2.0) ## Old and possibly more accurate center
filtSizeL = center-1
filtSizeR = filtSize-filtSizeL-1
sigmaq = (sigma[k-1]) * (sigma[k-1])
# Compute filter values
for iPhi in range(1,3):
for i in range(int(-1 * filtSizeL),int(filtSizeR+1)):
for j in range(int(-1 * filtSizeL),int(filtSizeR+1)):
if (sqrt((i**2)+(j**2))>(filtSize/2 )) :
E = 0
else :
x = i*np.cos(theta) - j*np.sin(theta)
y = i*np.sin(theta) + j*np.cos(theta)
E = np.exp((-1*((x**2)+ (G**2) * (y**2)))/(2*sigmaq))*np.cos(2*np.pi*x/lamb[k-1] + phases[iPhi-1])
f[int(j+img_center-1),int(i+img_center-1)] = E
## Append to fb (filterbank)
f = f - np.mean(np.mean(f))
f = f / sqrt(np.sum(np.sum(f**2)))
# Reshaped image
alt_fb[:,:,0,count] = f
count += 1
if (plot):
if count % num == 0:
plt.imshow(f,cmap='Greys')
plt.show()
return (np.array(alt_fb))
## Test Plotting ----------------------
#create_gabor(rot,RF_siz,Div)
## Create Dictionary ---------------
gabor_array = create_gabor(rot,RF_siz,Div,False)
bias = np.zeros([272])
gabor_dictionary = {}
gabor_dictionary['gabors'] = gabor_array,bias
f,x = gabor_dictionary['gabors']
## Save Dictionary ----------------------
np.save('/Users/charlieholtz/Desktop/gabors/gabor_dictionary.npy', gabor_dictionary)
## Test Dictionary
#weights = tf.get_variable('weights', shape = [filter_height, filter_width, input_channels/groups, num_filters])
## Load Dictionary From Save File ----------------------
# b = np.load('/Users/charlieholtz/Desktop/hermann/Gabor Filters/gabor_dictionary.npy').item()
# f,x = b['gabors']
# print(f.shape)