test_morphology.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. '''
  3. Morphology operations.
  4. '''
  5. # Python 2/3 compatibility
  6. from __future__ import print_function
  7. import sys
  8. PY3 = sys.version_info[0] == 3
  9. import numpy as np
  10. import cv2 as cv
  11. from tests_common import NewOpenCVTests
  12. class morphology_test(NewOpenCVTests):
  13. def test_morphology(self):
  14. fn = 'samples/data/rubberwhale1.png'
  15. img = self.get_sample(fn)
  16. modes = ['erode/dilate', 'open/close', 'blackhat/tophat', 'gradient']
  17. str_modes = ['ellipse', 'rect', 'cross']
  18. referenceHashes = { modes[0]: '071a526425b79e45b4d0d71ef51b0562', modes[1] : '071a526425b79e45b4d0d71ef51b0562',
  19. modes[2] : '427e89f581b7df1b60a831b1ed4c8618', modes[3] : '0dd8ad251088a63d0dd022bcdc57361c'}
  20. def update(cur_mode):
  21. cur_str_mode = str_modes[0]
  22. sz = 10
  23. iters = 1
  24. opers = cur_mode.split('/')
  25. if len(opers) > 1:
  26. sz = sz - 10
  27. op = opers[sz > 0]
  28. sz = abs(sz)
  29. else:
  30. op = opers[0]
  31. sz = sz*2+1
  32. str_name = 'MORPH_' + cur_str_mode.upper()
  33. oper_name = 'MORPH_' + op.upper()
  34. st = cv.getStructuringElement(getattr(cv, str_name), (sz, sz))
  35. return cv.morphologyEx(img, getattr(cv, oper_name), st, iterations=iters)
  36. for mode in modes:
  37. res = update(mode)
  38. self.assertEqual(self.hashimg(res), referenceHashes[mode])
  39. if __name__ == '__main__':
  40. NewOpenCVTests.bootstrap()