1 # Copyright 2014 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import unittest 6 7 from telemetry.internal.util import external_modules 8 9 try: 10 np = external_modules.ImportRequiredModule('numpy') 11 except ImportError: 12 pass 13 else: 14 class CVUtilTest(unittest.TestCase): 15 def __init__(self, *args, **kwargs): 16 super(CVUtilTest, self).__init__(*args, **kwargs) 17 # Import modules with dependencies that may not be preset in test setup so 18 # that importing this unit test doesn't cause the test runner to raise an 19 # exception. 20 from telemetry.internal.image_processing import cv_util 21 self.cv_util = cv_util 22 23 def testAreLinesOrthogonalish(self): 24 l1 = np.asfarray((0, 0, 1, 0)) 25 l2 = np.asfarray((0, 0, 0, 1)) 26 self.assertTrue(self.cv_util.AreLinesOrthogonal(l1, l2, 0)) 27 self.assertTrue(self.cv_util.AreLinesOrthogonal(l2, l1, 0)) 28 self.assertFalse(self.cv_util.AreLinesOrthogonal(l1, l1, 29 np.pi / 2 - 1e-10)) 30 self.assertFalse(self.cv_util.AreLinesOrthogonal(l2, l2, 31 np.pi / 2 - 1e-10)) 32 self.assertTrue(self.cv_util.AreLinesOrthogonal(l1, l1, np.pi / 2)) 33 self.assertTrue(self.cv_util.AreLinesOrthogonal(l2, l2, np.pi / 2)) 34 35 l3 = np.asfarray((0, 0, 1, 1)) 36 l4 = np.asfarray((1, 1, 0, 0)) 37 self.assertFalse(self.cv_util.AreLinesOrthogonal(l3, l4, 38 np.pi / 2 - 1e-10)) 39 self.assertTrue(self.cv_util.AreLinesOrthogonal(l3, l1, np.pi / 4)) 40 41 l5 = np.asfarray((0, 1, 1, 0)) 42 self.assertTrue(self.cv_util.AreLinesOrthogonal(l3, l5, 0)) 43 44 def testFindLineIntersection(self): 45 l1 = np.asfarray((1, 1, 2, 1)) 46 l2 = np.asfarray((1, 1, 1, 2)) 47 ret, p = self.cv_util.FindLineIntersection(l1, l2) 48 self.assertTrue(ret) 49 self.assertTrue(np.array_equal(p, np.array([1, 1]))) 50 l3 = np.asfarray((1.1, 1, 2, 1)) 51 ret, p = self.cv_util.FindLineIntersection(l2, l3) 52 self.assertFalse(ret) 53 self.assertTrue(np.array_equal(p, np.array([1, 1]))) 54 l4 = np.asfarray((2, 1, 1, 1)) 55 l5 = np.asfarray((1, 2, 1, 1)) 56 ret, p = self.cv_util.FindLineIntersection(l4, l5) 57 self.assertTrue(ret) 58 self.assertTrue(np.array_equal(p, np.array([1, 1]))) 59 l6 = np.asfarray((1, 1, 0, 0)) 60 l7 = np.asfarray((0, 1, 1, 0)) 61 ret, p = self.cv_util.FindLineIntersection(l7, l6) 62 self.assertTrue(ret) 63 self.assertTrue(np.array_equal(p, np.array([0.5, 0.5]))) 64 l8 = np.asfarray((0, 0, 0, 1)) 65 l9 = np.asfarray((1, 0, 1, 1)) 66 ret, p = self.cv_util.FindLineIntersection(l8, l9) 67 self.assertFalse(ret) 68 self.assertTrue(np.isnan(p[0])) 69 70 def testExtendLines(self): 71 l1 = (-1, 0, 1, 0) 72 l2 = (0, -1, 0, 1) 73 l3 = (4, 4, 6, 6) 74 l4 = (1, 1, 1, 1) 75 lines = self.cv_util.ExtendLines(np.asfarray([l1, l2, l3, l4], 76 dtype=np.float64), 10) 77 lines = np.around(lines, 10) 78 expected0 = ((5.0, 0.0, -5.0, 0.0)) 79 self.assertAlmostEqual(np.sum(np.abs(np.subtract(lines[0], expected0))), 80 0.0, 7) 81 expected1 = ((0.0, 5.0, 0.0, -5.0)) 82 self.assertAlmostEqual(np.sum(np.abs(np.subtract(lines[1], expected1))), 83 0.0, 7) 84 85 off = np.divide(np.sqrt(50), 2, dtype=np.float64) 86 expected2 = ((5 + off, 5 + off, 5 - off, 5 - off)) 87 self.assertAlmostEqual(np.sum(np.abs(np.subtract(lines[2], expected2))), 88 0.0, 7) 89 expected3 = ((-4, 1, 6, 1)) 90 self.assertAlmostEqual(np.sum(np.abs(np.subtract(lines[3], expected3))), 91 0.0, 7) 92 93 def testIsPointApproxOnLine(self): 94 p1 = np.asfarray((-1, -1)) 95 l1 = np.asfarray((0, 0, 100, 100)) 96 p2 = np.asfarray((1, 2)) 97 p3 = np.asfarray((2, 1)) 98 p4 = np.asfarray((3, 1)) 99 self.assertTrue(self.cv_util.IsPointApproxOnLine(p1, l1, 1 + 1e-7)) 100 self.assertTrue(self.cv_util.IsPointApproxOnLine(p2, l1, 1 + 1e-7)) 101 self.assertTrue(self.cv_util.IsPointApproxOnLine(p3, l1, 1 + 1e-7)) 102 self.assertFalse(self.cv_util.IsPointApproxOnLine(p4, l1, 1 + 1e-7)) 103 104 def testSqDistances(self): 105 p1 = np.array([[0, 2], [0, 3]]) 106 p2 = np.array([2, 0]) 107 dists = self.cv_util.SqDistance(p1, p2) 108 self.assertEqual(dists[0], 8) 109 self.assertEqual(dists[1], 13) 110 111 def testSqDistance(self): 112 p1 = np.array([0, 2]) 113 p2 = np.array([2, 0]) 114 self.assertEqual(self.cv_util.SqDistance(p1, p2), 8) 115