diff 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
line wrap: on
line diff
--- a/split_labelmap.py	Sat Feb 09 14:47:34 2019 -0500
+++ b/split_labelmap.py	Mon Nov 13 22:12:26 2023 +0000
@@ -1,20 +1,18 @@
+import argparse
+import warnings
+
+import numpy as np
+import scipy
+import skimage.draw
+import skimage.io
 from imageio import imread as io_imread
 from skimage.measure import regionprops
-import numpy as np
-#import matplotlib.pyplot as plt
-import scipy
-import skimage.io
-import skimage.draw
-from tifffile import imsave
-import os
-import argparse
-import warnings
 
 # split_label_image takes a label image and outputs a similar file with the given name where the labeled
 # parts of the image that touch (or overlap) are separated by at least 1 pixel (at most 2).
 
 
-def split_labelmap(labelmap,outputfile):
+def split_labelmap(labelmap, outputfile):
 
     # Information from the label map.
     label_img = io_imread(labelmap)
@@ -23,17 +21,16 @@
     N = len(props)
 
     # Creating the backgrounds.
-    background = np.zeros([xtot,ytot], 'uint8')
-    overlap = np.zeros([N,xtot,ytot],'uint8')
+    background = np.zeros([xtot, ytot], 'uint8')
+    overlap = np.zeros([N, xtot, ytot], 'uint8')
     compstruct = scipy.ndimage.generate_binary_structure(2, 2)  # Mask for image dilation.
 
     i = 0
     for cell in props:
         cell_image = cell.image.astype('uint8')
-        #plt.imshow(cell_image)
 
         # Replace the background area corresponding to the bounding box with the image representing the cell.
-        background[int(cell.bbox[0]):int(cell.bbox[2]),int(cell.bbox[1]):int(cell.bbox[3])] += cell_image
+        background[int(cell.bbox[0]):int(cell.bbox[2]), int(cell.bbox[1]):int(cell.bbox[3])] += cell_image
         overlap[i][int(cell.bbox[0]):int(cell.bbox[2]), int(cell.bbox[1]):int(cell.bbox[3])] = cell_image
 
         # In the overlap array, dilate the cell in all directions.
@@ -45,23 +42,24 @@
     if len(props) > 1:
         # Sum together the overlap.
         total_overlap = sum(overlap)
-    
+
         # Wherever the overlap is greater than 1 replace that point with zero in the final image.
         for x in range(xtot):
             for y in range(ytot):
-                if total_overlap[x,y] > 1:
-                    background[x,y] = 0
+                if total_overlap[x, y] > 1:
+                    background[x, y] = 0
 
     # Force the image into 8-bit.
     result = skimage.util.img_as_ubyte(background)
 
     # Save image
-    with warnings.catch_warnings(): 
+    with warnings.catch_warnings():
         warnings.simplefilter("ignore")
         skimage.io.imsave(outputfile, result, plugin="tifffile")
 
     return None
 
+
 # To run from command line.
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()