diff --git a/macros/creators.xml b/macros/creators.xml index 885c0711..9caf2a38 100644 --- a/macros/creators.xml +++ b/macros/creators.xml @@ -5,6 +5,11 @@ + + + + + diff --git a/tools/background_removal/.shed.yml b/tools/background_removal/.shed.yml new file mode 100644 index 00000000..845860ca --- /dev/null +++ b/tools/background_removal/.shed.yml @@ -0,0 +1,8 @@ +categories: + - Imaging +description: Background removal filters using scikit-image +long_description: Tool to perform a background removal using 1) Rolling-Ball Algorithm, 2) Difference of Gaussians and 3) Top-Hat Filter +name: background_removal +owner: ufz +homepage_url: https://github.com/bmcv +remote_repository_url: https://github.com/BMCV/galaxy-image-analysis/tools/background_removal diff --git a/tools/background_removal/background_removal.py b/tools/background_removal/background_removal.py new file mode 100644 index 00000000..109825cb --- /dev/null +++ b/tools/background_removal/background_removal.py @@ -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 --git a/tools/background_removal/background_removal.xml b/tools/background_removal/background_removal.xml new file mode 100644 index 00000000..922a7f5a --- /dev/null +++ b/tools/background_removal/background_removal.xml @@ -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 --git a/tools/background_removal/creators.xml b/tools/background_removal/creators.xml new file mode 120000 index 00000000..5d2b71e0 --- /dev/null +++ b/tools/background_removal/creators.xml @@ -0,0 +1 @@ +../../macros/creators.xml \ No newline at end of file diff --git a/tools/background_removal/test-data/input1_output_dog.tif b/tools/background_removal/test-data/input1_output_dog.tif new file mode 100644 index 00000000..3f5a87d5 Binary files /dev/null and b/tools/background_removal/test-data/input1_output_dog.tif differ diff --git a/tools/background_removal/test-data/input1_uint8.tif b/tools/background_removal/test-data/input1_uint8.tif new file mode 100644 index 00000000..29b8bdd8 Binary files /dev/null and b/tools/background_removal/test-data/input1_uint8.tif differ diff --git a/tools/background_removal/test-data/input2_output_dog.tif b/tools/background_removal/test-data/input2_output_dog.tif new file mode 100644 index 00000000..bc3b52d7 Binary files /dev/null and b/tools/background_removal/test-data/input2_output_dog.tif differ diff --git a/tools/background_removal/test-data/input2_output_rb.tif b/tools/background_removal/test-data/input2_output_rb.tif new file mode 100644 index 00000000..1a6ffc53 Binary files /dev/null and b/tools/background_removal/test-data/input2_output_rb.tif differ diff --git a/tools/background_removal/test-data/input2_uint16.tif b/tools/background_removal/test-data/input2_uint16.tif new file mode 100644 index 00000000..0894245c Binary files /dev/null and b/tools/background_removal/test-data/input2_uint16.tif differ diff --git a/tools/background_removal/test-data/input3_output_tophat.tif b/tools/background_removal/test-data/input3_output_tophat.tif new file mode 100644 index 00000000..e135576c Binary files /dev/null and b/tools/background_removal/test-data/input3_output_tophat.tif differ diff --git a/tools/background_removal/test-data/input3_uint8.tif b/tools/background_removal/test-data/input3_uint8.tif new file mode 100644 index 00000000..0d1b003b Binary files /dev/null and b/tools/background_removal/test-data/input3_uint8.tif differ diff --git a/tools/background_removal/tests.xml b/tools/background_removal/tests.xml new file mode 120000 index 00000000..e20d710a --- /dev/null +++ b/tools/background_removal/tests.xml @@ -0,0 +1 @@ +../../macros/tests.xml \ No newline at end of file