test_descriptors_regression.cpp 13.1 KB
Newer Older
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
/*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"

44
namespace opencv_test { namespace {
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

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

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

template<class Distance>
class CV_BD_DescriptorsTest : public cvtest::BaseTest
{

 public:
  typedef typename Distance::ValueType ValueType;
  typedef typename Distance::ResultType DistanceType;

  CV_BD_DescriptorsTest( std::string fs, DistanceType _maxDist ): maxDist(_maxDist)
  {
    bd = BinaryDescriptor::createBinaryDescriptor();
    fs_name = fs;
  }

 protected:
//  void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors );
//  void createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output );
//  virtual bool writeDescriptors( Mat& descs );
//  virtual Mat readDescriptors();
//  void emptyDataTest();
//  void regressionTest();
//  virtual void run( int );

  Ptr<BinaryDescriptor> bd;
  std::string fs_name;
  const DistanceType maxDist;
  Distance distance;

//};

  void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors )
  {
    if( validDescriptors.size != calcDescriptors.size || validDescriptors.type() != calcDescriptors.type() )
    {
      ts->printf( cvtest::TS::LOG, "Valid and computed descriptors matrices must have the same size and type.\n" );
      ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
      return;
    }

    CV_Assert( validDescriptors.type() == CV_8U );

    int dimension = validDescriptors.cols;
    DistanceType curMaxDist = std::numeric_limits<DistanceType>::min();
    for ( int y = 0; y < validDescriptors.rows; y++ )
    {
      DistanceType dist = distance( validDescriptors.ptr<ValueType>( y ), calcDescriptors.ptr<ValueType>( y ), dimension );
      if( dist > curMaxDist )
        curMaxDist = dist;
    }

    std::stringstream ss;
    ss << "Max distance between valid and computed descriptors " << curMaxDist;

    if( curMaxDist < maxDist )
      ss << "." << std::endl;

    else
    {
      ss << ">" << maxDist << " - bad accuracy!" << "\n";
      ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
    }

    ts->printf( cvtest::TS::LOG, ss.str().c_str() );
  }

  Mat readDescriptors()
  {
    Mat descriptors;
121
    FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + fs_name, FileStorage::READ );
122 123 124 125 126 127 128
    fs["descriptors"] >> descriptors;

    return descriptors;
  }

  bool writeDescriptors( Mat& descs )
  {
129
    FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + fs_name, FileStorage::WRITE );
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
    fs << "descriptors" << descs;

    return true;
  }

  void 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 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 );
    }
  }

  void emptyDataTest()
  {
    assert( bd );

    // One image.
    Mat image;
    std::vector<KeyLine> keypoints;
    Mat descriptors;

    try
    {
      bd->compute( image, keypoints, descriptors );
    }

    catch ( ... )
    {
      ts->printf( cvtest::TS::LOG, "compute() on empty image and empty keypoints must not generate exception (1).\n" );
      ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
    }

    image.create( 50, 50, CV_8UC3 );
    try
    {
      bd->compute( image, keypoints, descriptors );
    }

    catch ( ... )
    {
      ts->printf( cvtest::TS::LOG, "compute() on nonempty image and empty keylines must not generate exception (1).\n" );
      ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
    }

    // Several images.
    std::vector<Mat> images;
    std::vector<std::vector<KeyLine> > keylinesCollection;
    std::vector<Mat> descriptorsCollection;
    try
    {
      bd->compute( images, keylinesCollection, descriptorsCollection );
    }

    catch ( ... )
    {
      ts->printf( cvtest::TS::LOG, "compute() on empty images and empty keylines collection must not generate exception (2).\n" );
      ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
    }

  }

  void regressionTest()
  {
    assert( bd );

    // Read the test image.
    std::string imgFilename = std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + IMAGE_FILENAME;

    Mat img = imread( imgFilename );
    if( img.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;
    }

    std::vector<KeyLine> keylines;
271
    FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/edl_detector_keylines_cameraman.yaml", FileStorage::READ );
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
    if( fs.isOpened() )
    {
      //read( fs.getFirstTopLevelNode(), keypoints );

      /* load keylines */
      Mat loadedKeylines;
      fs["keylines"] >> loadedKeylines;
      createVecFromMat( loadedKeylines, keylines );

      /* compute descriptors */
      Mat calcDescriptors;
      double t = (double) getTickCount();
      bd->compute( img, keylines, calcDescriptors );
      t = getTickCount() - t;
      ts->printf( cvtest::TS::LOG, "\nAverage time of computing one descriptor = %g ms.\n",
                  t / ( (double) getTickFrequency() * 1000. ) / calcDescriptors.rows );

      if( calcDescriptors.rows != (int) keylines.size() )
      {
        ts->printf( cvtest::TS::LOG, "Count of computed descriptors and keylines count must be equal.\n" );
        ts->printf( cvtest::TS::LOG, "Count of keylines is            %d.\n", (int) keylines.size() );
        ts->printf( cvtest::TS::LOG, "Count of computed descriptors is %d.\n", calcDescriptors.rows );
        ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
        return;
      }

      if( calcDescriptors.cols != bd->descriptorSize() / 8 || calcDescriptors.type() != bd->descriptorType() )
      {
        ts->printf( cvtest::TS::LOG, "Incorrect descriptor size or descriptor type.\n" );
        ts->printf( cvtest::TS::LOG, "Expected size is   %d.\n", bd->descriptorSize() );
        ts->printf( cvtest::TS::LOG, "Calculated size is %d.\n", calcDescriptors.cols );
        ts->printf( cvtest::TS::LOG, "Expected type is   %d.\n", bd->descriptorType() );
        ts->printf( cvtest::TS::LOG, "Calculated type is %d.\n", calcDescriptors.type() );
        ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
        return;
      }

      // TODO read and write descriptor extractor parameters and check them
      Mat validDescriptors = readDescriptors();
      if( !validDescriptors.empty() )
        compareDescriptors( validDescriptors, calcDescriptors );
      else
      {
        if( !writeDescriptors( calcDescriptors ) )
        {
          ts->printf( cvtest::TS::LOG, "Descriptors can not be written.\n" );
          ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
          return;
        }
      }

    }
    else
    {
      ts->printf( cvtest::TS::LOG, "Compute and write keylines.\n" );
327
      fs.open( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/edl_detector_keylines_cameraman.yaml", FileStorage::WRITE );
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
      if( fs.isOpened() )
      {
        bd->detect( img, keylines );
        Mat keyLinesToYaml;
        createMatFromVec( keylines, keyLinesToYaml );
        fs << "keylines" << keyLinesToYaml;
      }
      else
      {
        ts->printf( cvtest::TS::LOG, "File for writting keylines can not be opened.\n" );
        ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
        return;
      }
    }
  }

  void 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 );
  }

 private:
  CV_BD_DescriptorsTest& operator=( const CV_BD_DescriptorsTest& )
  {
    return *this;
  }
};
/****************************************************************************************\
*                                Tests registrations                                     *
 \****************************************************************************************/

TEST( BinaryDescriptor_Descriptors, regression )
{
  CV_BD_DescriptorsTest<Hamming> test( std::string( "lbd_descriptors_cameraman" ), 1 );
  test.safe_run();
}
374 375 376 377 378

/****************************************************************************************\
*                                Other tests                                             *
 \****************************************************************************************/

379
TEST( BinaryDescriptor, DISABLED_no_lines_found )
380 381 382 383 384 385 386 387 388
{
  Mat Image = Mat::zeros(100, 100, CV_8U);
  Ptr<line_descriptor::BinaryDescriptor> binDescriptor =
    line_descriptor::BinaryDescriptor::createBinaryDescriptor();

  std::vector<cv::line_descriptor::KeyLine> keyLines;
  binDescriptor->detect(Image, keyLines);
  ASSERT_EQ(keyLines.size(), 0u);
}
389 390

}} // namespace