Mercurial > repos > imgteam > overlay_images
changeset 9:66fe62b96281 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
| author | imgteam |
|---|---|
| date | Fri, 28 Nov 2025 08:41:02 +0000 |
| parents | ca362a9bfa20 |
| children | |
| files | creators.xml overlay_images.py overlay_images.xml test-data/mask1.tif test-data/mask1.tiff test-data/mask1r.tif test-data/mask1r.tiff test-data/mask2.tif test-data/mask2.tiff test-data/mask3.tiff test-data/sample1_uint16.tif test-data/sample1_uint16.tiff test-data/sample1_uint8.tif test-data/sample1_uint8.tiff test-data/sample2.tiff test-data/sample3.tiff test-data/test1.tif test-data/test1.tiff test-data/test10.png test-data/test2.tif test-data/test2.tiff test-data/test6.png test-data/test7.png test-data/test8.png test-data/test9.png |
| diffstat | 25 files changed, 223 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/creators.xml Mon Sep 23 10:26:26 2024 +0000 +++ b/creators.xml Fri Nov 28 08:41:02 2025 +0000 @@ -5,6 +5,11 @@ <yield /> </xml> + <xml name="creators/kostrykin"> + <person givenName="Leonid" familyName="Kostrykin"/> + <yield/> + </xml> + <xml name="creators/rmassei"> <person givenName="Riccardo" familyName="Massei"/> <yield/> @@ -24,5 +29,15 @@ <person givenName="Till" familyName="Korten"/> <yield/> </xml> - + + <xml name="creators/pavanvidem"> + <person givenName="Pavan" familyName="Videm"/> + <yield/> + </xml> + + <xml name="creators/tuncK"> + <person givenName="Tunc" familyName="Kayikcioglu"/> + <yield/> + </xml> + </macros>
--- a/overlay_images.py Mon Sep 23 10:26:26 2024 +0000 +++ b/overlay_images.py Fri Nov 28 08:41:02 2025 +0000 @@ -30,15 +30,42 @@ return img -def get_rgb8_copy(img): +def get_rgb8_copy(img, fp_lower, fp_upper): img = np.squeeze(img) assert img.ndim == 2 or (img.ndim == 3 and img.shape[-1] in (3, 4)) + assert fp_lower == 'min' or abs(float(fp_lower)) < np.inf # 'min' or number + assert fp_upper == 'max' or abs(float(fp_upper)) < np.inf # 'max' or number + + # Convert from floating point if str(img.dtype).startswith('float'): + a = img.min() if fp_lower == 'min' else float(fp_lower) + b = img.max() if fp_upper == 'max' else float(fp_upper) + + if a > b: + raise ValueError( + f'Lower bound ({a:g}) must be less than upper bound ({b:g}).' + ) + if a == b: + raise ValueError( + 'Floating point conversion is undefined' + ' because lower and upper bounds are identical.' + ) + + # Perform linear mapping to [0, 1] + img = img.clip(a, b) + img = (img - a) / (b - a) + + # Convert to uint8 img = np.round(img * 255).astype(np.uint8) + + # Convert from uint16 elif img.dtype == np.uint16: img = (img // 256).astype(np.uint8) + + # Other dtypes than float, uint8, uint16 are not supported elif img.dtype != np.uint8: raise ValueError(f'unknown dtype: {img.dtype}') + if img.ndim == 2: result = np.dstack([img] * 3).copy() else: @@ -72,11 +99,11 @@ 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, color='#ff0000', show_label=False, label_color='#ffff00'): +def seg_contour(im1_fn, im2_fn, out_fn, fp_lower, fp_upper, linewidth, color='#ff0000', show_label=False, label_color='#ffff00'): img = giatools.io.imread(im1_fn) labels = giatools.io.imread(im2_fn) - result = get_rgb8_copy(img) + result = get_rgb8_copy(img, fp_lower, fp_upper) cp = ContourPaint(labels, linewidth, where='center') color_rgb = np.multiply(255, matplotlib.colors.to_rgb(color)) @@ -106,6 +133,8 @@ 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('--fp_lower', default='0', type=str, help='Lower bound for floating point conversion') + parser.add_argument('--fp_upper', default='1', type=str, help='Upper bound for floating point conversion') parser.add_argument('--alpha', dest='alpha', default=0.5, type=float, help='Blending weight') parser.add_argument('--thickness', dest='thickness', default=2, type=int, help='Contour thickness') parser.add_argument('--color', dest='color', default='#FF0000', help='Contour color') @@ -118,8 +147,14 @@ 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, - label_color=args.label_color) + seg_contour( + args.im1, + args.im2, + args.out, + fp_lower=args.fp_lower, + fp_upper=args.fp_upper, + linewidth=args.thickness, + color=args.color, + show_label=args.show_label, + label_color=args.label_color, + )
--- a/overlay_images.xml Mon Sep 23 10:26:26 2024 +0000 +++ b/overlay_images.xml Fri Nov 28 08:41:02 2025 +0000 @@ -1,13 +1,13 @@ -<tool id="ip_overlay_images" name="Overlay images" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="20.05"> +<tool id="ip_overlay_images" name="Overlay images" version="@TOOL_VERSION@" profile="20.05"> <description></description> <macros> <import>creators.xml</import> <import>tests.xml</import> - <token name="@TOOL_VERSION@">0.0.4</token> - <token name="@VERSION_SUFFIX@">4</token> + <token name="@TOOL_VERSION@">0.0.5</token> </macros> <creator> <expand macro="creators/bmcv"/> + <expand macro="creators/kostrykin"/> <expand macro="creators/thawn"/> </creator> <edam_operations> @@ -17,7 +17,7 @@ <xref type="bio.tools">galaxy_image_analysis</xref> </xrefs> <requirements> - <requirement type="package" version="0.18.1">scikit-image</requirement> + <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.21">numpy</requirement> @@ -31,12 +31,14 @@ #if $method_option.method == "seg_contour" ./output.png #else - ./output.tif + ./output.tiff #end if --method $method_option.method #if $method_option.method == "blending" --alpha $method_option.alpha #elif $method_option.method == "seg_contour" + --fp_lower '${method_option.fp_conversion.lower}' + --fp_upper '${method_option.fp_conversion.upper}' --thickness $method_option.thickness --color '$method_option.color' $method_option.show_label @@ -46,32 +48,50 @@ </command> <inputs> <conditional name="method_option"> + <param name="method" type="select" label="Type of the overlay"> <option value="coloc_vis" selected="True">Colocalization</option> <option value="blending">Linear blending</option> <option value="seg_contour">Segmentation contours 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: result = (1 - weight) * image1 + weight * image2." /> </when> + <when value="seg_contour"> <param name="im1" type="data" format="tiff,png" label="Intensity image" /> + + <section name="fp_conversion" title="Intensity image floating point conversion" expanded="false" help="This is only used if the values of the intensity image have a floating point format. Overlay images are created as PNG files, that do not support floating point values (but only integer values between 0 and 255). Conversion of floating point intensity values to integer values via linear mapping is generally ambiguous and can be adapted here."> + <param name="lower" type="select" label="Lower bound conversion" help="Choose which floating point intensity value will be mapped to 0 (i.e. black if grayscale)."> + <option value="0" selected="True">0</option> + <option value="-1">-1</option> + <option value="min">Minimum value of the image</option> + </param> + <param name="upper" type="select" label="Upper bound conversion" help="Choose which floating point intensity value will be mapped to 255 (i.e. white if grayscale)."> + <option value="1" selected="True">1</option> + <option value="max">Maximum value of the image</option> + </param> + </section> + <param name="im2" type="data" format="tiff,png" label="Label map" /> <param name="thickness" type="integer" value="2" min="1" label="Contour thickness (in pixels)" /> <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" /> <param name="label_color" type="color" value="#ffff00" label="Label color"/> </when> + </conditional> </inputs> <outputs> - <data format="tiff" name="out_tiff" from_work_dir="output.tif" label="${tool.name} on ${on_string} output:tiff"> + <data format="tiff" name="out_tiff" from_work_dir="output.tiff" label="${tool.name} on ${on_string} output:tiff"> <filter>method_option['method'] != 'seg_contour'</filter> </data> <data format="png" name="out_png" from_work_dir="output.png" label="${tool.name} on ${on_string} output:png"> @@ -80,51 +100,159 @@ </outputs> <tests> <test expect_num_outputs="1"> - <param name="im1" value="mask1.tif"/> - <param name="im2" value="mask1r.tif"/> - <param name="method" value="coloc_vis"/> - <expand macro="tests/intensity_image_diff" name="out_tiff" value="test1.tif" ftype="tiff"/> + <conditional name="method_option"> + <param name="method" value="coloc_vis"/> + <param name="im1" value="mask1.tiff"/> + <param name="im2" value="mask1r.tiff"/> + </conditional> + <expand macro="tests/intensity_image_diff" name="out_tiff" value="test1.tiff" ftype="tiff"/> </test> <test expect_num_outputs="1"> - <param name="im1" value="mask1.tif"/> - <param name="im2" value="mask1r.tif"/> - <param name="method" value="blending"/> - <param name="alpha" value="0.3"/> - <expand macro="tests/intensity_image_diff" name="out_tiff" value="test2.tif" ftype="tiff"/> + <conditional name="method_option"> + <param name="method" value="blending"/> + <param name="im1" value="mask1.tiff"/> + <param name="im2" value="mask1r.tiff"/> + <param name="alpha" value="0.3"/> + </conditional> + <expand macro="tests/intensity_image_diff" name="out_tiff" value="test2.tiff" ftype="tiff"/> </test> <test expect_num_outputs="1"> - <param name="im1" value="sample1_uint8.tif"/> - <param name="im2" value="mask1.tif"/> - <param name="method" value="seg_contour"/> - <param name="thickness" value="2"/> - <param name="color" value="#ff0000"/> - <param name="show_label" value="--show_label"/> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample1_uint8.tiff"/> + <param name="im2" value="mask1.tiff"/> + <param name="thickness" value="2"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + <param name="show_label" value="true"/> + </conditional> <expand macro="tests/intensity_image_diff" name="out_png" value="test3.png" ftype="png"/> </test> <test expect_num_outputs="1"><!-- segmentation overlay, 8bit test --> - <param name="im1" value="sample1_uint8.tif"/> - <param name="im2" value="mask2.tif"/> - <param name="method" value="seg_contour"/> - <param name="thickness" value="2"/> - <param name="color" value="#ff0000"/> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample1_uint8.tiff"/> + <param name="im2" value="mask2.tiff"/> + <param name="thickness" value="2"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + </conditional> <expand macro="tests/intensity_image_diff" name="out_png" value="test4.png" ftype="png"/> </test> <test expect_num_outputs="1"><!-- segmentation overlay, 16bit test --> - <param name="im1" value="sample1_uint16.tif"/> - <param name="im2" value="mask2.tif"/> - <param name="method" value="seg_contour"/> - <param name="thickness" value="2"/> - <param name="color" value="#ff0000"/> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample1_uint16.tiff"/> + <param name="im2" value="mask2.tiff"/> + <param name="thickness" value="2"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + </conditional> <expand macro="tests/intensity_image_diff" name="out_png" value="test4.png" ftype="png" eps="1"/> </test> <test expect_num_outputs="1"><!-- segmentation overlay, 8bit RGB test --> - <param name="im1" value="sample1_uint8_rgb.png"/> - <param name="im2" value="mask2.tif"/> - <param name="method" value="seg_contour"/> - <param name="thickness" value="2"/> - <param name="color" value="#ff0000"/> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample1_uint8_rgb.png"/> + <param name="im2" value="mask2.tiff"/> + <param name="thickness" value="2"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + </conditional> <expand macro="tests/intensity_image_diff" name="out_png" value="test5.png" ftype="png"/> </test> + <test expect_num_outputs="1"><!-- segmentation overlay, floating point conversion test 0/1 --> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample2.tiff"/> + <param name="im2" value="mask3.tiff"/> + <param name="thickness" value="1"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + <section name="fp_conversion"> + <param name="lower" value="0"/> + <param name="upper" value="1"/> + </section> + </conditional> + <expand macro="tests/intensity_image_diff" name="out_png" value="test6.png" ftype="png"/> + </test> + <test expect_num_outputs="1"><!-- segmentation overlay, floating point conversion test -1/1 --> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample2.tiff"/> + <param name="im2" value="mask3.tiff"/> + <param name="thickness" value="1"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + <section name="fp_conversion"> + <param name="lower" value="-1"/> + <param name="upper" value="1"/> + </section> + </conditional> + <expand macro="tests/intensity_image_diff" name="out_png" value="test7.png" ftype="png"/> + </test> + <test expect_num_outputs="1"><!-- segmentation overlay, floating point conversion test min/1 --> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample2.tiff"/> + <param name="im2" value="mask3.tiff"/> + <param name="thickness" value="1"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + <section name="fp_conversion"> + <param name="lower" value="min"/> + <param name="upper" value="1"/> + </section> + </conditional> + <expand macro="tests/intensity_image_diff" name="out_png" value="test8.png" ftype="png"/> + </test> + <test expect_num_outputs="1"><!-- segmentation overlay, floating point conversion test 0/max --> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample2.tiff"/> + <param name="im2" value="mask3.tiff"/> + <param name="thickness" value="1"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + <section name="fp_conversion"> + <param name="lower" value="0"/> + <param name="upper" value="max"/> + </section> + </conditional> + <expand macro="tests/intensity_image_diff" name="out_png" value="test9.png" ftype="png"/> + </test> + <test expect_num_outputs="1"><!-- segmentation overlay, floating point conversion test min/max --> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample2.tiff"/> + <param name="im2" value="mask3.tiff"/> + <param name="thickness" value="1"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + <section name="fp_conversion"> + <param name="lower" value="min"/> + <param name="upper" value="max"/> + </section> + </conditional> + <expand macro="tests/intensity_image_diff" name="out_png" value="test10.png" ftype="png"/> + </test> + <test expect_failure="true"><!-- segmentation overlay, floating point conversion test with illegal settings --> + <conditional name="method_option"> + <param name="method" value="seg_contour"/> + <param name="im1" value="sample3.tiff"/> + <param name="im2" value="mask3.tiff"/> + <param name="thickness" value="1"/> + <param name="color" value="#ff0000"/> + <param name="label_color" value="#ffff00"/> + <section name="fp_conversion"> + <param name="lower" value="0"/> + <param name="upper" value="max"/> + </section> + </conditional> + <assert_stderr> + <has_text text="Lower bound (0) must be less than upper bound (-10)." /> + </assert_stderr> + </test> </tests> <help><