annotate filter_image.py @ 0:f264ae82f735 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
author imgteam
date Sat, 09 Feb 2019 14:29:13 -0500
parents
children dba87c4b32d3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
2 import sys
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 import warnings
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
4 import numpy as np
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 import skimage.io
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 import skimage.filters
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7 import skimage.util
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
8 from skimage.morphology import disk
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10 filterOptions = {
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
11 'median' : lambda img_raw, radius: skimage.filters.median(img_raw, disk(radius)),
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12 'gaussian' : lambda img_raw, radius: skimage.filters.gaussian(img_raw, sigma=radius),
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
13 'prewitt' : lambda img_raw, radius: skimage.filters.prewitt(img_raw),
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
14 'sobel' : lambda img_raw, radius: skimage.filters.sobel(img_raw),
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
15 'scharr' : lambda img_raw, radius: skimage.filters.scharr(img_raw),
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
16 }
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
17
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
18 if __name__ == "__main__":
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
19 parser = argparse.ArgumentParser()
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
21 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)')
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
22 parser.add_argument('filter_type', choices=filterOptions.keys(), help='conversion type')
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
23 parser.add_argument('radius', default=3.0, type=float, help='Radius/Sigma')
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
24 args = parser.parse_args()
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
25
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
26 img_in = skimage.io.imread(args.input_file.name)
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
27 res = filterOptions[args.filter_type](img_in, args.radius)
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
28 res[res<-1]=-1
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
29 res[res>1]=1
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
31 with warnings.catch_warnings():
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
32 warnings.simplefilter("ignore")
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
33 res = skimage.util.img_as_uint(res) #Attention: precision loss
f264ae82f735 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_simple_filter/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34 skimage.io.imsave(args.out_file.name, res, plugin='tifffile')