annotate projective_transformation.py @ 7:6a8ae8cd7167 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/projective_transformation/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
author imgteam
date Wed, 24 Apr 2024 08:13:05 +0000
parents 37b079c98c38
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
1 """
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
2 Copyright 2019-2022 Biomedical Computer Vision Group, Heidelberg University.
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
3
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
4 Distributed under the MIT license.
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
6
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
7 """
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
8
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
9 import argparse
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
10 import warnings
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
11
7
6a8ae8cd7167 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/projective_transformation/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
12 import giatools.io
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
13 import numpy as np
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
14 import pandas as pd
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
15 import skimage.color
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
16 import skimage.io
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
17 import tifffile
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
18 from scipy.ndimage import map_coordinates
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
19 from skimage.transform import ProjectiveTransform
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
21
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
22 def _stackcopy(a, b):
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
23 if a.ndim == 3:
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
24 a[:] = b[:, :, np.newaxis]
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
25 else:
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
26 a[:] = b
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
27
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
28
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
29 def warp_coords_batch(coord_map, shape, dtype=np.float64, batch_size=1000000):
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30 rows, cols = shape[0], shape[1]
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
31 coords_shape = [len(shape), rows, cols]
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
32 if len(shape) == 3:
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
33 coords_shape.append(shape[2])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34 coords = np.empty(coords_shape, dtype=dtype)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
35
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
36 tf_coords = np.indices((cols, rows), dtype=dtype).reshape(2, -1).T
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
37
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
38 for i in range(0, (tf_coords.shape[0] // batch_size + 1)):
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
39 tf_coords[batch_size * i:batch_size * (i + 1)] = coord_map(tf_coords[batch_size * i:batch_size * (i + 1)])
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
40 tf_coords = tf_coords.T.reshape((-1, cols, rows)).swapaxes(1, 2)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
41
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
42 _stackcopy(coords[1, ...], tf_coords[0, ...])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
43 _stackcopy(coords[0, ...], tf_coords[1, ...])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
44 if len(shape) == 3:
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
45 coords[2, ...] = range(shape[2])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
46
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
47 return coords
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
48
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
49
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
50 def transform(moving_fn, fixed_fn, warp_mat, output_fn):
7
6a8ae8cd7167 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/projective_transformation/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
51 moving = giatools.io.imread(moving_fn)
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
52 nDims = len(moving.shape)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
53 assert nDims in [2, 3, 4, 5, 6], 'this tool only supports up to 6 dimensions'
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
54
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
55 if moving.shape[-1] in [3, 4] and nDims > 2:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
56 isRGB = True
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
57 moving = np.transpose(moving, (nDims - 1,) + tuple(_ for _ in range(nDims - 1)))
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
58 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
59 isRGB = False
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
60
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
61 if nDims > 3 or (nDims == 3 and not isRGB):
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
62 isMulCh = True
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
63 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
64 isMulCh = False
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
65
7
6a8ae8cd7167 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/projective_transformation/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
66 fixed = giatools.io.imread(fixed_fn)
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
67 if fixed.shape[-1] in [3, 4] and len(fixed.shape) > 2:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
68 hw_fixed = fixed.shape[-3:-1]
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
69 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
70 hw_fixed = fixed.shape[-2:]
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
71
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
72 if isRGB or isMulCh:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
73 shapeCh = moving.shape[0:-2]
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
74 nCh = np.prod(shapeCh)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
75 moving = np.reshape(moving, (nCh,) + moving.shape[-2:])
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
76 warped_moving = np.zeros((nCh,) + hw_fixed, dtype=moving.dtype)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
77
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
78 warp_mat = pd.read_csv(warp_mat, delimiter="\t", header=None)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
79 warp_mat = np.array(warp_mat)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
80 assert warp_mat.shape[0] in [3], 'only 2D image transformaton is supported'
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
81
4
37b079c98c38 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit ded1703bcd6e295b26201505edb58381991b265e"
imgteam
parents: 3
diff changeset
82 transI = ProjectiveTransform(matrix=np.linalg.inv(warp_mat))
37b079c98c38 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit ded1703bcd6e295b26201505edb58381991b265e"
imgteam
parents: 3
diff changeset
83 warped_coords = warp_coords_batch(transI, hw_fixed)
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
84
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
85 if isMulCh or isRGB:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
86 for i in range(nCh):
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
87 warped_moving[i, ...] = map_coordinates(moving[i, ...], warped_coords, cval=0)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
88 warped_moving = np.reshape(warped_moving, shapeCh + warped_moving.shape[-2:])
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
89 if isRGB:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
90 warped_moving = np.transpose(warped_moving, tuple(_ for _ in range(1, nDims)) + (0,))
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
91 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
92 warped_moving = map_coordinates(moving, warped_coords, cval=0)
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
93
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
94 with warnings.catch_warnings():
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
95 warnings.simplefilter("ignore")
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
96 if isMulCh:
3
be9a815e2240 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit ee372547082b693ef1bbe10b5b7ea8660b9779df"
imgteam
parents: 2
diff changeset
97 tifffile.imwrite(output_fn, warped_moving, imagej=True, metadata={'mode': 'composite'})
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
98 else:
3
be9a815e2240 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit ee372547082b693ef1bbe10b5b7ea8660b9779df"
imgteam
parents: 2
diff changeset
99 skimage.io.imsave(output_fn, warped_moving)
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
100
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
101
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
102 if __name__ == "__main__":
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
103 parser = argparse.ArgumentParser(description="Transform the image")
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
104 parser.add_argument("fixed_image", help="Path to the fixed image")
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
105 parser.add_argument("moving_image", help="Path to the moving image (to be transformed)")
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
106 parser.add_argument("warp_matrix", help="Path to the transformation matrix")
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
107 parser.add_argument("warped_image", help="Path to the output (transfirmed moving image)")
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
108 args = parser.parse_args()
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
109 transform(args.moving_image, args.fixed_image, args.warp_matrix, args.warped_image)