annotate split_labelmap.py @ 0:564dd83ce0c0 draft default tip

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