Mercurial > repos > imgteam > background_removal
changeset 0:e2c6bedc6b73 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
author | imgteam |
---|---|
date | Mon, 15 Jul 2024 20:55:08 +0000 |
parents | |
children | 6e9e98a7be4e |
files | background_removal.py background_removal.xml creators.xml test-data/input1_output_dog.tif test-data/input1_uint8.tif test-data/input2_output_dog.tif test-data/input2_output_rb.tif test-data/input2_uint16.tif test-data/input3_output_tophat.tif test-data/input3_uint8.tif tests.xml |
diffstat | 11 files changed, 254 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/background_removal.py Mon Jul 15 20:55:08 2024 +0000 @@ -0,0 +1,59 @@ +import argparse +import warnings + +import numpy as np +import skimage.io +from skimage.filters import difference_of_gaussians +from skimage.io import imread +from skimage.morphology import disk, white_tophat +from skimage.restoration import rolling_ball + + +def process_image(args): + image = imread(args.input_image) + + if args.filter == "rolling_ball": + background_rolling = rolling_ball(image, radius=args.radius) + output_image = image - background_rolling + + elif args.filter == "dog": + output_image = difference_of_gaussians(image, low_sigma=0, high_sigma=args.radius) + + elif args.filter == "top_hat": + output_image = white_tophat(image, disk(args.radius)) + + with warnings.catch_warnings(): + output_image = convert_image_to_format_of(output_image, image) + skimage.io.imsave(args.output, output_image, plugin="tifffile") + + +def convert_image_to_format_of(image, format_image): + """ + Convert the first image to the format of the second image. + """ + if format_image.dtype == image.dtype: + return image + elif format_image.dtype == np.uint8: + return skimage.util.img_as_ubyte(image) + elif format_image.dtype == np.uint16: + return skimage.util.img_as_uint(image) + elif format_image.dtype == np.int16: + return skimage.util.img_as_int(image) + else: + raise ValueError(f'Unsupported image data type: {format_image.dtype}') + + +def main(): + parser = argparse.ArgumentParser(description="Background removal script using skiimage") + parser.add_argument('input_image', help="Input image path") + parser.add_argument('filter', choices=['rolling_ball', 'dog', 'top_hat'], + help="Background removal algorithm") + parser.add_argument('radius', type=float, help="Radius") + parser.add_argument('output', help="Output image path") + + args = parser.parse_args() + process_image(args) + + +if __name__ == '__main__': + main()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/background_removal.xml Mon Jul 15 20:55:08 2024 +0000 @@ -0,0 +1,72 @@ +<tool id="background_removal" name="Remove image background" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="20.05"> + <description>with scikit-image</description> + <macros> + <import>creators.xml</import> + <import>tests.xml</import> + <token name="@TOOL_VERSION@">0.24.0</token> + <token name="@VERSION_SUFFIX@">0</token> + </macros> + <creator> + <expand macro="creators/rmassei"/> + </creator> + <requirements> + <requirement type="package" version="0.24.0">scikit-image</requirement> + </requirements> + <command detect_errors="aggressive"> + <![CDATA[ + python "$__tool_directory__/background_removal.py" $input_image $filter $radius $output + ]]> + </command> + <inputs> + <param name="input_image" type="data" format="tiff, jpg, jpeg, png, tif" label="Input Image"/> + <param name="filter" type="select" label="Select Filter"> + <option value="rolling_ball">Rolling-ball algorithm</option> + <option value="dog">Difference of Gaussians</option> + <option value="top_hat">Top-hat filter</option> + </param> + <param name="radius" type="float" label="Radius" value="20"/> + </inputs> + <outputs> + <data name="output" format="tiff" label="Background substraction output"/> + </outputs> + <tests> + <!-- uint8 tests --> + <test> + <param name="input_image" value="input1_uint8.tif"/> + <param name="filter" value="dog"/> + <param name="radius" value="20"/> + <expand macro="tests/intensity_image_diff" name="output" value="input1_output_dog.tif" ftype="tiff"/> + </test> + <test> + <param name="input_image" value="input3_uint8.tif"/> + <param name="filter" value="top_hat"/> + <param name="radius" value="15"/> + <expand macro="tests/intensity_image_diff" name="output" value="input3_output_tophat.tif" ftype="tiff"/> + </test> + <!-- uint16 tests --> + <test> + <param name="input_image" value="input2_uint16.tif"/> + <param name="filter" value="rolling_ball"/> + <param name="radius" value="20"/> + <expand macro="tests/intensity_image_diff" name="output" value="input2_output_rb.tif" ftype="tiff"/> + </test> + <test> + <param name="input_image" value="input2_uint16.tif"/> + <param name="filter" value="dog"/> + <param name="radius" value="20"/> + <expand macro="tests/intensity_image_diff" name="output" value="input2_output_dog.tif" ftype="tiff"/> + </test> + </tests> + <help> + This tool applies different background removal algorithms to an image: + + - Rolling-ball algorithm + + - Difference of Gaussians + + - Top-hat filter + </help> + <citations> + <citation type="doi">10.1109/MC.1983.1654163</citation> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/creators.xml Mon Jul 15 20:55:08 2024 +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>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests.xml Mon Jul 15 20:55:08 2024 +0000 @@ -0,0 +1,95 @@ +<macros> + + <!-- Macros for verification of image outputs --> + + <xml + name="tests/binary_image_diff" + tokens="name,value,ftype,metric,eps" + token_metric="mae" + token_eps="0.01"> + + <output name="@NAME@" value="@VALUE@" ftype="@FTYPE@" compare="image_diff" metric="@METRIC@" eps="@EPS@" pin_labels="0"> + <assert_contents> + <has_image_n_labels n="2"/> + <yield/> + </assert_contents> + </output> + + </xml> + + <xml + name="tests/label_image_diff" + tokens="name,value,ftype,metric,eps,pin_labels" + token_metric="iou" + token_eps="0.01" + token_pin_labels="0"> + + <output name="@NAME@" value="@VALUE@" ftype="@FTYPE@" compare="image_diff" metric="@METRIC@" eps="@EPS@" pin_labels="@PIN_LABELS@"> + <assert_contents> + <yield/> + </assert_contents> + </output> + + </xml> + + <xml + name="tests/intensity_image_diff" + tokens="name,value,ftype,metric,eps" + token_metric="rms" + token_eps="0.01"> + + <output name="@NAME@" value="@VALUE@" ftype="@FTYPE@" compare="image_diff" metric="@METRIC@" eps="@EPS@"> + <assert_contents> + <yield/> + </assert_contents> + </output> + + </xml> + + <!-- Variants of the above for verification of collection elements --> + + <xml + name="tests/binary_image_diff/element" + tokens="name,value,ftype,metric,eps" + token_metric="mae" + token_eps="0.01"> + + <element name="@NAME@" value="@VALUE@" ftype="@FTYPE@" compare="image_diff" metric="@METRIC@" eps="@EPS@" pin_labels="0"> + <assert_contents> + <has_image_n_labels n="2"/> + <yield/> + </assert_contents> + </element> + + </xml> + + <xml + name="tests/label_image_diff/element" + tokens="name,value,ftype,metric,eps" + token_metric="iou" + token_eps="0.01" + token_pin_labels="0"> + + <element name="@NAME@" value="@VALUE@" ftype="@FTYPE@" compare="image_diff" metric="@METRIC@" eps="@EPS@" pin_labels="@PIN_LABELS@"> + <assert_contents> + <yield/> + </assert_contents> + </element> + + </xml> + + <xml + name="tests/intensity_image_diff/element" + tokens="name,value,ftype,metric,eps" + token_metric="rms" + token_eps="0.01"> + + <element name="@NAME@" value="@VALUE@" ftype="@FTYPE@" compare="image_diff" metric="@METRIC@" eps="@EPS@"> + <assert_contents> + <yield/> + </assert_contents> + </element> + + </xml> + +</macros>