|
a |
|
b/4x/reference/Untitled0.py |
|
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
# <nbformat>3.0</nbformat> |
|
|
3 |
|
|
|
4 |
# <codecell> |
|
|
5 |
|
|
|
6 |
import os |
|
|
7 |
|
|
|
8 |
import numpy as np |
|
|
9 |
|
|
|
10 |
from scipy.ndimage import maximum_filter, minimum_filter, binary_fill_holes |
|
|
11 |
|
|
|
12 |
import skimage as ski |
|
|
13 |
from skimage import io, filter, color, exposure, morphology, feature, draw, measure, transform |
|
|
14 |
|
|
|
15 |
figsize(16, 10) |
|
|
16 |
|
|
|
17 |
# <codecell> |
|
|
18 |
|
|
|
19 |
l = os.listdir("../data") |
|
|
20 |
print l[6] |
|
|
21 |
|
|
|
22 |
qstain = np.array([[.26451728, .5205347, .81183386], [.9199094, .29797825, .25489032], [.28947765, .80015373, .5253158]]) |
|
|
23 |
|
|
|
24 |
# <codecell> |
|
|
25 |
|
|
|
26 |
# Original image |
|
|
27 |
A = io.imread("../data/" + l[2]) |
|
|
28 |
io.imshow(A) |
|
|
29 |
|
|
|
30 |
# <codecell> |
|
|
31 |
|
|
|
32 |
# Colour Deconvolution |
|
|
33 |
deconv = ski.img_as_float(color.separate_stains(A, np.linalg.inv(qstain))) |
|
|
34 |
|
|
|
35 |
io.imshow(exposure.adjust_sigmoid( |
|
|
36 |
filter.gaussian_filter( |
|
|
37 |
exposure.rescale_intensity( |
|
|
38 |
deconv[:, :, 0] + deconv[:, :, 2], |
|
|
39 |
out_range=(0, 1)), |
|
|
40 |
17), |
|
|
41 |
gain=7, cutoff=0.6)) |
|
|
42 |
|
|
|
43 |
# <codecell> |
|
|
44 |
|
|
|
45 |
# Blood |
|
|
46 |
|
|
|
47 |
blood = \ |
|
|
48 |
morphology.remove_small_objects( |
|
|
49 |
filter.threshold_adaptive( |
|
|
50 |
exposure.adjust_sigmoid( |
|
|
51 |
filter.gaussian_filter( |
|
|
52 |
exposure.rescale_intensity( |
|
|
53 |
deconv[:, :, 0] + deconv[:, :, 2], |
|
|
54 |
out_range=(0, 1)), |
|
|
55 |
15), |
|
|
56 |
gain=10, cutoff=0.7), |
|
|
57 |
301, offset=-0.15), |
|
|
58 |
500) |
|
|
59 |
|
|
|
60 |
io.imshow(blood) |
|
|
61 |
|
|
|
62 |
# <codecell> |
|
|
63 |
|
|
|
64 |
# Veins |
|
|
65 |
|
|
|
66 |
veins = \ |
|
|
67 |
binary_fill_holes( |
|
|
68 |
morphology.remove_small_objects( |
|
|
69 |
filter.threshold_adaptive( |
|
|
70 |
filter.gaussian_filter( |
|
|
71 |
exposure.adjust_sigmoid(A[:,:,1]), |
|
|
72 |
31), |
|
|
73 |
501, offset=-0.07), |
|
|
74 |
3000) #### SRSLY ? |
|
|
75 |
) |
|
|
76 |
|
|
|
77 |
io.imshow(veins) |
|
|
78 |
|
|
|
79 |
# <codecell> |
|
|
80 |
|
|
|
81 |
# Inflammation |
|
|
82 |
|
|
|
83 |
inflammation = \ |
|
|
84 |
morphology.remove_small_objects( |
|
|
85 |
filter.threshold_adaptive( |
|
|
86 |
exposure.adjust_sigmoid( |
|
|
87 |
filter.gaussian_filter( |
|
|
88 |
exposure.rescale_intensity( |
|
|
89 |
deconv[:, :, 1], |
|
|
90 |
out_range = (0, 1)), |
|
|
91 |
25), |
|
|
92 |
gain = 12, cutoff = 0.32), |
|
|
93 |
501, offset = -0.1), |
|
|
94 |
4000) |
|
|
95 |
|
|
|
96 |
|
|
|
97 |
io.imshow(inflammation) |
|
|
98 |
#io.imshow(filter.threshold_adaptive(exposure.adjust_sigmoid(filter.gaussian_filter(exposure.rescale_intensity(deconv[:, :, 1], out_range=(0, 1)), 19), gain=15, cutoff=0.4), 401)) |
|
|
99 |
|
|
|
100 |
# <codecell> |
|
|
101 |
|
|
|
102 |
# Labelled |
|
|
103 |
io.imshow(veins + 2 * blood + 3 * inflammation) |
|
|
104 |
|
|
|
105 |
# <codecell> |
|
|
106 |
|
|
|
107 |
# All regions of interest |
|
|
108 |
|
|
|
109 |
#%timeit |
|
|
110 |
|
|
|
111 |
""" |
|
|
112 |
all = \ |
|
|
113 |
minimum_filter( |
|
|
114 |
maximum_filter( |
|
|
115 |
np.logical_or(np.logical_or(blood, veins), inflammation).astype(float), |
|
|
116 |
size = 101), |
|
|
117 |
size = 51) |
|
|
118 |
""" |
|
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
""" |
|
|
123 |
morphology.binary_erosion( |
|
|
124 |
morphology.binary_dilation( |
|
|
125 |
np.logical_or(np.logical_or(blood, veins), inflammation), |
|
|
126 |
morphology.disk(31)), |
|
|
127 |
morphology.disk(21)) |
|
|
128 |
""" |
|
|
129 |
|
|
|
130 |
full = (np.logical_or(np.logical_or(blood, veins), inflammation)) |
|
|
131 |
|
|
|
132 |
#io.imshow(binary_fill_holes(minimum_filter(maximum_filter(full, size=51, footprint=morphology.diamond(31)), size=51))) |
|
|
133 |
|
|
|
134 |
# <codecell> |
|
|
135 |
|
|
|
136 |
io.imshow(ski.img_as_float(A)*0.5 + full[:, :, newaxis]*0.5) |
|
|
137 |
|
|
|
138 |
# <codecell> |
|
|
139 |
|
|
|
140 |
io.imsave("__1.gif", A) |
|
|
141 |
io.imsave("__2.gif", ski.img_as_float(full)) |
|
|
142 |
|
|
|
143 |
# <codecell> |
|
|
144 |
|
|
|
145 |
os.system("gifsicle --delay=80 --loop __*.gif > test.gif") |
|
|
146 |
|
|
|
147 |
# <codecell> |
|
|
148 |
|
|
|
149 |
|