mser.py 945 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. '''
  3. MSER detector demo
  4. ==================
  5. Usage:
  6. ------
  7. mser.py [<video source>]
  8. Keys:
  9. -----
  10. ESC - exit
  11. '''
  12. # Python 2/3 compatibility
  13. from __future__ import print_function
  14. import numpy as np
  15. import cv2 as cv
  16. import video
  17. import sys
  18. def main():
  19. try:
  20. video_src = sys.argv[1]
  21. except:
  22. video_src = 0
  23. cam = video.create_capture(video_src)
  24. mser = cv.MSER_create()
  25. while True:
  26. ret, img = cam.read()
  27. if ret == 0:
  28. break
  29. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  30. vis = img.copy()
  31. regions, _ = mser.detectRegions(gray)
  32. hulls = [cv.convexHull(p.reshape(-1, 1, 2)) for p in regions]
  33. cv.polylines(vis, hulls, 1, (0, 255, 0))
  34. cv.imshow('img', vis)
  35. if cv.waitKey(5) == 27:
  36. break
  37. print('Done')
  38. if __name__ == '__main__':
  39. print(__doc__)
  40. main()
  41. cv.destroyAllWindows()