Skip to content

Commit e795599

Browse files
committed
Merge pull request #1412 from berak:fix_saliency
2 parents 087db72 + 00ea0f4 commit e795599

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ class CV_EXPORTS_W ObjectnessBING : public Objectness
464464
void setColorSpace( int clr = MAXBGR );
465465

466466
// Load trained model.
467-
int loadTrainedModel( std::string modelName = "" );// Return -1, 0, or 1 if partial, none, or all loaded
467+
int loadTrainedModel();// Return -1, 0, or 1 if partial, none, or all loaded
468468

469469
// Get potential bounding boxes, each of which is represented by a Vec4i for (minX, minY, maxX, maxY).
470470
// The trained model should be prepared before calling this function: loadTrainedModel() or trainStageI() + trainStageII().

modules/saliency/samples/computeSaliency.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static const char* keys =
5252
{ "{@saliency_algorithm | | Saliency algorithm <saliencyAlgorithmType.[saliencyAlgorithmTypeSubType]> }"
5353
"{@video_name | | video name }"
5454
"{@start_frame |1| Start frame }"
55-
"{@training_path |1| Path of the folder containing the trained files}" };
55+
"{@training_path |ObjectnessTrainedModel| Path of the folder containing the trained files}" };
5656

5757
static void help()
5858
{
@@ -150,11 +150,28 @@ int main( int argc, char** argv )
150150
saliencyAlgorithm = ObjectnessBING::create();
151151
vector<Vec4i> saliencyMap;
152152
saliencyAlgorithm.dynamicCast<ObjectnessBING>()->setTrainingPath( training_path );
153-
saliencyAlgorithm.dynamicCast<ObjectnessBING>()->setBBResDir( training_path + "/Results" );
153+
saliencyAlgorithm.dynamicCast<ObjectnessBING>()->setBBResDir( "Results" );
154154

155155
if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
156156
{
157-
std::cout << "Objectness done" << std::endl;
157+
int ndet = int(saliencyMap.size());
158+
std::cout << "Objectness done " << ndet << std::endl;
159+
// The result are sorted by objectness. We only use the first maxd boxes here.
160+
int maxd = 7, step = 255 / maxd, jitter=9; // jitter to seperate single rects
161+
Mat draw = image.clone();
162+
for (int i = 0; i < std::min(maxd, ndet); i++) {
163+
Vec4i bb = saliencyMap[i];
164+
Scalar col = Scalar(((i*step)%255), 50, 255-((i*step)%255));
165+
Point off(theRNG().uniform(-jitter,jitter), theRNG().uniform(-jitter,jitter));
166+
rectangle(draw, Point(bb[0]+off.x, bb[1]+off.y), Point(bb[2]+off.x, bb[3]+off.y), col, 2);
167+
rectangle(draw, Rect(20, 20+i*10, 10,10), col, -1); // mini temperature scale
168+
}
169+
imshow("BING", draw);
170+
waitKey();
171+
}
172+
else
173+
{
174+
std::cout << "No saliency found for " << video_name << std::endl;
158175
}
159176
}
160177

modules/saliency/src/BING/CmFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool CmFile::MkDir( std::string &_path )
7676
buffer[i] = '/';
7777
}
7878
}
79-
mkdir( _path.c_str(), 0 );
79+
mkdir( _path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
8080
return true;
8181
#endif
8282
}

modules/saliency/src/BING/objectnessBING.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,14 @@ void ObjectnessBING::setBBResDir(const String &resultsDir )
9595
_resultsDir = resultsDir;
9696
}
9797

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

105103
if( !matRead( s1, filters1f ) || !matRead( sI, idx1i ) )
106104
{
107-
printf( "Can't load model: %s or %s\n", s1.c_str(), sI.c_str() );
105+
printf( "Can't load model: %s or %s\r\n", s1.c_str(), sI.c_str() );
108106
return 0;
109107
}
110108

@@ -384,7 +382,9 @@ void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float,
384382
for ( int clr = MAXBGR; clr <= G; clr++ )
385383
{
386384
setColorSpace( clr );
387-
loadTrainedModel();
385+
if (!loadTrainedModel())
386+
continue;
387+
388388
CmTimer tm( "Predict" );
389389
tm.Start();
390390

@@ -439,6 +439,9 @@ bool ObjectnessBING::matRead( const std::string& filename, Mat& _M )
439439
String filenamePlusExt( filename.c_str() );
440440
filenamePlusExt += ".yml.gz";
441441
FileStorage fs2( filenamePlusExt, FileStorage::READ );
442+
if (! fs2.isOpened()) // wrong trainingPath
443+
return false;
444+
442445
Mat M;
443446
fs2[String( removeExtension( basename( filename ) ).c_str() )] >> M;
444447

0 commit comments

Comments
 (0)