Skip to content

Commit 47771c1

Browse files
committed
Merge pull request #1390 from sovrasov:saliency_update
2 parents 9b275d5 + e1cd60c commit 47771c1

File tree

5 files changed

+306
-185
lines changed

5 files changed

+306
-185
lines changed

modules/saliency/include/opencv2/saliency/saliencyBaseClasses.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ class CV_EXPORTS_W Saliency : public virtual Algorithm
6767
*/
6868
virtual ~Saliency();
6969

70-
/**
71-
* \brief Create Saliency by saliency type.
72-
*/
73-
static Ptr<Saliency> create( const String& saliencyType );
74-
7570
/**
7671
* \brief Compute the saliency
7772
* \param image The image.
@@ -80,12 +75,6 @@ class CV_EXPORTS_W Saliency : public virtual Algorithm
8075
*/
8176
CV_WRAP bool computeSaliency( InputArray image, OutputArray saliencyMap );
8277

83-
/**
84-
* \brief Get the name of the specific saliency type
85-
* \return The name of the tracker initializer
86-
*/
87-
CV_WRAP String getClassName() const;
88-
8978
protected:
9079

9180
virtual bool computeSaliencyImpl( InputArray image, OutputArray saliencyMap ) = 0;

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,28 @@ class CV_EXPORTS_W MotionSaliencyBinWangApr2014 : public MotionSaliency
234234
bool templateOrdering();
235235
bool templateReplacement( const Mat& finalBFMask, const Mat& image );
236236

237+
// Decision threshold adaptation and Activity control function
238+
bool activityControl(const Mat& current_noisePixelsMask);
239+
bool decisionThresholdAdaptation();
240+
237241
// changing structure
238242
std::vector<Ptr<Mat> > backgroundModel;// The vector represents the background template T0---TK of reference paper.
239243
// Matrices are two-channel matrix. In the first layer there are the B (background value)
240244
// for each pixel. In the second layer, there are the C (efficacy) value for each pixel
241245
Mat potentialBackground;// Two channel Matrix. For each pixel, in the first level there are the Ba value (potential background value)
242246
// and in the secon level there are the Ca value, the counter for each potential value.
243-
Mat epslonPixelsValue; // epslon threshold
247+
Mat epslonPixelsValue;// epslon threshold
248+
249+
Mat activityPixelsValue;// Activity level of each pixel
250+
251+
//vector<Mat> noisePixelMask; // We define a ‘noise-pixel’ as a pixel that has been classified as a foreground pixel during the full resolution
252+
Mat noisePixelMask;// We define a ‘noise-pixel’ as a pixel that has been classified as a foreground pixel during the full resolution
253+
//detection process,however, after the low resolution detection, it has become a
254+
// background pixel. The matrix is two-channel matrix. In the first layer there is the mask ( the identified noise-pixels are set to 1 while other pixels are 0)
255+
// for each pixel. In the second layer, there is the value of activity level A for each pixel.
244256

245257
//fixed parameter
258+
bool activityControlFlag;
246259
bool neighborhoodCheck;
247260
int N_DS;// Number of template to be downsampled and used in lowResolutionDetection function
248261
CV_PROP_RW int imageWidth;// Width of input image
@@ -257,6 +270,13 @@ class CV_EXPORTS_W MotionSaliencyBinWangApr2014 : public MotionSaliency
257270
// long-term template, regardless of any subsequent background changes. A relatively large (eg gamma=3) will
258271
//restrain the generation of ghosts.
259272

273+
uchar Ainc;// Activity Incrementation;
274+
int Bmax;// Upper-bound value for pixel activity
275+
int Bth;// Max activity threshold
276+
int Binc, Bdec;// Threshold for pixel-level decision threshold (epslon) adaptation
277+
float deltaINC, deltaDEC;// Increment-decrement value for epslon adaptation
278+
int epslonMIN, epslonMAX;// Range values for epslon threshold
279+
260280
};
261281

262282
/************************************ Specific Objectness Specialized Classes ************************************/
@@ -417,38 +437,38 @@ class CV_EXPORTS_W ObjectnessBING : public Objectness
417437
int _Clr;//
418438
static const char* _clrName[3];
419439

420-
// Names and paths to read model and to store results
440+
// Names and paths to read model and to store results
421441
std::string _modelName, _bbResDir, _trainingPath, _resultsDir;
422442

423443
std::vector<int> _svmSzIdxs;// Indexes of active size. It's equal to _svmFilters.size() and _svmReW1f.rows
424444
Mat _svmFilter;// Filters learned at stage I, each is a _H by _W CV_32F matrix
425445
FilterTIG _tigF;// TIG filter
426446
Mat _svmReW1f;// Re-weight parameters learned at stage II.
427447

428-
// List of the rectangles' objectness value, in the same order as
429-
// the vector<Vec4i> objectnessBoundingBox returned by the algorithm (in computeSaliencyImpl function)
448+
// List of the rectangles' objectness value, in the same order as
449+
// the vector<Vec4i> objectnessBoundingBox returned by the algorithm (in computeSaliencyImpl function)
430450
std::vector<float> objectnessValues;
431451

432452
private:
433-
// functions
453+
// functions
434454

435455
inline static float LoG( float x, float y, float delta )
436456
{
437457
float d = - ( x * x + y * y ) / ( 2 * delta * delta );
438458
return -1.0f / ( (float) ( CV_PI ) * pow( delta, 4 ) ) * ( 1 + d ) * exp( d );
439459
} // Laplacian of Gaussian
440460

