Mercurial > repos > imgteam > landmark_registration
annotate landmark_registration.py @ 2:4e089a0983b1 draft
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
author | imgteam |
---|---|
date | Sun, 20 Feb 2022 15:46:58 +0000 |
parents | b0503eec7bd6 |
children | aee73493bf53 |
rev | line source |
---|---|
2
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
1 """ |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
2 Copyright 2017-2022 Biomedical Computer Vision Group, Heidelberg University. |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
3 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
4 Distributed under the MIT license. |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
6 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
7 """ |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
8 |
0
a71239f3543a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit d5df0e2f37920d09b5d942a7b128041ee1f0b6f5
imgteam
parents:
diff
changeset
|
9 import argparse |
a71239f3543a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit d5df0e2f37920d09b5d942a7b128041ee1f0b6f5
imgteam
parents:
diff
changeset
|
10 |
2
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
11 import numpy as np |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
12 import pandas as pd |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
13 from scipy.linalg import lstsq |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
14 from skimage.measure import ransac |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
15 from skimage.transform import AffineTransform |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
16 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
17 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
18 def landmark_registration(pts_f1, pts_f2, out_f, res_th=None, max_ite=None, delimiter="\t"): |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
19 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
20 points1 = pd.read_csv(pts_f1, delimiter=delimiter) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
21 points2 = pd.read_csv(pts_f2, delimiter=delimiter) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
22 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
23 src = np.concatenate([np.array(points1['x']).reshape([-1, 1]), |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
24 np.array(points1['y']).reshape([-1, 1])], |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
25 axis=-1) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
26 dst = np.concatenate([np.array(points2['x']).reshape([-1, 1]), |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
27 np.array(points2['y']).reshape([-1, 1])], |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
28 axis=-1) |
0
a71239f3543a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit d5df0e2f37920d09b5d942a7b128041ee1f0b6f5
imgteam
parents:
diff
changeset
|
29 |
2
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
30 if res_th is not None and max_ite is not None: |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
31 model_robust, inliers = ransac((src, dst), AffineTransform, min_samples=3, residual_threshold=res_th, max_trials=max_ite) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
32 pd.DataFrame(model_robust.params).to_csv(out_f, header=None, index=False, sep="\t") |
0
a71239f3543a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit d5df0e2f37920d09b5d942a7b128041ee1f0b6f5
imgteam
parents:
diff
changeset
|
33 |
2
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
34 else: |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
35 A = np.zeros((src.size, 6)) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
36 A[0:src.shape[0], [0, 1]] = src |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
37 A[0:src.shape[0], 2] = 1 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
38 A[src.shape[0]:, [3, 4]] = src |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
39 A[src.shape[0]:, 5] = 1 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
40 b = dst.T.flatten().astype('float64') |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
41 x = lstsq(A, b) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
42 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
43 tmat = np.eye(3) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
44 tmat[0, :] = x[0].take([0, 1, 2]) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
45 tmat[1, :] = x[0].take([3, 4, 5]) |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
46 pd.DataFrame(tmat).to_csv(out_f, header=None, index=False, sep="\t") |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
47 |
0
a71239f3543a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit d5df0e2f37920d09b5d942a7b128041ee1f0b6f5
imgteam
parents:
diff
changeset
|
48 |
a71239f3543a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit d5df0e2f37920d09b5d942a7b128041ee1f0b6f5
imgteam
parents:
diff
changeset
|
49 if __name__ == "__main__": |
2
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
50 parser = argparse.ArgumentParser(description="Estimate affine transformation matrix based on landmark coordinates") |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
51 parser.add_argument("fn_pts1", help="Coordinates of SRC landmarks (tsv file)") |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
52 parser.add_argument("fn_pts2", help="Coordinates of DST landmarks (tsv file)") |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
53 parser.add_argument("fn_tmat", help="Path the output (transformation matrix)") |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
54 parser.add_argument("--res_th", dest="res_th", type=float, help="Maximum distance for a data point to be classified as an inlier") |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
55 parser.add_argument("--max_ite", dest="max_ite", type=int, help="Maximum number of iterations for random sample selection") |
0
a71239f3543a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit d5df0e2f37920d09b5d942a7b128041ee1f0b6f5
imgteam
parents:
diff
changeset
|
56 args = parser.parse_args() |
2
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
57 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
58 res_th = None |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
59 if args.res_th: |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
60 res_th = args.res_th |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
61 max_ite = None |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
62 if args.max_ite: |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
63 max_ite = args.max_ite |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
64 |
4e089a0983b1
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents:
1
diff
changeset
|
65 landmark_registration(args.fn_pts1, args.fn_pts2, args.fn_tmat, res_th=res_th, max_ite=max_ite) |