Mercurial > repos > imgteam > background_removal
comparison background_removal.py @ 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e2c6bedc6b73 |
---|---|
1 import argparse | |
2 import warnings | |
3 | |
4 import numpy as np | |
5 import skimage.io | |
6 from skimage.filters import difference_of_gaussians | |
7 from skimage.io import imread | |
8 from skimage.morphology import disk, white_tophat | |
9 from skimage.restoration import rolling_ball | |
10 | |
11 | |
12 def process_image(args): | |
13 image = imread(args.input_image) | |
14 | |
15 if args.filter == "rolling_ball": | |
16 background_rolling = rolling_ball(image, radius=args.radius) | |
17 output_image = image - background_rolling | |
18 | |
19 elif args.filter == "dog": | |
20 output_image = difference_of_gaussians(image, low_sigma=0, high_sigma=args.radius) | |
21 | |
22 elif args.filter == "top_hat": | |
23 output_image = white_tophat(image, disk(args.radius)) | |
24 | |
25 with warnings.catch_warnings(): | |
26 output_image = convert_image_to_format_of(output_image, image) | |
27 skimage.io.imsave(args.output, output_image, plugin="tifffile") | |
28 | |
29 | |
30 def convert_image_to_format_of(image, format_image): | |
31 """ | |
32 Convert the first image to the format of the second image. | |
33 """ | |
34 if format_image.dtype == image.dtype: | |
35 return image | |
36 elif format_image.dtype == np.uint8: | |
37 return skimage.util.img_as_ubyte(image) | |
38 elif format_image.dtype == np.uint16: | |
39 return skimage.util.img_as_uint(image) | |
40 elif format_image.dtype == np.int16: | |
41 return skimage.util.img_as_int(image) | |
42 else: | |
43 raise ValueError(f'Unsupported image data type: {format_image.dtype}') | |
44 | |
45 | |
46 def main(): | |
47 parser = argparse.ArgumentParser(description="Background removal script using skiimage") | |
48 parser.add_argument('input_image', help="Input image path") | |
49 parser.add_argument('filter', choices=['rolling_ball', 'dog', 'top_hat'], | |
50 help="Background removal algorithm") | |
51 parser.add_argument('radius', type=float, help="Radius") | |
52 parser.add_argument('output', help="Output image path") | |
53 | |
54 args = parser.parse_args() | |
55 process_image(args) | |
56 | |
57 | |
58 if __name__ == '__main__': | |
59 main() |