2d2ed58381c749e8f4e938fcdb1b1f68229de0f4
[opencv] / tests / swig_python / homography_tests.py
1 #!/usr/bin/env python
2
3 # 2009-01-16, Xavier Delacour <xavier.delacour@gmail.com>
4
5 import unittest
6 from numpy import *;
7 from numpy.linalg import *;
8 import sys;
9
10 import cvtestutils
11 from cv import *;
12 from adaptors import *;
13
14 def transform(H,x):
15     x1 = H * asmatrix(r_[x[0],x[1],1]).transpose()
16     x1 = asarray(x1).flatten()
17     return r_[x1[0]/x1[2],x1[1]/x1[2]]
18
19 class homography_test(unittest.TestCase):
20
21     def test_ransac_identity(self):
22         pts1 = random.rand(100,2);
23         result,H = cvFindHomography(pts1, pts1, CV_RANSAC, 1e-5);
24         assert(result and all(abs(Ipl2NumPy(H) - eye(3)) < 1e-5));
25
26     def test_ransac_0_outliers(self):
27         pts1 = random.rand(100,2);
28         H1 = asmatrix(random.rand(3,3));
29         H1 = H1 / H1[2,2]
30         pts2 = [transform(H1,x) for x in pts1]
31         result,H = cvFindHomography(pts1, pts2, CV_RANSAC, 1e-5);
32         assert(result and all(abs(H1-H)<1e-5))
33
34     def test_ransac_30_outliers(self):
35         pts1 = random.rand(100,2);
36         H1 = asmatrix(random.rand(3,3));
37         H1 = H1 / H1[2,2]
38         pts2 = [transform(H1,x) for x in pts1]
39         pts2[0:30] = random.rand(30,2)
40         result,H = cvFindHomography(pts1, pts2, CV_RANSAC, 1e-5);
41         assert(result and all(abs(H1-H)<1e-5))
42
43     def test_ransac_70_outliers(self):
44         pts1 = random.rand(100,2);
45         H1 = asmatrix(random.rand(3,3));
46         H1 = H1 / H1[2,2]
47         pts2 = [transform(H1,x) for x in pts1]
48         pts2[0:70] = random.rand(70,2)
49         result,H = cvFindHomography(pts1, pts2, CV_RANSAC, 1e-5);
50         assert(result and all(abs(H1-H)<1e-5))
51
52     def test_ransac_90_outliers(self):
53         pts1 = random.rand(100,2);
54         H1 = asmatrix(random.rand(3,3));
55         H1 = H1 / H1[2,2]
56         pts2 = [transform(H1,x) for x in pts1]
57         pts2[0:90] = random.rand(90,2)
58         result,H = cvFindHomography(pts1, pts2, CV_RANSAC, 1e-5);
59         assert(not result or not all(abs(H1-H)<1e-5))
60
61     def test_lmeds_identity(self):
62         pts1 = random.rand(100,2);
63         result,H = cvFindHomography(pts1, pts1, CV_LMEDS);
64         assert(result and all(abs(Ipl2NumPy(H) - eye(3)) < 1e-5));
65
66     def test_lmeds_0_outliers(self):
67         pts1 = random.rand(100,2);
68         H1 = asmatrix(random.rand(3,3));
69         H1 = H1 / H1[2,2]
70         pts2 = [transform(H1,x) for x in pts1]
71         result,H = cvFindHomography(pts1, pts2, CV_LMEDS);
72         assert(result and all(abs(H1-H)<1e-5))
73
74     def test_lmeds_30_outliers(self):
75         pts1 = random.rand(100,2);
76         H1 = asmatrix(random.rand(3,3));
77         H1 = H1 / H1[2,2]
78         pts2 = [transform(H1,x) for x in pts1]
79         pts2[0:30] = random.rand(30,2)
80         result,H = cvFindHomography(pts1, pts2, CV_LMEDS);
81         assert(result and all(abs(H1-H)<1e-5))
82
83     def test_lmeds_70_outliers(self):
84         pts1 = random.rand(100,2);
85         H1 = asmatrix(random.rand(3,3));
86         H1 = H1 / H1[2,2]
87         pts2 = vstack([transform(H1,x) for x in pts1])
88         pts2[0:70] = random.rand(70,2)
89         result,H = cvFindHomography(pts1, pts2, CV_LMEDS);
90         assert(not result or not all(abs(H1-H)<1e-5))
91
92 def suite():
93     return unittest.TestLoader().loadTestsFromTestCase(homography_test)
94
95 if __name__ == '__main__':
96     unittest.TextTestRunner(verbosity=2).run(suite())
97