Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_houghcircles.cpp
16339 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
// Copyright (C) 2014, Itseez, Inc, all rights reserved.
16
// Third party copyrights are property of their respective owners.
17
//
18
// Redistribution and use in source and binary forms, with or without modification,
19
// are permitted provided that the following conditions are met:
20
//
21
// * Redistribution's of source code must retain the above copyright notice,
22
// this list of conditions and the following disclaimer.
23
//
24
// * Redistribution's in binary form must reproduce the above copyright notice,
25
// this list of conditions and the following disclaimer in the documentation
26
// and/or other materials provided with the distribution.
27
//
28
// * The name of the copyright holders may not be used to endorse or promote products
29
// derived from this software without specific prior written permission.
30
//
31
// This software is provided by the copyright holders and contributors "as is" and
32
// any express or implied warranties, including, but not limited to, the implied
33
// warranties of merchantability and fitness for a particular purpose are disclaimed.
34
// In no event shall the Intel Corporation or contributors be liable for any direct,
35
// indirect, incidental, special, exemplary, or consequential damages
36
// (including, but not limited to, procurement of substitute goods or services;
37
// loss of use, data, or profits; or business interruption) however caused
38
// and on any theory of liability, whether in contract, strict liability,
39
// or tort (including negligence or otherwise) arising in any way out of
40
// the use of this software, even if advised of the possibility of such damage.
41
//
42
//M*/
43
44
#include "test_precomp.hpp"
45
46
namespace opencv_test { namespace {
47
48
#ifndef DEBUG_IMAGES
49
#define DEBUG_IMAGES 0
50
#endif
51
52
//#define GENERATE_DATA // generate data in debug mode via CPU code path (without IPP / OpenCL and other accelerators)
53
54
using namespace cv;
55
using namespace std;
56
57
static string getTestCaseName(const string& picture_name, double minDist, double edgeThreshold, double accumThreshold, int minRadius, int maxRadius)
58
{
59
string results_name = format("circles_%s_%.0f_%.0f_%.0f_%d_%d",
60
picture_name.c_str(), minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
61
string temp(results_name);
62
size_t pos = temp.find_first_of("\\/.");
63
while (pos != string::npos) {
64
temp.replace(pos, 1, "_");
65
pos = temp.find_first_of("\\/.");
66
}
67
return temp;
68
}
69
70
#if DEBUG_IMAGES
71
static void highlightCircles(const string& imagePath, const vector<Vec3f>& circles, const string& outputImagePath)
72
{
73
Mat imgDebug = imread(imagePath, IMREAD_COLOR);
74
const Scalar yellow(0, 255, 255);
75
76
for (vector<Vec3f>::const_iterator iter = circles.begin(); iter != circles.end(); ++iter)
77
{
78
const Vec3f& circle = *iter;
79
float x = circle[0];
80
float y = circle[1];
81
float r = max(circle[2], 2.0f);
82
cv::circle(imgDebug, Point(int(x), int(y)), int(r), yellow);
83
}
84
imwrite(outputImagePath, imgDebug);
85
}
86
#endif
87
88
typedef tuple<string, double, double, double, int, int> Image_MinDist_EdgeThreshold_AccumThreshold_MinRadius_MaxRadius_t;
89
class HoughCirclesTestFixture : public testing::TestWithParam<Image_MinDist_EdgeThreshold_AccumThreshold_MinRadius_MaxRadius_t>
90
{
91
string picture_name;
92
double minDist;
93
double edgeThreshold;
94
double accumThreshold;
95
int minRadius;
96
int maxRadius;
97
98
public:
99
HoughCirclesTestFixture()
100
{
101
picture_name = get<0>(GetParam());
102
minDist = get<1>(GetParam());
103
edgeThreshold = get<2>(GetParam());
104
accumThreshold = get<3>(GetParam());
105
minRadius = get<4>(GetParam());
106
maxRadius = get<5>(GetParam());
107
}
108
109
HoughCirclesTestFixture(const string& picture, double minD, double edge, double accum, int minR, int maxR) :
110
picture_name(picture), minDist(minD), edgeThreshold(edge), accumThreshold(accum), minRadius(minR), maxRadius(maxR)
111
{
112
}
113
114
template <typename CircleType>
115
void run_test(const char* xml_name)
116
{
117
string test_case_name = getTestCaseName(picture_name, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
118
string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
119
Mat src = imread(filename, IMREAD_GRAYSCALE);
120
EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
121
122
GaussianBlur(src, src, Size(9, 9), 2, 2);
123
124
vector<CircleType> circles;
125
const double dp = 1.0;
126
HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
127
128
string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
129
#if DEBUG_IMAGES
130
highlightCircles(filename, circles, imgProc + test_case_name + ".png");
131
#endif
132
133
string xml = imgProc + xml_name;
134
#ifdef GENERATE_DATA
135
{
136
FileStorage fs(xml, FileStorage::READ);
137
ASSERT_TRUE(!fs.isOpened() || fs[test_case_name].empty());
138
}
139
{
140
FileStorage fs(xml, FileStorage::APPEND);
141
EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
142
fs << test_case_name << circles;
143
}
144
#else
145
FileStorage fs(xml, FileStorage::READ);
146
FileNode node = fs[test_case_name];
147
ASSERT_FALSE(node.empty()) << "Missing test data: " << test_case_name << std::endl << "XML: " << xml;
148
vector<CircleType> exp_circles;
149
read(fs[test_case_name], exp_circles, vector<CircleType>());
150
fs.release();
151
EXPECT_EQ(exp_circles.size(), circles.size());
152
#endif
153
}
154
};
155
156
TEST_P(HoughCirclesTestFixture, regression)
157
{
158
run_test<Vec3f>("HoughCircles.xml");
159
}
160
161
TEST_P(HoughCirclesTestFixture, regression4f)
162
{
163
run_test<Vec4f>("HoughCircles4f.xml");
164
}
165
166
INSTANTIATE_TEST_CASE_P(ImgProc, HoughCirclesTestFixture, testing::Combine(
167
// picture_name:
168
testing::Values("imgproc/stuff.jpg"),
169
// minDist:
170
testing::Values(20),
171
// edgeThreshold:
172
testing::Values(20),
173
// accumThreshold:
174
testing::Values(30),
175
// minRadius:
176
testing::Values(20),
177
// maxRadius:
178
testing::Values(200)
179
));
180
181
TEST(HoughCirclesTest, DefaultMaxRadius)
182
{
183
string picture_name = "imgproc/stuff.jpg";
184
const double dp = 1.0;
185
double minDist = 20;
186
double edgeThreshold = 20;
187
double accumThreshold = 30;
188
int minRadius = 20;
189
int maxRadius = 0;
190
191
string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
192
Mat src = imread(filename, IMREAD_GRAYSCALE);
193
EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
194
195
GaussianBlur(src, src, Size(9, 9), 2, 2);
196
197
vector<Vec3f> circles;
198
vector<Vec4f> circles4f;
199
HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
200
HoughCircles(src, circles4f, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
201
202
#if DEBUG_IMAGES
203
string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
204
highlightCircles(filename, circles, imgProc + "HoughCirclesTest_DefaultMaxRadius.png");
205
#endif
206
207
int maxDimension = std::max(src.rows, src.cols);
208
209
EXPECT_GT(circles.size(), size_t(0)) << "Should find at least some circles";
210
for (size_t i = 0; i < circles.size(); ++i)
211
{
212
EXPECT_GE(circles[i][2], minRadius) << "Radius should be >= minRadius";
213
EXPECT_LE(circles[i][2], maxDimension) << "Radius should be <= max image dimension";
214
}
215
}
216
217
TEST(HoughCirclesTest, CentersOnly)
218
{
219
string picture_name = "imgproc/stuff.jpg";
220
const double dp = 1.0;
221
double minDist = 20;
222
double edgeThreshold = 20;
223
double accumThreshold = 30;
224
int minRadius = 20;
225
int maxRadius = -1;
226
227
string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
228
Mat src = imread(filename, IMREAD_GRAYSCALE);
229
EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
230
231
GaussianBlur(src, src, Size(9, 9), 2, 2);
232
233
vector<Vec3f> circles;
234
vector<Vec4f> circles4f;
235
HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
236
HoughCircles(src, circles4f, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
237
238
#if DEBUG_IMAGES
239
string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
240
highlightCircles(filename, circles, imgProc + "HoughCirclesTest_CentersOnly.png");
241
#endif
242
243
EXPECT_GT(circles.size(), size_t(0)) << "Should find at least some circles";
244
for (size_t i = 0; i < circles.size(); ++i)
245
{
246
EXPECT_EQ(circles[i][2], 0.0f) << "Did not ask for radius";
247
EXPECT_EQ(circles[i][0], circles4f[i][0]);
248
EXPECT_EQ(circles[i][1], circles4f[i][1]);
249
EXPECT_EQ(circles[i][2], circles4f[i][2]);
250
}
251
}
252
253
TEST(HoughCirclesTest, ManySmallCircles)
254
{
255
string picture_name = "imgproc/beads.jpg";
256
const double dp = 1.0;
257
double minDist = 10;
258
double edgeThreshold = 90;
259
double accumThreshold = 11;
260
int minRadius = 7;
261
int maxRadius = 18;
262
263
string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
264
Mat src = imread(filename, IMREAD_GRAYSCALE);
265
EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
266
267
vector<Vec3f> circles;
268
vector<Vec4f> circles4f;
269
HoughCircles(src, circles, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
270
HoughCircles(src, circles4f, CV_HOUGH_GRADIENT, dp, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
271
272
#if DEBUG_IMAGES
273
string imgProc = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/";
274
string test_case_name = getTestCaseName(picture_name, minDist, edgeThreshold, accumThreshold, minRadius, maxRadius);
275
highlightCircles(filename, circles, imgProc + test_case_name + ".png");
276
#endif
277
278
EXPECT_GT(circles.size(), size_t(3000)) << "Should find a lot of circles";
279
EXPECT_EQ(circles.size(), circles4f.size());
280
}
281
282
}} // namespace
283
284