BING Objectness porting progress

pull/39/head
jaco 11 years ago
parent 6f1b6eb469
commit dae0dc9c8c
  1. 10
      modules/saliency/include/opencv2/saliency/saliencyBaseClasses.hpp
  2. 23
      modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp
  3. 129
      modules/saliency/src/CmFile.cpp
  4. 78
      modules/saliency/src/CmFile.h
  5. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8HSV.idx.yml.gz
  6. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8HSV.wS1.yml.gz
  7. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8HSV.wS2.yml.gz
  8. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8I.idx.yml.gz
  9. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8I.wS1.yml.gz
  10. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8I.wS2.yml.gz
  11. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8MAXBGR.idx.yml.gz
  12. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8MAXBGR.wS1.yml.gz
  13. BIN
      modules/saliency/src/ObjectnessTrainedModel/ObjNessB2W8MAXBGR.wS2.yml.gz
  14. 2
      modules/saliency/src/motionSaliencyPBAS.cpp
  15. 207
      modules/saliency/src/objectnessBING.cpp
  16. 5
      modules/saliency/src/saliency_init.cpp
  17. 2
      modules/saliency/src/staticSaliencySpectralResidual.cpp

@ -74,7 +74,7 @@ class CV_EXPORTS_W Saliency : public virtual Algorithm
* \return true if the saliency map is computed, false otherwise
*/
//bool computeSaliency( const Mat& image, Mat& saliencyMap );
bool computeSaliency( const InputArray& image, OutputArray& saliencyMap );
bool computeSaliency( const InputArray image, OutputArray saliencyMap );
/**
* \brief Get the name of the specific saliency type
@ -84,7 +84,7 @@ class CV_EXPORTS_W Saliency : public virtual Algorithm
protected:
//virtual bool computeSaliencyImpl( const Mat& image, Mat& saliencyMap ) = 0;
virtual bool computeSaliencyImpl( const InputArray& image, OutputArray& saliencyMap ) = 0;
virtual bool computeSaliencyImpl( const InputArray image, OutputArray saliencyMap ) = 0;
String className;
};
@ -96,7 +96,7 @@ class CV_EXPORTS_W StaticSaliency : public virtual Saliency
bool computeBinaryMap( const Mat& saliencyMap, Mat& binaryMap );
protected:
virtual bool computeSaliencyImpl( const InputArray& image, OutputArray& saliencyMap ) = 0;
virtual bool computeSaliencyImpl( const InputArray image, OutputArray saliencyMap ) = 0;
};
@ -106,7 +106,7 @@ class CV_EXPORTS_W MotionSaliency : public virtual Saliency
protected:
virtual bool computeSaliencyImpl( const InputArray& image, OutputArray& saliencyMap ) = 0;
virtual bool computeSaliencyImpl( const InputArray image, OutputArray saliencyMap ) = 0;
};
@ -116,7 +116,7 @@ class CV_EXPORTS_W Objectness : public virtual Saliency
{
protected:
virtual bool computeSaliencyImpl( const InputArray& image, OutputArray& saliencyMap ) = 0;
virtual bool computeSaliencyImpl( const InputArray image, OutputArray saliencyMap ) = 0;
};

@ -90,7 +90,7 @@ class CV_EXPORTS_W StaticSaliencySpectralResidual : public StaticSaliency
void write( FileStorage& fs ) const;
protected:
bool computeSaliencyImpl( const InputArray& src, OutputArray& dst );
bool computeSaliencyImpl( const InputArray src, OutputArray dst );
AlgorithmInfo* info() const; //{ return 0; }
CV_PROP_RW Ptr<Size> resizedImageSize;
@ -124,7 +124,7 @@ class CV_EXPORTS_W MotionSaliencyPBAS : public MotionSaliency
void write( FileStorage& fs ) const;
protected:
bool computeSaliencyImpl( const InputArray& src, OutputArray& dst );
bool computeSaliencyImpl( const InputArray src, OutputArray dst );
AlgorithmInfo* info() const; // { return 0; }
private:
@ -154,7 +154,8 @@ class CV_EXPORTS_W ObjectnessBING : public Objectness
// 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)
void getObjBndBoxes(CMat &img3u, ValStructVec<float, Vec4i> &valBoxes, int numDetPerSize = 120);
void getObjBndBoxesForImage(Mat img, ValStructVec<float, Vec4i> &boxes, int numDetPerSize);
void getObjBndBoxesForSingleImage(Mat img, ValStructVec<float, Vec4i> &boxes, int numDetPerSize);
vector<float> getobjectnessValues();
void setColorSpace(int clr = MAXBGR);
@ -167,18 +168,19 @@ class CV_EXPORTS_W ObjectnessBING : public Objectness
protected:
bool computeSaliencyImpl( const InputArray& src, OutputArray& dst );
bool computeSaliencyImpl( const InputArray src, OutputArray dst );
AlgorithmInfo* info() const; //{ return 0; }
private: // Parameters
const double _base, _logBase; // base for window size quantization
const int _W; // As described in the paper: #Size, Size(_W, _H) of feature window.
const int _NSS; // Size for non-maximal suppress
const int _maxT, _minT, _numT; // The minimal and maximal dimensions of the template
double _base, _logBase; // base for window size quantization
int _W; // As described in the paper: #Size, Size(_W, _H) of feature window.
int _NSS; // Size for non-maximal suppress
int _maxT, _minT, _numT; // The minimal and maximal dimensions of the template
int _Clr; //
static const char* _clrName[3];
//TODO Probably remove this parameters
//DataSetVOC &_voc; // The dataset for training, testing
std:: string _modelName, _trainDirSI, _bbResDir;
@ -187,6 +189,11 @@ class CV_EXPORTS_W ObjectnessBING : public Objectness
FilterTIG _tigF; // TIG filter
Mat _svmReW1f; // Re-weight parameters learned at stage II.
// List of the rectangles' objectness value, in the same order as
// vector<Vec4i> objectnessBoundingBox returned by the algorithm (in computeSaliencyImpl function)
vector<float> objectnessValues;
//vector<Vec4i> objectnessBoundingBox;
private: // Help functions
bool filtersLoaded() {int n = _svmSzIdxs.size(); return n > 0 && _svmReW1f.size() == Size(2, n) && _svmFilter.size() == Size(_W, _W);}

@ -0,0 +1,129 @@
#include "kyheader.h"
#include "CmFile.h"
// Get image names from a wildcard. Eg: GetNames("D:\\*.jpg", imgNames);
int CmFile::GetNames(CStr &_nameW, vecS &_names, string _dir)
{
_dir = GetFolder(_nameW);
_names.clear();
DIR *dir;
struct dirent *ent;
if((dir = opendir(_dir.c_str()))!=NULL){
//print all the files and directories within directory
while((ent = readdir(dir))!=NULL){
if(ent->d_name[0] == '.')
continue;
if(ent->d_type ==4)
continue;
_names.push_back(ent->d_name);
}
closedir(dir);
} else {
perror("");
return EXIT_FAILURE;
}
return (int)_names.size();
}
int CmFile::GetSubFolders(CStr &folder, vecS &subFolders)
{
subFolders.clear();
string nameWC = GetFolder(folder);//folder + "/*";
DIR *dir;
struct dirent *ent;
if((dir = opendir(nameWC.c_str()))!=NULL){
while((ent = readdir(dir))!=NULL){
if(ent->d_name[0] == '.')
continue;
if(ent->d_type == 4){
subFolders.push_back(ent->d_name);
}
}
closedir(dir);
} else {
perror("");
return EXIT_FAILURE;
}
return (int)subFolders.size();
}
int CmFile::GetNames(CStr& rootFolder, CStr &fileW, vecS &names)
{
GetNames(rootFolder + fileW, names);
vecS subFolders, tmpNames;
int subNum = CmFile::GetSubFolders(rootFolder, subFolders);//
for (int i = 0; i < subNum; i++){
subFolders[i] += "/";
int subNum = GetNames(rootFolder + subFolders[i], fileW, tmpNames);
for (int j = 0; j < subNum; j++)
names.push_back(subFolders[i] + tmpNames[j]);
}
return (int)names.size();
}
int CmFile::GetNamesNE(CStr& nameWC, vecS &names, string dir, string ext)
{
int fNum = GetNames(nameWC, names, dir);
ext = GetExtention(nameWC);
for (int i = 0; i < fNum; i++)
names[i] = GetNameNE(names[i]);
return fNum;
}
int CmFile::GetNamesNE(CStr& rootFolder, CStr &fileW, vecS &names)
{
int fNum = GetNames(rootFolder, fileW, names);
int extS = GetExtention(fileW).size();
for (int i = 0; i < fNum; i++)
names[i].resize(names[i].size() - extS);
return fNum;
}
bool CmFile::MkDir(CStr &_path)
{
if(_path.size() == 0)
return false;
static char buffer[1024];
strcpy(buffer, _S(_path));
#ifdef _WIN32
for (int i = 0; buffer[i] != 0; i ++) {
if (buffer[i] == '\\' || buffer[i] == '/') {
buffer[i] = '\0';
CreateDirectoryA(buffer, 0);
buffer[i] = '/';
}
}
return CreateDirectoryA(_S(_path), 0);
#else
for (int i = 0; buffer[i] != 0; i ++) {
if (buffer[i] == '\\' || buffer[i] == '/') {
buffer[i] = '\0';
mkdir(buffer, 0);
buffer[i] = '/';
}
}
return mkdir(_S(_path), 0);
#endif
}
void CmFile::loadStrList(CStr &fName, vecS & strs, bool flag)
{
ifstream fIn(fName);
string line;
//vecS strs;
while(getline(fIn, line)){
unsigned sz = line.size();
if(flag)
line.resize(sz - 1);
printf("%s\n",_S(line));
strs.push_back(line);
}
//return strs;
}
bool CmFile::writeStrList(CStr &fName, const vecS &strs)
{
FILE *f = fopen(_S(fName), "w");
if (f == NULL)
return false;
for (size_t i = 0; i < strs.size(); i++)
fprintf(f, "%s\n", _S(strs[i]));
fclose(f);
return true;
}

@ -0,0 +1,78 @@
#pragma once
#ifdef _WIN32
#include <windows.h>
#else
#include <iostream>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#endif
struct CmFile
{
static inline string GetFolder(CStr& path);
static inline string GetName(CStr& path);
static inline string GetNameNE(CStr& path);
static inline string GetPathNE(CStr& path);
// Get file names from a wildcard. Eg: GetNames("D:\\*.jpg", imgNames);
static int GetNames(CStr &nameW, vecS &names, std::string _dir = std::string());
static int GetNames(CStr& rootFolder, CStr &fileW, vecS &names);
static int GetNamesNE(CStr& nameWC, vecS &names, string dir = string(), string ext = string());
static int GetNamesNE(CStr& rootFolder, CStr &fileW, vecS &names);
static inline string GetExtention(CStr name);
static int GetSubFolders(CStr& folder, vecS& subFolders);
static inline string GetWkDir();
static bool MkDir(CStr& path);
static void loadStrList(CStr &fName, vecS &strs, bool flag=false);
static bool writeStrList(CStr &fName, const vecS &strs);
};
/************************************************************************/
/* Implementation of inline functions */
/************************************************************************/
string CmFile::GetFolder(CStr& path)
{
return path.substr(0, path.find_last_of("\\/")+1);
}
string CmFile::GetName(CStr& path)
{
int start = path.find_last_of("\\/")+1;
int end = path.find_last_not_of(' ')+1;
return path.substr(start, end - start);
}
string CmFile::GetNameNE(CStr& path)
{
int start = path.find_last_of("\\/")+1;
int end = path.find_last_of('.');
if (end >= 0)
return path.substr(start, end - start);
else
return path.substr(start, path.find_last_not_of(' ')+1 - start);
}
string CmFile::GetPathNE(CStr& path)
{
int end = path.find_last_of('.');
if (end >= 0)
return path.substr(0, end);
else
return path.substr(0, path.find_last_not_of(' ') + 1);
}
string CmFile::GetExtention(CStr name)
{
return name.substr(name.find_last_of('.'));
}
/************************************************************************/
/* Implementations */
/************************************************************************/

