test_shape.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python
  2. # Python 2/3 compatibility
  3. from __future__ import print_function
  4. import os
  5. import cv2 as cv
  6. from tests_common import NewOpenCVTests
  7. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  8. MODULE_DIR = os.path.join(SCRIPT_DIR, '../../../')
  9. class shape_test(NewOpenCVTests):
  10. def test_computeDistance(self):
  11. a = cv.imread(os.path.join(MODULE_DIR, 'samples/data/shape_sample/1.png'), cv.IMREAD_GRAYSCALE)
  12. b = cv.imread(os.path.join(MODULE_DIR, 'samples/data/shape_sample/2.png'), cv.IMREAD_GRAYSCALE)
  13. if a is None or b is None:
  14. raise unittest.SkipTest("Missing files with test data")
  15. ca, _ = cv.findContours(a, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS)
  16. cb, _ = cv.findContours(b, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS)
  17. hd = cv.createHausdorffDistanceExtractor()
  18. sd = cv.createShapeContextDistanceExtractor()
  19. d1 = hd.computeDistance(ca[0], cb[0])
  20. d2 = sd.computeDistance(ca[0], cb[0])
  21. self.assertAlmostEqual(d1, 26.4196891785, 3, "HausdorffDistanceExtractor")
  22. self.assertAlmostEqual(d2, 0.25804194808, 3, "ShapeContextDistanceExtractor")
  23. if __name__ == '__main__':
  24. NewOpenCVTests.bootstrap()