File: test_detector_regression.cpp

package info (click to toggle)
opencv 3.2.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 238,480 kB
  • sloc: xml: 901,650; cpp: 703,419; lisp: 20,142; java: 17,843; python: 17,641; ansic: 603; cs: 601; sh: 516; perl: 494; makefile: 117
file content (339 lines) | stat: -rw-r--r-- 11,281 bytes parent folder | download
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*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.
 //
 //
 //                           License Agreement
 //                For Open Source Computer Vision Library
 //
 // Copyright (C) 2014, Biagio Montesano, 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 the copyright holders 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"

using namespace cv;
using namespace cv::line_descriptor;

/****************************************************************************************\
*            Regression tests for line detector comparing keylines.                 *
 \****************************************************************************************/

const std::string LINE_DESCRIPTOR_DIR = "line_descriptor";
const std::string IMAGE_FILENAME = "cameraman.jpg";

class CV_BinaryDescriptorDetectorTest : public cvtest::BaseTest
{

 public:
  CV_BinaryDescriptorDetectorTest( std::string fs )
  {
    bd = BinaryDescriptor::createBinaryDescriptor();
    fs_name = fs;
  }

 protected:
  bool isSimilarKeylines( const KeyLine& k1, const KeyLine& k2 );
  void compareKeylineSets( const std::vector<KeyLine>& validKeylines, const std::vector<KeyLine>& calcKeylines );
  void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output );
  void createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output );

  void emptyDataTest();
  void regressionTest();
  virtual void run( int );

  Ptr<BinaryDescriptor> bd;
  std::string fs_name;

};

void CV_BinaryDescriptorDetectorTest::emptyDataTest()
{
  /* one image */
  Mat image;
  std::vector<KeyLine> keylines;

  try
  {
    bd->detect( image, keylines );
  }

  catch ( ... )
  {
    ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keylines vector (1).\n" );
    ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  }

  if( !keylines.empty() )
  {
    ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keylines vector (1).\n" );
    ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
    return;
  }

  /* more than one image */
  std::vector<Mat> images;
  std::vector<std::vector<KeyLine> > keylineCollection;

  try
  {
    bd->detect( images, keylineCollection );
  }

  catch ( ... )
  {
    ts->printf( cvtest::TS::LOG, "detect() on empty image vector must not generate exception (2).\n" );
    ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  }

}

void CV_BinaryDescriptorDetectorTest::createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output )
{
  output = Mat( (int) linesVec.size(), 17, CV_32FC1 );

  for ( int i = 0; i < (int) linesVec.size(); i++ )
  {
    std::vector<float> klData;
    KeyLine kl = linesVec[i];
    klData.push_back( kl.angle );
    klData.push_back( (float) kl.class_id );
    klData.push_back( kl.ePointInOctaveX );
    klData.push_back( kl.ePointInOctaveY );
    klData.push_back( kl.endPointX );
    klData.push_back( kl.endPointY );
    klData.push_back( kl.lineLength );
    klData.push_back( (float) kl.numOfPixels );
    klData.push_back( (float) kl.octave );
    klData.push_back( kl.pt.x );
    klData.push_back( kl.pt.y );
    klData.push_back( kl.response );
    klData.push_back( kl.sPointInOctaveX );
    klData.push_back( kl.sPointInOctaveY );
    klData.push_back( kl.size );
    klData.push_back( kl.startPointX );
    klData.push_back( kl.startPointY );

    float* pointerToRow = output.ptr<float>( i );
    for ( int j = 0; j < 17; j++ )
    {
      *pointerToRow = klData[j];
      pointerToRow++;
    }
  }
}

void CV_BinaryDescriptorDetectorTest::createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output )
{
  for ( int i = 0; i < inputMat.rows; i++ )
  {
    std::vector<float> tempFloat;
    KeyLine kl;
    float* pointerToRow = inputMat.ptr<float>( i );

    for ( int j = 0; j < 17; j++ )
    {
      tempFloat.push_back( *pointerToRow );
      pointerToRow++;
    }

    kl.angle = tempFloat[0];
    kl.class_id = (int) tempFloat[1];
    kl.ePointInOctaveX = tempFloat[2];
    kl.ePointInOctaveY = tempFloat[3];
    kl.endPointX = tempFloat[4];
    kl.endPointY = tempFloat[5];
    kl.lineLength = tempFloat[6];
    kl.numOfPixels = (int) tempFloat[7];
    kl.octave = (int) tempFloat[8];
    kl.pt.x = tempFloat[9];
    kl.pt.y = tempFloat[10];
    kl.response = tempFloat[11];
    kl.sPointInOctaveX = tempFloat[12];
    kl.sPointInOctaveY = tempFloat[13];
    kl.size = tempFloat[14];
    kl.startPointX = tempFloat[15];
    kl.startPointY = tempFloat[16];

    output.push_back( kl );
  }
}

bool CV_BinaryDescriptorDetectorTest::isSimilarKeylines( const KeyLine& k1, const KeyLine& k2 )
{
  const float maxPtDif = 1.f;
  const float maxSizeDif = 1.f;
  const float maxAngleDif = 2.f;
  const float maxResponseDif = 0.1f;

  float dist = (float) norm( k1.pt - k2.pt );
  return ( dist < maxPtDif && fabs( k1.size - k2.size ) < maxSizeDif && abs( k1.angle - k2.angle ) < maxAngleDif
      && abs( k1.response - k2.response ) < maxResponseDif && k1.octave == k2.octave && k1.class_id == k2.class_id );
}

void CV_BinaryDescriptorDetectorTest::compareKeylineSets( const std::vector<KeyLine>& validKeylines, const std::vector<KeyLine>& calcKeylines )
{
  const float maxCountRatioDif = 0.01f;

  // Compare counts of validation and calculated keylines.
  float countRatio = (float) validKeylines.size() / (float) calcKeylines.size();
  if( countRatio < 1 - maxCountRatioDif || countRatio > 1.f + maxCountRatioDif )
  {
    ts->printf( cvtest::TS::LOG, "Bad keylines count ratio (validCount = %d, calcCount = %d).\n", validKeylines.size(), calcKeylines.size() );
    ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
    return;
  }

  int progress = 0;
  int progressCount = (int) ( validKeylines.size() * calcKeylines.size() );
  int badLineCount = 0;
  int commonLineCount = max( (int) validKeylines.size(), (int) calcKeylines.size() );
  for ( size_t v = 0; v < validKeylines.size(); v++ )
  {
    int nearestIdx = -1;
    float minDist = std::numeric_limits<float>::max();

    for ( size_t c = 0; c < calcKeylines.size(); c++ )
    {
      progress = update_progress( progress, (int) ( v * calcKeylines.size() + c ), progressCount, 0 );
      float curDist = (float) norm( calcKeylines[c].pt - validKeylines[v].pt );
      if( curDist < minDist )
      {
        minDist = curDist;
        nearestIdx = (int) c;
      }
    }

    assert( minDist >= 0 );
    if( !isSimilarKeylines( validKeylines[v], calcKeylines[nearestIdx] ) )
      badLineCount++;
  }

  ts->printf( cvtest::TS::LOG, "badLineCount = %d; validLineCount = %d; calcLineCount = %d\n", badLineCount, validKeylines.size(),
              calcKeylines.size() );

  if( badLineCount > 0.9 * commonLineCount )
  {
    ts->printf( cvtest::TS::LOG, " - Bad accuracy!\n" );
    ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
    return;
  }

  ts->printf( cvtest::TS::LOG, " - OK\n" );
}

void CV_BinaryDescriptorDetectorTest::regressionTest()
{
  assert( bd );
  std::string imgFilename = std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + IMAGE_FILENAME;
  std::string resFilename = std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + fs_name + ".yaml";

  // Read the test image.
  Mat image = imread( imgFilename );
  if( image.empty() )
  {
    ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
    ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
    return;
  }

  // open a storage for reading
  FileStorage fs( resFilename, FileStorage::READ );

  // Compute keylines.
  std::vector<KeyLine> calcKeylines;
  bd->detect( image, calcKeylines );

  if( fs.isOpened() )  // Compare computed and valid keylines.
  {
    // Read validation keylines set.
    std::vector<KeyLine> validKeylines;
    Mat storedKeylines;
    fs["keylines"] >> storedKeylines;
    createVecFromMat( storedKeylines, validKeylines );

    if( validKeylines.empty() )
    {
      ts->printf( cvtest::TS::LOG, "keylines can not be read.\n" );
      ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
      return;
    }

    compareKeylineSets( validKeylines, calcKeylines );
  }

  else  // Write detector parameters and computed keylines as validation data.
  {
    fs.open( resFilename, FileStorage::WRITE );
    if( !fs.isOpened() )
    {
      ts->printf( cvtest::TS::LOG, "File %s can not be opened to write.\n", resFilename.c_str() );
      ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
      return;
    }

    else
    {
      fs << "detector_params" << "{";
      bd->write( fs );
      fs << "}";
      Mat lines;
      createMatFromVec( calcKeylines, lines );
      fs << "keylines" << lines;
    }
  }
}

void CV_BinaryDescriptorDetectorTest::run( int )
{
  if( !bd )
  {
    ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );
    ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
    return;
  }

  emptyDataTest();
  regressionTest();

  ts->set_failed_test_info( cvtest::TS::OK );
}

/****************************************************************************************\
*                                Tests registrations                                     *
 \****************************************************************************************/

TEST( BinaryDescriptor_Detector, regression )
{
  CV_BinaryDescriptorDetectorTest test( std::string( "edl_detector_keylines_cameraman" ) );
  test.safe_run();
}