Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/python/test/test_mser.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
MSER detector test
5
'''
6
# Python 2/3 compatibility
7
from __future__ import print_function
8
9
import numpy as np
10
import cv2 as cv
11
12
from tests_common import NewOpenCVTests
13
14
class mser_test(NewOpenCVTests):
15
def test_mser(self):
16
17
img = self.get_sample('cv/mser/puzzle.png', 0)
18
smallImg = [
19
[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],
20
[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],
21
[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],
22
[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],
23
[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],
24
[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],
25
[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],
26
[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],
27
[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],
28
[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],
29
[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],
30
[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],
31
[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],
32
[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]
33
]
34
thresharr = [ 0, 70, 120, 180, 255 ]
35
kDelta = 5
36
mserExtractor = cv.MSER_create()
37
mserExtractor.setDelta(kDelta)
38
np.random.seed(10)
39
40
for _i in range(100):
41
42
use_big_image = int(np.random.rand(1,1)*7) != 0
43
invert = int(np.random.rand(1,1)*2) != 0
44
binarize = int(np.random.rand(1,1)*5) != 0 if use_big_image else False
45
blur = int(np.random.rand(1,1)*2) != 0
46
thresh = thresharr[int(np.random.rand(1,1)*5)]
47
src0 = img if use_big_image else np.array(smallImg).astype('uint8')
48
src = src0.copy()
49
50
kMinArea = 256 if use_big_image else 10
51
kMaxArea = int(src.shape[0]*src.shape[1]/4)
52
53
mserExtractor.setMinArea(kMinArea)
54
mserExtractor.setMaxArea(kMaxArea)
55
if invert:
56
cv.bitwise_not(src, src)
57
if binarize:
58
_, src = cv.threshold(src, thresh, 255, cv.THRESH_BINARY)
59
if blur:
60
src = cv.GaussianBlur(src, (5, 5), 1.5, 1.5)
61
minRegs = 7 if use_big_image else 2
62
maxRegs = 1000 if use_big_image else 20
63
if binarize and (thresh == 0 or thresh == 255):
64
minRegs = maxRegs = 0
65
msers, boxes = mserExtractor.detectRegions(src)
66
nmsers = len(msers)
67
self.assertEqual(nmsers, len(boxes))
68
self.assertLessEqual(minRegs, nmsers)
69
self.assertGreaterEqual(maxRegs, nmsers)
70
71
if __name__ == '__main__':
72
NewOpenCVTests.bootstrap()
73
74