Mercurial > repos > imgteam > slice_image
comparison slice_image.py @ 2:f312d414f234 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
author | imgteam |
---|---|
date | Mon, 13 Nov 2023 22:12:22 +0000 |
parents | 8856a7c85e4c |
children | 1faa7e3c94ff |
comparison
equal
deleted
inserted
replaced
1:8856a7c85e4c | 2:f312d414f234 |
---|---|
1 import argparse | 1 import argparse |
2 import sys | 2 import os.path |
3 import random | |
3 import warnings | 4 import warnings |
5 | |
4 import numpy as np | 6 import numpy as np |
5 import random | 7 import skimage.feature |
6 import os.path | |
7 import skimage.io | 8 import skimage.io |
8 import skimage.util | 9 import skimage.util |
9 import skimage.feature | 10 |
10 from scipy.stats import entropy as scipy_entropy | 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, label=None, label_out_folder=None, window_size=64, | |
13 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 | 14 # TODO NOT Implemented:process labels |
15 # --> label and label_out_folder useless so far | 15 # --> label and label_out_folder useless so far |
16 | 16 |
17 # primarily for testing purposes: | 17 # primarily for testing purposes: |
18 if seed is not None: | 18 if seed is not None: |
19 random.seed(seed) | 19 random.seed(seed) |
20 | 20 |
21 img_raw = skimage.io.imread(input_file) | 21 img_raw = skimage.io.imread(input_file) |
22 if len(img_raw.shape) == 2: | 22 if len(img_raw.shape) == 2: |
23 img_raw = np.expand_dims(img_raw, 3) | 23 img_raw = np.expand_dims(img_raw, 3) |
24 | 24 |
25 with warnings.catch_warnings(): # ignore FutureWarning | 25 with warnings.catch_warnings(): # ignore FutureWarning |
26 warnings.simplefilter("ignore") | 26 warnings.simplefilter("ignore") |
27 patches_raw = skimage.util.view_as_windows(img_raw, (window_size, window_size, img_raw.shape[2]), step=stride) | 27 patches_raw = skimage.util.view_as_windows(img_raw, (window_size, window_size, img_raw.shape[2]), step=stride) |
28 patches_raw = patches_raw.reshape([-1, window_size, window_size, img_raw.shape[2]]) | 28 patches_raw = patches_raw.reshape([-1, window_size, window_size, img_raw.shape[2]]) |
29 | 29 |
30 new_path = os.path.join(out_folder, "%d.tiff") | 30 new_path = os.path.join(out_folder, "%d.tiff") |
31 | 31 |
32 #samples for thresholding the amount of slices | 32 # samples for thresholding the amount of slices |
33 sample = random.sample(range(patches_raw.shape[0]), n_thresh) | 33 sample = random.sample(range(patches_raw.shape[0]), n_thresh) |
34 | 34 |
35 for i in range(0, patches_raw.shape[0]): | 35 for i in range(0, patches_raw.shape[0]): |
36 # TODO improve | 36 # TODO improve |
37 sum_image = np.sum(patches_raw[i], 2)/img_raw.shape[2] | 37 sum_image = np.sum(patches_raw[i], 2) / img_raw.shape[2] |
38 total_entr = np.var(sum_image.reshape([-1])) | |
39 | 38 |
40 if bg_thresh > 0: | 39 if bg_thresh > 0: |
41 sum_image = skimage.util.img_as_uint(sum_image) | 40 sum_image = skimage.util.img_as_uint(sum_image) |
42 g = skimage.feature.greycomatrix(sum_image, [1,2], [0, np.pi/2], nnormed=True, symmetric=True) | 41 g = skimage.feature.greycomatrix(sum_image, [1, 2], [0, np.pi / 2], nnormed=True, symmetric=True) |
43 hom = np.var(skimage.feature.greycoprops(g, prop='homogeneity')) | 42 hom = np.var(skimage.feature.greycoprops(g, prop='homogeneity')) |
44 if hom > bg_thresh: #0.0005 | 43 if hom > bg_thresh: # 0.0005 |
45 continue | 44 continue |
46 | 45 |
47 if limit_slices == True: | 46 if limit_slices: |
48 if i in sample: | 47 if i in sample: |
49 res = skimage.util.img_as_uint(patches_raw[i]) #Attention: precision loss | 48 res = skimage.util.img_as_uint(patches_raw[i]) # Attention: precision loss |
50 skimage.io.imsave(new_path % i, res, plugin='tifffile') | 49 skimage.io.imsave(new_path % i, res, plugin='tifffile') |
51 else: | 50 else: |
52 res = skimage.util.img_as_uint(patches_raw[i]) #Attention: precision loss | 51 res = skimage.util.img_as_uint(patches_raw[i]) # Attention: precision loss |
53 skimage.io.imsave(new_path % i, res, plugin='tifffile') | 52 skimage.io.imsave(new_path % i, res, plugin='tifffile') |
54 | 53 |
55 | 54 |
56 if __name__ == "__main__": | 55 if __name__ == "__main__": |
57 parser = argparse.ArgumentParser() | 56 parser = argparse.ArgumentParser() |
58 parser.add_argument('input_file', type=argparse.FileType('r'), help='input file') | 57 parser.add_argument('input_file', type=argparse.FileType('r'), help='input file') |
59 parser.add_argument('out_folder', help='out folder') | 58 parser.add_argument('out_folder', help='out folder') |
67 parser.add_argument('--seed', dest='seed', type=int, default=None, help='seed for random choice of limited slices') | 66 parser.add_argument('--seed', dest='seed', type=int, default=None, help='seed for random choice of limited slices') |
68 args = parser.parse_args() | 67 args = parser.parse_args() |
69 | 68 |
70 slice_image(args.input_file.name, args.out_folder, | 69 slice_image(args.input_file.name, args.out_folder, |
71 label=args.label_file, label_out_folder=args.label_out_folder, | 70 label=args.label_file, label_out_folder=args.label_out_folder, |
72 stride=args.stride, window_size=args.window_size, bg_thresh=args.bg_thresh, | 71 stride=args.stride, window_size=args.window_size, bg_thresh=args.bg_thresh, |
73 limit_slices=args.limit_slices, n_thresh=args.n_thresh, seed=args.seed) | 72 limit_slices=args.limit_slices, n_thresh=args.n_thresh, seed=args.seed) |