@@ -22,6 +22,7 @@ def convert_segmentation_to_spheres(
22
22
num_workers : Optional [int ] = None ,
23
23
resolution : Optional [Tuple [float , float , float ]] = None ,
24
24
radius_factor : float = 1.0 ,
25
+ estimate_radius_2d : bool = True ,
25
26
):
26
27
"""Extract spheres parameterized by center and radius from a segmentation.
27
28
@@ -31,6 +32,9 @@ def convert_segmentation_to_spheres(
31
32
num_workers: Number of workers to use for parallelization.
32
33
resolution: The physical resolution of the data.
33
34
radius_factor: Factor for increasing the radius to account for too small exported spheres.
35
+ estimate_radius_2d: If true the distance to boundary for determining the centroid and computing
36
+ the radius will be computed only in 2d rather than in 3d. This can lead to better results
37
+ in case of deformation across the depth axis.
34
38
35
39
Returns:
36
40
np.array: the center coordinates
@@ -45,10 +49,14 @@ def coords_and_rads(prop):
45
49
bbox = prop .bbox
46
50
bb = np .s_ [bbox [0 ]:bbox [3 ], bbox [1 ]:bbox [4 ], bbox [2 ]:bbox [5 ]]
47
51
mask = segmentation [bb ] == seg_id
48
- seg_radii = distance_transform_edt (mask , sampling = resolution )
49
52
50
- max_coord = np .unravel_index (np .argmax (seg_radii ), mask .shape )
51
- radius = seg_radii [max_coord ] * radius_factor
53
+ if estimate_radius_2d :
54
+ dists = np .array ([distance_transform_edt (ma , sampling = resolution [1 :]) for ma in mask ])
55
+ else :
56
+ dists = distance_transform_edt (mask , sampling = resolution )
57
+
58
+ max_coord = np .unravel_index (np .argmax (dists ), mask .shape )
59
+ radius = dists [max_coord ] * radius_factor
52
60
53
61
offset = np .array (bbox [:3 ])
54
62
coord = np .array (max_coord ) + offset
@@ -120,6 +128,7 @@ def write_segmentation_to_imod_as_points(
120
128
output_path : str ,
121
129
min_radius : Union [int , float ],
122
130
radius_factor : float = 1.0 ,
131
+ estimate_radius_2d : bool = True ,
123
132
):
124
133
"""Write segmentation results to .mod file with imod point annotations.
125
134
@@ -131,6 +140,9 @@ def write_segmentation_to_imod_as_points(
131
140
output_path: Where to save the .mod file.
132
141
min_radius: Minimum radius for export.
133
142
radius_factor: Factor for increasing the radius to account for too small exported spheres.
143
+ estimate_radius_2d: If true the distance to boundary for determining the centroid and computing
144
+ the radius will be computed only in 2d rather than in 3d. This can lead to better results
145
+ in case of deformation across the depth axis.
134
146
"""
135
147
136
148
# Read the resolution information from the mrcfile.
@@ -142,7 +154,9 @@ def write_segmentation_to_imod_as_points(
142
154
143
155
# Extract the center coordinates and radii from the segmentation.
144
156
segmentation = imageio .imread (segmentation_path )
145
- coordinates , radii = convert_segmentation_to_spheres (segmentation , resolution = resolution , radius_factor = radius_factor )
157
+ coordinates , radii = convert_segmentation_to_spheres (
158
+ segmentation , resolution = resolution , radius_factor = radius_factor , estimate_radius_2d = estimate_radius_2d
159
+ )
146
160
147
161
# Write the point annotations to imod.
148
162
write_points_to_imod (coordinates , radii , segmentation .shape , min_radius , output_path )
0 commit comments