From 00ea0f49df3e93affdfd101c13e249cb5b25b19b Mon Sep 17 00:00:00 2001 From: berak Date: Tue, 17 Oct 2017 17:59:08 +0200 Subject: [PATCH] saliency: improve BING handling --- .../saliency/saliencySpecializedClasses.hpp | 2 +- modules/saliency/samples/computeSaliency.cpp | 23 ++++++++++++++++--- modules/saliency/src/BING/CmFile.cpp | 2 +- modules/saliency/src/BING/objectnessBING.cpp | 15 +++++++----- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp b/modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp index 2db8dc68b..9f8f4a922 100644 --- a/modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp +++ b/modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp @@ -464,7 +464,7 @@ private: void setColorSpace( int clr = MAXBGR ); // Load trained model. - int loadTrainedModel( std::string modelName = "" );// Return -1, 0, or 1 if partial, none, or all loaded + int loadTrainedModel();// Return -1, 0, or 1 if partial, none, or all loaded // 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(). diff --git a/modules/saliency/samples/computeSaliency.cpp b/modules/saliency/samples/computeSaliency.cpp index c4208135e..d55d3cfac 100644 --- a/modules/saliency/samples/computeSaliency.cpp +++ b/modules/saliency/samples/computeSaliency.cpp @@ -52,7 +52,7 @@ static const char* keys = { "{@saliency_algorithm | | Saliency algorithm }" "{@video_name | | video name }" "{@start_frame |1| Start frame }" - "{@training_path |1| Path of the folder containing the trained files}" }; + "{@training_path |ObjectnessTrainedModel| Path of the folder containing the trained files}" }; static void help() { @@ -150,11 +150,28 @@ int main( int argc, char** argv ) saliencyAlgorithm = ObjectnessBING::create(); vector saliencyMap; saliencyAlgorithm.dynamicCast()->setTrainingPath( training_path ); - saliencyAlgorithm.dynamicCast()->setBBResDir( training_path + "/Results" ); + saliencyAlgorithm.dynamicCast()->setBBResDir( "Results" ); if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) ) { - std::cout << "Objectness done" << std::endl; + int ndet = int(saliencyMap.size()); + std::cout << "Objectness done " << ndet << std::endl; + // The result are sorted by objectness. We only use the first maxd boxes here. + int maxd = 7, step = 255 / maxd, jitter=9; // jitter to seperate single rects + Mat draw = image.clone(); + for (int i = 0; i < std::min(maxd, ndet); i++) { + Vec4i bb = saliencyMap[i]; + Scalar col = Scalar(((i*step)%255), 50, 255-((i*step)%255)); + Point off(theRNG().uniform(-jitter,jitter), theRNG().uniform(-jitter,jitter)); + rectangle(draw, Point(bb[0]+off.x, bb[1]+off.y), Point(bb[2]+off.x, bb[3]+off.y), col, 2); + rectangle(draw, Rect(20, 20+i*10, 10,10), col, -1); // mini temperature scale + } + imshow("BING", draw); + waitKey(); + } + else + { + std::cout << "No saliency found for " << video_name << std::endl; } } diff --git a/modules/saliency/src/BING/CmFile.cpp b/modules/saliency/src/BING/CmFile.cpp index c65ae703b..562182146 100644 --- a/modules/saliency/src/BING/CmFile.cpp +++ b/modules/saliency/src/BING/CmFile.cpp @@ -76,7 +76,7 @@ bool CmFile::MkDir( std::string &_path ) buffer[i] = '/'; } } - mkdir( _path.c_str(), 0 ); + mkdir( _path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ); return true; #endif } diff --git a/modules/saliency/src/BING/objectnessBING.cpp b/modules/saliency/src/BING/objectnessBING.cpp index 6e83a094f..3847ef537 100644 --- a/modules/saliency/src/BING/objectnessBING.cpp +++ b/modules/saliency/src/BING/objectnessBING.cpp @@ -95,16 +95,14 @@ void ObjectnessBING::setBBResDir(const String &resultsDir ) _resultsDir = resultsDir; } -int ObjectnessBING::loadTrainedModel( std::string modelName ) // Return -1, 0, or 1 if partial, none, or all loaded +int ObjectnessBING::loadTrainedModel() // Return -1, 0, or 1 if partial, none, or all loaded { - if( modelName.size() == 0 ) - modelName = _modelName; - CStr s1 = modelName + ".wS1", s2 = modelName + ".wS2", sI = modelName + ".idx"; + CStr s1 = _modelName + ".wS1", s2 = _modelName + ".wS2", sI = _modelName + ".idx"; Mat filters1f, reW1f, idx1i, show3u; if( !matRead( s1, filters1f ) || !matRead( sI, idx1i ) ) { - printf( "Can't load model: %s or %s\n", s1.c_str(), sI.c_str() ); + printf( "Can't load model: %s or %s\r\n", s1.c_str(), sI.c_str() ); return 0; } @@ -384,7 +382,9 @@ void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec> M;