changeset 0:165a9330fc90 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_moving_and_fixed_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
author imgteam
date Sat, 09 Feb 2019 14:39:40 -0500
parents
children bc324ec66719
files overlay_moving_and_fixed_image.py overlay_moving_and_fixed_image.xml test-data/out.png test-data/sample1.png test-data/sample2.png test-data/warp_matrix.tsv
diffstat 6 files changed, 124 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/overlay_moving_and_fixed_image.py	Sat Feb 09 14:39:40 2019 -0500
@@ -0,0 +1,73 @@
+import argparse
+from PIL import Image
+import skimage.io
+import skimage.color
+from skimage.transform import ProjectiveTransform
+from scipy.ndimage import map_coordinates
+import numpy as np
+import pandas as pd
+
+
+def _stackcopy(a, b):
+    if a.ndim == 3:
+        a[:] = b[:, :, np.newaxis]
+    else:
+        a[:] = b
+
+
+def warp_coords_batch(coord_map, shape, dtype=np.float64, batch_size=1000000):
+    rows, cols = shape[0], shape[1]
+    coords_shape = [len(shape), rows, cols]
+    if len(shape) == 3:
+        coords_shape.append(shape[2])
+    coords = np.empty(coords_shape, dtype=dtype)
+
+    tf_coords = np.indices((cols, rows), dtype=dtype).reshape(2, -1).T
+
+    for i in range(0, (tf_coords.shape[0]//batch_size+1)):
+        tf_coords[batch_size*i:batch_size*(i+1)] = coord_map(tf_coords[batch_size*i:batch_size*(i+1)])
+    tf_coords = tf_coords.T.reshape((-1, cols, rows)).swapaxes(1, 2)
+
+    _stackcopy(coords[1, ...], tf_coords[0, ...])
+    _stackcopy(coords[0, ...], tf_coords[1, ...])
+    if len(shape) == 3:
+        coords[2, ...] = range(shape[2])
+
+    return coords
+
+
+def transform(moving_image, fixed_image, warp_matrix):
+    trans = ProjectiveTransform(matrix=warp_matrix)
+    warped_coords = warp_coords_batch(trans, fixed_image.shape)
+    return map_coordinates(moving_image, warped_coords)
+
+
+def overlay(moving_image, fixed_image, factor, overlay_out_path):
+    moving_image = Image.fromarray(moving_image).convert("RGBA")
+    fixed_image = Image.fromarray(fixed_image).convert("RGBA")
+    overlay_out = Image.blend(moving_image, fixed_image, factor)
+    overlay_out.save(overlay_out_path, "PNG")
+
+
+if __name__=="__main__":
+    parser = argparse.ArgumentParser(description = "Overlay two images")
+    parser.add_argument("fixed_image", help = "Path to fixed image")
+    parser.add_argument("moving_image", help = "Path to moving image")
+    parser.add_argument("warp_matrix", help="Paste path to warp_matrix.csv that should be used for transformation")
+    parser.add_argument("--inverse_transform", dest='inverse_transform', action='store_true', help="Set if inverse transform should be visualized")
+    parser.add_argument("--factor", dest = "factor", help = "Enter the factor by which images should be blended, 1.0 returns a copy of second image", type = float, default = 0.5)
+    parser.add_argument("overlay_out", help = "Overlay output path")
+    args = parser.parse_args()
+
+    fixed_image = skimage.io.imread(args.fixed_image)
+    moving_image = skimage.io.imread(args.moving_image)
+
+    warp_matrix = pd.read_csv(args.warp_matrix, delimiter="\t", header=None)
+    warp_matrix = np.array(warp_matrix)
+    if args.inverse_transform:
+        fixed_image = transform(fixed_image, moving_image, warp_matrix)
+    else:
+        warp_matrix = np.linalg.inv(warp_matrix)
+        moving_image = transform(moving_image, fixed_image, warp_matrix)
+
+    overlay(moving_image, fixed_image, args.factor, args.overlay_out)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/overlay_moving_and_fixed_image.xml	Sat Feb 09 14:39:40 2019 -0500
@@ -0,0 +1,48 @@
+<tool id="ip_viz_overlay_moving_and_fixed_image" name="Overlay" version="0.0.1">
+    <description>moving and fixed image</description>
+    <requirements>
+        <requirement type="package" version="0.14.2">scikit-image</requirement>
+        <requirement type="package" version="0.23.4">pandas</requirement>
+        <requirement type="package" version="1.15.4">numpy</requirement>
+        <requirement type="package" version="1.1.0">scipy</requirement>
+        <requirement type="package" version="5.3.0">pillow</requirement>
+    </requirements>
+    <command><![CDATA[
+         python '$__tool_directory__/overlay_moving_and_fixed_image.py'
+             '$fixed_image'
+             '$moving_image'
+             '$warp_matrix'
+             '$overlay_out'
+             --factor $factor
+             $inverse_transform
+    ]]></command>
+    <inputs>
+        <param name="fixed_image" type= "data" format="png" label="Fixed image" />
+        <param name="moving_image" type= "data" format="png" label="Moving image" />
+        <param name="warp_matrix" type= "data" format="tabular" label="Warp Matrix" />
+        <param name="factor" type="float" value="0.5"
+            label="Factor by which images are blended. 1.0 returns a copy of the fixed image, 0.0 returns a copy of the moving image."/>
+        <param name="inverse_transform" type="boolean" checked="false" truevalue="--inverse_transform" falsevalue=""
+            label="Set to Yes if inverse transformation should be displayed" />
+    </inputs>
+    <outputs>
+       <data format="png" name="overlay_out" />
+    </outputs>
+    <tests>
+      <test>
+        <param name="fixed_image" value="sample1.png" />
+        <param name="moving_image" value="sample2.png" />
+        <param name="warp_matrix" value="warp_matrix.tsv" />
+        <param name="factor" value="0.75" />
+        <output name="overlay_out" value="out.png" ftype="png" compare="sim_size" />
+      </test>
+    </tests>
+    <help>
+    **What it does**
+
+    This tool performs an overlay of two images of which one was transformed to match the other.
+    </help>
+    <citations>
+        <citation type="doi">10.1016/j.jbiotec.2017.07.019</citation>
+    </citations>
+</tool>
Binary file test-data/out.png has changed
Binary file test-data/sample1.png has changed
Binary file test-data/sample2.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/warp_matrix.tsv	Sat Feb 09 14:39:40 2019 -0500
@@ -0,0 +1,3 @@
+1	0	10
+0	1	20
+0	0	1