-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_base.py
98 lines (65 loc) · 2.19 KB
/
get_base.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
#!/usr/bin/env python3
import numpy as np
import numpy.ma as ma
import scipy.ndimage as ndi
from skimage import color, draw, exposure, feature, filters, graph, measure
from skimage import morphology, restoration, segmentation, transform, util
from skimage.exposure import rescale_intensity, equalize_adapthist
from skimage.color.adapt_rgb import adapt_rgb, each_channel
import matplotlib.pyplot as plt
def get_bg_mask(img):
if img.ndim == 3:
bg_mask = img.any(axis=-1)
bg_mask = np.invert(bg_mask) # consistent with np.ma, True if masked
# make multichannel (is it really this hard?)
bg_mask = np.repeat(bg_mask[:,:,np.newaxis], 3, axis=2)
else:
bg_mask = (img != 0)
bg_mask = np.invert(bg_mask) # see above
bound = segmentation.find_boundaries(bg_mask, mode='inner', background=1)
bg_mask[bound] = 1
holes = morphology.remove_small_holes(bg_mask)
bg_mask[holes] = 1
return bg_mask
def get_trace(tracefile=None):
if tracefile is None:
tracefile = 'samples/trace.png'
t = ndi.imread(tracefile, 'L');
t = t.astype('bool')
return np.invert(t)
def get_masked_raw(imgfile=None, mode='RGB'):
if mode == 'G':
readmode = 'RGB'
else:
readmode = mode
if imgfile is None:
imgfile = 'samples/raw.png'
img = ndi.imread(imgfile, mode=readmode)
if mode == 'G':
img = img[:,:,1] # green channel only (disregard rest)
bg_mask = get_bg_mask(img)
I = ma.masked_array(img, mask=bg_mask)
return I
@adapt_rgb(each_channel)
def clahe_each(img):
# note you need to fix the fucking mask after this :/
return exposure.equalize_adapthist(img)
def preprocess(img):
if img.ndim == 3:
I = clahe_each(img)
else:
I = equalize_adapthist(img)
try:
mask = img.mask
except AttributeError:
pass
else:
I = ma.masked_array(I, mask=mask)
return I
def get_preprocessed(imgfile=None, mode='L'):
img = get_masked_raw(imgfile, mode=mode)
I = preprocess(img)
return I
if __name__ == "__main__":
img = get_masked_raw(mode='G')
I = preprocess(img)