|
| 1 | +import os |
| 2 | +from glob import glob |
| 3 | + |
| 4 | +import napari |
| 5 | +import numpy as np |
| 6 | +import imageio.v3 as imageio |
| 7 | +import vigra |
| 8 | + |
| 9 | +from skimage.filters import gaussian |
| 10 | +from skimage.segmentation import find_boundaries, watershed |
| 11 | +from scipy.ndimage import distance_transform_edt |
| 12 | +from skimage.feature import peak_local_max |
| 13 | +from skimage.measure import regionprops, label |
| 14 | + |
| 15 | + |
| 16 | +def _size_filter(segmentation, heightmap, min_size): |
| 17 | + ids, sizes = np.unique(segmentation, return_counts=True) |
| 18 | + discard_ids = ids[sizes < min_size] |
| 19 | + mask = segmentation > 0 |
| 20 | + segmentation[np.isin(segmentation, discard_ids)] = 0 |
| 21 | + return watershed(heightmap, markers=segmentation, mask=mask) |
| 22 | + |
| 23 | + |
| 24 | +def postproc(image, segmentation, view=False): |
| 25 | + # First get rid of small objects. |
| 26 | + min_size = 250 |
| 27 | + heightmap = vigra.filters.laplacianOfGaussian(image.astype("float32"), 3) |
| 28 | + |
| 29 | + segmentation = _size_filter(segmentation, heightmap, min_size) |
| 30 | + |
| 31 | + mask = ~find_boundaries(segmentation) |
| 32 | + dist = distance_transform_edt(mask, sampling=(2, 1, 1)) |
| 33 | + dist[segmentation == 0] = 0 |
| 34 | + dist = gaussian(dist, (0.6, 1.2, 1.2)) |
| 35 | + maxima = peak_local_max(dist, min_distance=3, exclude_border=False) |
| 36 | + |
| 37 | + maxima_image = np.zeros(segmentation.shape, dtype="uint8") |
| 38 | + pos = tuple(maxima[:, i] for i in range(3)) |
| 39 | + maxima_image[pos] = 1 |
| 40 | + maxima_image = label(maxima_image) |
| 41 | + |
| 42 | + def maxima_ids(seg, im): |
| 43 | + ids = np.unique(im[seg]) |
| 44 | + return ids[1:] |
| 45 | + |
| 46 | + seed_maxima_ids, keep_seg_ids, split_seg_ids = [], [], [] |
| 47 | + props = regionprops(segmentation, maxima_image, extra_properties=[maxima_ids]) |
| 48 | + for prop in props: |
| 49 | + this_maxima_ids = prop.maxima_ids |
| 50 | + if len(this_maxima_ids) == 1: |
| 51 | + keep_seg_ids.append(prop.label) |
| 52 | + continue |
| 53 | + seed_maxima_ids.extend(this_maxima_ids.tolist()) |
| 54 | + split_seg_ids.append(prop.label) |
| 55 | + |
| 56 | + split_mask = np.isin(segmentation, split_seg_ids) |
| 57 | + # segmentation[split_mask] = 0 |
| 58 | + |
| 59 | + new_seeds = maxima_image.copy() |
| 60 | + new_seeds[~np.isin(maxima_image, seed_maxima_ids)] = 0 |
| 61 | + new_seg = watershed(heightmap, markers=new_seeds, mask=split_mask) |
| 62 | + |
| 63 | + segmentation[split_mask] = 0 |
| 64 | + offset = segmentation.max() |
| 65 | + new_seg[new_seg != 0] += offset |
| 66 | + segmentation[split_mask] = new_seg[split_mask] |
| 67 | + segmentation = label(segmentation) |
| 68 | + segmentation = _size_filter(segmentation, heightmap, min_size) |
| 69 | + |
| 70 | + if view: |
| 71 | + v = napari.Viewer() |
| 72 | + v.add_image(image) |
| 73 | + v.add_labels(segmentation) |
| 74 | + # v.add_labels(new_seg) |
| 75 | + # v.add_image(heightmap) |
| 76 | + # v.add_image(dist) |
| 77 | + # v.add_points(maxima) |
| 78 | + # v.add_labels(split_mask) |
| 79 | + napari.run() |
| 80 | + |
| 81 | + return segmentation |
| 82 | + |
| 83 | + |
| 84 | +def postprocess_volume(im_path, seg_path, out_root): |
| 85 | + image = imageio.imread(im_path) |
| 86 | + segmentation = imageio.imread(seg_path) |
| 87 | + segmentation = postproc(image, segmentation, view=True) |
| 88 | + |
| 89 | + os.makedirs(out_root, exist_ok=True) |
| 90 | + fname = os.path.basename(im_path) |
| 91 | + imageio.imwrite(os.path.join(out_root, fname), segmentation, compression="zlib") |
| 92 | + |
| 93 | + |
| 94 | +def postprocess_volume_scalable(im_path, seg_path, out_root): |
| 95 | + from flamingo_tools.segmentation.postprocessing import split_nonconvex_objects, compute_table_on_the_fly |
| 96 | + |
| 97 | + image = imageio.imread(im_path) |
| 98 | + segmentation = imageio.imread(seg_path) |
| 99 | + |
| 100 | + # TODO aniso resolution |
| 101 | + resolution = 0.38 |
| 102 | + table = compute_table_on_the_fly(segmentation, resolution) |
| 103 | + |
| 104 | + out = np.zeros_like(segmentation) |
| 105 | + id_mapping = split_nonconvex_objects(segmentation, out, table, n_threads=1, resolution=resolution, min_size=250) |
| 106 | + n_prev = len(id_mapping) |
| 107 | + n_after = sum([len(v) for v in id_mapping.values()]) |
| 108 | + print("Before splitting:", n_prev) |
| 109 | + print("After splitting:", n_after) |
| 110 | + |
| 111 | + v = napari.Viewer() |
| 112 | + v.add_image(image) |
| 113 | + v.add_labels(segmentation, visible=False) |
| 114 | + v.add_labels(out) |
| 115 | + napari.run() |
| 116 | + |
| 117 | + |
| 118 | +def main(): |
| 119 | + im_paths = sorted(glob("la-vision-sgn-new/images/*.tif")) |
| 120 | + seg_paths = sorted(glob("la-vision-sgn-new/segmentation/*.tif")) |
| 121 | + out_root = "la-vision-sgn-new/segmentation-postprocessed" |
| 122 | + for im_path, seg_path in zip(im_paths, seg_paths): |
| 123 | + # postprocess_volume(im_path, seg_path, out_root) |
| 124 | + postprocess_volume_scalable(im_path, seg_path, out_root) |
| 125 | + break |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments