annotate image_registration_affine.py @ 0:e34222a620d4 draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
author imgteam
date Wed, 30 Dec 2020 20:24:35 +0000
parents
children fa769715b6b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
1 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
2 from skimage.transform import ProjectiveTransform
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
3 from skimage.filters import gaussian
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
4 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
5 from scipy.optimize import least_squares
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
6 import numpy as np
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
7 import pandas as pd
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
8 import argparse
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
9
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
10
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
11
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
12 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
13 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
14 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
15 else:
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
16 a[:] = b
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
17
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
18
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 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
21 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
22 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
23 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
24 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
25 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
26
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
27 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
28
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
29 for i in range(0, (tf_coords.shape[0]//batch_size+1)):
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
30 tf_coords[batch_size*i:batch_size*(i+1)] = coord_map(tf_coords[batch_size*i:batch_size*(i+1)])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
31 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
32
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
33 _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
34 _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
35 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
36 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
37
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
38 return coords
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
39
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
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
42 def affine_registration(params,moving,fixed):
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
43 tmat = np.eye(3)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
44 tmat[0,:] = params.take([0,1,2])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
45 tmat[1,:] = params.take([3,4,5])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
46
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
47 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
48 warped_coords = warp_coords_batch(trans, fixed.shape)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
49 t = map_coordinates(moving, warped_coords, mode='reflect')
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
50
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
51 eI = (t - fixed)**2
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
52 return eI.flatten()
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
53
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
54
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
55
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
56 def image_registration(fn_moving, fn_fixed, fn_out, smooth_sigma=1):
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
57 moving = skimage.io.imread(fn_moving,as_gray=True)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
58 fixed = skimage.io.imread(fn_fixed,as_gray=True)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
59
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
60 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
61 fixed = gaussian(fixed, 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
62
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
63 x = np.array([1, 0, 0, 0, 1, 0],dtype='float64')
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
64 result = least_squares(affine_registration, x, args=(moving,fixed))
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
65
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
66 tmat = np.eye(3)
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
67 tmat[0,:] = result.x.take([0,1,2])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
68 tmat[1,:] = result.x.take([3,4,5])
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
69
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
70 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
71
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
72
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
73
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
74 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
75
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
76 parser = argparse.ArgumentParser(description="Estimate the transformation matrix")
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
77 parser.add_argument("fn_moving", help="Name of the moving image.png")
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
78 parser.add_argument("fn_fixed", help="Name of the fixed image.png")
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
79 parser.add_argument("fn_tmat", help="Name of output file to save the transformation matrix")
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
80 args = parser.parse_args()
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
81
e34222a620d4 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
82 image_registration(args.fn_moving, args.fn_fixed, args.fn_tmat)