annotate split_labelmap.py @ 2:7a2e2e3cf836 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
author imgteam
date Thu, 04 Apr 2024 15:26:45 +0000
parents 9db1c22dbe17
children 0e7be7c35f15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
1 import argparse
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
2 import warnings
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
3
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
4 import numpy as np
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
5 import scipy
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
6 import skimage.draw
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
7 import skimage.io
0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
8 from imageio import imread as io_imread
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9 from skimage.measure import regionprops
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
11 # split_label_image takes a label image and outputs a similar file with the given name where the labeled
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12 # parts of the image that touch (or overlap) are separated by at least 1 pixel (at most 2).
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
13
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
14
1
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
15 def split_labelmap(labelmap, outputfile):
0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
16
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
17 # Information from the label map.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
18 label_img = io_imread(labelmap)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
19 xtot, ytot = label_img.shape
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20 props = regionprops(label_img)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
21 N = len(props)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
22
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
23 # Creating the backgrounds.
1
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
24 background = np.zeros([xtot, ytot], 'uint8')
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
25 overlap = np.zeros([N, xtot, ytot], 'uint8')
0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
26 compstruct = scipy.ndimage.generate_binary_structure(2, 2) # Mask for image dilation.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
27
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
28 i = 0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
29 for cell in props:
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30 cell_image = cell.image.astype('uint8')
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
31
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
32 # Replace the background area corresponding to the bounding box with the image representing the cell.
1
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
33 background[int(cell.bbox[0]):int(cell.bbox[2]), int(cell.bbox[1]):int(cell.bbox[3])] += cell_image
0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34 overlap[i][int(cell.bbox[0]):int(cell.bbox[2]), int(cell.bbox[1]):int(cell.bbox[3])] = cell_image
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
35
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
36 # In the overlap array, dilate the cell in all directions.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
37 overlap[i] = scipy.ndimage.binary_dilation(
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
38 overlap[i], structure=compstruct).astype(overlap[i].dtype)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
39
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
40 i += 1
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
41
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
42 if len(props) > 1:
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
43 # Sum together the overlap.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
44 total_overlap = sum(overlap)
1
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
45
0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
46 # Wherever the overlap is greater than 1 replace that point with zero in the final image.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
47 for x in range(xtot):
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
48 for y in range(ytot):
1
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
49 if total_overlap[x, y] > 1:
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
50 background[x, y] = 0
0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
51
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
52 # Force the image into 8-bit.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
53 result = skimage.util.img_as_ubyte(background)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
54
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
55 # Save image
1
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
56 with warnings.catch_warnings():
0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
57 warnings.simplefilter("ignore")
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
58 skimage.io.imsave(outputfile, result, plugin="tifffile")
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
59
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
60 return None
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
61
1
9db1c22dbe17 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_labelmaps/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
62
0
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
63 # To run from command line.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
64 if __name__ == "__main__":
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
65 parser = argparse.ArgumentParser()
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
66 parser.add_argument('labelmap',
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
67 help='Label map image.')
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
68 parser.add_argument('outputfile',
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
69 help='Output file. Without extension (although it corrects if you '
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
70 'add it; will always return a .tif')
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
71
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
72 args = parser.parse_args()
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
73 split_labelmap(args.labelmap, args.outputfile)