annotate background_removal.py @ 1:f8548b884a85 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:40 +0000
parents 926057bd6b38
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
1 import argparse
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
2 import warnings
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
3
926057bd6b38 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
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
5 import skimage.io
926057bd6b38 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
926057bd6b38 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
926057bd6b38 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
926057bd6b38 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
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
10
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
11
926057bd6b38 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):
926057bd6b38 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)
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
14
926057bd6b38 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":
926057bd6b38 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)
926057bd6b38 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
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
18
926057bd6b38 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":
926057bd6b38 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)
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
21
926057bd6b38 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":
926057bd6b38 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))
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
24
926057bd6b38 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():
926057bd6b38 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)
926057bd6b38 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")
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
28
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
29
926057bd6b38 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):
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
31 """
926057bd6b38 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.
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
33 """
926057bd6b38 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:
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
35 return image
926057bd6b38 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:
926057bd6b38 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)
926057bd6b38 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:
926057bd6b38 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)
926057bd6b38 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:
926057bd6b38 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)
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
42 else:
926057bd6b38 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}')
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
44
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
45
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
46 def main():
926057bd6b38 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")
926057bd6b38 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")
926057bd6b38 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'],
926057bd6b38 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")
926057bd6b38 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")
926057bd6b38 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")
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
53
926057bd6b38 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()
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
55 process_image(args)
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
56
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
57
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
58 if __name__ == '__main__':
926057bd6b38 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/background_removal commit 004112ac8c2ebcdb9763096df440227fda174ae3
imgteam
parents:
diff changeset
59 main()