comparison slice_image.py @ 0:ef763552586d draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
author thomaswollmann
date Mon, 07 Jan 2019 05:40:17 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ef763552586d
1 import argparse
2 import sys
3 import warnings
4 import numpy as np
5 import random
6 import os.path
7 import skimage.io
8 import skimage.util
9 import skimage.feature
10 from scipy.stats import entropy as scipy_entropy
11
12 def slice_image(input_file, out_folder, label=None, label_out_folder=None, window_size=64, stride=1, bg_thresh=1, limit_slices=False, n_thresh=5000):
13 #TODO NOT Implemented:process labels
14
15 img_raw = skimage.io.imread(input_file)
16 if len(img_raw.shape) == 2:
17 img_raw = np.expand_dims(img_raw, 3)
18
19 patches_raw = skimage.util.view_as_windows(img_raw, (window_size, window_size, img_raw.shape[2]), step=stride)
20 patches_raw = patches_raw.reshape([-1, window_size, window_size, img_raw.shape[2]])
21
22 filename = os.path.splitext(os.path.basename(input_file))[0]
23 new_path = out_folder+"/"+filename+"_%d.tiff"
24
25 #samples for thresholding the amount of slices
26 sample = random.sample(range(patches_raw.shape[0]), n_thresh)
27
28 with warnings.catch_warnings():
29 warnings.simplefilter("ignore")
30
31 for i in range(0, patches_raw.shape[0]):
32 # TODO improve
33 sum_image = np.sum(patches_raw[i], 2)/img_raw.shape[2]
34 total_entr = np.var(sum_image.reshape([-1]))
35
36 if bg_thresh > 0:
37 sum_image = skimage.util.img_as_uint(sum_image)
38 g = skimage.feature.greycomatrix(sum_image, [1,2], [0, np.pi/2], nnormed=True, symmetric=True)
39 hom = np.var(skimage.feature.greycoprops(g, prop='homogeneity'))
40 if hom > bg_thresh: #0.0005
41 continue
42
43 if limit_slices == True:
44 if i in sample:
45 res = skimage.util.img_as_uint(patches_raw[i]) #Attention: precision loss
46 skimage.io.imsave(new_path % i, res, plugin='tifffile')
47 else:
48 res = skimage.util.img_as_uint(patches_raw[i]) #Attention: precision loss
49 skimage.io.imsave(new_path % i, res, plugin='tifffile')
50
51
52
53 if __name__ == "__main__":
54 parser = argparse.ArgumentParser()
55 parser.add_argument('input_file', type=argparse.FileType('r'), help='input file')
56 parser.add_argument('out_folder', help='out folder')
57 parser.add_argument('--label', dest='label_file', default=None, help='auxiliary label file to split in the same way')
58 parser.add_argument('--label_out_folder', dest='label_out_folder', default=None, help='label out folder')
59 parser.add_argument('--stride', dest='stride', type=int, default=1, help='applied stride')
60 parser.add_argument('--window_size', dest='window_size', type=int, default=64, help='size of resulting patches')
61 parser.add_argument('--bg_thresh', dest='bg_thresh', type=float, default=0, help='skip patches without information using a treshold')
62 parser.add_argument('--limit_slices', dest='limit_slices', type=bool, default=False, help='limit amount of slices')
63 parser.add_argument('--n_thresh', dest='n_thresh', type=int, default=5000, help='amount of slices')
64 args = parser.parse_args()
65
66 slice_image(args.input_file.name, args.out_folder,
67 label=args.label_file, label_out_folder=args.label_out_folder,
68 stride=args.stride, window_size=args.window_size, bg_thresh=args.bg_thresh, limit_slices=args.limit_slices, n_thresh=args.n_thresh)