annotate concat_channels.py @ 3:4c43875c790c draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
author imgteam
date Thu, 04 Apr 2024 15:25:29 +0000
parents 212627bfb759
children 2592a29a785e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
2
212627bfb759 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
2
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 import numpy as np
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
4 import skimage.io
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 import skimage.util
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6
2
212627bfb759 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
7
3
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
8 def concat_channels(input_image_paths, output_image_path, axis, preserve_values):
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9 images = []
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10 for image_path in input_image_paths:
3
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
11
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12 raw_image = skimage.io.imread(image_path)
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
13 if len(raw_image.shape) == 2:
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
14 if axis == 0:
2
212627bfb759 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
15 raw_image = [raw_image]
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
16 else:
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
17 raw_image = np.expand_dims(raw_image, 2)
3
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
18
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
19 # Preserve values: Convert to `float` dtype without changing the values
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
20 if preserve_values:
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
21 raw_image = raw_image.astype(float)
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
22
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
23 # Preserve brightness: Scale values to 0..1
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
24 else:
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
25 raw_image = skimage.util.img_as_float(raw_image)
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
26
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
27 images.append(raw_image)
3
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
28
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
29 # Do the concatenation and save
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30 res = np.concatenate(images, axis)
3
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
31 skimage.io.imsave(output_image_path, res, plugin='tifffile')
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
32
2
212627bfb759 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
33
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34 if __name__ == "__main__":
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
35 parser = argparse.ArgumentParser()
3
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
36 parser.add_argument('input_files', type=argparse.FileType('r'), nargs='+')
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
37 parser.add_argument('-o', dest='out_file', type=argparse.FileType('w'))
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
38 parser.add_argument('--axis', dest='axis', type=int, default=0, choices=[0, 2])
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
39 parser.add_argument('--preserve_values', default=False, action='store_true')
0
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
40 args = parser.parse_args()
30517f733f7b planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
41
3
4c43875c790c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents: 2
diff changeset
42 concat_channels([x.name for x in args.input_files], args.out_file.name, args.axis, args.preserve_values)