Mercurial > repos > imgteam > slice_image
annotate slice_image.py @ 4:a72590196440 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
author | imgteam |
---|---|
date | Wed, 24 Apr 2024 08:13:20 +0000 |
parents | 1faa7e3c94ff |
children |
rev | line source |
---|---|
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
1 import argparse |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
2 import os.path |
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
3 import random |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
4 import warnings |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
5 |
4
a72590196440
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents:
3
diff
changeset
|
6 import giatools.io |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
7 import numpy as np |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
8 import skimage.feature |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
9 import skimage.io |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
10 import skimage.util |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
11 |
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
12 |
3
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
13 def slice_image(input_file, out_folder, window_size=64, stride=1, bg_thresh=1, limit_slices=False, n_thresh=5000, seed=None): |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
14 |
3
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
15 # Primarily for testing purposes |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
16 if seed is not None: |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
17 random.seed(seed) |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
18 |
4
a72590196440
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents:
3
diff
changeset
|
19 img_raw = giatools.io.imread(input_file) |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
20 if len(img_raw.shape) == 2: |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
21 img_raw = np.expand_dims(img_raw, 3) |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
22 |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
23 with warnings.catch_warnings(): # ignore FutureWarning |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
24 warnings.simplefilter("ignore") |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
25 patches_raw = skimage.util.view_as_windows(img_raw, (window_size, window_size, img_raw.shape[2]), step=stride) |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
26 patches_raw = patches_raw.reshape([-1, window_size, window_size, img_raw.shape[2]]) |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
27 |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
28 new_path = os.path.join(out_folder, "%d.tiff") |
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
29 |
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
30 # samples for thresholding the amount of slices |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
31 sample = random.sample(range(patches_raw.shape[0]), n_thresh) |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
32 |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
33 for i in range(0, patches_raw.shape[0]): |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
34 # TODO improve |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
35 sum_image = np.sum(patches_raw[i], 2) / img_raw.shape[2] |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
36 |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
37 if bg_thresh > 0: |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
38 sum_image = skimage.util.img_as_uint(sum_image) |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
39 g = skimage.feature.greycomatrix(sum_image, [1, 2], [0, np.pi / 2], nnormed=True, symmetric=True) |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
40 hom = np.var(skimage.feature.greycoprops(g, prop='homogeneity')) |
3
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
41 if hom > bg_thresh: |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
42 continue |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
43 |
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
44 if limit_slices: |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
45 if i in sample: |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
46 res = skimage.util.img_as_uint(patches_raw[i]) # Attention: precision loss |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
47 skimage.io.imsave(new_path % i, res, plugin='tifffile') |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
48 else: |
2
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
49 res = skimage.util.img_as_uint(patches_raw[i]) # Attention: precision loss |
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
50 skimage.io.imsave(new_path % i, res, plugin='tifffile') |
f312d414f234
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
51 |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
52 |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
53 if __name__ == "__main__": |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
54 parser = argparse.ArgumentParser() |
3
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
55 parser.add_argument('input_file', type=argparse.FileType('r'), help='Input file') |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
56 parser.add_argument('out_folder', help='Output directory') |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
57 parser.add_argument('--stride', dest='stride', type=int, default=1, help='Applied stride') |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
58 parser.add_argument('--window_size', dest='window_size', type=int, default=64, help='Size of resulting patches') |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
59 parser.add_argument('--bg_thresh', dest='bg_thresh', type=float, default=0, help='Skip background patches without information using a treshold') |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
60 parser.add_argument('--n_thresh', dest='n_thresh', type=int, default=5000, help='Maximum number of slices to retain') |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
61 parser.add_argument('--seed', dest='seed', type=int, default=None, help='Seed for random choice of slices') |
0
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
62 args = parser.parse_args() |
aacfbd7c4af6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
63 |
3
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
64 slice_image( |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
65 args.input_file.name, |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
66 args.out_folder, |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
67 stride=args.stride, |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
68 window_size=args.window_size, |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
69 bg_thresh=args.bg_thresh, |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
70 limit_slices=args.n_thresh > 0, |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
71 n_thresh=args.n_thresh, |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
72 seed=args.seed, |
1faa7e3c94ff
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/slice_image/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
2
diff
changeset
|
73 ) |