changeset 3:a041e4e9d449 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
author imgteam
date Mon, 12 May 2025 08:15:32 +0000
parents 938e2358eb80
children
files 2d_split_binaryimage_by_watershed.py binary2label.py binary2label.xml creators.xml test-data/galaxyIcon_noText.tif test-data/galaxyIcon_noText.tiff test-data/in.tiff test-data/label.tif test-data/label.tiff test-data/out.tiff test-data/uint8_z12_x11_y10-output.tiff test-data/uint8_z12_x11_y10.tiff
diffstat 12 files changed, 146 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/2d_split_binaryimage_by_watershed.py	Mon May 12 08:15:32 2025 +0000
@@ -0,0 +1,33 @@
+import argparse
+import sys
+
+import numpy as np
+import skimage.io
+import skimage.util
+from scipy import ndimage as ndi
+from skimage.feature import peak_local_max
+from skimage.segmentation import watershed
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description='Split binaryimage by watershed')
+    parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
+    parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)')
+    parser.add_argument('min_distance', type=int, default=100, help='Minimum distance to next object')
+    args = parser.parse_args()
+
+    img_in = skimage.io.imread(args.input_file.name)
+    distance = ndi.distance_transform_edt(img_in)
+
+    local_max_indices = peak_local_max(
+        distance,
+        min_distance=args.min_distance,
+        labels=img_in,
+    )
+    local_max_mask = np.zeros(img_in.shape, dtype=bool)
+    local_max_mask[tuple(local_max_indices.T)] = True
+    markers = ndi.label(local_max_mask)[0]
+    res = watershed(-distance, markers, mask=img_in)
+
+    res = skimage.util.img_as_uint(res)
+    skimage.io.imsave(args.out_file.name, res, plugin="tifffile")
--- a/binary2label.py	Wed Dec 18 09:56:41 2019 +0000
+++ b/binary2label.py	Mon May 12 08:15:32 2025 +0000
@@ -1,20 +1,27 @@
 import argparse
-import sys
-import skimage.io
-from skimage.measure import label
-import numpy as np
-import warnings
-from PIL import Image
-import skimage.util
 
+import giatools
+import scipy.ndimage as ndi
+import tifffile
+
+
+# Parse CLI parameters
 parser = argparse.ArgumentParser()
-parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
-parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') 
+parser.add_argument('input', type=str, help='input file')
+parser.add_argument('output', type=str, help='output file (TIFF)')
 args = parser.parse_args()
- 
-img_in = skimage.io.imread(args.input_file.name) > 0
-res = label(img_in)
-res = skimage.util.img_as_uint(res)
+
+# Read the input image with the original axes
+img = giatools.Image.read(args.input)
+img = img.normalize_axes_like(
+    img.original_axes,
+)
 
-res = Image.fromarray(res)
-res.save(args.out_file.name, "tiff")
+# Make sure the image is truly binary
+img_arr_bin = (img.data > 0)
+
+# Perform the labeling
+img.data = ndi.label(img_arr_bin)[0]
+
+# Write the result image (same axes as input image)
+tifffile.imwrite(args.output, img.data, metadata=dict(axes=img.axes))
--- a/binary2label.xml	Wed Dec 18 09:56:41 2019 +0000
+++ b/binary2label.xml	Mon May 12 08:15:32 2025 +0000
@@ -1,30 +1,83 @@
-<tool id="ip_binary_to_labelimage" name="Binary 2 Label" version="0.4"> 
-    <description>Converts Binary to Label Image</description>
+<tool id="ip_binary_to_labelimage" name="Convert binary image to label map" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@">
+    <description></description>
+    <macros>
+        <import>creators.xml</import>
+        <token name="@TOOL_VERSION@">0.6</token>
+        <token name="@VERSION_SUFFIX@">0</token>
+    </macros>
+    <creator>
+        <expand macro="creators/bmcv"/>
+    </creator>
+    <edam_operations>
+        <edam_operation>operation_3443</edam_operation>
+    </edam_operations>
+    <xrefs>
+        <xref type="bio.tools">galaxy_image_analysis</xref>
+    </xrefs>
     <requirements>
