Mercurial > repos > imgteam > projective_transformation
annotate projective_transformation.py @ 3:be9a815e2240 draft
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit ee372547082b693ef1bbe10b5b7ea8660b9779df"
author | imgteam |
---|---|
date | Thu, 20 Jan 2022 17:14:55 +0000 |
parents | 1ffdb07020ee |
children | 37b079c98c38 |
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 |
0
17f5d0c3f8a3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
12 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
|
13 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
|
14 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
|
15 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
|
16 import tifffile |
1ffdb07020ee
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents:
1
diff
changeset
|
17 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
|
18 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
|
19 |
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 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
|
22 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
|
23 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
|
24 else: |
17f5d0c3f8a3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
25 a[:] = b |
17f5d0c3f8a3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
26 |
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 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 |
17f5d0c3f8a3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
35 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
|
36 |
2
1ffdb07020ee
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents:
1
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 |
17f5d0c3f8a3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
41 _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
|
42 _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
|
43 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
|
44 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
|
45 |
17f5d0c3f8a3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
46 return coords |
17f5d0c3f8a3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
47 |
17f5d0c3f8a3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
48 |
2
1ffdb07020ee
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents:
1
diff
changeset
|
49 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
|
50 |
1ffdb07020ee
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents:
1
diff
changeset
|
51 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
|
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 |
2
1ffdb07020ee
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents:
1
diff
changeset
|
66 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
|
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 |
1ffdb07020ee
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 40b2eeecc9e15be0b710f5e5195ac6f3b3b83c39"
imgteam
parents:
1
diff
changeset
|
82 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
|
83 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
|
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) |