1- from __future__ import print_function , division
1+ from __future__ import division
2+ from __future__ import print_function
3+
24import os
3- from dask_image .imread import imread
4- from PIL import Image
55
66import numpy as np
7+ from dask_image .imread import imread
8+ from PIL import Image
79from skimage .measure import label
810from skimage .morphology import remove_small_objects
911from skimage .segmentation import watershed
1012from skimage .transform import resize
1113
1214
13- def binary_connected (volume , thres = 0.5 , thres_small = 3 , scale_factors = (1.0 , 1.0 , 1.0 )):
15+ def binary_connected (
16+ volume , thres = 0.5 , thres_small = 3 , scale_factors = (1.0 , 1.0 , 1.0 )
17+ ):
1418 r"""Convert binary foreground probability maps to instance masks via
1519 connected-component labeling.
1620
@@ -21,20 +25,35 @@ def binary_connected(volume, thres=0.5, thres_small=3, scale_factors=(1.0, 1.0,
2125 scale_factors (tuple): scale factors for resizing in :math:`(Z, Y, X)` order. Default: (1.0, 1.0, 1.0)
2226 """
2327 semantic = volume [0 ]
24- foreground = ( semantic > int (255 * thres ) )
28+ foreground = semantic > int (255 * thres )
2529 segm = label (foreground )
2630 segm = remove_small_objects (segm , thres_small )
2731
2832 if not all (x == 1.0 for x in scale_factors ):
29- target_size = (int (semantic .shape [0 ] * scale_factors [0 ]),
30- int (semantic .shape [1 ] * scale_factors [1 ]),
31- int (semantic .shape [2 ] * scale_factors [2 ]))
32- segm = resize (segm , target_size , order = 0 , anti_aliasing = False , preserve_range = True )
33+ target_size = (
34+ int (semantic .shape [0 ] * scale_factors [0 ]),
35+ int (semantic .shape [1 ] * scale_factors [1 ]),
36+ int (semantic .shape [2 ] * scale_factors [2 ]),
37+ )
38+ segm = resize (
39+ segm ,
40+ target_size ,
41+ order = 0 ,
42+ anti_aliasing = False ,
43+ preserve_range = True ,
44+ )
3345
3446 return segm
3547
3648
37- def binary_watershed (volume , thres1 = 0.9 , thres2 = 0.3 , thres_small = 3 , scale_factors = (1.0 , 1.0 , 1.0 ), seed_thres = 3 ):
49+ def binary_watershed (
50+ volume ,
51+ thres1 = 0.9 ,
52+ thres2 = 0.3 ,
53+ thres_small = 3 ,
54+ scale_factors = (1.0 , 1.0 , 1.0 ),
55+ seed_thres = 3 ,
56+ ):
3857 r"""Convert binary foreground probability maps to instance masks via
3958 watershed segmentation algorithm.
4059
@@ -58,10 +77,18 @@ def binary_watershed(volume, thres1=0.9, thres2=0.3, thres_small=3, scale_factor
5877 segm = remove_small_objects (segm , thres_small )
5978
6079 if not all (x == 1.0 for x in scale_factors ):
61- target_size = (int (semantic .shape [0 ] * scale_factors [0 ]),
62- int (semantic .shape [1 ] * scale_factors [1 ]),
63- int (semantic .shape [2 ] * scale_factors [2 ]))
64- segm = resize (segm , target_size , order = 0 , anti_aliasing = False , preserve_range = True )
80+ target_size = (
81+ int (semantic .shape [0 ] * scale_factors [0 ]),
82+ int (semantic .shape [1 ] * scale_factors [1 ]),
83+ int (semantic .shape [2 ] * scale_factors [2 ]),
84+ )
85+ segm = resize (
86+ segm ,
87+ target_size ,
88+ order = 0 ,
89+ anti_aliasing = False ,
90+ preserve_range = True ,
91+ )
6592
6693 return segm
6794
@@ -89,12 +116,16 @@ def write_tiff_stack(vol, fname):
89116y_pred = y_pred .astype ("uint8" )
90117
91118# Run post process
92- output_watershed_path = base_path + "/data/testing/instance-segmentation-w.tiff"
93- output_connected_path = base_path + "/data/testing/instance-segmentation-c.tiff"
119+ output_watershed_path = (
120+ base_path + "/data/testing/instance-segmentation-w.tiff"
121+ )
122+ output_connected_path = (
123+ base_path + "/data/testing/instance-segmentation-c.tiff"
124+ )
94125
95126bw_result = binary_watershed (y_pred )
96127bc_result = binary_connected (y_pred )
97128
98129# Save instance predictions
99130write_tiff_stack (bw_result , output_watershed_path )
100- write_tiff_stack (bc_result , output_connected_path )
131+ write_tiff_stack (bc_result , output_connected_path )
0 commit comments