NB: used by default in most registration types!
Mask for single neuron images with bright soma region
with Sauvola local threshold. Func drops soma and returns the mask for processes only.
In the original method, a threshold T is calculated
for every pixel in the image using the following formula
(m(x,y)
is mean and s(x,y)
is SD in rectangular window):
T = m(x,y) * (1 + k * ((s(x,y) / r) - 1))
Parameters: |
-
input_img
(ndarray )
–
input img for mask creation,
recomend choose max int projection of brighter channel (GFP/YFP in our case)
-
proc_sigma
(float , default:
1.0
)
–
sigma value for gaussian blur of input image
-
win_size
(int , default:
801
)
–
Sauvola local threshold parameter, window size specified as a single odd integer
-
k_val
(float , default:
0.0001
)
–
Sauvola local threshold parameter, value of the positive parameter k
-
r_val
(float , default:
0.5
)
–
Sauvola local threshold parameter, value of r , the dynamic range of standard deviation
-
soma_mask
(bool , default:
False
)
–
if True brighter region on image will identified and masked as soma
-
soma_th
(float , default:
0.5
)
–
soma detection threshold in % of max img int
-
soma_ext
(int , default:
100
)
–
soma mask extension size in px
-
ext_fin_mask
(bool , default:
False
)
–
if True - final processes mask will be extended on proc_ext value
-
proc_ext
(int , default:
5
)
–
neuron processes mask extention value in px
-
select_largest_mask
(bool , default:
False
)
–
if True - final mask will contain the largest detected element only
|
Returns: |
-
proc_mask ( ndarray, dtype boolean
) –
|
src/domb/utils/masking.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 | def proc_mask(input_img:np.ndarray,
proc_sigma:float=1.0, win_size:int=801, k_val:float=1e-4, r_val:float=0.5,
soma_mask:bool=False, soma_th:float=.5, soma_ext:int=100,
ext_fin_mask:bool=False, proc_ext:int=5,
select_largest_mask:bool=False):
"""
__NB: used by default in most registration types!__
Mask for single neuron images with bright soma region
with Sauvola local threshold. Func drops soma and returns the mask for processes only.
In the original method, a threshold T is calculated
for every pixel in the image using the following formula
(`m(x,y)` is mean and `s(x,y)` is SD in rectangular window):
`
T = m(x,y) * (1 + k * ((s(x,y) / r) - 1))
`
Parameters
----------
input_img: ndarray
input img for mask creation,
recomend choose max int projection of brighter channel (GFP/YFP in our case)
proc_sigma: float
sigma value for gaussian blur of input image
win_size: int
Sauvola local threshold parameter, window size specified as a single __odd__ integer
k_val: float
Sauvola local threshold parameter, value of the positive parameter `k`
r_val: float
Sauvola local threshold parameter, value of `r`, the dynamic range of standard deviation
soma_mask: boolean, optional
if True brighter region on image will identified and masked as soma
soma_th: float, optional
soma detection threshold in % of max img int
soma_ext: int, optional
soma mask extension size in px
ext_fin_mask: boolean, optional
if `True` - final processes mask will be extended on proc_ext value
proc_ext: int
neuron processes mask extention value in px
select_largest_mask: bool, optional
if `True` - final mask will contain the largest detected element only
Returns
-------
proc_mask: ndarray, dtype boolean
neuron processes mask
"""
input_img = filters.gaussian(input_img, sigma=proc_sigma)
# processes masking
proc_th = filters.threshold_sauvola(input_img, window_size=win_size, k=k_val, r=r_val)
proc_mask = input_img > proc_th
# mask extention
if ext_fin_mask:
proc_dist = ndi.distance_transform_edt(~proc_mask, return_indices=False)
proc_mask = proc_dist <= proc_ext
# soma masking
if soma_mask:
soma_region = np.copy(input_img)
soma_region = soma_region > soma_region.max() * soma_th
soma_region = morphology.opening(soma_region, footprint=morphology.disk(5))
soma_dist = ndi.distance_transform_edt(~soma_region, return_indices=False)
soma_region = soma_dist < soma_ext
proc_mask[soma_region] = 0
# minor mask elements filtering
if select_largest_mask:
def largest_only(raw_mask):
# get larger mask element
element_label = measure.label(raw_mask)
element_area = {element.area : element.label for element in measure.regionprops(element_label)}
larger_mask = element_label == element_area[max(element_area.keys())]
return larger_mask
proc_mask = largest_only(proc_mask)
return proc_mask
|