# HG changeset patch # User imgteam # Date 1721076908 0 # Node ID e2c6bedc6b734e1216dee23bc5bcb82845226c81 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3 diff -r 000000000000 -r e2c6bedc6b73 background_removal.py --- /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() diff -r 000000000000 -r e2c6bedc6b73 background_removal.xml --- /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 @@ + + with scikit-image + + creators.xml + tests.xml + 0.24.0 + 0 + + + + + + scikit-image + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + This tool applies different background removal algorithms to an image: + + - Rolling-ball algorithm + + - Difference of Gaussians + + - Top-hat filter + + + 10.1109/MC.1983.1654163 + + diff -r 000000000000 -r e2c6bedc6b73 creators.xml --- /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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 000000000000 -r e2c6bedc6b73 test-data/input1_output_dog.tif Binary file test-data/input1_output_dog.tif has changed diff -r 000000000000 -r e2c6bedc6b73 test-data/input1_uint8.tif Binary file test-data/input1_uint8.tif has changed diff -r 000000000000 -r e2c6bedc6b73 test-data/input2_output_dog.tif Binary file test-data/input2_output_dog.tif has changed diff -r 000000000000 -r e2c6bedc6b73 test-data/input2_output_rb.tif Binary file test-data/input2_output_rb.tif has changed diff -r 000000000000 -r e2c6bedc6b73 test-data/input2_uint16.tif Binary file test-data/input2_uint16.tif has changed diff -r 000000000000 -r e2c6bedc6b73 test-data/input3_output_tophat.tif Binary file test-data/input3_output_tophat.tif has changed diff -r 000000000000 -r e2c6bedc6b73 test-data/input3_uint8.tif Binary file test-data/input3_uint8.tif has changed diff -r 000000000000 -r e2c6bedc6b73 tests.xml --- /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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +