changeset 0:589af0005df5 draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
author imgteam
date Sat, 26 Feb 2022 15:16:10 +0000
parents
children bf590a9733ed
files overlay_images.py overlay_images.xml test-data/mask1.tif test-data/mask1r.tif test-data/sample1.tif test-data/test1.tif test-data/test2.tif test-data/test3.tif
diffstat 8 files changed, 183 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/overlay_images.py	Sat Feb 26 15:16:10 2022 +0000
@@ -0,0 +1,95 @@
+"""
+Copyright 2022 Biomedical Computer Vision Group, Heidelberg University.
+
+Distributed under the MIT license.
+See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+
+"""
+
+import argparse
+
+import matplotlib.pyplot as plt
+import numpy as np
+import skimage.color
+import skimage.io
+import skimage.measure
+import tifffile
+
+
+def read_im_gray(fn):
+    img = skimage.io.imread(fn)
+    nDims = len(img.shape)
+    assert nDims in [2, 3], 'this tool only supports single 2D images'
+    if nDims == 3 and img.shape[-1] in [3, 4]:
+        img = skimage.color.rgb2gray(img)
+    if len(img.shape) == 3:
+        return img[:, :, 0]
+    else:
+        return img
+
+
+def coloc_vis(in_red_fn, in_green_fn, out_fn):
+    im1 = read_im_gray(in_red_fn)
+    im2 = read_im_gray(in_green_fn)
+    assert im1.shape == im2.shape, 'Two images should have the same dimension'
+
+    vmin = np.min([np.min(im1), np.min(im2)])
+    scal = 255.0 / (np.max([np.max(im1), np.max(im2)]) - vmin)
+
+    out_im = np.zeros(im1.shape + (3,), dtype=np.ubyte)
+    out_im[:, :, 0] = (im1 - vmin) * scal
+    out_im[:, :, 1] = (im2 - vmin) * scal
+    skimage.io.imsave(out_fn, out_im)  # output is RGB
+
+
+def blending(im1_fn, im2_fn, out_fn, alpha=0.5):
+    im1 = skimage.io.imread(im1_fn)
+    im2 = skimage.io.imread(im2_fn)
+    assert im1.shape == im2.shape, 'Two images should have the same dimension'
+    out_im = (1 - alpha) * im1 + alpha * im2
+    if len(im1.shape) > 3:
+        tifffile.imwrite(out_fn, out_im.astype(im1.dtype), imagej=True)
+    else:
+        skimage.io.imsave(out_fn, out_im.astype(im1.dtype))  # format of output is the same as input
+
+
+def seg_contour(im1_fn, im2_fn, out_fn, linewidth=0.3, color='#ff0000', show_label=False):
+    img = skimage.io.imread(im1_fn)
+    label = skimage.io.imread(im2_fn)
+
+    fig = plt.figure()
+    ax = fig.add_axes([0, 0, 1, 1])
+    ax.axis('off')
+    if show_label:
+        for reg in skimage.measure.regionprops(label):
+            ax.text(reg.centroid[1], reg.centroid[0], str(reg.label), color=color)
+
+    if len(img.shape) == 2:
+        plt.imshow(img, cmap=plt.cm.gray)
+    else:
+        plt.imshow(img)
+    plt.contour(label, linewidths=linewidth, colors=color)
+    fig.canvas.print_png(out_fn)  # output is RGB
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description="Overlay two images")
+    parser.add_argument("im1", help="The first image")
+    parser.add_argument("im2", help="The second image")
+    parser.add_argument("out", help="Output image")
+    parser.add_argument('--method', dest='method', default='coloc_vis', help='How to overlay images')
+    parser.add_argument('--alpha', dest='alpha', default=0.5, type=float, help='Blending weight')
+    parser.add_argument('--thickness', dest='thickness', default=0.3, type=float, help='Contour thickness')
+    parser.add_argument('--color', dest='color', default='#FFFF00', help='Contour color')
+    parser.add_argument('--show_label', dest='show_label', action='store_true', help='Plot label')
+    args = parser.parse_args()
+
+    if args.method == 'coloc_vis':
+        coloc_vis(args.im1, args.im2, args.out)
+    elif args.method == 'blending':
+        blending(args.im1, args.im2, args.out, alpha=args.alpha)
+    elif args.method == 'seg_contour':
+        seg_contour(args.im1, args.im2, args.out,
+                    linewidth=args.thickness,
+                    color=args.color,
+                    show_label=args.show_label)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/overlay_images.xml	Sat Feb 26 15:16:10 2022 +0000
@@ -0,0 +1,88 @@
+<tool id="ip_overlay_images" name="Overlay Images" version="0.0.1" profile="20.05"> 
+    <description>for visualization</description>
+    <requirements>
+        <requirement type="package" version="0.18.1">scikit-image</requirement> 
+        <requirement type="package" version="3.3.4">matplotlib</requirement>
+        <requirement type="package" version="2020.10.1">tifffile</requirement>
+        <requirement type="package" version="1.20.2">numpy</requirement>
+    </requirements>
+    <command detect_errors="aggressive">
+    <![CDATA[
+        python '$__tool_directory__/overlay_images.py'
+        '$im1' '$im2' ./output.tif
+        --method '$method_option.method'
+        #if '$method_option.method' == 'blending':
+            --alpha '$alpha'
+        #elif '$method_option.method' == 'seg_contour':
+            --thickness '$thickness'
+            --color '$color'
+            $show_label
+        #end if
+    ]]>
+    </command>
+    <inputs>
+        <conditional name="method_option">
+            <param name="method" type="select" label="How to visualize the overlay?">
+                <option value="coloc_vis" selected="True">Colocalization</option>
+                <option value="blending">Blending</option>
+                <option value="seg_contour">Segmentation mask over image</option>
+            </param>
+            <when value="coloc_vis">
+                <param name="im1" type="data" format="tiff,png" label="Image #1 (red channel)" />
+                <param name="im2" type="data" format="tiff,png" label="Image #2 (green channel)" />
+            </when>
+            <when value="blending">
+                <param name="im1" type="data" format="tiff,png" label="Image #1" />
+                <param name="im2" type="data" format="tiff,png" label="Image #2" />
+                <param name="alpha" type="float" value="0.5" min="0.0" max="1.0" label="The weight for blending: I_out = (1 - w) * I_1 + w * I_2."/>
+            </when>
+            <when value="seg_contour">
+                <param name="im1" type="data" format="tiff,png" label="Image" />
+                <param name="im2" type="data" format="tiff,png" label="Label image" />
+                <param name="thickness" type="float" value="0.3" label="Contour thickness" />
+                <param name="color" type="color" value="#ff0000" label="Contour color"/>
+                <param argument="--show_label" type="boolean" checked='false' truevalue="--show_label" falsevalue="" label="Show labels" />
+            </when>
+        </conditional>
+    </inputs>
+    <outputs>
+        <data format="tiff" name="out" from_work_dir="output.tif" />
+    </outputs>
+    <tests>
+        <test>
+            <param name="im1" value="mask1.tif"/>
+            <param name="im2" value="mask1r.tif"/>
+            <conditional name="method_option">
+                <param name="method" value="coloc_vis"/>
+            </conditional>
+            <output name="out" value="test1.tif" ftype="tiff" compare="sim_size" delta_frac="0.1"/>
+        </test>
+        <test>
+            <param name="im1" value="mask1.tif"/>
+            <param name="im2" value="mask1r.tif"/>
+            <conditional name="method_option">
+                <param name="method" value="blending"/>
+            </conditional>
+            <param name="alpha" value="0.3"/>
+            <output name="out" value="test2.tif" ftype="tiff" compare="sim_size" delta_frac="0.1"/>
+        </test>
+        <test>
+            <param name="im1" value="sample1.tif"/>
+            <param name="im2" value="mask1.tif"/>
+            <conditional name="method_option">
+                <param name="method" value="seg_contour"/>
+            </conditional>
+            <param name="thickness" value="0.4"/>
+            <param name="color" value="#ffaa00"/>
+            <param name="show_label" value="true"/>
+            <output name="out" value="test3.tif" ftype="tiff" compare="sim_size" delta="20000" delta_frac="0.2"/>
+        </test>
+    </tests>
+    <help>
+    **What it does**
+
+    This tool overlays two image to visualize 1) image blending, 2) colocalization, or 3) a segmentation mask over an image.
+    </help>
+    <citations>
+    </citations>
+</tool>
Binary file test-data/mask1.tif has changed
Binary file test-data/mask1r.tif has changed
Binary file test-data/sample1.tif has changed
Binary file test-data/test1.tif has changed
Binary file test-data/test2.tif has changed
Binary file test-data/test3.tif has changed