annotate projective_transformation.py @ 2:1ffdb07020ee draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
author imgteam
date Fri, 14 Jan 2022 03:07:08 +0000
parents 974cf4357707
children be9a815e2240
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 imghdr
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
11 import warnings
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
12
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):
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
51
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
52 moving = skimage.io.imread(moving_fn)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
53 extension = imghdr.what(moving_fn)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
54 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
55 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
56
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
57 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
58 isRGB = True
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
59 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
60 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
61 isRGB = False
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
62
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
63 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
64 isMulCh = True
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
65 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
66 isMulCh = False
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
67
2
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
68 fixed = skimage.io.imread(fixed_fn)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
69 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
70 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
71 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
72 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
73
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
74 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
75 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
76 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
77 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
78 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
79
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
80 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
81 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
82 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
83
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
84 trans = ProjectiveTransform(matrix=warp_mat)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
85 warped_coords = warp_coords_batch(trans, hw_fixed)
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
86
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
87 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
88 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
89 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
90 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
91 if isRGB:
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 = 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
93 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
94 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
95
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
96 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
97 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
98 if isMulCh:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
99 tifffile.imwrite(output_fn + '.tif', warped_moving, imagej=True, metadata={'mode': 'composite'})
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
100 else:
1ffdb07020ee "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents: 1
diff changeset
101 skimage.io.imsave(output_fn + '.' + extension, warped_moving)
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
102
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
103
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
104 if __name__ == "__main__":
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
105 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
106 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
107 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
108 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
109 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
110 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
111 transform(args.moving_image, args.fixed_image, args.warp_matrix, args.warped_image)