-        <requirement type="package" version="0.12.3">scikit-image</requirement> 
-        <requirement type="package" version="0.15.1">tifffile</requirement>
+        <requirement type="package" version="0.4.0">giatools</requirement>
+        <requirement type="package" version="1.12.0">scipy</requirement>
     </requirements>
     <command detect_errors="aggressive">
         <![CDATA[
-        python '$__tool_directory__/binary2label.py' '$input' '$output'
+        #if str($mode.mode_selector) == 'cca':
+            python '$__tool_directory__/binary2label.py' '$input' '$output'
+        #elif str($mode.mode_selector) == 'watershed':
+            python '$__tool_directory__/2d_split_binaryimage_by_watershed.py' '$input' '$output' $min_distance
+        #end if
         ]]>
     </command>
     <inputs>
-        <param name="input" type="data" format="tiff,png,jpg,bmp" label="Binary Image File"/>
+        <param name="input" type="data" format="tiff,png,jpg,bmp" label="Binary image"/>
+        <conditional name="mode">
+            <param name="mode_selector" type="select" label="Mode">
+                <option value="cca" selected="true">Connected component analysis</option>
+                <option value="watershed">Watershed transform</option>
+            </param>
+            <when value="cca">
+            </when>
+            <when value="watershed">
+                <param name="min_distance" type="integer" min="0" value="5" label="Minimum distance between two objects" />
+            </when>
+        </conditional>
     </inputs>
     <outputs>
         <data format="tiff" name="output"/>
     </outputs>
     <tests>
         <test>
-            <param name="input" value="galaxyIcon_noText.tif" />
-            <output name="output" value="label.tif" ftype="tiff" compare="sim_size"/>
+            <param name="input" value="galaxyIcon_noText.tiff" />
+            <conditional name="mode">
+                <param name="mode_selector" value="cca" />
+            </conditional>
+            <output name="output" value="label.tiff" ftype="tiff" compare="image_diff"/>
+        </test>
+        <test>
+            <param name="input" value="in.tiff"/>
+            <conditional name="mode">
+                <param name="mode_selector" value="watershed" />
+                <param name="min_distance" value="10" />
+            </conditional>
+            <output name="output" value="out.tiff" ftype="tiff" compare="image_diff"/>
+        </test>
+        <test>
+            <param name="input" value="uint8_z12_x11_y10.tiff"/>
+            <conditional name="mode">
+                <param name="mode_selector" value="cca" />
+            </conditional>
+            <output name="output" value="uint8_z12_x11_y10-output.tiff" ftype="tiff" compare="image_diff">
+                <assert_contents>
+                    <has_image_width width="11"/>
+                    <has_image_height height="10"/>
+                    <has_image_depth depth="12"/>
+                </assert_contents>
+            </output>
         </test>
     </tests>
     <help>
-    **What it does**
+    This tool assigns each object a unique label.
 
-    This tool assigns every object an own grey value.
+    Individual objects are determined using connected component analysis, or distance transform and watershed.
     </help>
     <citations>
         <citation type="doi">10.1016/j.jbiotec.2017.07.019</citation>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/creators.xml	Mon May 12 08:15:32 2025 +0000
@@ -0,0 +1,28 @@
+<macros>
+
+    <xml name="creators/bmcv">
+        <organization name="Biomedical Computer Vision Group, Heidelberg Universtiy" alternateName="BMCV" url="http://www.bioquant.uni-heidelberg.de/research/groups/biomedical_computer_vision.html" />
+        <yield />
+    </xml>
+
+    <xml name="creators/rmassei">
+        <person givenName="Riccardo" familyName="Massei"/>
+        <yield/>
+    </xml>
+
+    <xml name="creators/alliecreason">
+        <person givenName="Allison" familyName="Creason"/>
+        <yield/>
+    </xml>
+
+    <xml name="creators/bugraoezdemir">
+        <person givenName="Bugra" familyName="Oezdemir"/>
+        <yield/>
+    </xml>
+
+    <xml name="creators/thawn">
+        <person givenName="Till" familyName="Korten"/>
+        <yield/>
+    </xml>
+    
+</macros>
Binary file test-data/galaxyIcon_noText.tif has changed
Binary file test-data/galaxyIcon_noText.tiff has changed
Binary file test-data/in.tiff has changed
Binary file test-data/label.tif has changed
Binary file test-data/label.tiff has changed
Binary file test-data/out.tiff has changed
Binary file test-data/uint8_z12_x11_y10-output.tiff has changed
Binary file test-data/uint8_z12_x11_y10.tiff has changed