test_mser.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python
  2. '''
  3. MSER detector 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 mser_test(NewOpenCVTests):
  11. def test_mser(self):
  12. img = self.get_sample('cv/mser/puzzle.png', 0)
  13. smallImg = [
  14. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
  15. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
  16. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
  17. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
  18. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
  19. [255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255],
  20. [255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255],
  21. [255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255],
  22. [255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255],
  23. [255, 255, 255, 255, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255, 255, 255, 255],
  24. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
  25. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
  26. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
  27. [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
  28. ]
  29. thresharr = [ 0, 70, 120, 180, 255 ]
  30. kDelta = 5
  31. mserExtractor = cv.MSER_create()
  32. mserExtractor.setDelta(kDelta)
  33. np.random.seed(10)
  34. for _i in range(100):
  35. use_big_image = int(np.random.rand(1,1)*7) != 0
  36. invert = int(np.random.rand(1,1)*2) != 0
  37. binarize = int(np.random.rand(1,1)*5) != 0 if use_big_image else False
  38. blur = int(np.random.rand(1,1)*2) != 0
  39. thresh = thresharr[int(np.random.rand(1,1)*5)]
  40. src0 = img if use_big_image else np.array(smallImg).astype('uint8')
  41. src = src0.copy()
  42. kMinArea = 256 if use_big_image else 10
  43. kMaxArea = int(src.shape[0]*src.shape[1]/4)
  44. mserExtractor.setMinArea(kMinArea)
  45. mserExtractor.setMaxArea(kMaxArea)
  46. if invert:
  47. cv.bitwise_not(src, src)
  48. if binarize:
  49. _, src = cv.threshold(src, thresh, 255, cv.THRESH_BINARY)
  50. if blur:
  51. src = cv.GaussianBlur(src, (5, 5), 1.5, 1.5)
  52. minRegs = 7 if use_big_image else 2
  53. maxRegs = 1000 if use_big_image else 20
  54. if binarize and (thresh == 0 or thresh == 255):
  55. minRegs = maxRegs = 0
  56. msers, boxes = mserExtractor.detectRegions(src)
  57. nmsers = len(msers)
  58. self.assertEqual(nmsers, len(boxes))
  59. self.assertLessEqual(minRegs, nmsers)
  60. self.assertGreaterEqual(maxRegs, nmsers)
  61. if __name__ == '__main__':
  62. NewOpenCVTests.bootstrap()