Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/shape/test/test_shape.cpp
16337 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
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "test_precomp.hpp"
43
44
namespace opencv_test { namespace {
45
46
template <typename T, typename compute>
47
class ShapeBaseTest : public cvtest::BaseTest
48
{
49
public:
50
typedef Point_<T> PointType;
51
ShapeBaseTest(int _NSN, int _NP, float _CURRENT_MAX_ACCUR)
52
: NSN(_NSN), NP(_NP), CURRENT_MAX_ACCUR(_CURRENT_MAX_ACCUR)
53
{
54
// generate file list
55
vector<string> shapeNames;
56
shapeNames.push_back("apple"); //ok
57
shapeNames.push_back("children"); // ok
58
shapeNames.push_back("device7"); // ok
59
shapeNames.push_back("Heart"); // ok
60
shapeNames.push_back("teddy"); // ok
61
for (vector<string>::const_iterator i = shapeNames.begin(); i != shapeNames.end(); ++i)
62
{
63
for (int j = 0; j < NSN; ++j)
64
{
65
std::stringstream filename;
66
filename << cvtest::TS::ptr()->get_data_path()
67
<< "shape/mpeg_test/" << *i << "-" << j + 1 << ".png";
68
filenames.push_back(filename.str());
69
}
70
}
71
// distance matrix
72
const int totalCount = (int)filenames.size();
73
distanceMat = Mat::zeros(totalCount, totalCount, CV_32F);
74
}
75
76
protected:
77
void run(int)
78
{
79
mpegTest();
80
displayMPEGResults();
81
}
82
83
vector<PointType> convertContourType(const Mat& currentQuery) const
84
{
85
if (currentQuery.empty()) {
86
return vector<PointType>();
87
}
88
vector<vector<Point> > _contoursQuery;
89
findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
90
91
vector <PointType> contoursQuery;
92
for (size_t border=0; border<_contoursQuery.size(); border++)
93
{
94
for (size_t p=0; p<_contoursQuery[border].size(); p++)
95
{
96
contoursQuery.push_back(PointType((T)_contoursQuery[border][p].x,
97
(T)_contoursQuery[border][p].y));
98
}
99
}
100
101
// In case actual number of points is less than n
102
for (int add=(int)contoursQuery.size()-1; add<NP; add++)
103
{
104
contoursQuery.push_back(contoursQuery[contoursQuery.size()-add+1]); //adding dummy values
105
}
106
107
// Uniformly sampling
108
cv::randShuffle(contoursQuery);
109
int nStart=NP;
110
vector<PointType> cont;
111
for (int i=0; i<nStart; i++)
112
{
113
cont.push_back(contoursQuery[i]);
114
}
115
return cont;
116
}
117
118
void mpegTest()
119
{
120
// query contours (normal v flipped, h flipped) and testing contour
121
vector<PointType> contoursQuery1, contoursQuery2, contoursQuery3, contoursTesting;
122
// reading query and computing its properties
123
for (vector<string>::const_iterator a = filenames.begin(); a != filenames.end(); ++a)
124
{
125
// read current image
126
int aIndex = (int)(a - filenames.begin());
127
Mat currentQuery = imread(*a, IMREAD_GRAYSCALE);
128
Mat flippedHQuery, flippedVQuery;
129
flip(currentQuery, flippedHQuery, 0);
130
flip(currentQuery, flippedVQuery, 1);
131
// compute border of the query and its flipped versions
132
contoursQuery1=convertContourType(currentQuery);
133
contoursQuery2=convertContourType(flippedHQuery);
134
contoursQuery3=convertContourType(flippedVQuery);
135
// compare with all the rest of the images: testing
136
for (vector<string>::const_iterator b = filenames.begin(); b != filenames.end(); ++b)
137
{
138
int bIndex = (int)(b - filenames.begin());
139
float distance = 0;
140
// skip self-comparisson
141
if (a != b)
142
{
143
// read testing image
144
Mat currentTest = imread(*b, IMREAD_GRAYSCALE);
145
// compute border of the testing
146
contoursTesting=convertContourType(currentTest);
147
// compute shape distance
148
distance = cmp(contoursQuery1, contoursQuery2,
149
contoursQuery3, contoursTesting);
150
}
151
distanceMat.at<float>(aIndex, bIndex) = distance;
152
}
153
}
154
}
155
156
void displayMPEGResults()
157
{
158
const int FIRST_MANY=2*NSN;
159
160
int corrects=0;
161
int divi=0;
162
for (int row=0; row<distanceMat.rows; row++)
163
{
164
if (row%NSN==0) //another group
165
{
166
divi+=NSN;
167
}
168
for (int col=divi-NSN; col<divi; col++)
169
{
170
int nsmall=0;
171
for (int i=0; i<distanceMat.cols; i++)
172
{
173
if (distanceMat.at<float>(row,col) > distanceMat.at<float>(row,i))
174
{
175
nsmall++;
176
}
177
}
178
if (nsmall<=FIRST_MANY)
179
{
180
corrects++;
181
}
182
}
183
}
184
float porc = 100*float(corrects)/(NSN*distanceMat.rows);
185
std::cout << "Test result: " << porc << "%" << std::endl;
186
if (porc >= CURRENT_MAX_ACCUR)
187
ts->set_failed_test_info(cvtest::TS::OK);
188
else
189
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
190
}
191
192
protected:
193
int NSN;
194
int NP;
195
float CURRENT_MAX_ACCUR;
196
vector<string> filenames;
197
Mat distanceMat;
198
compute cmp;
199
};
200
201
//------------------------------------------------------------------------
202
// Test Shape_SCD.regression
203
//------------------------------------------------------------------------
204
205
class computeShapeDistance_Chi
206
{
207
Ptr <ShapeContextDistanceExtractor> mysc;
208
public:
209
computeShapeDistance_Chi()
210
{
211
const int angularBins=12;
212
const int radialBins=4;
213
const float minRad=0.2f;
214
const float maxRad=2;
215
mysc = createShapeContextDistanceExtractor(angularBins, radialBins, minRad, maxRad);
216
mysc->setIterations(1);
217
mysc->setCostExtractor(createChiHistogramCostExtractor(30,0.15f));
218
mysc->setTransformAlgorithm( createThinPlateSplineShapeTransformer() );
219
}
220
float operator()(vector <Point2f>& query1, vector <Point2f>& query2,
221
vector <Point2f>& query3, vector <Point2f>& testq)
222
{
223
return std::min(mysc->computeDistance(query1, testq),
224
std::min(mysc->computeDistance(query2, testq),
225
mysc->computeDistance(query3, testq)));
226
}
227
};
228
229
TEST(Shape_SCD, regression)
230
{
231
const int NSN_val=5;//10;//20; //number of shapes per class
232
const int NP_val=120; //number of points simplifying the contour
233
const float CURRENT_MAX_ACCUR_val=95; //99% and 100% reached in several tests, 95 is fixed as minimum boundary
234
ShapeBaseTest<float, computeShapeDistance_Chi> test(NSN_val, NP_val, CURRENT_MAX_ACCUR_val);
235
test.safe_run();
236
}
237
238
//------------------------------------------------------------------------
239
// Test ShapeEMD_SCD.regression
240
//------------------------------------------------------------------------
241
242
class computeShapeDistance_EMD
243
{
244
Ptr <ShapeContextDistanceExtractor> mysc;
245
public:
246
computeShapeDistance_EMD()
247
{
248
const int angularBins=12;
249
const int radialBins=4;
250
const float minRad=0.2f;
251
const float maxRad=2;
252
mysc = createShapeContextDistanceExtractor(angularBins, radialBins, minRad, maxRad);
253
mysc->setIterations(1);
254
mysc->setCostExtractor( createEMDL1HistogramCostExtractor() );
255
mysc->setTransformAlgorithm( createThinPlateSplineShapeTransformer() );
256
}
257
float operator()(vector <Point2f>& query1, vector <Point2f>& query2,
258
vector <Point2f>& query3, vector <Point2f>& testq)
259
{
260
return std::min(mysc->computeDistance(query1, testq),
261
std::min(mysc->computeDistance(query2, testq),
262
mysc->computeDistance(query3, testq)));
263
}
264
};
265
266
TEST(ShapeEMD_SCD, regression)
267
{
268
const int NSN_val=5;//10;//20; //number of shapes per class
269
const int NP_val=100; //number of points simplifying the contour
270
const float CURRENT_MAX_ACCUR_val=95; //98% and 99% reached in several tests, 95 is fixed as minimum boundary
271
ShapeBaseTest<float, computeShapeDistance_EMD> test(NSN_val, NP_val, CURRENT_MAX_ACCUR_val);
272
test.safe_run();
273
}
274
275
//------------------------------------------------------------------------
276
// Test Hauss.regression
277
//------------------------------------------------------------------------
278
279
class computeShapeDistance_Haussdorf
280
{
281
Ptr <HausdorffDistanceExtractor> haus;
282
public:
283
computeShapeDistance_Haussdorf()
284
{
285
haus = createHausdorffDistanceExtractor();
286
}
287
float operator()(vector<Point> &query1, vector<Point> &query2,
288
vector<Point> &query3, vector<Point> &testq)
289
{
290
return std::min(haus->computeDistance(query1,testq),
291
std::min(haus->computeDistance(query2,testq),
292
haus->computeDistance(query3,testq)));
293
}
294
};
295
296
TEST(Hauss, regression)
297
{
298
const int NSN_val=5;//10;//20; //number of shapes per class
299
const int NP_val = 180; //number of points simplifying the contour
300
const float CURRENT_MAX_ACCUR_val=85; //90% and 91% reached in several tests, 85 is fixed as minimum boundary
301
ShapeBaseTest<int, computeShapeDistance_Haussdorf> test(NSN_val, NP_val, CURRENT_MAX_ACCUR_val);
302
test.safe_run();
303
}
304
305
TEST(computeDistance, regression_4976)
306
{
307
Mat a = imread(cvtest::findDataFile("shape/samples/1.png"), 0);
308
Mat b = imread(cvtest::findDataFile("shape/samples/2.png"), 0);
309
310
vector<vector<Point> > ca,cb;
311
findContours(a, ca, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS);
312
findContours(b, cb, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS);
313
314
Ptr<HausdorffDistanceExtractor> hd = createHausdorffDistanceExtractor();
315
Ptr<ShapeContextDistanceExtractor> sd = createShapeContextDistanceExtractor();
316
317
double d1 = hd->computeDistance(ca[0],cb[0]);
318
double d2 = sd->computeDistance(ca[0],cb[0]);
319
320
EXPECT_NEAR(d1, 26.4196891785, 1e-3) << "HausdorffDistanceExtractor";
321
EXPECT_NEAR(d2, 0.25804194808, 1e-3) << "ShapeContextDistanceExtractor";
322
}
323
324
}} // namespace
325
326