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