forked from cbh123/gabors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone_scale_one_phase.py
93 lines (63 loc) · 2.58 KB
/
one_scale_one_phase.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
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,22.5,45,67.5])
RF_siz = [33]
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[0] * 2)/4.0
sigma = [lamb * 0.8]
G = 0.3
phases = [0]
# Initialize Filterbank
alt_fb = np.zeros((33,33,1,8),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(filtSize/2.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 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 + phases)
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))
## Create Dictionary ---------------
gabor_array = create_gabor(rot,RF_siz,Div,False)
bias = []
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)