@ -87,7 +87,7 @@ void MotionSaliencyPBAS::write( cv::FileStorage& fs ) const
//params.write( fs );
}
bool MotionSaliencyPBAS::computeSaliencyImpl( const InputArray& src, OutputArray& dst )
bool MotionSaliencyPBAS::computeSaliencyImpl( const InputArray src, OutputArray dst )
{
return true;

@ -56,11 +56,11 @@ ObjectnessBING::ObjectnessBING()
_base = 2; // base for window size quantization
_W = 8; // feature window size (W, W)
_NSS = 2; //non-maximal suppress size NSS
_logBase( log( _base ) );
_minT( cvCeil( log( 10. ) / _logBase ) );
_maxT( cvCeil( log( 500. ) / _logBase ) );
_numT( _maxT - _minT + 1 );
_Clr( MAXBGR );
_logBase=log( _base );
_minT=cvCeil( log( 10. ) / _logBase );
_maxT=cvCeil( log( 500. ) / _logBase );
_numT= _maxT - _minT + 1 ;
_Clr= MAXBGR ;
setColorSpace( _Clr );
@ -75,7 +75,7 @@ ObjectnessBING::~ObjectnessBING()
void ObjectnessBING::setColorSpace( int clr )
{
_Clr = clr;
// _modelName = _voc.resDir + string(format("ObjNessB%gW%d%s", _base, _W, _clrName[_Clr]).c_str());
_modelName = "/home/puja/src/opencv_contrib/modules/saliency/src/ObjectnessTrainedModel" + string(format("ObjNessB%gW%d%s", _base, _W, _clrName[_Clr]).c_str());
// _trainDirSI = _voc.localDir + string(format("TrainS1B%gW%d%s/", _base, _W, _clrName[_Clr]).c_str());
// _bbResDir = _voc.resDir + string(format("BBoxesB%gW%d%s/", _base, _W, _clrName[_Clr]).c_str());
}
@ -86,7 +86,8 @@ int ObjectnessBING::loadTrainedModel(string modelName) // Return -1, 0, or 1 if
modelName = _modelName;
CStr s1 = modelName + ".wS1", s2 = modelName + ".wS2", sI = modelName + ".idx";
Mat filters1f, reW1f, idx1i, show3u;
if (!matRead(s1, filters1f) || !matRead(sI, idx1i)){
if( !matRead( s1, filters1f ) || !matRead( sI, idx1i ) )
{
printf( "Can't load model: %s or %s\n", _S( s1 ), _S( sI ) );
return 0;
}
@ -102,7 +103,8 @@ int ObjectnessBING::loadTrainedModel(string modelName) // Return -1, 0, or 1 if
CV_Assert( _svmSzIdxs.size() > 1 && filters1f.size() == Size(_W, _W) && filters1f.type() == CV_32F );
_svmFilter = filters1f;
if (!matRead(s2, _svmReW1f) || _svmReW1f.size() != Size(2, _svmSzIdxs.size())){
if( !matRead( s2, _svmReW1f ) || _svmReW1f.size() != Size( 2, _svmSzIdxs.size() ) )
{
_svmReW1f = Mat();
return -1;
}
@ -114,8 +116,10 @@ void ObjectnessBING::predictBBoxSI(CMat &img3u, ValStructVec<float, Vec4i> &valB
const int numSz = _svmSzIdxs.size();
const int imgW = img3u.cols, imgH = img3u.rows;
valBoxes.reserve( 10000 );
sz.clear(); sz.reserve(10000);
for (int ir = numSz - 1; ir >= 0; ir--){
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 )
@ -139,7 +143,8 @@ void ObjectnessBING::predictBBoxSI(CMat &img3u, ValStructVec<float, Vec4i> &valB
// 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++){
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 ) );
@ -157,11 +162,15 @@ void ObjectnessBING::predictBBoxSI(CMat &img3u, ValStructVec<float, Vec4i> &valB
void ObjectnessBING::predictBBoxSII( ValStructVec<float, Vec4i> &valBoxes, const vecI &sz )
{
int numI = valBoxes.size();
for (int i = 0; i < numI; i++){
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();
//valBoxes.sort();
// Ascending order. At the top there are the values with lower
// values of objectness, ie more likely to have objects in the their corresponding rectangles.
valBoxes.sort( false );
}
// Get potential bounding boxes, each of which is represented by a Vec4i for (minX, minY, maxX, maxY).
@ -183,9 +192,11 @@ void ObjectnessBING::nonMaxSup(CMat &matchCost1f, ValStructVec<float, Point> &ma
ValStructVec<float, Point> valPnt;
matchCost.reserve( _h * _w );
valPnt.reserve( _h * _w );
if (fast){
if( fast )
{
blur( matchCost1f, costSmooth1f, Size( 3, 3 ) );
for (int r = 0; r < _h; r++){
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++ )
@ -193,8 +204,10 @@ void ObjectnessBING::nonMaxSup(CMat &matchCost1f, ValStructVec<float, Point> &ma
valPnt.pushBack( d[c], Point( c, r ) );
}
}
else{
for (int r = 0; r < _h; r++){
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 ) );
@ -202,11 +215,15 @@ void ObjectnessBING::nonMaxSup(CMat &matchCost1f, ValStructVec<float, Point> &ma
}
valPnt.sort();
for (int i = 0; i < valPnt.size(); i++){
for ( int i = 0; i < valPnt.size(); i++ )
{
Point &pnt = valPnt[i];
if (isMax1u.at<byte>(pnt)){
if( isMax1u.at<byte>( pnt ) )
{
matchCost.pushBack( valPnt( i ), pnt );
for (int dy = -NSS; dy <= NSS; dy++) for (int dx = -NSS; dx <= NSS; dx++){
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;
@ -220,13 +237,17 @@ void ObjectnessBING::nonMaxSup(CMat &matchCost1f, ValStructVec<float, Point> &ma
void ObjectnessBING::gradientMag( CMat &imgBGR3u, Mat &mag1u )
{
switch (_Clr){
switch ( _Clr )
{
case MAXBGR:
gradientRGB(imgBGR3u, mag1u); break;
gradientRGB( imgBGR3u, mag1u );
break;
case G:
gradientGray(imgBGR3u, mag1u); break;
gradientGray( imgBGR3u, mag1u );
break;
case HSV:
gradientHSV(imgBGR3u, mag1u); break;
gradientHSV( imgBGR3u, mag1u );
break;
default:
printf( "Error: not recognized color space\n" );
}
@ -238,24 +259,28 @@ void ObjectnessBING::gradientRGB(CMat &bgr3u, Mat &mag1u)
Mat Ix( H, W, CV_32S ), Iy( H, W, CV_32S );
// Left/right most column Ix
for (int y = 0; y < H; y++){
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++) {
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++){
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++){
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++ )
@ -272,13 +297,15 @@ void ObjectnessBING::gradientGray(CMat &bgr3u, Mat &mag1u)
Mat Ix( H, W, CV_32S ), Iy( H, W, CV_32S );
// Left/right most column Ix
for (int y = 0; y < H; y++){
for ( int y = 0; y < H; y++ )
{
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;
}
// Top/bottom most column Iy
for (int x = 0; x < W; x++) {
for ( int x = 0; x < W; x++ )
{
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;
}
@ -294,7 +321,6 @@ void ObjectnessBING::gradientGray(CMat &bgr3u, Mat &mag1u)
gradientXY( Ix, Iy, mag1u );
}
void ObjectnessBING::gradientHSV( CMat &bgr3u, Mat &mag1u )
{
Mat hsv3u;
@ -303,13 +329,15 @@ void ObjectnessBING::gradientHSV(CMat &bgr3u, Mat &mag1u)
Mat Ix( H, W, CV_32S ), Iy( H, W, CV_32S );
// Left/right most column Ix
for (int y = 0; y < H; y++){
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++) {
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 ) );
}
@ -329,7 +357,8 @@ void ObjectnessBING::gradientXY(CMat &x1i, CMat &y1i, Mat &mag1u)
{
const int H = x1i.rows, W = x1i.cols;
mag1u.create( H, W, CV_8U );
for (int r = 0; r < H; r++){
for ( int r = 0; r < H; r++ )
{
const int *x = x1i.ptr<int>( r ), *y = y1i.ptr<int>( r );
byte* m = mag1u.ptr<byte>( r );
for ( int c = 0; c < W; c++ )
@ -337,70 +366,23 @@ void ObjectnessBING::gradientXY(CMat &x1i, CMat &y1i, Mat &mag1u)
}
}
void ObjectnessBING::getObjBndBoxesForImage(Mat img, ValStructVec<float, Vec4i> &finalBoxes, int numDetPerSize)
void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float, Vec4i> &finalBoxes, int numDetPerSize )
{
const int TestNum = 1;
vecM imgs3u(TestNum);
vector<ValStructVec<float, Vec4i>> boxesTests;
boxesTests.resize(TestNum);
#pragma omp parallel for
for (int i = 0; i < TestNum; i++){
imgs3u[i] = img;
boxesTests[i].reserve(10000);
}
int scales[3] = {1, 3, 5};
for (int clr = MAXBGR; clr <= G; clr++){
int scales[3] =
{ 1, 3, 5 };
for ( int clr = MAXBGR; clr <= G; clr++ )
{
setColorSpace( clr );
loadTrainedModel();
CmTimer tm( "Predict" );
//tm.Start();
#pragma omp parallel for
for (int i = 0; i < TestNum; i++){
ValStructVec<float, Vec4i> boxes;
getObjBndBoxes(imgs3u[i], boxes, numDetPerSize);
boxesTests[i].append(boxes, scales[clr]);
//boxes.valIdxes[0].first;
}
//tm.Stop();
//printf("Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds()/TestNum);
}
/* _boxesTests.resize(TestNum);
CmFile::MkDir(_bbResDir);
#pragma omp parallel for
for (int i = 0; i < TestNum; i++){
CStr fName = _bbResDir + _voc.testSet[i];
ValStructVec<float, Vec4i> &boxes = boxesTests[i];
FILE *f = fopen(_S(fName + ".txt"), "w");
fprintf(f, "%d\n", boxes.size());
for (size_t k = 0; k < boxes.size(); k++)
//fprintf(f, "%g, %s\n", boxes(k), _S(strVec4i(boxes[k])));
fclose(f);
_boxesTests[i].resize(boxesTests[i].size());
for (int j = 0; j < boxesTests[i].size(); j++)
_boxesTests[i][j] = boxesTests[i][j];
} */
tm.Start();
//evaluatePerImgRecall(_boxesTests, "PerImgAllNS.m", 5000);
#pragma omp parallel for
for (int i = 0; i < TestNum; i++){
boxesTests[i].sort(false);
//for (int j = 0; j < boxesTests[i].size(); j++)
// _boxesTests[i][j] = boxesTests[i][j];
finalBoxes=boxesTests[i];
getObjBndBoxes( img, finalBoxes, numDetPerSize );
tm.Stop();
printf( "Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds() );
}
//evaluatePerImgRecall(_boxesTests, "PerImgAllS.m", 5000);
}
struct MatchPathSeparator
@ -413,25 +395,18 @@ struct MatchPathSeparator
std::string inline basename( std::string const& pathname )
{
return std::string(
std::find_if( pathname.rbegin(), pathname.rend(),
MatchPathSeparator() ).base(),
pathname.end() );
return std::string( std::find_if( pathname.rbegin(), pathname.rend(), MatchPathSeparator() ).base(), pathname.end() );
}
std::string inline removeExtension( std::string const& filename )
{
std::string::const_reverse_iterator
pivot
= std::find( filename.rbegin(), filename.rend(), '.' );
return pivot == filename.rend()
? filename
: std::string( filename.begin(), pivot.base() - 1 );
std::string::const_reverse_iterator pivot = std::find( filename.rbegin(), filename.rend(), '.' );
return pivot == filename.rend() ? filename : std::string( filename.begin(), pivot.base() - 1 );
}
// Read matrix from binary file
bool ObjectnessBING::matRead(const string& filename, Mat& _M){
bool ObjectnessBING::matRead( const string& filename, Mat& _M )
{
FileStorage fs2( filename + ".yml.gz", FileStorage::READ );
String fileNameString( filename.c_str() );
@ -457,26 +432,36 @@ bool ObjectnessBING::matRead(const string& filename, Mat& _M){
M.copyTo( _M );
return true;
}
vector<float> ObjectnessBING::getobjectnessValues()
{
return objectnessValues;
}
void ObjectnessBING::read( const cv::FileNode& fn )
{
//params.read( fn );
}
void ObjectnessBING::write( cv::FileStorage& fs ) const
{
//params.write( fs );
}
bool ObjectnessBING::computeSaliencyImpl( const InputArray& src, OutputArray& dst )
bool ObjectnessBING::computeSaliencyImpl( const InputArray image, OutputArray objBoundingBox )
{
ValStructVec<float, Vec4i> &finalBoxes;
getObjBndBoxesForImage( src.getMat(), finalBoxes, 250 );
// list of rectangles returned by objectess function
//std::vector<Vec4i> boxesList = finalBoxes.structVals;
dst = finalBoxes.getSortedStructVal();
//dst = finalBoxes.structVals;
getObjBndBoxesForSingleImage( image.getMat(), finalBoxes, 250 );
// List of rectangles returned by objectess function in ascending order.
// At the top there are the rectangles with lower values of objectness, ie more
// likely to have objects in them.
objBoundingBox = finalBoxes.getSortedStructVal();
// List of the rectangles' objectness value
unsigned long int valIdxesSize = finalBoxes.valIdxes.size();
objectnessValues.resize( valIdxesSize );
for ( uint i = 0; i < valIdxesSize; i++ )
objectnessValues[i] = finalBoxes.valIdxes[i].first;
return true;
}

@ -50,7 +50,10 @@ CV_INIT_ALGORITHM( StaticSaliencySpectralResidual, "SALIENCY.SPECTRAL_RESIDUAL",
CV_INIT_ALGORITHM( MotionSaliencyPBAS, "SALIENCY.PBAS",);
CV_INIT_ALGORITHM( ObjectnessBING, "SALIENCY.BING", );
CV_INIT_ALGORITHM( ObjectnessBING, "SALIENCY.BING",
obj.info()->addParam(obj, "_base", obj._base);
obj.info()->addParam(obj, "_NSS", obj._NSS);
obj.info()->addParam(obj, "_W", obj._W));
bool initModule_saliency( void )
{

@ -97,7 +97,7 @@ void StaticSaliencySpectralResidual::write( cv::FileStorage& fs ) const
//params.write( fs );
}
bool StaticSaliencySpectralResidual::computeSaliencyImpl( const InputArray& image, OutputArray& saliencyMap )
bool StaticSaliencySpectralResidual::computeSaliencyImpl( const InputArray image, OutputArray saliencyMap )
{
Mat grayTemp, grayDown;
std::vector<Mat> mv;

Loading…
Cancel
Save