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