annotate image_registration_affine.py @ 2:16eedbaba295 draft default tip

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit ded1703bcd6e295b26201505edb58381991b265e"
author imgteam
date Mon, 28 Feb 2022 17:34:34 +0000
parents 704881eab59f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
1 """
704881eab59f "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.
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
3
704881eab59f "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.
704881eab59f "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
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
6
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
7 """
704881eab59f "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
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
9
704881eab59f "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
704881eab59f "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
c717e998453e "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
c717e998453e "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
c717e998453e "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
704881eab59f "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
704881eab59f "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
704881eab59f "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
704881eab59f "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
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
19
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
20
c717e998453e "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):
c717e998453e "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:
c717e998453e "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]
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
24 else:
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
25 a[:] = b
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
26
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
27
c717e998453e "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):
c717e998453e "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]
c717e998453e "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]
c717e998453e "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:
c717e998453e "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])
c717e998453e "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)
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
34
c717e998453e "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
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
36
1
704881eab59f "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)):
704881eab59f "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
c717e998453e "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)
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
40
c717e998453e "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, ...])
c717e998453e "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, ...])
c717e998453e "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:
c717e998453e "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])
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
45
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
46 return coords
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
47
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
48
1
704881eab59f "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
c717e998453e "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
704881eab59f "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])
704881eab59f "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])
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
53
0
c717e998453e "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)
c717e998453e "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
704881eab59f "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')
704881eab59f "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
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
58
704881eab59f "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':
704881eab59f "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
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
61
704881eab59f "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':
704881eab59f "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)
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
64
704881eab59f "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':
704881eab59f "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))
704881eab59f "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
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
68
704881eab59f "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')
704881eab59f "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')
704881eab59f "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')
704881eab59f "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')
704881eab59f "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')
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
74
704881eab59f "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
704881eab59f "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
704881eab59f "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
704881eab59f "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)
704881eab59f "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
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
80
704881eab59f "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
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
82
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
83
1
704881eab59f "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):
704881eab59f "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)
704881eab59f "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)
704881eab59f "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'
704881eab59f "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:
704881eab59f "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'
704881eab59f "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:
704881eab59f "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)
704881eab59f "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)
704881eab59f "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)
704881eab59f "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)
704881eab59f "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
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
96
1
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
97
704881eab59f "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'):
704881eab59f "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)
704881eab59f "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
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
101
c717e998453e "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)
c717e998453e "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
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
104
704881eab59f "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')
704881eab59f "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))
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
107
0
c717e998453e "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
704881eab59f "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])
704881eab59f "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])
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
111
2
16eedbaba295 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit ded1703bcd6e295b26201505edb58381991b265e"
imgteam
parents: 1
diff changeset
112 tmat = np.linalg.inv(tmat)
16eedbaba295 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit ded1703bcd6e295b26201505edb58381991b265e"
imgteam
parents: 1
diff changeset
113
0
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
114 pd.DataFrame(tmat).to_csv(fn_out, header=None, index=False, sep="\t")
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
115
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
116
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
117 if __name__ == "__main__":
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
118
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
119 parser = argparse.ArgumentParser(description="Estimate the transformation matrix")
1
704881eab59f "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_moving", help="Path to the moving image")
704881eab59f "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("fn_fixed", help="Path to the fixed (reference) image")
704881eab59f "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("fn_tmat", help="Path to the output file for saving the transformation matrix")
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
123 parser.add_argument("sigma", type=float, help="Sigma of Gaussian filter for smoothing input images")
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
124 parser.add_argument("metric", help="Image similarity metric")
0
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
125 args = parser.parse_args()
1
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
126
704881eab59f "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit e82400162e337b36c29d6e79fb2deb9871475397"
imgteam
parents: 0
diff changeset
127 image_registration(args.fn_moving, args.fn_fixed, args.fn_tmat, args.sigma, args.metric)