Mercurial > repos > imgteam > landmark_registration_ls
comparison landmark_registration_ls.py @ 1:69db8c7d4244 draft default tip
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
author | imgteam |
---|---|
date | Sun, 20 Feb 2022 15:47:16 +0000 |
parents | 2f36165c49fb |
children |
comparison
equal
deleted
inserted
replaced
0:2f36165c49fb | 1:69db8c7d4244 |
---|---|
1 from scipy.linalg import lstsq | |
2 import pandas as pd | |
3 import numpy as np | |
4 import argparse | 1 import argparse |
5 | 2 |
3 import numpy as np | |
4 import pandas as pd | |
5 from scipy.linalg import lstsq | |
6 | |
7 | |
6 def landmark_registration_ls(pts_f1, pts_f2, out_f, delimiter="\t"): | 8 def landmark_registration_ls(pts_f1, pts_f2, out_f, delimiter="\t"): |
7 | 9 |
8 points1 = pd.read_csv(pts_f1, delimiter=delimiter) | 10 points1 = pd.read_csv(pts_f1, delimiter=delimiter) |
9 points2 = pd.read_csv(pts_f2, delimiter=delimiter) | 11 points2 = pd.read_csv(pts_f2, delimiter=delimiter) |
10 | 12 |
11 src = np.concatenate([np.array(points1['x']).reshape([-1,1]), | 13 src = np.concatenate([np.array(points1['x']).reshape([-1, 1]), |
12 np.array(points1['y']).reshape([-1,1])], | 14 np.array(points1['y']).reshape([-1, 1])], |
13 axis=-1) | 15 axis=-1) |
14 dst = np.concatenate([np.array(points2['x']).reshape([-1,1]), | 16 dst = np.concatenate([np.array(points2['x']).reshape([-1, 1]), |
15 np.array(points2['y']).reshape([-1,1])], | 17 np.array(points2['y']).reshape([-1, 1])], |
16 axis=-1) | 18 axis=-1) |
17 | 19 |
18 A = np.zeros((src.size,6)) | 20 A = np.zeros((src.size, 6)) |
19 A[0:src.shape[0],[0,1]] = src | 21 A[0:src.shape[0], [0, 1]] = src |
20 A[0:src.shape[0],2] = 1 | 22 A[0:src.shape[0], 2] = 1 |
21 A[src.shape[0]:,[3,4]] = src | 23 A[src.shape[0]:, [3, 4]] = src |
22 A[src.shape[0]:,5] = 1 | 24 A[src.shape[0]:, 5] = 1 |
23 b = dst.T.flatten().astype('float64') | 25 b = dst.T.flatten().astype('float64') |
24 x = lstsq(A,b) | 26 x = lstsq(A, b) |
25 | 27 |
26 tmat = np.eye(3) | 28 tmat = np.eye(3) |
27 tmat[0,:] = x[0].take([0,1,2]) | 29 tmat[0, :] = x[0].take([0, 1, 2]) |
28 tmat[1,:] = x[0].take([3,4,5]) | 30 tmat[1, :] = x[0].take([3, 4, 5]) |
29 pd.DataFrame(tmat).to_csv(out_f, header=None, index=False, sep="\t") | 31 pd.DataFrame(tmat).to_csv(out_f, header=None, index=False, sep="\t") |
30 | 32 |
31 | 33 |
32 if __name__ == "__main__": | 34 if __name__ == "__main__": |
33 | |
34 parser = argparse.ArgumentParser(description="Estimate transformation from points using least squares") | 35 parser = argparse.ArgumentParser(description="Estimate transformation from points using least squares") |
35 | |
36 parser.add_argument("fn_pts1", help="File name src points") | 36 parser.add_argument("fn_pts1", help="File name src points") |
37 parser.add_argument("fn_pts2", help="File name dst points") | 37 parser.add_argument("fn_pts2", help="File name dst points") |
38 parser.add_argument("fn_tmat", help="File name transformation matrix") | 38 parser.add_argument("fn_tmat", help="File name transformation matrix") |
39 args = parser.parse_args() | 39 args = parser.parse_args() |
40 | 40 |
41 landmark_registration_ls(args.fn_pts1, args.fn_pts2, args.fn_tmat) | 41 landmark_registration_ls(args.fn_pts1, args.fn_pts2, args.fn_tmat) |
42 | |
43 |