objectnessBING.cpp 17.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*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
 //
jaco's avatar
jaco committed
13
 // Copyright (C) 2014, OpenCV Foundation, all rights reserved.
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
 // 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*/

42
#include "../precomp.hpp"
43

jaco's avatar
jaco committed
44
#include "BING/kyheader.hpp"
jaco's avatar
jaco committed
45 46
#include "CmTimer.hpp"
#include "CmFile.hpp"
jaco's avatar
jaco committed
47

48 49
namespace cv
{
jaco's avatar
jaco committed
50 51
namespace saliency
{
52 53 54 55 56

/**
 * BING Objectness
 */

57 58
const char* ObjectnessBING::_clrName[3] =
{ "MAXBGR", "HSV", "I" };
59

60
ObjectnessBING::ObjectnessBING()
61
{
62 63 64
  _base = 2;  // base for window size quantization
  _W = 8;  // feature window size (W, W)
  _NSS = 2;  //non-maximal suppress size NSS
65 66 67 68 69
  _logBase = log( _base );
  _minT = cvCeil( log( 10. ) / _logBase );
  _maxT = cvCeil( log( 500. ) / _logBase );
  _numT = _maxT - _minT + 1;
  _Clr = MAXBGR;
70 71

  setColorSpace( _Clr );
72

73
  className = "BING";
74 75
}

76
ObjectnessBING::~ObjectnessBING()
77 78
{

jaco's avatar
jaco committed
79
}
80

jaco's avatar
jaco committed
81
void ObjectnessBING::setColorSpace( int clr )
jaco's avatar
jaco committed
82
{
jaco's avatar
jaco committed
83
  _Clr = clr;
jaco's avatar
jaco committed
84 85
  _modelName = _trainingPath + "/" + std::string( format( "ObjNessB%gW%d%s", _base, _W, _clrName[_Clr] ).c_str() );
  _bbResDir = _resultsDir + "/" + std::string( format( "BBoxesB%gW%d%s/", _base, _W, _clrName[_Clr] ).c_str() );
jaco's avatar
jaco committed
86 87
}

88
void ObjectnessBING::setTrainingPath( const String& trainingPath )
jaco's avatar
jaco committed
89
{
jaco's avatar
jaco committed
90
  _trainingPath = trainingPath;
jaco's avatar
jaco committed
91 92
}

93
void ObjectnessBING::setBBResDir(const String &resultsDir )
jaco's avatar
jaco committed
94
{
jaco's avatar
jaco committed
95
  _resultsDir = resultsDir;
96
}
97

jaco's avatar
jaco committed
98
int ObjectnessBING::loadTrainedModel( std::string modelName )  // Return -1, 0, or 1 if partial, none, or all loaded
jaco's avatar
jaco committed
99
{
jaco's avatar
jaco committed
100 101 102 103
  if( modelName.size() == 0 )
    modelName = _modelName;
  CStr s1 = modelName + ".wS1", s2 = modelName + ".wS2", sI = modelName + ".idx";
  Mat filters1f, reW1f, idx1i, show3u;
104

jaco's avatar
jaco committed
105 106
  if( !matRead( s1, filters1f ) || !matRead( sI, idx1i ) )
  {
107
    printf( "Can't load model: %s or %s\n", s1.c_str(), sI.c_str() );
jaco's avatar
jaco committed
108 109 110 111 112 113 114 115 116 117 118
    return 0;
  }


  normalize( filters1f, show3u, 1, 255, NORM_MINMAX, CV_8U );
  _tigF.update( filters1f );

  _svmSzIdxs = idx1i;
  CV_Assert( _svmSzIdxs.size() > 1 && filters1f.size() == Size(_W, _W) && filters1f.type() == CV_32F );
  _svmFilter = filters1f;

jaco's avatar
jaco committed
119
  if( !matRead( s2, _svmReW1f ) || _svmReW1f.size() != Size( 2, (int) _svmSzIdxs.size() ) )
jaco's avatar
jaco committed
120 121 122 123 124
  {
    _svmReW1f = Mat();
    return -1;
  }
  return 1;
jaco's avatar
jaco committed
125
}
126

jaco's avatar
jaco committed
127
void ObjectnessBING::predictBBoxSI( Mat &img3u, ValStructVec<float, Vec4i> &valBoxes, std::vector<int> &sz, int NUM_WIN_PSZ, bool fast )
128
{
jaco's avatar
jaco committed
129
  const int numSz = (int) _svmSzIdxs.size();
jaco's avatar
jaco committed
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
  const int imgW = img3u.cols, imgH = img3u.rows;
  valBoxes.reserve( 10000 );
  sz.clear();
  sz.reserve( 10000 );
  for ( int ir = numSz - 1; ir >= 0; ir-- )
  {
    int r = _svmSzIdxs[ir];
    int height = cvRound( pow( _base, r / _numT + _minT ) ), width = cvRound( pow( _base, r % _numT + _minT ) );
    if( height > imgH * _base || width > imgW * _base )
      continue;

    height = min( height, imgH ), width = min( width, imgW );
    Mat im3u, matchCost1f, mag1u;
    resize( img3u, im3u, Size( cvRound( _W * imgW * 1.0 / width ), cvRound( _W * imgH * 1.0 / height ) ) );
    gradientMag( im3u, mag1u );

    matchCost1f = _tigF.matchTemplate( mag1u );

    ValStructVec<float, Point> matchCost;
    nonMaxSup( matchCost1f, matchCost, _NSS, NUM_WIN_PSZ, fast );

    // Find true locations and match values
    double ratioX = width / _W, ratioY = height / _W;
    int iMax = min( matchCost.size(), NUM_WIN_PSZ );
    for ( int i = 0; i < iMax; i++ )
    {
      float mVal = matchCost( i );
      Point pnt = matchCost[i];
      Vec4i box( cvRound( pnt.x * ratioX ), cvRound( pnt.y * ratioY ) );
      box[2] = cvRound( min( box[0] + width, imgW ) );
      box[3] = cvRound( min( box[1] + height, imgH ) );
      box[0]++;
      box[1]++;
      valBoxes.pushBack( mVal, box );
      sz.push_back( ir );
165
    }
jaco's avatar
jaco committed
166
  }
167 168 169

}

jaco's avatar
jaco committed
170
void ObjectnessBING::predictBBoxSII( ValStructVec<float, Vec4i> &valBoxes, const std::vector<int> &sz )
171
{
jaco's avatar
jaco committed
172 173 174 175 176 177 178
  int numI = valBoxes.size();
  for ( int i = 0; i < numI; i++ )
  {
    const float* svmIIw = _svmReW1f.ptr<float>( sz[i] );
    valBoxes( i ) = valBoxes( i ) * svmIIw[0] + svmIIw[1];
  }
  //valBoxes.sort();
179 180 181
  // Descending order. At the top there are the values with higher
  // values, ie more likely to have objects in the their corresponding rectangles.
  valBoxes.sort( true );
182 183 184 185 186
}

// Get potential bounding boxes, each of which is represented by a Vec4i for (minX, minY, maxX, maxY).
// The trained model should be prepared before calling this function: loadTrainedModel() or trainStageI() + trainStageII().
// Use numDet to control the final number of proposed bounding boxes, and number of per size (scale and aspect ratio)
jaco's avatar
jaco committed
187
void ObjectnessBING::getObjBndBoxes( Mat &img3u, ValStructVec<float, Vec4i> &valBoxes, int numDetPerSize )
188
{
jaco's avatar
jaco committed
189 190 191 192 193
  //CV_Assert_(filtersLoaded() , ("SVM filters should be initialized before getting object proposals\n"));
  vecI sz;
  predictBBoxSI( img3u, valBoxes, sz, numDetPerSize, false );
  predictBBoxSII( valBoxes, sz );
  return;
194 195
}

jaco's avatar
jaco committed
196
void ObjectnessBING::nonMaxSup( Mat &matchCost1f, ValStructVec<float, Point> &matchCost, int NSS, int maxPoint, bool fast )
197
{
jaco's avatar
jaco committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  const int _h = matchCost1f.rows, _w = matchCost1f.cols;
  Mat isMax1u = Mat::ones( _h, _w, CV_8U ), costSmooth1f;
  ValStructVec<float, Point> valPnt;
  matchCost.reserve( _h * _w );
  valPnt.reserve( _h * _w );
  if( fast )
  {
    blur( matchCost1f, costSmooth1f, Size( 3, 3 ) );
    for ( int r = 0; r < _h; r++ )
    {
      const float* d = matchCost1f.ptr<float>( r );
      const float* ds = costSmooth1f.ptr<float>( r );
      for ( int c = 0; c < _w; c++ )
        if( d[c] >= ds[c] )
          valPnt.pushBack( d[c], Point( c, r ) );
213
    }
jaco's avatar
jaco committed
214 215 216 217 218 219 220 221
  }
  else
  {
    for ( int r = 0; r < _h; r++ )
    {
      const float* d = matchCost1f.ptr<float>( r );
      for ( int c = 0; c < _w; c++ )
        valPnt.pushBack( d[c], Point( c, r ) );
222
    }
jaco's avatar
jaco committed
223
  }
224

jaco's avatar
jaco committed
225 226 227 228
  valPnt.sort();
  for ( int i = 0; i < valPnt.size(); i++ )
  {
    Point &pnt = valPnt[i];
jaco's avatar
jaco committed
229
    if( isMax1u.at<BYTE>( pnt ) )
jaco's avatar
jaco committed
230 231 232 233 234 235 236 237
    {
      matchCost.pushBack( valPnt( i ), pnt );
      for ( int dy = -NSS; dy <= NSS; dy++ )
        for ( int dx = -NSS; dx <= NSS; dx++ )
        {
          Point neighbor = pnt + Point( dx, dy );
          if( !CHK_IND( neighbor ) )
            continue;
jaco's avatar
jaco committed
238
          isMax1u.at<BYTE>( neighbor ) = false;
239 240
        }
    }
jaco's avatar
jaco committed
241 242 243
    if( matchCost.size() >= maxPoint )
      return;
  }
244 245
}

jaco's avatar
jaco committed
246
void ObjectnessBING::gradientMag( Mat &imgBGR3u, Mat &mag1u )
247
{
jaco's avatar
jaco committed
248 249
  switch ( _Clr )
  {
250
    case MAXBGR:
jaco's avatar
jaco committed
251 252
      gradientRGB( imgBGR3u, mag1u );
      break;
253
    case G:
jaco's avatar
jaco committed
254 255
      gradientGray( imgBGR3u, mag1u );
      break;
256
    case HSV:
jaco's avatar
jaco committed
257 258
      gradientHSV( imgBGR3u, mag1u );
      break;
259
    default:
jaco's avatar
jaco committed
260 261
      printf( "Error: not recognized color space\n" );
  }
262 263
}

jaco's avatar
jaco committed
264
void ObjectnessBING::gradientRGB( Mat &bgr3u, Mat &mag1u )
265
{
jaco's avatar
jaco committed
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
  const int H = bgr3u.rows, W = bgr3u.cols;
  Mat Ix( H, W, CV_32S ), Iy( H, W, CV_32S );

  // Left/right most column Ix
  for ( int y = 0; y < H; y++ )
  {
    Ix.at<int>( y, 0 ) = bgrMaxDist( bgr3u.at<Vec3b>( y, 1 ), bgr3u.at<Vec3b>( y, 0 ) ) * 2;
    Ix.at<int>( y, W - 1 ) = bgrMaxDist( bgr3u.at<Vec3b>( y, W - 1 ), bgr3u.at<Vec3b>( y, W - 2 ) ) * 2;
  }

  // Top/bottom most column Iy
  for ( int x = 0; x < W; x++ )
  {
    Iy.at<int>( 0, x ) = bgrMaxDist( bgr3u.at<Vec3b>( 1, x ), bgr3u.at<Vec3b>( 0, x ) ) * 2;
    Iy.at<int>( H - 1, x ) = bgrMaxDist( bgr3u.at<Vec3b>( H - 1, x ), bgr3u.at<Vec3b>( H - 2, x ) ) * 2;
  }

  // Find the gradient for inner regions
  for ( int y = 0; y < H; y++ )
  {
    const Vec3b *dataP = bgr3u.ptr<Vec3b>( y );
    for ( int x = 2; x < W; x++ )
      Ix.at<int>( y, x - 1 ) = bgrMaxDist( dataP[x - 2], dataP[x] );  //  bgr3u.at<Vec3b>(y, x+1), bgr3u.at<Vec3b>(y, x-1));
  }
  for ( int y = 1; y < H - 1; y++ )
  {
    const Vec3b *tP = bgr3u.ptr<Vec3b>( y - 1 );
    const Vec3b *bP = bgr3u.ptr<Vec3b>( y + 1 );
    for ( int x = 0; x < W; x++ )
      Iy.at<int>( y, x ) = bgrMaxDist( tP[x], bP[x] );
  }
  gradientXY( Ix, Iy, mag1u );
298 299
}

jaco's avatar
jaco committed
300
void ObjectnessBING::gradientGray( Mat &bgr3u, Mat &mag1u )
301
{
jaco's avatar
jaco committed
302 303 304 305 306 307 308 309
  Mat g1u;
  cvtColor( bgr3u, g1u, COLOR_BGR2GRAY );
  const int H = g1u.rows, W = g1u.cols;
  Mat Ix( H, W, CV_32S ), Iy( H, W, CV_32S );

  // Left/right most column Ix
  for ( int y = 0; y < H; y++ )
  {
jaco's avatar
jaco committed
310 311
    Ix.at<int>( y, 0 ) = abs( g1u.at<BYTE>( y, 1 ) - g1u.at<BYTE>( y, 0 ) ) * 2;
    Ix.at<int>( y, W - 1 ) = abs( g1u.at<BYTE>( y, W - 1 ) - g1u.at<BYTE>( y, W - 2 ) ) * 2;
jaco's avatar
jaco committed
312 313 314 315 316
  }

  // Top/bottom most column Iy
  for ( int x = 0; x < W; x++ )
  {
jaco's avatar
jaco committed
317 318
    Iy.at<int>( 0, x ) = abs( g1u.at<BYTE>( 1, x ) - g1u.at<BYTE>( 0, x ) ) * 2;
    Iy.at<int>( H - 1, x ) = abs( g1u.at<BYTE>( H - 1, x ) - g1u.at<BYTE>( H - 2, x ) ) * 2;
jaco's avatar
jaco committed
319 320 321 322 323
  }

  // Find the gradient for inner regions
  for ( int y = 0; y < H; y++ )
    for ( int x = 1; x < W - 1; x++ )
jaco's avatar
jaco committed
324
      Ix.at<int>( y, x ) = abs( g1u.at<BYTE>( y, x + 1 ) - g1u.at<BYTE>( y, x - 1 ) );
jaco's avatar
jaco committed
325 326
  for ( int y = 1; y < H - 1; y++ )
    for ( int x = 0; x < W; x++ )
jaco's avatar
jaco committed
327
      Iy.at<int>( y, x ) = abs( g1u.at<BYTE>( y + 1, x ) - g1u.at<BYTE>( y - 1, x ) );
jaco's avatar
jaco committed
328 329

  gradientXY( Ix, Iy, mag1u );
330 331
}

jaco's avatar
jaco committed
332
void ObjectnessBING::gradientHSV( Mat &bgr3u, Mat &mag1u )
333
{
jaco's avatar
jaco committed
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
  Mat hsv3u;
  cvtColor( bgr3u, hsv3u, COLOR_BGR2HSV );
  const int H = hsv3u.rows, W = hsv3u.cols;
  Mat Ix( H, W, CV_32S ), Iy( H, W, CV_32S );

  // Left/right most column Ix
  for ( int y = 0; y < H; y++ )
  {
    Ix.at<int>( y, 0 ) = vecDist3b( hsv3u.at<Vec3b>( y, 1 ), hsv3u.at<Vec3b>( y, 0 ) );
    Ix.at<int>( y, W - 1 ) = vecDist3b( hsv3u.at<Vec3b>( y, W - 1 ), hsv3u.at<Vec3b>( y, W - 2 ) );
  }

  // Top/bottom most column Iy
  for ( int x = 0; x < W; x++ )
  {
    Iy.at<int>( 0, x ) = vecDist3b( hsv3u.at<Vec3b>( 1, x ), hsv3u.at<Vec3b>( 0, x ) );
    Iy.at<int>( H - 1, x ) = vecDist3b( hsv3u.at<Vec3b>( H - 1, x ), hsv3u.at<Vec3b>( H - 2, x ) );
  }

  // Find the gradient for inner regions
  for ( int y = 0; y < H; y++ )
    for ( int x = 1; x < W - 1; x++ )
      Ix.at<int>( y, x ) = vecDist3b( hsv3u.at<Vec3b>( y, x + 1 ), hsv3u.at<Vec3b>( y, x - 1 ) ) / 2;
  for ( int y = 1; y < H - 1; y++ )
    for ( int x = 0; x < W; x++ )
      Iy.at<int>( y, x ) = vecDist3b( hsv3u.at<Vec3b>( y + 1, x ), hsv3u.at<Vec3b>( y - 1, x ) ) / 2;

  gradientXY( Ix, Iy, mag1u );
362 363
}

jaco's avatar
jaco committed
364
void ObjectnessBING::gradientXY( Mat &x1i, Mat &y1i, Mat &mag1u )
365
{
jaco's avatar
jaco committed
366 367 368 369 370
  const int H = x1i.rows, W = x1i.cols;
  mag1u.create( H, W, CV_8U );
  for ( int r = 0; r < H; r++ )
  {
    const int *x = x1i.ptr<int>( r ), *y = y1i.ptr<int>( r );
jaco's avatar
jaco committed
371
    BYTE* m = mag1u.ptr<BYTE>( r );
jaco's avatar
jaco committed
372
    for ( int c = 0; c < W; c++ )
jaco's avatar
jaco committed
373
      m[c] = (BYTE) min( x[c] + y[c], 255 );   //((int)sqrt(sqr(x[c]) + sqr(y[c])), 255);
jaco's avatar
jaco committed
374
  }
375 376
}

jaco's avatar
jaco committed
377
void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float, Vec4i> &finalBoxes, int numDetPerSize )
378
{
379 380
  ValStructVec<float, Vec4i> boxes;
  finalBoxes.reserve( 10000 );
381

jaco's avatar
jaco committed
382 383 384 385 386 387 388 389
  int scales[3] =
  { 1, 3, 5 };
  for ( int clr = MAXBGR; clr <= G; clr++ )
  {
    setColorSpace( clr );
    loadTrainedModel();
    CmTimer tm( "Predict" );
    tm.Start();
390

391 392
    getObjBndBoxes( img, boxes, numDetPerSize );
    finalBoxes.append( boxes, scales[clr] );
393

jaco's avatar
jaco committed
394 395 396
    tm.Stop();
    printf( "Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds() );
  }
jaco's avatar
jaco committed
397

jaco's avatar
jaco committed
398 399 400
  //Write on file the total number and the list of rectangles returned by objectess, one for each row.

  CmFile::MkDir( _bbResDir );
jaco's avatar
jaco committed
401
  CStr fName = _bbResDir + "bb";
jaco's avatar
jaco committed
402
  std::vector<Vec4i> sortedBB = finalBoxes.getSortedStructVal();
jaco's avatar
jaco committed
403
  std::ofstream ofs;
404
  ofs.open( ( fName + ".txt" ).c_str(), std::ofstream::out );
jaco's avatar
jaco committed
405
  std::stringstream dim;
jaco's avatar
jaco committed
406 407 408 409
  dim << sortedBB.size();
  ofs << dim.str() << "\n";
  for ( size_t k = 0; k < sortedBB.size(); k++ )
  {
jaco's avatar
jaco committed
410
    std::stringstream str;
jaco's avatar
jaco committed
411 412
    str << sortedBB[k][0] << " " << sortedBB[k][1] << " " << sortedBB[k][2] << " " << sortedBB[k][3] << "\n";
    ofs << str.str();
jaco's avatar
jaco committed
413
  }
jaco's avatar
jaco committed
414
  ofs.close();
415 416 417 418
}

struct MatchPathSeparator
{
jaco's avatar
jaco committed
419 420 421 422
  bool operator()( char ch ) const
  {
    return ch == '/';
  }
423 424 425 426
};

std::string inline basename( std::string const& pathname )
{
jaco's avatar
jaco committed
427
  return std::string( std::find_if( pathname.rbegin(), pathname.rend(), MatchPathSeparator() ).base(), pathname.end() );
428 429 430 431
}

std::string inline removeExtension( std::string const& filename )
{
jaco's avatar
jaco committed
432 433
  std::string::const_reverse_iterator pivot = std::find( filename.rbegin(), filename.rend(), '.' );
  return pivot == filename.rend() ? filename : std::string( filename.begin(), pivot.base() - 1 );
434 435 436
}

// Read matrix from binary file
jaco's avatar
jaco committed
437
bool ObjectnessBING::matRead( const std::string& filename, Mat& _M )
jaco's avatar
jaco committed
438
{
jaco's avatar
jaco committed
439 440
  String filenamePlusExt( filename.c_str() );
  filenamePlusExt += ".yml.gz";
441
  FileStorage fs2( filenamePlusExt, FileStorage::READ );
jaco's avatar
jaco committed
442
  Mat M;
jaco's avatar
jaco committed
443
  fs2[String( removeExtension( basename( filename ) ).c_str() )] >> M;
jaco's avatar
jaco committed
444 445 446 447

  M.copyTo( _M );
  return true;
}
jaco's avatar
jaco committed
448
std::vector<float> ObjectnessBING::getobjectnessValues()
jaco's avatar
jaco committed
449 450
{
  return objectnessValues;
451 452
}

jaco's avatar
jaco committed
453
void ObjectnessBING::read()
454
{
jaco's avatar
jaco committed
455

456 457
}

jaco's avatar
jaco committed
458
void ObjectnessBING::write() const
459
{
jaco's avatar
jaco committed
460

461 462
}

463
bool ObjectnessBING::computeSaliencyImpl( InputArray image, OutputArray objectnessBoundingBox )
464
{
465
  ValStructVec<float, Vec4i> finalBoxes;
jaco's avatar
jaco committed
466 467
  getObjBndBoxesForSingleImage( image.getMat(), finalBoxes, 250 );

468 469
  // List of rectangles returned by objectess function in descending order.
  // At the top there are the rectangles with higher values, ie more
jaco's avatar
jaco committed
470
  // likely to have objects in them.
jaco's avatar
jaco committed
471
  std::vector<Vec4i> sortedBB = finalBoxes.getSortedStructVal();
jaco's avatar
jaco committed
472
  Mat( sortedBB ).copyTo( objectnessBoundingBox );
jaco's avatar
jaco committed
473 474

  // List of the rectangles' objectness value
jaco's avatar
jaco committed
475
  unsigned long int valIdxesSize = (unsigned long int) finalBoxes.getvalIdxes().size();
jaco's avatar
jaco committed
476 477
  objectnessValues.resize( valIdxesSize );
  for ( uint i = 0; i < valIdxesSize; i++ )
478
    objectnessValues[finalBoxes.getvalIdxes()[i].second] = finalBoxes.getvalIdxes()[i].first;
479 480 481 482

  return true;
}

jaco's avatar
jaco committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
template<typename VT, typename ST>
void ObjectnessBING::ValStructVec<VT, ST>::append( const ValStructVec<VT, ST> &newVals, int startV )
{
  int newValsSize = newVals.size();
  for ( int i = 0; i < newValsSize; i++ )
    pushBack( (float) ( ( i + 300 ) * startV ), newVals[i] );
}

template<typename VT, typename ST>
void ObjectnessBING::ValStructVec<VT, ST>::sort( bool descendOrder /* = true */)
{
  if( descendOrder )
    std::sort( valIdxes.begin(), valIdxes.end(), std::greater<std::pair<VT, int> >() );
  else
    std::sort( valIdxes.begin(), valIdxes.end(), std::less<std::pair<VT, int> >() );
}

template<typename VT, typename ST>
const std::vector<ST>& ObjectnessBING::ValStructVec<VT, ST>::getSortedStructVal()
{
  sortedStructVals.resize( sz );
  for ( int i = 0; i < sz; i++ )
    sortedStructVals[i] = structVals[valIdxes[i].second];
  return sortedStructVals;
}

template<typename VT, typename ST>
std::vector<std::pair<VT, int> > ObjectnessBING::ValStructVec<VT, ST>::getvalIdxes()
{
  return valIdxes;
}

template<typename VT, typename ST>
ObjectnessBING::ValStructVec<VT, ST>::ValStructVec()
{
  clear();
}

template<typename VT, typename ST>
int ObjectnessBING::ValStructVec<VT, ST>::size() const
{
  return sz;
}

template<typename VT, typename ST>
void ObjectnessBING::ValStructVec<VT, ST>::clear()
{
  sz = 0;
  structVals.clear();
  valIdxes.clear();
}

template<typename VT, typename ST>
void ObjectnessBING::ValStructVec<VT, ST>::reserve( int resSz )
{
  clear();
  structVals.reserve( resSz );
  valIdxes.reserve( resSz );
}

template<typename VT, typename ST>
void ObjectnessBING::ValStructVec<VT, ST>::pushBack( const VT& val, const ST& structVal )
{
  valIdxes.push_back( std::make_pair( val, sz ) );
  structVals.push_back( structVal );
  sz++;
}

template<typename VT, typename ST>
const VT& ObjectnessBING::ValStructVec<VT, ST>::operator ()( int i ) const
{
  return valIdxes[i].first;
}  // Should be called after sort

template<typename VT, typename ST>
const ST& ObjectnessBING::ValStructVec<VT, ST>::operator []( int i ) const
{
  return structVals[valIdxes[i].second];
}  // Should be called after sort

template<typename VT, typename ST>
VT& ObjectnessBING::ValStructVec<VT, ST>::operator ()( int i )
{
  return valIdxes[i].first;
}  // Should be called after sort

template<typename VT, typename ST>
ST& ObjectnessBING::ValStructVec<VT, ST>::operator []( int i )
{
  return structVals[valIdxes[i].second];
}

jaco's avatar
jaco committed
575
} /* namespace saliency */
576
}/* namespace cv */