Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/features2d/test/test_mser.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
45
namespace opencv_test { namespace {
46
47
#undef RENDER_MSERS
48
#define RENDER_MSERS 0
49
50
#if defined RENDER_MSERS && RENDER_MSERS
51
static void renderMSERs(const Mat& gray, Mat& img, const vector<vector<Point> >& msers)
52
{
53
cvtColor(gray, img, COLOR_GRAY2BGR);
54
RNG rng((uint64)1749583);
55
for( int i = 0; i < (int)msers.size(); i++ )
56
{
57
uchar b = rng.uniform(0, 256);
58
uchar g = rng.uniform(0, 256);
59
uchar r = rng.uniform(0, 256);
60
Vec3b color(b, g, r);
61
62
const Point* pt = &msers[i][0];
63
size_t j, n = msers[i].size();
64
for( j = 0; j < n; j++ )
65
img.at<Vec3b>(pt[j]) = color;
66
}
67
}
68
#endif
69
70
TEST(Features2d_MSER, cases)
71
{
72
uchar buf[] =
73
{
74
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,
75
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,
76
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,
77
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,
78
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,
79
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,
80
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,
81
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,
82
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,
83
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,
84
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,
85
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,
86
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,
87
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
88
};
89
Mat big_image = imread(cvtest::TS::ptr()->get_data_path() + "mser/puzzle.png", 0);
90
Mat small_image(14, 26, CV_8U, buf);
91
static const int thresharr[] = { 0, 70, 120, 180, 255 };
92
93
const int kDelta = 5;
94
Ptr<MSER> mserExtractor = MSER::create( kDelta );
95
vector<vector<Point> > msers;
96
vector<Rect> boxes;
97
98
RNG rng((uint64)123456);
99
100
for( int i = 0; i < 100; i++ )
101
{
102
bool use_big_image = rng.uniform(0, 7) != 0;
103
bool invert = rng.uniform(0, 2) != 0;
104
bool binarize = use_big_image ? rng.uniform(0, 5) != 0 : false;
105
bool blur = rng.uniform(0, 2) != 0;
106
int thresh = thresharr[rng.uniform(0, 5)];
107
108
/*if( i == 0 )
109
{
110
use_big_image = true;
111
invert = binarize = blur = false;
112
}*/
113
114
const Mat& src0 = use_big_image ? big_image : small_image;
115
Mat src = src0.clone();
116
117
int kMinArea = use_big_image ? 256 : 10;
118
int kMaxArea = (int)src.total()/4;
119
120
mserExtractor->setMinArea(kMinArea);
121
mserExtractor->setMaxArea(kMaxArea);
122
123
if( invert )
124
bitwise_not(src, src);
125
if( binarize )
126
cv::threshold(src, src, thresh, 255, THRESH_BINARY);
127
if( blur )
128
GaussianBlur(src, src, Size(5, 5), 1.5, 1.5);
129
130
int minRegs = use_big_image ? 7 : 2;
131
int maxRegs = use_big_image ? 1000 : 20;
132
if( binarize && (thresh == 0 || thresh == 255) )
133
minRegs = maxRegs = 0;
134
135
mserExtractor->detectRegions( src, msers, boxes );
136
int nmsers = (int)msers.size();
137
ASSERT_EQ(nmsers, (int)boxes.size());
138
139
if( maxRegs < nmsers || minRegs > nmsers )
140
{
141
printf("%d. minArea=%d, maxArea=%d, nmsers=%d, minRegs=%d, maxRegs=%d, "
142
"image=%s, invert=%d, binarize=%d, thresh=%d, blur=%d\n",
143
i, kMinArea, kMaxArea, nmsers, minRegs, maxRegs, use_big_image ? "big" : "small",
144
(int)invert, (int)binarize, thresh, (int)blur);
145
#if defined RENDER_MSERS && RENDER_MSERS
146
Mat image;
147
imshow("source", src);
148
renderMSERs(src, image, msers);
149
imshow("result", image);
150
waitKey();
151
#endif
152
}
153
154
ASSERT_LE(minRegs, nmsers);
155
ASSERT_GE(maxRegs, nmsers);
156
}
157
}
158
159
TEST(Features2d_MSER, history_update_regression)
160
{
161
String dataPath = cvtest::TS::ptr()->get_data_path() + "mser/";
162
vector<Mat> tstImages;
163
tstImages.push_back(imread(dataPath + "mser_test.png", IMREAD_GRAYSCALE));
164
tstImages.push_back(imread(dataPath + "mser_test2.png", IMREAD_GRAYSCALE));
165
166
for(size_t j = 0; j < tstImages.size(); j++)
167
{
168
size_t previous_size = 0;
169
for(int minArea = 100; minArea > 10; minArea--)
170
{
171
Ptr<MSER> mser = MSER::create(1, minArea, (int)(tstImages[j].cols * tstImages[j].rows * 0.2));
172
mser->setPass2Only(true);
173
vector<vector<Point> > mserContours;
174
vector<Rect> boxRects;
175
mser->detectRegions(tstImages[j], mserContours, boxRects);
176
ASSERT_LE(previous_size, mserContours.size());
177
previous_size = mserContours.size();
178
}
179
}
180
}
181
182
}} // namespace
183
184