3 # 2009-01-16, Xavier Delacour <xavier.delacour@gmail.com>
7 from numpy.linalg import *;
12 from adaptors import *;
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]]
19 class homography_test(unittest.TestCase):
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));
26 def test_ransac_0_outliers(self):
27 pts1 = random.rand(100,2);
28 H1 = asmatrix(random.rand(3,3));
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))
34 def test_ransac_30_outliers(self):
35 pts1 = random.rand(100,2);
36 H1 = asmatrix(random.rand(3,3));
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))
43 def test_ransac_70_outliers(self):
44 pts1 = random.rand(100,2);
45 H1 = asmatrix(random.rand(3,3));
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))
52 def test_ransac_90_outliers(self):
53 pts1 = random.rand(100,2);
54 H1 = asmatrix(random.rand(3,3));
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))
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));
66 def test_lmeds_0_outliers(self):
67 pts1 = random.rand(100,2);
68 H1 = asmatrix(random.rand(3,3));
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))
74 def test_lmeds_30_outliers(self):
75 pts1 = random.rand(100,2);
76 H1 = asmatrix(random.rand(3,3));
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))
83 def test_lmeds_70_outliers(self):
84 pts1 = random.rand(100,2);
85 H1 = asmatrix(random.rand(3,3));
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))
93 return unittest.TestLoader().loadTestsFromTestCase(homography_test)
95 if __name__ == '__main__':
96 unittest.TextTestRunner(verbosity=2).run(suite())