Mercurial > repos > imgteam > 2d_auto_threshold
annotate auto_threshold.py @ 9:50fa6150e340 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
author | imgteam |
---|---|
date | Sat, 07 Jun 2025 18:38:31 +0000 |
parents | 699a5e9146b3 |
children |
rev | line source |
---|---|
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
1 """ |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
2 Copyright 2017-2024 Biomedical Computer Vision Group, Heidelberg University. |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
3 |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
4 Distributed under the MIT license. |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
6 """ |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
7 |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
8 import argparse |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
9 |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
10 import numpy as np |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
11 import skimage.filters |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
12 import skimage.util |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
13 from giatools.image import Image |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
14 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
15 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
16 class DefaultThresholdingMethod: |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
17 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
18 def __init__(self, thres, accept: list[str] | None = None, **kwargs): |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
19 self.thres = thres |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
20 self.accept = accept if accept else [] |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
21 self.kwargs = kwargs |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
22 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
23 def __call__(self, image, *args, offset=0, **kwargs): |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
24 accepted_kwargs = self.kwargs.copy() |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
25 for key, val in kwargs.items(): |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
26 if key in self.accept: |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
27 accepted_kwargs[key] = val |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
28 thres = self.thres(image, *args, **accepted_kwargs) |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
29 return image > thres + offset |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
30 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
31 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
32 class ManualThresholding: |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
33 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
34 def __call__(self, image, thres1: float, thres2: float | None, **kwargs): |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
35 if thres2 is None: |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
36 return image > thres1 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
37 else: |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
38 thres1, thres2 = sorted((thres1, thres2)) |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
39 return skimage.filters.apply_hysteresis_threshold(image, thres1, thres2) |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
40 |
8
699a5e9146b3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents:
7
diff
changeset
|
41 |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
42 th_methods = { |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
43 'manual': ManualThresholding(), |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
44 |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
45 'otsu': DefaultThresholdingMethod(skimage.filters.threshold_otsu), |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
46 'li': DefaultThresholdingMethod(skimage.filters.threshold_li), |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
47 'yen': DefaultThresholdingMethod(skimage.filters.threshold_yen), |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
48 'isodata': DefaultThresholdingMethod(skimage.filters.threshold_isodata), |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
49 |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
50 'loc_gaussian': DefaultThresholdingMethod(skimage.filters.threshold_local, accept=['block_size'], method='gaussian'), |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
51 'loc_median': DefaultThresholdingMethod(skimage.filters.threshold_local, accept=['block_size'], method='median'), |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
52 'loc_mean': DefaultThresholdingMethod(skimage.filters.threshold_local, accept=['block_size'], method='mean'), |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
53 } |
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
54 |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
55 |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
56 def do_thresholding( |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
57 input_filepath: str, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
58 output_filepath: str, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
59 th_method: str, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
60 block_size: int, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
61 offset: float, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
62 threshold1: float, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
63 threshold2: float | None, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
64 invert_output: bool, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
65 ): |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
66 assert th_method in th_methods, f'Unknown method "{th_method}"' |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
67 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
68 # Load image |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
69 img_in = Image.read(input_filepath) |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
70 |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
71 # Perform thresholding |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
72 result = th_methods[th_method]( |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
73 image=img_in.data, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
74 block_size=block_size, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
75 offset=offset, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
76 thres1=threshold1, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
77 thres2=threshold2, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
78 ) |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
79 if invert_output: |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
80 result = np.logical_not(result) |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
81 |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
82 # Convert to canonical representation for binary images |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
83 result = (result * 255).astype(np.uint8) |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
84 |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
85 # Write result |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
86 Image( |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
87 data=skimage.util.img_as_ubyte(result), |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
88 axes=img_in.axes, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
89 ).normalize_axes_like( |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
90 img_in.original_axes, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
91 ).write( |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
92 output_filepath, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
93 ) |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
94 |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
95 |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
96 if __name__ == "__main__": |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
97 parser = argparse.ArgumentParser(description='Automatic image thresholding') |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
98 parser.add_argument('input', type=str, help='Path to the input image') |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
99 parser.add_argument('output', type=str, help='Path to the output image (uint8)') |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
100 parser.add_argument('th_method', choices=th_methods.keys(), help='Thresholding method') |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
101 parser.add_argument('block_size', type=int, help='Odd size of pixel neighborhood for calculating the threshold') |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
102 parser.add_argument('offset', type=float, help='Offset of automatically determined threshold value') |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
103 parser.add_argument('threshold1', type=float, help='Manual threshold value') |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
104 parser.add_argument('--threshold2', type=float, help='Second manual threshold value (for hysteresis thresholding)') |
6
8bccb36e055a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit cb3d628f4c91e996e10dcb35b1168332e692293a
imgteam
parents:
5
diff
changeset
|
105 parser.add_argument('--invert_output', default=False, action='store_true', help='Values below/above the threshold are labeled with 0/255 by default, and with 255/0 if this argument is used') |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
106 args = parser.parse_args() |
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
107 |
9
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
108 do_thresholding( |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
109 args.input, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
110 args.output, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
111 args.th_method, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
112 args.block_size, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
113 args.offset, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
114 args.threshold1, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
115 args.threshold2, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
116 args.invert_output, |
50fa6150e340
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 01343602708de3cc7fa4986af9000adc36dd0651
imgteam
parents:
8
diff
changeset
|
117 ) |