test_watershed.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python
  2. '''
  3. Watershed segmentation test
  4. '''
  5. # Python 2/3 compatibility
  6. from __future__ import print_function
  7. import numpy as np
  8. import cv2 as cv
  9. from tests_common import NewOpenCVTests
  10. class watershed_test(NewOpenCVTests):
  11. def test_watershed(self):
  12. img = self.get_sample('cv/inpaint/orig.png')
  13. markers = self.get_sample('cv/watershed/wshed_exp.png', 0)
  14. refSegments = self.get_sample('cv/watershed/wshed_segments.png')
  15. if img is None or markers is None:
  16. self.assertEqual(0, 1, 'Missing test data')
  17. colors = np.int32( list(np.ndindex(3, 3, 3)) ) * 122
  18. cv.watershed(img, np.int32(markers))
  19. segments = colors[np.maximum(markers, 0)]
  20. if refSegments is None:
  21. refSegments = segments.copy()
  22. cv.imwrite(self.extraTestDataPath + '/cv/watershed/wshed_segments.png', refSegments)
  23. self.assertLess(cv.norm(segments - refSegments, cv.NORM_L1) / 255.0, 50)
  24. if __name__ == '__main__':
  25. NewOpenCVTests.bootstrap()