annotate split_labelmap.py @ 0:597b7ef44b05 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
author imgteam
date Sat, 09 Feb 2019 14:47:34 -0500
parents
children 9db1c22dbe17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
597b7ef44b05 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
597b7ef44b05 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
597b7ef44b05 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
597b7ef44b05 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
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 import scipy
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 import skimage.io
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7 import skimage.draw
597b7ef44b05 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
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9 import os
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10 import argparse
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
11 import warnings
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12
597b7ef44b05 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
597b7ef44b05 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).
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
15
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 def split_labelmap(labelmap,outputfile):
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
18
597b7ef44b05 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.
597b7ef44b05 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)
597b7ef44b05 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
597b7ef44b05 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)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
23 N = len(props)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
24
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
25 # Creating the backgrounds.
597b7ef44b05 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')
597b7ef44b05 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')
597b7ef44b05 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.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
29
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30 i = 0
597b7ef44b05 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:
597b7ef44b05 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')
597b7ef44b05 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)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34
597b7ef44b05 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.
597b7ef44b05 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
597b7ef44b05 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
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
38
597b7ef44b05 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.
597b7ef44b05 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(
597b7ef44b05 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)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
42
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
43 i += 1
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
44
597b7ef44b05 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:
597b7ef44b05 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.
597b7ef44b05 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)
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
48
597b7ef44b05 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.
597b7ef44b05 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):
597b7ef44b05 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):
597b7ef44b05 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:
597b7ef44b05 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
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 # 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
56 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
57
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
58 # Save image
597b7ef44b05 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():
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
60 warnings.simplefilter("ignore")
597b7ef44b05 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")
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
62
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
63 return None
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
64
597b7ef44b05 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.
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
66 if __name__ == "__main__":
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
67 parser = argparse.ArgumentParser()
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('labelmap',
597b7ef44b05 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.')
597b7ef44b05 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',
597b7ef44b05 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 '
597b7ef44b05 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')
597b7ef44b05 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
73
597b7ef44b05 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()
597b7ef44b05 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)