comparison split_labelmap.py @ 1:9db1c22dbe17 draft

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