1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "test_precomp.hpp"
#include <stdlib.h>
using namespace cv;
using namespace std;
const int NSN=5;//10;//20; //number of shapes per class
const float CURRENT_MAX_ACCUR=85; //90% and 91% reached in several tests, 85 is fixed as minimum boundary
class CV_HaussTest : public cvtest::BaseTest
{
public:
CV_HaussTest();
~CV_HaussTest();
protected:
void run(int);
private:
float computeShapeDistance(vector<Point> &query1, vector<Point> &query2,
vector<Point> &query3, vector<Point> &testq);
vector <Point> convertContourType(const Mat& currentQuery, int n=180);
vector<Point2f> normalizeContour(const vector <Point>& contour);
void listShapeNames( vector<string> &listHeaders);
void mpegTest();
void displayMPEGResults();
};
CV_HaussTest::CV_HaussTest()
{
}
CV_HaussTest::~CV_HaussTest()
{
}
vector<Point2f> CV_HaussTest::normalizeContour(const vector<Point> &contour)
{
vector<Point2f> output(contour.size());
Mat disMat((int)contour.size(),(int)contour.size(),CV_32F);
Point2f meanpt(0,0);
float meanVal=1;
for (int ii=0, end1 = (int)contour.size(); ii<end1; ii++)
{
for (int jj=0, end2 = (int)contour.size(); end2; jj++)
{
if (ii==jj) disMat.at<float>(ii,jj)=0;
else
{
disMat.at<float>(ii,jj)=
float(fabs(double(contour[ii].x*contour[jj].x)))+float(fabs(double(contour[ii].y*contour[jj].y)));
}
}
meanpt.x+=contour[ii].x;
meanpt.y+=contour[ii].y;
}
meanpt.x/=contour.size();
meanpt.y/=contour.size();
meanVal=float(cv::mean(disMat)[0]);
for (size_t ii=0; ii<contour.size(); ii++)
{
output[ii].x = (contour[ii].x-meanpt.x)/meanVal;
output[ii].y = (contour[ii].y-meanpt.y)/meanVal;
}
return output;
}
void CV_HaussTest::listShapeNames( vector<string> &listHeaders)
{
listHeaders.push_back("apple"); //ok
listHeaders.push_back("children"); // ok
listHeaders.push_back("device7"); // ok
listHeaders.push_back("Heart"); // ok
listHeaders.push_back("teddy"); // ok
}
vector <Point> CV_HaussTest::convertContourType(const Mat& currentQuery, int n)
{
vector<vector<Point> > _contoursQuery;
vector <Point> contoursQuery;
findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
for (size_t border=0; border<_contoursQuery.size(); border++)
{
for (size_t p=0; p<_contoursQuery[border].size(); p++)
{
contoursQuery.push_back(_contoursQuery[border][p]);
}
}
// In case actual number of points is less than n
for (int add=(int)contoursQuery.size()-1; add<n; add++)
{
contoursQuery.push_back(contoursQuery[contoursQuery.size()-add+1]); //adding dummy values
}
// Uniformly sampling
random_shuffle(contoursQuery.begin(), contoursQuery.end());
int nStart=n;
vector<Point> cont;
for (int i=0; i<nStart; i++)
{
cont.push_back(contoursQuery[i]);
}
return cont;
}
float CV_HaussTest::computeShapeDistance(vector <Point>& query1, vector <Point>& query2,
vector <Point>& query3, vector <Point>& testq)
{
Ptr <HausdorffDistanceExtractor> haus = createHausdorffDistanceExtractor();
return std::min(haus->computeDistance(query1,testq), std::min(haus->computeDistance(query2,testq),
haus->computeDistance(query3,testq)));
}
void CV_HaussTest::mpegTest()
{
string baseTestFolder="shape/mpeg_test/";
string path = cvtest::TS::ptr()->get_data_path() + baseTestFolder;
vector<string> namesHeaders;
listShapeNames(namesHeaders);
// distance matrix //
Mat distanceMat=Mat::zeros(NSN*(int)namesHeaders.size(), NSN*(int)namesHeaders.size(), CV_32F);
// query contours (normal v flipped, h flipped) and testing contour //
vector<Point> contoursQuery1, contoursQuery2, contoursQuery3, contoursTesting;
// reading query and computing its properties //
int counter=0;
const int loops=NSN*(int)namesHeaders.size()*NSN*(int)namesHeaders.size();
for (size_t n=0; n<namesHeaders.size(); n++)
{
for (int i=1; i<=NSN; i++)
{
// read current image //
stringstream thepathandname;
thepathandname<<path+namesHeaders[n]<<"-"<<i<<".png";
Mat currentQuery, flippedHQuery, flippedVQuery;
currentQuery=imread(thepathandname.str(), IMREAD_GRAYSCALE);
flip(currentQuery, flippedHQuery, 0);
flip(currentQuery, flippedVQuery, 1);
// compute border of the query and its flipped versions //
vector<Point> origContour;
contoursQuery1=convertContourType(currentQuery);
origContour=contoursQuery1;
contoursQuery2=convertContourType(flippedHQuery);
contoursQuery3=convertContourType(flippedVQuery);
// compare with all the rest of the images: testing //
for (size_t nt=0; nt<namesHeaders.size(); nt++)
{
for (int it=1; it<=NSN; it++)
{
/* skip self-comparisson */
counter++;
if (nt==n && it==i)
{
distanceMat.at<float>(NSN*(int)n+i-1,
NSN*(int)nt+it-1)=0;
continue;
}
// read testing image //
stringstream thetestpathandname;
thetestpathandname<<path+namesHeaders[nt]<<"-"<<it<<".png";
Mat currentTest;
currentTest=imread(thetestpathandname.str().c_str(), 0);
// compute border of the testing //
contoursTesting=convertContourType(currentTest);
// compute shape distance //
std::cout<<std::endl<<"Progress: "<<counter<<"/"<<loops<<": "<<100*double(counter)/loops<<"% *******"<<std::endl;
std::cout<<"Computing shape distance between "<<namesHeaders[n]<<i<<
" and "<<namesHeaders[nt]<<it<<": ";
distanceMat.at<float>(NSN*(int)n+i-1, NSN*(int)nt+it-1)=
computeShapeDistance(contoursQuery1, contoursQuery2, contoursQuery3, contoursTesting);
std::cout<<distanceMat.at<float>(NSN*(int)n+i-1, NSN*(int)nt+it-1)<<std::endl;
}
}
}
}
// save distance matrix //
FileStorage fs(cvtest::TS::ptr()->get_data_path() + baseTestFolder + "distanceMatrixMPEGTest.yml", FileStorage::WRITE);
fs << "distanceMat" << distanceMat;
}
const int FIRST_MANY=2*NSN;
void CV_HaussTest::displayMPEGResults()
{
string baseTestFolder="shape/mpeg_test/";
Mat distanceMat;
FileStorage fs(cvtest::TS::ptr()->get_data_path() + baseTestFolder + "distanceMatrixMPEGTest.yml", FileStorage::READ);
vector<string> namesHeaders;
listShapeNames(namesHeaders);
// Read generated MAT //
fs["distanceMat"]>>distanceMat;
int corrects=0;
int divi=0;
for (int row=0; row<distanceMat.rows; row++)
{
if (row%NSN==0) //another group
{
divi+=NSN;
}
for (int col=divi-NSN; col<divi; col++)
{
int nsmall=0;
for (int i=0; i<distanceMat.cols; i++)
{
if (distanceMat.at<float>(row,col)>distanceMat.at<float>(row,i))
{
nsmall++;
}
}
if (nsmall<=FIRST_MANY)
{
corrects++;
}
}
}
float porc = 100*float(corrects)/(NSN*distanceMat.rows);
std::cout<<"%="<<porc<<std::endl;
if (porc >= CURRENT_MAX_ACCUR)
ts->set_failed_test_info(cvtest::TS::OK);
else
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
}
void CV_HaussTest::run(int /* */)
{
mpegTest();
displayMPEGResults();
ts->set_failed_test_info(cvtest::TS::OK);
}
TEST(Hauss, regression) { CV_HaussTest test; test.safe_run(); }