|
| 1 | +import imageio.v3 as imageio |
| 2 | +import napari |
| 3 | + |
| 4 | +from micro_sam import instance_segmentation, util |
| 5 | + |
| 6 | + |
| 7 | +def cell_segmentation(): |
| 8 | + """Run the instance segmentation functionality from micro_sam for segmentation of |
| 9 | + HeLA cells. You need to run examples/sam_annotator_2d.py:hela_2d_annotator once before |
| 10 | + running this script so that all required data is downloaded and pre-computed. |
| 11 | + """ |
| 12 | + image_path = "../data/hela-2d-image.png" |
| 13 | + embedding_path = "../embeddings/embeddings-hela2d.zarr" |
| 14 | + |
| 15 | + # Load the image, the SAM Model, and the pre-computed embeddings. |
| 16 | + image = imageio.imread(image_path) |
| 17 | + predictor = util.get_sam_model() |
| 18 | + embeddings = util.precompute_image_embeddings(predictor, image, save_path=embedding_path) |
| 19 | + |
| 20 | + # Use the instance segmentation logic of SegmentAnything. |
| 21 | + # This works by covering the image with a grid of points, getting the masks for all the poitns |
| 22 | + # and only keeping the plausible ones (according to the model predictions). |
| 23 | + # While the functionality here does the same as the implementation from SegmentAnything, |
| 24 | + # we enable changing the hyperparameters, e.g. 'pred_iou_thresh', without recomputing masks and embeddings, |
| 25 | + # to support (interactive) evaluation of different hyperparameters. |
| 26 | + |
| 27 | + # Create the automatic mask generator class. |
| 28 | + amg = instance_segmentation.AutomaticMaskGenerator(predictor) |
| 29 | + |
| 30 | + # Initialize the mask generator with the image and the pre-computed embeddings. |
| 31 | + amg.initialize(image, embeddings, verbose=True) |
| 32 | + |
| 33 | + # Generate the instance segmentation. You can call this again for different values of 'pred_iou_thresh' |
| 34 | + # without having to call initialize again. |
| 35 | + instances_amg = amg.generate(pred_iou_thresh=0.88) |
| 36 | + instances_amg = instance_segmentation.mask_data_to_segmentation( |
| 37 | + instances_amg, shape=image.shape, with_background=True |
| 38 | + ) |
| 39 | + |
| 40 | + # Use the mutex waterhsed based instance segmentation logic. |
| 41 | + # Here, we generate initial segmentation masks from the image embeddings, using the mutex watershed algorithm. |
| 42 | + # These initial masks are used as prompts for the actual instance segmentation. |
| 43 | + # This class uses the same overall design as 'AutomaticMaskGenerator'. |
| 44 | + |
| 45 | + # Create the automatic mask generator class. |
| 46 | + amg_mws = instance_segmentation.EmbeddingMaskGenerator(predictor, min_initial_size=10) |
| 47 | + |
| 48 | + # Initialize the mask generator with the image and the pre-computed embeddings. |
| 49 | + amg_mws.initialize(image, embeddings, verbose=True) |
| 50 | + |
| 51 | + # Generate the instance segmentation. You can call this again for different values of 'pred_iou_thresh' |
| 52 | + # without having to call initialize again. |
| 53 | + # NOTE: the main advantage of this method is that it's considerably faster than the original implementation. |
| 54 | + instances_mws = amg_mws.generate(pred_iou_thresh=0.88) |
| 55 | + instances_mws = instance_segmentation.mask_data_to_segmentation( |
| 56 | + instances_mws, shape=image.shape, with_background=True |
| 57 | + ) |
| 58 | + |
| 59 | + # Show the results. |
| 60 | + v = napari.Viewer() |
| 61 | + v.add_image(image) |
| 62 | + v.add_labels(instances_amg) |
| 63 | + v.add_labels(instances_mws) |
| 64 | + napari.run() |
| 65 | + |
| 66 | + |
| 67 | +def segmentation_with_tiling(): |
| 68 | + """Run the instance segmentation functionality from micro_sam for segmentation of |
| 69 | + cells in a large image. You need to run examples/sam_annotator_2d.py:wholeslide_annotator once before |
| 70 | + running this script so that all required data is downloaded and pre-computed. |
| 71 | + """ |
| 72 | + image_path = "../data/whole-slide-example-image.tif" |
| 73 | + embedding_path = "../embeddings/whole-slide-embeddings.zarr" |
| 74 | + |
| 75 | + # Load the image, the SAM Model, and the pre-computed embeddings. |
| 76 | + image = imageio.imread(image_path) |
| 77 | + predictor = util.get_sam_model() |
| 78 | + embeddings = util.precompute_image_embeddings( |
| 79 | + predictor, image, save_path=embedding_path, tile_shape=(1024, 1024), halo=(256, 256) |
| 80 | + ) |
| 81 | + |
| 82 | + # Use the instance segmentation logic of SegmentAnything. |
| 83 | + # This works by covering the image with a grid of points, getting the masks for all the poitns |
| 84 | + # and only keeping the plausible ones (according to the model predictions). |
| 85 | + # The functionality here is similar to the instance segmentation in Segment Anything, |
| 86 | + # but uses the pre-computed tiled embeddings. |
| 87 | + |
| 88 | + # Create the automatic mask generator class. |
| 89 | + amg = instance_segmentation.TiledAutomaticMaskGenerator(predictor) |
| 90 | + |
| 91 | + # Initialize the mask generator with the image and the pre-computed embeddings. |
| 92 | + amg.initialize(image, embeddings, verbose=True) |
| 93 | + |
| 94 | + # Generate the instance segmentation. You can call this again for different values of 'pred_iou_thresh' |
| 95 | + # without having to call initialize again. |
| 96 | + instances_amg = amg.generate(pred_iou_thresh=0.88) |
| 97 | + instances_amg = instance_segmentation.mask_data_to_segmentation( |
| 98 | + instances_amg, shape=image.shape, with_background=True |
| 99 | + ) |
| 100 | + |
| 101 | + # Use the mutex waterhsed based instance segmentation logic. |
| 102 | + # Here, we generate initial segmentation masks from the image embeddings, using the mutex watershed algorithm. |
| 103 | + # These initial masks are used as prompts for the actual instance segmentation. |
| 104 | + # This class uses the same overall design as 'AutomaticMaskGenerator'. |
| 105 | + |
| 106 | + # Create the automatic mask generator class. |
| 107 | + amg_mws = instance_segmentation.TiledEmbeddingMaskGenerator(predictor, min_initial_size=10) |
| 108 | + |
| 109 | + # Initialize the mask generator with the image and the pre-computed embeddings. |
| 110 | + amg_mws.initialize(image, embeddings, verbose=True) |
| 111 | + |
| 112 | + # Generate the instance segmentation. You can call this again for different values of 'pred_iou_thresh' |
| 113 | + # without having to call initialize again. |
| 114 | + # NOTE: the main advantage of this method is that it's considerably faster than the original implementation. |
| 115 | + instances_mws = amg_mws.generate(pred_iou_thresh=0.88) |
| 116 | + |
| 117 | + # Show the results. |
| 118 | + v = napari.Viewer() |
| 119 | + v.add_image(image) |
| 120 | + # v.add_labels(instances_amg) |
| 121 | + v.add_labels(instances_mws) |
| 122 | + napari.run() |
| 123 | + |
| 124 | + |
| 125 | +def main(): |
| 126 | + cell_segmentation() |
| 127 | + # segmentation_with_tiling() |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments