Mercurial > repos > imgteam > slice_image
comparison slice_image.py @ 3:1faa7e3c94ff draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
author | imgteam |
---|---|
date | Thu, 04 Apr 2024 15:26:38 +0000 |
parents | f312d414f234 |
children | a72590196440 |
comparison
equal
deleted
inserted
replaced
2:f312d414f234 | 3:1faa7e3c94ff |
---|---|
7 import skimage.feature | 7 import skimage.feature |
8 import skimage.io | 8 import skimage.io |
9 import skimage.util | 9 import skimage.util |
10 | 10 |
11 | 11 |
12 def slice_image(input_file, out_folder, label=None, label_out_folder=None, window_size=64, | 12 def slice_image(input_file, out_folder, window_size=64, stride=1, bg_thresh=1, limit_slices=False, n_thresh=5000, seed=None): |
13 stride=1, bg_thresh=1, limit_slices=False, n_thresh=5000, seed=None): | |
14 # TODO NOT Implemented:process labels | |
15 # --> label and label_out_folder useless so far | |
16 | 13 |
17 # primarily for testing purposes: | 14 # Primarily for testing purposes |
18 if seed is not None: | 15 if seed is not None: |
19 random.seed(seed) | 16 random.seed(seed) |
20 | 17 |
21 img_raw = skimage.io.imread(input_file) | 18 img_raw = skimage.io.imread(input_file) |
22 if len(img_raw.shape) == 2: | 19 if len(img_raw.shape) == 2: |
38 | 35 |
39 if bg_thresh > 0: | 36 if bg_thresh > 0: |
40 sum_image = skimage.util.img_as_uint(sum_image) | 37 sum_image = skimage.util.img_as_uint(sum_image) |
41 g = skimage.feature.greycomatrix(sum_image, [1, 2], [0, np.pi / 2], nnormed=True, symmetric=True) | 38 g = skimage.feature.greycomatrix(sum_image, [1, 2], [0, np.pi / 2], nnormed=True, symmetric=True) |
42 hom = np.var(skimage.feature.greycoprops(g, prop='homogeneity')) | 39 hom = np.var(skimage.feature.greycoprops(g, prop='homogeneity')) |
43 if hom > bg_thresh: # 0.0005 | 40 if hom > bg_thresh: |
44 continue | 41 continue |
45 | 42 |
46 if limit_slices: | 43 if limit_slices: |
47 if i in sample: | 44 if i in sample: |
48 res = skimage.util.img_as_uint(patches_raw[i]) # Attention: precision loss | 45 res = skimage.util.img_as_uint(patches_raw[i]) # Attention: precision loss |
52 skimage.io.imsave(new_path % i, res, plugin='tifffile') | 49 skimage.io.imsave(new_path % i, res, plugin='tifffile') |
53 | 50 |
54 | 51 |
55 if __name__ == "__main__": | 52 if __name__ == "__main__": |
56 parser = argparse.ArgumentParser() | 53 parser = argparse.ArgumentParser() |
57 parser.add_argument('input_file', type=argparse.FileType('r'), help='input file') | 54 parser.add_argument('input_file', type=argparse.FileType('r'), help='Input file') |
58 parser.add_argument('out_folder', help='out folder') | 55 parser.add_argument('out_folder', help='Output directory') |
59 parser.add_argument('--label', dest='label_file', default=None, help='auxiliary label file to split in the same way') | 56 parser.add_argument('--stride', dest='stride', type=int, default=1, help='Applied stride') |
60 parser.add_argument('--label_out_folder', dest='label_out_folder', default=None, help='label out folder') | 57 parser.add_argument('--window_size', dest='window_size', type=int, default=64, help='Size of resulting patches') |
61 parser.add_argument('--stride', dest='stride', type=int, default=1, help='applied stride') | 58 parser.add_argument('--bg_thresh', dest='bg_thresh', type=float, default=0, help='Skip background patches without information using a treshold') |
62 parser.add_argument('--window_size', dest='window_size', type=int, default=64, help='size of resulting patches') | 59 parser.add_argument('--n_thresh', dest='n_thresh', type=int, default=5000, help='Maximum number of slices to retain') |
63 parser.add_argument('--bg_thresh', dest='bg_thresh', type=float, default=0, help='skip patches without information using a treshold') | 60 parser.add_argument('--seed', dest='seed', type=int, default=None, help='Seed for random choice of slices') |
64 parser.add_argument('--limit_slices', dest='limit_slices', type=bool, default=False, help='limit amount of slices') | |
65 parser.add_argument('--n_thresh', dest='n_thresh', type=int, default=5000, help='amount of slices') | |
66 parser.add_argument('--seed', dest='seed', type=int, default=None, help='seed for random choice of limited slices') | |
67 args = parser.parse_args() | 61 args = parser.parse_args() |
68 | 62 |
69 slice_image(args.input_file.name, args.out_folder, | 63 slice_image( |
70 label=args.label_file, label_out_folder=args.label_out_folder, | 64 args.input_file.name, |
71 stride=args.stride, window_size=args.window_size, bg_thresh=args.bg_thresh, | 65 args.out_folder, |
72 limit_slices=args.limit_slices, n_thresh=args.n_thresh, seed=args.seed) | 66 stride=args.stride, |
67 window_size=args.window_size, | |
68 bg_thresh=args.bg_thresh, | |
69 limit_slices=args.n_thresh > 0, | |
70 n_thresh=args.n_thresh, | |
71 seed=args.seed, | |
72 ) |