annotate 2d_split_binaryimage_by_watershed.py @ 3:a041e4e9d449 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
author imgteam
date Mon, 12 May 2025 08:15:32 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
1 import argparse
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
2 import sys
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
3
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
4 import numpy as np
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
5 import skimage.io
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
6 import skimage.util
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
7 from scipy import ndimage as ndi
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
8 from skimage.feature import peak_local_max
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
9 from skimage.segmentation import watershed
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
10
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
11
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
12 if __name__ == "__main__":
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
13 parser = argparse.ArgumentParser(description='Split binaryimage by watershed')
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
14 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
15 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)')
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
16 parser.add_argument('min_distance', type=int, default=100, help='Minimum distance to next object')
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
17 args = parser.parse_args()
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
18
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
19 img_in = skimage.io.imread(args.input_file.name)
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
20 distance = ndi.distance_transform_edt(img_in)
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
21
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
22 local_max_indices = peak_local_max(
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
23 distance,
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
24 min_distance=args.min_distance,
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
25 labels=img_in,
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
26 )
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
27 local_max_mask = np.zeros(img_in.shape, dtype=bool)
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
28 local_max_mask[tuple(local_max_indices.T)] = True
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
29 markers = ndi.label(local_max_mask)[0]
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
30 res = watershed(-distance, markers, mask=img_in)
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
31
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
32 res = skimage.util.img_as_uint(res)
a041e4e9d449 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
diff changeset
33 skimage.io.imsave(args.out_file.name, res, plugin="tifffile")