objectnessBING.cpp 17.7 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

44
#include "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

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

jaco's avatar
jaco committed
103 104
  if( !matRead( s1, filters1f ) || !matRead( sI, idx1i ) )
  {
105
    printf( "Can't load model: %s or %s\r\n", s1.c_str(), sI.c_str() );
jaco's avatar
jaco committed
106 107 108 109 110 111 112 113 114 115 116
    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
117
  if( !matRead( s2, _svmReW1f ) || _svmReW1f.size() != Size( 2, (int) _svmSzIdxs.size() ) )
jaco's avatar
jaco committed
118 119 120 121 122
  {
    _svmReW1f = Mat();
    return -1;
  }
  return 1;
jaco's avatar
jaco committed
123
}
124

jaco's avatar
jaco committed
125
void ObjectnessBING::predictBBoxSI( Mat &img3u, ValStructVec<float, Vec4i> &valBoxes, std::vector<int> &sz, int NUM_WIN_PSZ, bool fast )
126
{
jaco's avatar
jaco committed
127
  const int numSz = (int) _svmSzIdxs.size();
jaco's avatar
jaco committed
128 129 130 131 132 133 134 135 136 137 138 139 140
  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;
141
    resize( img3u, im3u, Size( cvRound( _W * imgW * 1.0 / width ), cvRound( _W * imgH * 1.0 / height ) ), 0, 0, INTER_LINEAR_EXACT );
jaco's avatar
jaco committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    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 );
163
    }
jaco's avatar
jaco committed
164
  }
165 166 167

}

jaco's avatar
jaco committed
168
void ObjectnessBING::predictBBoxSII( ValStructVec<float, Vec4i> &valBoxes, const std::vector<int> &sz )
169
{
jaco's avatar
jaco committed
170 171 172 173 174 175 176
  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();
177 178 179
  // 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 );
180 181 182 183 184
}

// 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
185
void ObjectnessBING::getObjBndBoxes( Mat &img3u, ValStructVec<float, Vec4i> &valBoxes, int numDetPerSize )
186
{
jaco's avatar
jaco committed
187 188 189 190 191
  //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;
192 193
}

jaco's avatar
jaco committed
194
void ObjectnessBING::nonMaxSup( Mat &matchCost1f, ValStructVec<float, Point> &matchCost, int NSS, int maxPoint, bool fast )
195
{
jaco's avatar
jaco committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  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 ) );
211
    }
jaco's avatar
jaco committed
212 213 214 215 216 217 218 219
  }
  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 ) );
220
    }
jaco's avatar
jaco committed
221
  }
222

jaco's avatar
jaco committed
223 224 225 226
  valPnt.sort();
  for ( int i = 0; i < valPnt.size(); i++ )
  {
    Point &pnt = valPnt[i];
jaco's avatar
jaco committed
227
    if( isMax1u.at<BYTE>( pnt ) )
jaco's avatar
jaco committed
228 229 230 231 232 233 234 235
    {
      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
236
          isMax1u.at<BYTE>( neighbor ) = false;
237 238
        }
    }
jaco's avatar
jaco committed
239 240 241
    if( matchCost.size() >= maxPoint )
      return;
  }
242 243
}

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

jaco's avatar
jaco committed
262
void ObjectnessBING::gradientRGB( Mat &bgr3u, Mat &mag1u )
263
{
jaco's avatar
jaco committed
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
  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 );
296 297
}

jaco's avatar
jaco committed
298
void ObjectnessBING::gradientGray( Mat &bgr3u, Mat &mag1u )
299
{
jaco's avatar
jaco committed
300 301 302 303 304 305 306 307
  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
308 309
    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
310 311 312 313 314
  }

  // Top/bottom most column Iy
  for ( int x = 0; x < W; x++ )
  {
jaco's avatar
jaco committed
315 316
    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
317 318 319 320 321
  }

  // 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
322
      Ix.at<int>( y, x ) = abs( g1u.at<BYTE>( y, x + 1 ) - g1u.at<BYTE>( y, x - 1 ) );
jaco's avatar
jaco committed
323 324
  for ( int y = 1; y < H - 1; y++ )
    for ( int x = 0; x < W; x++ )
jaco's avatar
jaco committed
325
      Iy.at<int>( y, x ) = abs( g1u.at<BYTE>( y + 1, x ) - g1u.at<BYTE>( y - 1, x ) );
jaco's avatar
jaco committed
326 327

  gradientXY( Ix, Iy, mag1u );
328 329
}

jaco's avatar
jaco committed
330
void ObjectnessBING::gradientHSV( Mat &bgr3u, Mat &mag1u )
331
{
jaco's avatar
jaco committed
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
  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 );
360 361
}

jaco's avatar
jaco committed
362
void ObjectnessBING::gradientXY( Mat &x1i, Mat &y1i, Mat &mag1u )
363
{
jaco's avatar
jaco committed
364 365 366 367 368
  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
369
    BYTE* m = mag1u.ptr<BYTE>( r );
jaco's avatar
jaco committed
370
    for ( int c = 0; c < W; c++ )
jaco's avatar
jaco committed
371
      m[c] = (BYTE) min( x[c] + y[c], 255 );   //((int)sqrt(sqr(x[c]) + sqr(y[c])), 255);
jaco's avatar
jaco committed
372
  }
373 374
}

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

jaco's avatar
jaco committed
380 381 382 383 384
  int scales[3] =
  { 1, 3, 5 };
  for ( int clr = MAXBGR; clr <= G; clr++ )
  {
    setColorSpace( clr );
385 386 387
    if (!loadTrainedModel())
      continue;

jaco's avatar
jaco committed
388 389
    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 );
442 443 444
  if (! fs2.isOpened()) // wrong trainingPath
    return false;

jaco's avatar
jaco committed
445
  Mat M;
jaco's avatar
jaco committed
446
  fs2[String( removeExtension( basename( filename ) ).c_str() )] >> M;
jaco's avatar
jaco committed
447 448 449 450

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

jaco's avatar
jaco committed
456
void ObjectnessBING::read()
457
{
jaco's avatar
jaco committed
458

459 460
}

jaco's avatar
jaco committed
461
void ObjectnessBING::write() const
462
{
jaco's avatar
jaco committed
463

464 465
}

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

471 472
  // 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
473
  // likely to have objects in them.
jaco's avatar
jaco committed
474
  std::vector<Vec4i> sortedBB = finalBoxes.getSortedStructVal();
jaco's avatar
jaco committed
475
  Mat( sortedBB ).copyTo( objectnessBoundingBox );
jaco's avatar
jaco committed
476 477

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

  return true;
}

jaco's avatar
jaco committed
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 575 576 577
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
578
} /* namespace saliency */
579
}/* namespace cv */