|
| 1 | +import argparse |
| 2 | + |
| 3 | +import imageio.v3 as imageio |
| 4 | +import numpy as np |
| 5 | +from scipy.ndimage import binary_dilation, binary_closing, distance_transform_edt |
| 6 | + |
| 7 | + |
| 8 | +def intensity_masking(image_path, seg_path, out_path, modulation_strength=10, dilation=2, view=False): |
| 9 | + seg = imageio.imread(seg_path) |
| 10 | + mask = binary_dilation(seg != 0, iterations=2) |
| 11 | + mask = binary_closing(mask, iterations=4) |
| 12 | + |
| 13 | + image = imageio.imread(image_path) |
| 14 | + lo, hi = np.percentile(image, 2), np.percentile(image, 98) |
| 15 | + print(lo, hi) |
| 16 | + image_modulated = np.clip(image, lo, hi).astype("float32") |
| 17 | + image_modulated -= lo |
| 18 | + image_modulated /= image_modulated.max() |
| 19 | + |
| 20 | + modulation_mask = distance_transform_edt(~mask) |
| 21 | + modulation_mask /= modulation_mask.max() |
| 22 | + modulation_mask = 1 - modulation_mask |
| 23 | + modulation_mask[mask] = 1 |
| 24 | + modulation_mask = np.pow(modulation_mask, 3) |
| 25 | + modulation_mask *= modulation_strength |
| 26 | + image_modulated *= modulation_mask |
| 27 | + |
| 28 | + if view: |
| 29 | + import napari |
| 30 | + v = napari.Viewer() |
| 31 | + v.add_image(modulation_mask) |
| 32 | + v.add_image(image, visible=False) |
| 33 | + v.add_image(image_modulated) |
| 34 | + v.add_labels(mask, visible=False) |
| 35 | + napari.run() |
| 36 | + return |
| 37 | + imageio.imwrite(out_path, image_modulated, compression="zlib") |
| 38 | + |
| 39 | + |
| 40 | +# image_path = "M_LR_000227_R/scale3/PV.tif" |
| 41 | +# seg_path = "M_LR_000227_R/scale3/SGN_v2.tif" |
| 42 | +if __name__ == "__main__": |
| 43 | + parser = argparse.ArgumentParser() |
| 44 | + parser.add_argument("image_path") |
| 45 | + parser.add_argument("seg_path") |
| 46 | + parser.add_argument("out_path") |
| 47 | + parser.add_argument("--view", "-v", action="store_true") |
| 48 | + parser.add_argument("--dilation", type=int, default=2) |
| 49 | + args = parser.parse_args() |
| 50 | + intensity_masking(args.image_path, args.seg_path, args.out_path, view=args.view, dilation=args.dilation) |
0 commit comments