mser.py 945 Bytes
Newer Older
1
#!/usr/bin/env python
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16
'''
MSER detector demo
==================

Usage:
------
    mser.py [<video source>]

Keys:
-----
    ESC   - exit

'''

17 18 19
# Python 2/3 compatibility
from __future__ import print_function

20
import numpy as np
21
import cv2 as cv
22

23
import video
24
import sys
25

26
def main():
27 28 29 30
    try:
        video_src = sys.argv[1]
    except:
        video_src = 0
31 32

    cam = video.create_capture(video_src)
33
    mser = cv.MSER_create()
34

35 36
    while True:
        ret, img = cam.read()
37 38
        if ret == 0:
            break
39
        gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
40 41
        vis = img.copy()

42
        regions, _ = mser.detectRegions(gray)
43 44
        hulls = [cv.convexHull(p.reshape(-1, 1, 2)) for p in regions]
        cv.polylines(vis, hulls, 1, (0, 255, 0))
45

46 47
        cv.imshow('img', vis)
        if cv.waitKey(5) == 27:
48
            break
49 50 51 52 53 54 55

    print('Done')


if __name__ == '__main__':
    print(__doc__)
    main()
56
    cv.destroyAllWindows()