annotate background_removal.py @ 1:6e9e98a7be4e draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 649a1e6ecb925bed885a8477fe82dfd9dfea8baa
author imgteam
date Tue, 30 Jul 2024 08:08:50 +0000
parents e2c6bedc6b73
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
1 import argparse
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
2 import warnings
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
3
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
4 import numpy as np
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
5 import skimage.io
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
6 from skimage.filters import difference_of_gaussians
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
7 from skimage.io import imread
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
8 from skimage.morphology import disk, white_tophat
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
9 from skimage.restoration import rolling_ball
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
10
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
11
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
12 def process_image(args):
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
13 image = imread(args.input_image)
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
14
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
15 if args.filter == "rolling_ball":
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
16 background_rolling = rolling_ball(image, radius=args.radius)
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
17 output_image = image - background_rolling
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
18
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
19 elif args.filter == "dog":
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
20 output_image = difference_of_gaussians(image, low_sigma=0, high_sigma=args.radius)
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
21
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
22 elif args.filter == "top_hat":
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
23 output_image = white_tophat(image, disk(args.radius))
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
24
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
25 with warnings.catch_warnings():
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
26 output_image = convert_image_to_format_of(output_image, image)
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
27 skimage.io.imsave(args.output, output_image, plugin="tifffile")
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
28
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
29
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
30 def convert_image_to_format_of(image, format_image):
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
31 """
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
32 Convert the first image to the format of the second image.
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
33 """
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
34 if format_image.dtype == image.dtype:
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
35 return image
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
36 elif format_image.dtype == np.uint8:
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
37 return skimage.util.img_as_ubyte(image)
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
38 elif format_image.dtype == np.uint16:
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
39 return skimage.util.img_as_uint(image)
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
40 elif format_image.dtype == np.int16:
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
41 return skimage.util.img_as_int(image)
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
42 else:
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
43 raise ValueError(f'Unsupported image data type: {format_image.dtype}')
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
44
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
45
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
46 def main():
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
47 parser = argparse.ArgumentParser(description="Background removal script using skiimage")
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
48 parser.add_argument('input_image', help="Input image path")
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
49 parser.add_argument('filter', choices=['rolling_ball', 'dog', 'top_hat'],
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
50 help="Background removal algorithm")
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
51 parser.add_argument('radius', type=float, help="Radius")
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
52 parser.add_argument('output', help="Output image path")
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
53
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
54 args = parser.parse_args()
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
55 process_image(args)
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
56
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
57
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
58 if __name__ == '__main__':
e2c6bedc6b73 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
59 main()