annotate image_registration_affine.py @ 1:fa769715b6b0 draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
author imgteam
date Thu, 20 Jan 2022 00:45:30 +0000
parents e34222a620d4
children 77dc68af2b40
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
1 """
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
2 Copyright 2021-2022 Biomedical Computer Vision Group, Heidelberg University.
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
3
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
4 Distributed under the MIT license.
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
6
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
7 """
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
8 import argparse
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
9
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
10 import numpy as np
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
11 import pandas as pd
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
12 import skimage.io
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
13 from scipy.ndimage import map_coordinates
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
14 from scipy.optimize import least_squares
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
15 from scipy.signal import convolve2d
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
16 from skimage.color import rgb2gray, rgba2rgb
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
17 from skimage.filters import gaussian
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
18 from skimage.transform import ProjectiveTransform
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
19
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
20
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
21 def _stackcopy(a, b):
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
22 if a.ndim == 3:
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
23 a[:] = b[:, :, np.newaxis]
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
24 else:
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
25 a[:] = b
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
26
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
27
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
28 def warp_coords_batch(coord_map, shape, dtype=np.float64, batch_size=1000000):
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
29 rows, cols = shape[0], shape[1]
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
30 coords_shape = [len(shape), rows, cols]
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
31 if len(shape) == 3:
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
32 coords_shape.append(shape[2])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
33 coords = np.empty(coords_shape, dtype=dtype)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
34
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
35 tf_coords = np.indices((cols, rows), dtype=dtype).reshape(2, -1).T
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
36
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
37 for i in range(0, (tf_coords.shape[0] // batch_size + 1)):
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
38 tf_coords[batch_size * i:batch_size * (i + 1)] = coord_map(tf_coords[batch_size * i:batch_size * (i + 1)])
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
39 tf_coords = tf_coords.T.reshape((-1, cols, rows)).swapaxes(1, 2)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
40
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
41 _stackcopy(coords[1, ...], tf_coords[0, ...])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
42 _stackcopy(coords[0, ...], tf_coords[1, ...])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
43 if len(shape) == 3:
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
44 coords[2, ...] = range(shape[2])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
45
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
46 return coords
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
47
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
48
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
49 def affine_registration(params, moving, fixed, metric='mae'):
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
50 tmat = np.eye(3)
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
51 tmat[0, :] = params.take([0, 1, 2])
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
52 tmat[1, :] = params.take([3, 4, 5])
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
53
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
54 trans = ProjectiveTransform(matrix=tmat)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
55 warped_coords = warp_coords_batch(trans, fixed.shape)
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
56 t = map_coordinates(moving, warped_coords, mode='nearest')
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
57 f = fixed
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
58
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
59 if metric == 'mse':
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
60 err = (t - f) ** 2
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
61
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
62 elif metric == 'mae':
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
63 err = (t - f)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
64
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
65 elif metric == 'lcc':
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
66 sum_filt = np.ones((9, 9))
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
67 win_size = 81
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
68
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
69 t_sum = convolve2d(t, sum_filt, mode='same', boundary='symm')
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
70 f_sum = convolve2d(f, sum_filt, mode='same', boundary='symm')
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
71 t2_sum = convolve2d(t * t, sum_filt, mode='same', boundary='symm')
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
72 f2_sum = convolve2d(f * f, sum_filt, mode='same', boundary='symm')
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
73 tf_sum = convolve2d(t * f, sum_filt, mode='same', boundary='symm')
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
74
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
75 cross = tf_sum - f_sum * t_sum / win_size
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
76 t_var = t2_sum - t_sum * t_sum / win_size
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
77 f_var = f2_sum - f_sum * f_sum / win_size
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
78 cc = cross * cross / (t_var * f_var + 1e-5)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
79 err = 1 - cc
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
80
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
81 return err.flatten()
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
82
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
83
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
84 def read_img_as_gray(fn):
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
85 im = skimage.io.imread(fn)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
86 nDims = len(im.shape)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
87 assert nDims in [2, 3], 'this tool does not support multichannel images'
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
88 if nDims == 3:
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
89 assert im.shape[-1] in [3, 4], 'this tool does not support multichannel images'
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
90 if im.shape[-1] == 4:
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
91 im = rgba2rgb(im)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
92 im = rgb2gray(im)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
93 im = im.astype(float)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
94 im = im / np.max(im)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
95 return im
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
96
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
97
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
98 def image_registration(fn_moving, fn_fixed, fn_out, smooth_sigma=3, metric='lcc'):
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
99 moving = read_img_as_gray(fn_moving)
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
100 fixed = read_img_as_gray(fn_fixed)
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
101
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
102 moving = gaussian(moving, sigma=smooth_sigma)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
103 fixed = gaussian(fixed, sigma=smooth_sigma)
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
104
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
105 x = np.array([1, 0, 0, 0, 1, 0], dtype='float64')
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
106 result = least_squares(affine_registration, x, args=(moving, fixed, metric))
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
107
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
108 tmat = np.eye(3)
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
109 tmat[0, :] = result.x.take([0, 1, 2])
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
110 tmat[1, :] = result.x.take([3, 4, 5])
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
111
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
112 pd.DataFrame(tmat).to_csv(fn_out, header=None, index=False, sep="\t")
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
113
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
114
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
115 if __name__ == "__main__":
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
116
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
117 parser = argparse.ArgumentParser(description="Estimate the transformation matrix")
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
118 parser.add_argument("fn_moving", help="Path to the moving image")
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
119 parser.add_argument("fn_fixed", help="Path to the fixed (reference) image")
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
120 parser.add_argument("fn_tmat", help="Path to the output file for saving the transformation matrix")
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
121 parser.add_argument("sigma", type=float, help="Sigma of Gaussian filter for smoothing input images")
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
122 parser.add_argument("metric", help="Image similarity metric")
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
123 args = parser.parse_args()
1
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
124
fa769715b6b0 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
125 image_registration(args.fn_moving, args.fn_fixed, args.fn_tmat, args.sigma, args.metric)