Repository 'split_labelmap'
hg clone https://toolshed.g2.bx.psu.edu/repos/imgteam/split_labelmap

Changeset 0:597b7ef44b05 (2019-02-09)
Next changeset 1:9db1c22dbe17 (2023-11-13)
Commit message:
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/split_labelmaps/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
added:
split_labelmap.py
split_labelmap.xml
test-data/out.tif
test-data/sample_seg.tif
b
diff -r 000000000000 -r 597b7ef44b05 split_labelmap.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/split_labelmap.py Sat Feb 09 14:47:34 2019 -0500
[
@@ -0,0 +1,75 @@
+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):
+
+    # Information from the label map.
+    label_img = io_imread(labelmap)
+    xtot, ytot = label_img.shape
+    props = regionprops(label_img)
+    N = len(props)
+
+    # Creating the backgrounds.
+    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
+        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.
+        overlap[i] = scipy.ndimage.binary_dilation(
+            overlap[i], structure=compstruct).astype(overlap[i].dtype)
+
+        i += 1
+
+    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
+
+    # Force the image into 8-bit.
+    result = skimage.util.img_as_ubyte(background)
+
+    # Save image
+    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()
+    parser.add_argument('labelmap',
+                        help='Label map image.')
+    parser.add_argument('outputfile',
+                        help='Output file. Without extension (although it corrects if you '
+                             'add it; will always return a .tif')
+
+    args = parser.parse_args()
+    split_labelmap(args.labelmap, args.outputfile)
b
diff -r 000000000000 -r 597b7ef44b05 split_labelmap.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/split_labelmap.xml Sat Feb 09 14:47:34 2019 -0500
[
@@ -0,0 +1,34 @@
+<tool id="ip_split_labelmap" name="Split Labelmap" version="0.1">
+    <description></description>
+    <requirements> 
+        <requirement type="package" version="0.14.2">scikit-image</requirement>
+        <requirement type="package" version="1.15.4">numpy</requirement>
+        <requirement type="package" version="0.15.1">tifffile</requirement> <!--not sure if this is required-->
+    </requirements>
+    <command> 
+        <![CDATA[
+        python '$__tool_directory__/split_labelmap.py' '$input' '$output'
+        ]]>
+    </command>
+    <inputs> 
+        <param name="input" type="data" format="tiff,png,jpg,bmp" label="Image containing labels"/> 
+    </inputs>
+    <outputs>
+        <data name="output" format="tiff"/>
+    </outputs>
+    <tests>
+        <test>
+            <param name="input" value="sample_seg.tif"/>
+            <output name="output" value="out.tif" ftype="tiff" compare="sim_size"/>
+        </test>
+    </tests>
+    <help>
+    **What it does**
+
+    Takes a labeled image and outputs a similar file where the labeled parts
+    of the image that touch (or overlap) are separated by at least 1 pixel (at most 2).
+    </help>
+    <citations>
+        <citation type="doi">10.1016/j.jbiotec.2017.07.019</citation>
+    </citations>
+</tool>
b
diff -r 000000000000 -r 597b7ef44b05 test-data/out.tif
b
Binary file test-data/out.tif has changed
b
diff -r 000000000000 -r 597b7ef44b05 test-data/sample_seg.tif
b
Binary file test-data/sample_seg.tif has changed