Skip to content

Commit 80f4af4

Browse files
authored
Minor fix to automatic segmentation in 3d (#882)
Fix to automatic segmentation in 3d for data with channels
1 parent 6dc6a9e commit 80f4af4

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

micro_sam/automatic_segmentation.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ def automatic_instance_segmentation(
101101
Returns:
102102
The segmentation result.
103103
"""
104+
# Avoid overwriting already stored segmentations.
105+
if output_path is not None:
106+
output_path = Path(output_path).with_suffix(".tif")
107+
if os.path.exists(output_path):
108+
print(f"The segmentation results are already stored at '{os.path.abspath(output_path)}'.")
109+
return
110+
104111
# Load the input image file.
105112
if isinstance(input_path, np.ndarray):
106113
image_data = input_path
@@ -189,8 +196,8 @@ def automatic_instance_segmentation(
189196

190197
# Save the instance segmentation, if 'output_path' provided.
191198
if output_path is not None:
192-
output_path = Path(output_path).with_suffix(".tif")
193199
imageio.imwrite(output_path, instances, compression="zlib")
200+
print(f"The segmentation results are stored at '{os.path.abspath(output_path)}'.")
194201

195202
if return_embeddings:
196203
return instances, image_embeddings
@@ -256,6 +263,9 @@ def main():
256263
help="The device to use for the predictor. Can be one of 'cuda', 'cpu' or 'mps' (only MAC)."
257264
"By default the most performant available device will be selected."
258265
)
266+
parser.add_argument(
267+
"-v", "--verbose", action="store_true", help="Whether to allow verbosity of outputs."
268+
)
259269

260270
args, parameter_args = parser.parse_known_args()
261271

@@ -314,6 +324,7 @@ def _convert_argval(value):
314324
tile_shape=args.tile_shape,
315325
halo=args.halo,
316326
annotate=args.annotate,
327+
verbose=args.verbose,
317328
**generate_kwargs,
318329
)
319330

micro_sam/instance_segmentation.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626

2727
from nifty.tools import blocking
2828

29-
import elf.parallel as parallel
30-
from elf.parallel.filters import apply_filter
31-
3229
import segment_anything.utils.amg as amg_utils
3330
from segment_anything.predictor import SamPredictor
3431

micro_sam/multi_dimensional_segmentation.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def automatic_3d_segmentation(
397397
The segmentation.
398398
"""
399399
offset = 0
400-
segmentation = np.zeros(volume.shape, dtype="uint32")
400+
segmentation = np.zeros(volume.shape[:3], dtype="uint32")
401401

402402
min_object_size = kwargs.pop("min_object_size", 0)
403403
image_embeddings = util.precompute_image_embeddings(
@@ -413,15 +413,22 @@ def automatic_3d_segmentation(
413413
for i in tqdm(range(segmentation.shape[0]), desc="Segment slices", disable=not verbose):
414414
segmentor.initialize(volume[i], image_embeddings=image_embeddings, verbose=False, i=i)
415415
seg = segmentor.generate(**kwargs)
416-
if len(seg) == 0:
416+
417+
if isinstance(seg, list) and len(seg) == 0:
417418
continue
418419
else:
419-
seg = mask_data_to_segmentation(seg, with_background=with_background, min_object_size=min_object_size)
420+
if isinstance(seg, list):
421+
seg = mask_data_to_segmentation(
422+
seg, with_background=with_background, min_object_size=min_object_size
423+
)
424+
425+
# Set offset for instance per slice.
420426
max_z = seg.max()
421427
if max_z == 0:
422428
continue
423429
seg[seg != 0] += offset
424430
offset = max_z + offset
431+
425432
segmentation[i] = seg
426433

427434
segmentation = merge_instance_segmentation_3d(

0 commit comments

Comments
 (0)