Load libraries:
import brainstem as b
from sklearn.decomposition import PCA
from sklearn.cluster import MiniBatchKMeans
from matplotlib import pyplot as pp
import texture as tx
Stage 1:
# get a list of all available image files
filenames = b.get_filenames()
img = b.get_img(filenames[0], 4)
img = b.make_grey(img)
img = img[300:-200, 300:-300]
pylab.imshow(img)
print tx.get_freqs(img)
[0.0015713484026367724, 0.0031426968052735448, 0.0062853936105470897, 0.012570787221094179, 0.025141574442188359, 0.050283148884376717, 0.10056629776875343]
Stage 2:
to number of columns in image and number of angles requested
angle = 10
freqs = tx.get_freqs(img)
thetas = np.deg2rad(np.arange(0, 180, angle))
# kernels, kernel_freqs = tx.make_filter_bank(freqs, thetas)
Stage 3:
fig = pylab.figure()
num_rows = size(freqs)
num_cols = size(thetas)
len_x, len_y = img.shape
my_output = [[[[]]*len_y]*len_x]*num_rows*num_cols
idx = 1
for frequency in freqs:
sigma_x, sigma_y = tx._compute_sigmas(frequency)
for theta in thetas:
kernel = np.real(tx.gabor_kernel(frequency, theta, bandwidth=1))
""""pylab.imshow(np.real(kernel))
pylab.show()"""
conv_img = tx.fftconvolve(img, kernel, 'same')
subp = fig.add_subplot(num_rows, num_cols, idx)
pylab.axis('off')
pylab.imshow(conv_img)
my_output[idx-1] = conv_img
idx = idx + 1
Stage 4:
len_x, len_y = img.shape
my_vars = zeros((len_x, len_y))
my_means = zeros((len_x, len_y))
for i in range(0, len_x):
for j in range(0, len_y):
a = []
for x in range(0, num_cols*num_rows):
a.append(my_output[x][i][j])
my_vars[i][j] = np.var(a)
my_means[i][j] = np.mean(a)
Stage 5:
new_img = (img - my_means) / my_vars
pylab.imshow(new_img)
pylab.imsave('week1_output.png', new_img)