|
a |
|
b/4x/reference/individual.py |
|
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
# <nbformat>3.0</nbformat> |
|
|
3 |
|
|
|
4 |
# <codecell> |
|
|
5 |
|
|
|
6 |
# Files |
|
|
7 |
import os |
|
|
8 |
|
|
|
9 |
# Basic |
|
|
10 |
import numpy as np |
|
|
11 |
|
|
|
12 |
# Image Processing |
|
|
13 |
import skimage as ski |
|
|
14 |
from skimage import io, feature, morphology, filter, exposure, color, transform |
|
|
15 |
import scipy.signal as sig |
|
|
16 |
|
|
|
17 |
# Stats |
|
|
18 |
import scipy.stats as st |
|
|
19 |
|
|
|
20 |
# Nonlinear Fitting |
|
|
21 |
import lmfit as lm |
|
|
22 |
|
|
|
23 |
# Visualisation |
|
|
24 |
import matplotlib |
|
|
25 |
import matplotlib.pyplot as plt |
|
|
26 |
import seaborn |
|
|
27 |
#matplotlib.rcParams['savefig.dpi'] = 3. * matplotlib.rcParams['savefig.dpi'] |
|
|
28 |
|
|
|
29 |
# <codecell> |
|
|
30 |
|
|
|
31 |
|
|
|
32 |
# <codecell> |
|
|
33 |
|
|
|
34 |
def normalise(im) : |
|
|
35 |
return (im - im.min()).astype(float) / (im - im.min()).astype(float).max() |
|
|
36 |
|
|
|
37 |
# <codecell> |
|
|
38 |
|
|
|
39 |
# Read files |
|
|
40 |
files = [] |
|
|
41 |
|
|
|
42 |
directory = "data/" |
|
|
43 |
for i in os.listdir(directory) : |
|
|
44 |
if i.endswith(".jpg") : |
|
|
45 |
if not i.endswith("processed.jpg") : |
|
|
46 |
files.append(directory + i) |
|
|
47 |
|
|
|
48 |
print files |
|
|
49 |
|
|
|
50 |
# <codecell> |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
# <codecell> |
|
|
54 |
|
|
|
55 |
A = io.imread(files[0]) |
|
|
56 |
As = transform.rescale(A, 0.25) |
|
|
57 |
io.imshow(A) |
|
|
58 |
plt.grid(False) |
|
|
59 |
|
|
|
60 |
# <codecell> |
|
|
61 |
|
|
|
62 |
#B = exposure.adjust_sigmoid(A, gain=12) |
|
|
63 |
Bs = exposure.adjust_sigmoid(ski.img_as_float(As), gain=12) |
|
|
64 |
#io.imshow(B - exposure.adjust_sigmoid(ski.img_as_float(A), gain=12)) |
|
|
65 |
|
|
|
66 |
# <codecell> |
|
|
67 |
|
|
|
68 |
#C = color.rgb2xyz(B)[:, :, 1] |
|
|
69 |
Cs = color.rgb2xyz(Bs)[:, :, 1] |
|
|
70 |
io.imshow(Cs) |
|
|
71 |
plt.grid(0) |
|
|
72 |
|
|
|
73 |
# <codecell> |
|
|
74 |
|
|
|
75 |
#D = filter.threshold_adaptive(C, 301) |
|
|
76 |
Ds = filter.threshold_adaptive(Cs, 75) |
|
|
77 |
io.imshow(Ds) |
|
|
78 |
plt.grid(0) |
|
|
79 |
|
|
|
80 |
# <codecell> |
|
|
81 |
|
|
|
82 |
#E = morphology.remove_small_objects(~morphology.remove_small_objects(~D, 100), 100) |
|
|
83 |
Es = morphology.remove_small_objects(~morphology.remove_small_objects(~Ds, 10), 10) |
|
|
84 |
io.imshow(Es) |
|
|
85 |
plt.grid(False) |
|
|
86 |
|
|
|
87 |
# <codecell> |
|
|
88 |
|
|
|
89 |
X = Es.copy()#transform.rescale(E, 0.25) |
|
|
90 |
|
|
|
91 |
scales = 1. / np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20]) # 2 to 20 pixels |
|
|
92 |
orientations = np.linspace(0, np.pi * 17./18., 18) # 0 to 180 degrees in 10 degree increments |
|
|
93 |
|
|
|
94 |
# Results array |
|
|
95 |
gabor = np.zeros((len(orientations), len(scales))) |
|
|
96 |
|
|
|
97 |
# Perform Gabor filtering |
|
|
98 |
for i, iv in enumerate(orientations) : |
|
|
99 |
for j, jv in enumerate(scales) : |
|
|
100 |
Y, Z = filter.gabor_filter(ski.img_as_float(X), jv, iv) |
|
|
101 |
gabor[i, j] = np.sqrt(np.sum(np.abs(Y)**2) + np.sum(np.abs(Z)**2)) # Return energy |
|
|
102 |
|
|
|
103 |
print i |
|
|
104 |
|
|
|
105 |
# <codecell> |
|
|
106 |
|
|
|
107 |
px = Y.ravel() |
|
|
108 |
py = 1 - px |
|
|
109 |
|
|
|
110 |
# <codecell> |
|
|
111 |
|
|
|
112 |
|
|
|
113 |
# <codecell> |
|
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
io.imshow(gabor) |
|
|
118 |
plt.grid(False) |
|
|
119 |
|
|
|
120 |
# <codecell> |
|
|
121 |
|
|
|
122 |
plt.plot(gabor[:,5]) |
|
|
123 |
|
|
|
124 |
# <codecell> |
|
|
125 |
|
|
|
126 |
yy = y[:, 8] |
|
|
127 |
zz = z[:, 6] |
|
|
128 |
params = lm.Parameters() |
|
|
129 |
params.add("amp", value = yy.max() - yy.min(), min = 0, max = yy.max()) |
|
|
130 |
params.add("intercept", value = yy.min(), min = 0, max = yy.max()) |
|
|
131 |
params.add("std", value = 0.2, min = 0) |
|
|
132 |
params.add("mean", value = np.where(yy == yy.max())[0][0], min = 0) |
|
|
133 |
|
|
|
134 |
result = lm.minimize(gaussian, params, args = (orientations, yy)) |
|
|
135 |
|
|
|
136 |
# <codecell> |
|
|
137 |
|
|
|
138 |
lm.report_fit(params) |
|
|
139 |
yyy = params["amp"].value * np.exp( (-(orientations - params["mean"].value)**2) / (2*(params["std"].value**2)) ) + params["intercept"].value |
|
|
140 |
|
|
|
141 |
# <codecell> |
|
|
142 |
|
|
|
143 |
print (yy.max() - yy.min()) / yy.mean(), (zz.max() - zz.min()) / zz.mean() |
|
|
144 |
|
|
|
145 |
# <codecell> |
|
|
146 |
|
|
|
147 |
%%timeit |
|
|
148 |
xx = np.zeros((500,500)) |
|
|
149 |
Y, Z = filter.gabor_filter(xx, 0.05, 0) |
|
|
150 |
|