annotate image_registration_affine.py @ 0:c717e998453e 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:12 +0000
parents
children 704881eab59f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c717e998453e "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
c717e998453e "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
c717e998453e "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
c717e998453e "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
c717e998453e "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
c717e998453e "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
c717e998453e "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
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
8 import argparse
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
9
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
10
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
11
c717e998453e "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):
c717e998453e "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:
c717e998453e "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]
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
15 else:
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
16 a[:] = b
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
17
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
18
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 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
21 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
22 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
23 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
24 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
25 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
26
c717e998453e "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
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
28
c717e998453e "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)):
c717e998453e "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)])
c717e998453e "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)
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
32
c717e998453e "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, ...])
c717e998453e "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, ...])
c717e998453e "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:
c717e998453e "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])
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
37
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
38 return coords
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
39
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
c717e998453e "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):
c717e998453e "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)
c717e998453e "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])
c717e998453e "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])
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
46
c717e998453e "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)
c717e998453e "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)
c717e998453e "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')
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
50
c717e998453e "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
c717e998453e "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()
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
53
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
54
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
55
c717e998453e "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):
c717e998453e "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)
c717e998453e "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)
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
59
c717e998453e "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)
c717e998453e "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)
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
62
c717e998453e "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')
c717e998453e "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))
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
65
c717e998453e "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)
c717e998453e "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])
c717e998453e "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])
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
69
c717e998453e "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")
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
71
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
72
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
73
c717e998453e "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__":
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
75
c717e998453e "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")
c717e998453e "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")
c717e998453e "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")
c717e998453e "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")
c717e998453e "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()
c717e998453e "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/image_registration_affine/ commit 79c2fd560fce8ded4d7f7fe97e876871794e2f9d"
imgteam
parents:
diff changeset
81
c717e998453e "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)