441-
// Read matrix from binary file
461+
// Read matrix from binary file
442462
static bool matRead( const std::string& filename, Mat& M );
443463

444464
void setColorSpace( int clr = MAXBGR );
445465

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

449-
// Get potential bounding boxes, each of which is represented by a Vec4i for (minX, minY, maxX, maxY).
450-
// The trained model should be prepared before calling this function: loadTrainedModel() or trainStageI() + trainStageII().
451-
// Use numDet to control the final number of proposed bounding boxes, and number of per size (scale and aspect ratio)
469+
// Get potential bounding boxes, each of which is represented by a Vec4i for (minX, minY, maxX, maxY).
470+
// The trained model should be prepared before calling this function: loadTrainedModel() or trainStageI() + trainStageII().
471+
// Use numDet to control the final number of proposed bounding boxes, and number of per size (scale and aspect ratio)
452472
void getObjBndBoxes( Mat &img3u, ValStructVec<float, Vec4i> &valBoxes, int numDetPerSize = 120 );
453473
void getObjBndBoxesForSingleImage( Mat img, ValStructVec<float, Vec4i> &boxes, int numDetPerSize );
454474

@@ -460,7 +480,7 @@ class CV_EXPORTS_W ObjectnessBING : public Objectness
460480
void predictBBoxSI( Mat &mag3u, ValStructVec<float, Vec4i> &valBoxes, std::vector<int> &sz, int NUM_WIN_PSZ = 100, bool fast = true );
461481
void predictBBoxSII( ValStructVec<float, Vec4i> &valBoxes, const std::vector<int> &sz );
462482

463-
// Calculate the image gradient: center option as in VLFeat
483+
// Calculate the image gradient: center option as in VLFeat
464484
void gradientMag( Mat &imgBGR3u, Mat &mag1u );
465485

466486
static void gradientRGB( Mat &bgr3u, Mat &mag1u );
@@ -479,7 +499,7 @@ class CV_EXPORTS_W ObjectnessBING : public Objectness
479499
return abs( u[0] - v[0] ) + abs( u[1] - v[1] ) + abs( u[2] - v[2] );
480500
}
481501

482-
//Non-maximal suppress
502+
//Non-maximal suppress
483503
static void nonMaxSup( Mat &matchCost1f, ValStructVec<float, Point> &matchCost, int NSS = 1, int maxPoint = 50, bool fast = true );
484504

485505
};

modules/saliency/samples/computeSaliency.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static void help()
6464

6565
int main( int argc, char** argv )
6666
{
67+
6768
CommandLineParser parser( argc, argv, keys );
6869

6970
String saliency_algorithm = parser.get<String>( 0 );
@@ -94,13 +95,7 @@ int main( int argc, char** argv )
9495
Mat frame;
9596

9697
//instantiates the specific Saliency
97-
Ptr<Saliency> saliencyAlgorithm = Saliency::create( saliency_algorithm );
98-
99-
if( saliencyAlgorithm == NULL )
100-
{
101-
cout << "***Error in the instantiation of the saliency algorithm...***\n";
102-
return -1;
103-
}
98+
Ptr<Saliency> saliencyAlgorithm;
10499

105100
Mat binaryMap;
106101
Mat image;
@@ -116,6 +111,7 @@ int main( int argc, char** argv )
116111
if( saliency_algorithm.find( "SPECTRAL_RESIDUAL" ) == 0 )
117112
{
118113
Mat saliencyMap;
114+
saliencyAlgorithm = StaticSaliencySpectralResidual::create();
119115
if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
120116
{
121117
StaticSaliencySpectralResidual spec;
@@ -131,6 +127,7 @@ int main( int argc, char** argv )
131127
else if( saliency_algorithm.find( "FINE_GRAINED" ) == 0 )
132128
{
133129
Mat saliencyMap;
130+
saliencyAlgorithm = StaticSaliencyFineGrained::create();
134131
if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
135132
{
136133
imshow( "Saliency Map", saliencyMap );
@@ -150,6 +147,7 @@ int main( int argc, char** argv )
150147

151148
else
152149
{
150+
saliencyAlgorithm = ObjectnessBING::create();
153151
vector<Vec4i> saliencyMap;
154152
saliencyAlgorithm.dynamicCast<ObjectnessBING>()->setTrainingPath( training_path );
155153
saliencyAlgorithm.dynamicCast<ObjectnessBING>()->setBBResDir( training_path + "/Results" );
@@ -163,8 +161,7 @@ int main( int argc, char** argv )
163161
}
164162
else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 )
165163
{
166-
167-
//Ptr<Size> size = Ptr<Size>( new Size( image.cols, image.rows ) );
164+
saliencyAlgorithm = MotionSaliencyBinWangApr2014::create();
168165
saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->setImagesize( image.cols, image.rows );
169166
saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->init();
170167

@@ -175,13 +172,14 @@ int main( int argc, char** argv )
175172
{
176173

177174
cap >> frame;
175+
if( frame.empty() )
176+
{
177+
return 0;
178+
}
178179
cvtColor( frame, frame, COLOR_BGR2GRAY );
179180

180181
Mat saliencyMap;
181-
if( saliencyAlgorithm->computeSaliency( frame, saliencyMap ) )
182-
{
183-
std::cout << "current frame motion saliency done" << std::endl;
184-
}
182+
saliencyAlgorithm->computeSaliency( frame, saliencyMap );
185183

186184
imshow( "image", frame );
187185
imshow( "saliencyMap", saliencyMap * 255 );

0 commit comments

Comments
 (0)