Mercurial > repos > imgteam > 2d_auto_threshold
annotate auto_threshold.py @ 0:d4da97f51700 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
| author | imgteam | 
|---|---|
| date | Sat, 09 Feb 2019 14:27:36 -0500 | 
| parents | |
| children | 4853fc2b50bf | 
| rev | line source | 
|---|---|
| 
0
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
1 import argparse | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
2 import numpy as np | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
3 import os | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
4 import sys | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
5 import warnings | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
6 import skimage.io | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
7 import skimage.filters | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
8 import skimage.util | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
9 | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
10 threshOptions = { | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
11 'otsu' : lambda img_raw: skimage.filters.threshold_otsu(img_raw), | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
12 'gaussian_adaptive' : lambda img_raw: skimage.filters.threshold_local(img_raw.reshape(img_raw.shape[0], img_raw.shape[1]), 3, method='gaussian'), # todo reshape 2d | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
13 'mean_adaptive' : lambda img_raw: skimage.filters.threshold_local(img_raw.reshape(img_raw.shape[0], img_raw.shape[1]), 3, method='mean'), # todo reshape 2d | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
14 'isodata' : lambda img_raw: skimage.filters.threshold_isodata(img_raw), | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
15 'li' : lambda img_raw: skimage.filters.threshold_li(img_raw), | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
16 'yen' : lambda img_raw: skimage.filters.threshold_yen(img_raw), | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
17 } | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
18 | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
19 if __name__ == "__main__": | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
20 parser = argparse.ArgumentParser(description='Segment Foci') | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
21 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
22 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
23 parser.add_argument('thresh_type', choices=threshOptions.keys(), help='thresholding method') | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
24 parser.add_argument('dark_background', default=True, type=bool, help='True if background is dark') | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
25 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
 | 
26 | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
27 img_in = skimage.io.imread(args.input_file.name) | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
28 thresh = threshOptions[args.thresh_type](img_in) | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
29 | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
30 if args.dark_background: | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
31 res = img_in > thresh | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
32 else: | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
33 res = img_in <= thresh | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
34 | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
35 with warnings.catch_warnings(): | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
36 warnings.simplefilter("ignore") | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
37 res = skimage.util.img_as_uint(res) | 
| 
 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
38 skimage.io.imsave(args.out_file.name, res, plugin="tifffile") | 
