diff --git a/modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp b/modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp index 956a51ded..2c0596bbe 100644 --- a/modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp +++ b/modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp @@ -100,12 +100,13 @@ class CV_EXPORTS_W MotionSaliencyBinWangApr2014 : public MotionSaliency MotionSaliencyBinWangApr2014(); ~MotionSaliencyBinWangApr2014(); - typedef Ptr (Algorithm::*SizeGetter)(); - typedef void (Algorithm::*SizeSetter)( const Ptr & ); + /*typedef Ptr (Algorithm::*SizeGetter)(); + typedef void (Algorithm::*SizeSetter)( const Ptr & ); - Ptr getWsize(); - void setWsize( const Ptr &newSize ); + Ptr getWsize(); + void setWsize( const Ptr &newSize ); */ + void setImagesize( int W, int H ); bool init(); protected: @@ -129,8 +130,8 @@ class CV_EXPORTS_W MotionSaliencyBinWangApr2014 : public MotionSaliency // changing structure std::vector > backgroundModel; // The vector represents the background template T0---TK of reference paper. - // Matrices are two-channel matrix. In the first layer there are the B (background value) - // for each pixel. In the second layer, there are the C (efficacy) value for each pixel + // Matrices are two-channel matrix. In the first layer there are the B (background value) + // for each pixel. In the second layer, there are the C (efficacy) value for each pixel Mat potentialBackground; // Two channel Matrix. For each pixel, in the first level there are the Ba value (potential background value) // and in the secon level there are the Ca value, the counter for each potential value. Mat epslonPixelsValue; // epslon threshold @@ -141,8 +142,10 @@ class CV_EXPORTS_W MotionSaliencyBinWangApr2014 : public MotionSaliency //fixed parameter bool neighborhoodCheck; - int N_DS; // Number of template to be downsampled and used in lowResolutionDetection function - Ptr imgSize; // Size of input image + int N_DS; // Number of template to be downsampled and used in lowResolutionDetection function + //Ptr imgSize; + int imageWidth; // Width of input image + int imageHeight; //Height of input image int K; // Number of background model template int N; // NxN is the size of the block for downsampling in the lowlowResolutionDetection float alpha; // Learning rate @@ -241,7 +244,7 @@ class CV_EXPORTS_W ObjectnessBING : public Objectness bool filtersLoaded() { - int n = (int)_svmSzIdxs.size(); + int n = (int) _svmSzIdxs.size(); return n > 0 && _svmReW1f.size() == Size( 2, n ) && _svmFilter.size() == Size( _W, _W ); } void predictBBoxSI( CMat &mag3u, ValStructVec &valBoxes, vecI &sz, int NUM_WIN_PSZ = 100, bool fast = true ); diff --git a/modules/saliency/samples/computeSaliency.cpp b/modules/saliency/samples/computeSaliency.cpp index 9d977836d..ef06cff20 100644 --- a/modules/saliency/samples/computeSaliency.cpp +++ b/modules/saliency/samples/computeSaliency.cpp @@ -157,18 +157,18 @@ int main( int argc, char** argv ) else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 ) { - Ptr size = Ptr( new Size( image.cols, image.rows ) ); - saliencyAlgorithm.dynamicCast()->setWsize( size ); + //Ptr size = Ptr( new Size( image.cols, image.rows ) ); + saliencyAlgorithm.dynamicCast()->setImagesize( image.cols, image.rows ); saliencyAlgorithm.dynamicCast()->init(); - bool paused=false; + bool paused = false; while ( true ) { if( !paused ) { cap >> frame; - cvtColor(frame, frame, COLOR_BGR2GRAY); + cvtColor( frame, frame, COLOR_BGR2GRAY ); Mat saliencyMap; if( saliencyAlgorithm->computeSaliency( frame, saliencyMap ) ) diff --git a/modules/saliency/src/BING/CmFile.cpp b/modules/saliency/src/BING/CmFile.cpp index a8848c0b0..56b72ae79 100644 --- a/modules/saliency/src/BING/CmFile.cpp +++ b/modules/saliency/src/BING/CmFile.cpp @@ -41,7 +41,6 @@ #include "CmFile.h" - bool CmFile::MkDir( CStr &_path ) { if( _path.size() == 0 ) @@ -58,7 +57,9 @@ bool CmFile::MkDir( CStr &_path ) buffer[i] = '/'; } } - return CreateDirectoryA(_S(_path), 0); + + CreateDirectoryA(_S(_path), 0); + return true; #else for ( int i = 0; buffer[i] != 0; i++ ) { @@ -69,6 +70,7 @@ bool CmFile::MkDir( CStr &_path ) buffer[i] = '/'; } } - return mkdir( _S( _path ), 0 ); + mkdir( _S( _path ), 0 ); + return true; #endif } diff --git a/modules/saliency/src/BING/objectnessBING.cpp b/modules/saliency/src/BING/objectnessBING.cpp index 2a8008641..3afd3e337 100644 --- a/modules/saliency/src/BING/objectnessBING.cpp +++ b/modules/saliency/src/BING/objectnessBING.cpp @@ -116,7 +116,7 @@ int ObjectnessBING::loadTrainedModel( std::string modelName ) // Return -1, 0, 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, (int)_svmSzIdxs.size() ) ) { _svmReW1f = Mat(); return -1; @@ -126,7 +126,7 @@ int ObjectnessBING::loadTrainedModel( std::string modelName ) // Return -1, 0, void ObjectnessBING::predictBBoxSI( CMat &img3u, ValStructVec &valBoxes, vecI &sz, int NUM_WIN_PSZ, bool fast ) { - const int numSz = _svmSzIdxs.size(); + const int numSz =(int) _svmSzIdxs.size(); const int imgW = img3u.cols, imgH = img3u.rows; valBoxes.reserve( 10000 ); sz.clear(); @@ -374,7 +374,7 @@ void ObjectnessBING::gradientXY( CMat &x1i, CMat &y1i, Mat &mag1u ) const int *x = x1i.ptr( r ), *y = y1i.ptr( r ); BYTE* m = mag1u.ptr( r ); for ( int c = 0; c < W; c++ ) - m[c] = min( x[c] + y[c], 255 ); //((int)sqrt(sqr(x[c]) + sqr(y[c])), 255); + m[c] = (BYTE)min( x[c] + y[c], 255 ); //((int)sqrt(sqr(x[c]) + sqr(y[c])), 255); } } @@ -491,7 +491,7 @@ bool ObjectnessBING::computeSaliencyImpl( const InputArray image, OutputArray ob Mat( sortedBB ).copyTo( objectnessBoundingBox ); // List of the rectangles' objectness value - unsigned long int valIdxesSize = finalBoxes.getvalIdxes().size(); + unsigned long int valIdxesSize = (unsigned long int)finalBoxes.getvalIdxes().size(); objectnessValues.resize( valIdxesSize ); for ( uint i = 0; i < valIdxesSize; i++ ) objectnessValues[i] = finalBoxes.getvalIdxes()[i].first; diff --git a/modules/saliency/src/motionSaliencyBinWangApr2014.cpp b/modules/saliency/src/motionSaliencyBinWangApr2014.cpp index 48b75a73f..a37178eec 100644 --- a/modules/saliency/src/motionSaliencyBinWangApr2014.cpp +++ b/modules/saliency/src/motionSaliencyBinWangApr2014.cpp @@ -49,13 +49,19 @@ namespace cv { -cv::Ptr MotionSaliencyBinWangApr2014::getWsize() +/* cv::Ptr MotionSaliencyBinWangApr2014::getWsize() + { + return imgSize; + } + void MotionSaliencyBinWangApr2014::setWsize( const cv::Ptr& newSize ) + { + imgSize = newSize; + } */ + +void MotionSaliencyBinWangApr2014::setImagesize( int W, int H ) { - return imgSize; -} -void MotionSaliencyBinWangApr2014::setWsize( const cv::Ptr& newSize ) -{ - imgSize = newSize; + imageWidth = W; + imageHeight = H; } MotionSaliencyBinWangApr2014::MotionSaliencyBinWangApr2014() @@ -63,7 +69,7 @@ MotionSaliencyBinWangApr2014::MotionSaliencyBinWangApr2014() N_DS = 2; // Number of template to be downsampled and used in lowResolutionDetection function K = 3; // Number of background model template N = 4; // NxN is the size of the block for downsampling in the lowlowResolutionDetection - alpha = 0.01; // Learning rate + alpha = (float) 0.01; // Learning rate L0 = 300; // Upper-bound values for C0 (efficacy of the first template (matrices) of backgroundModel L1 = 200; // Upper-bound values for C1 (efficacy of the second template (matrices) of backgroundModel thetaL = thetaL_VAL; // T0, T1 swap threshold @@ -76,13 +82,13 @@ MotionSaliencyBinWangApr2014::MotionSaliencyBinWangApr2014() bool MotionSaliencyBinWangApr2014::init() { - - epslonPixelsValue = Mat( imgSize->height, imgSize->width, CV_32F, Scalar( 20 ) ); + Size imgSize( imageWidth, imageHeight ); + epslonPixelsValue = Mat( imgSize.height, imgSize.width, CV_32F, Scalar( 20 ) ); // Median of range [18, 80] advised in reference paper. // Since data is even, the median is estimated using two values ​​that occupy // the position (n / 2) and ((n / 2) +1) (choose their arithmetic mean). - potentialBackground = Mat( imgSize->height, imgSize->width, CV_32FC2, Scalar( NAN, 0 ) ); + potentialBackground = Mat( imgSize.height, imgSize.width, CV_32FC2, Scalar( NAN, 0 ) ); //backgroundModel = std::vector( K + 1, Mat::zeros( imgSize->height, imgSize->width, CV_32FC2 ) ); backgroundModel.resize( K + 1 ); @@ -90,7 +96,7 @@ bool MotionSaliencyBinWangApr2014::init() for ( int i = 0; i < K + 1; i++ ) { Mat* tmpm = new Mat; - tmpm->create( imgSize->height, imgSize->width, CV_32FC2 ); + tmpm->create( imgSize.height, imgSize.width, CV_32FC2 ); tmpm->setTo( Scalar( NAN, 0 ) ); Ptr tmp = Ptr( tmpm ); backgroundModel[i] = tmp; @@ -114,16 +120,16 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M float currentEpslonValue; bool backgFlag = false; -/* for ( int i = 0; i <= K; i++ ) - { - vector spl; - split( * ( backgroundModel[i] ), spl ); - stringstream windowTitle; - windowTitle << "TEST_t" << i << "B"; - Mat convert; - spl[0].convertTo( convert, CV_8UC1 ); - imshow( windowTitle.str().c_str(), convert ); - }*/ + /* for ( int i = 0; i <= K; i++ ) + { + vector spl; + split( * ( backgroundModel[i] ), spl ); + stringstream windowTitle; + windowTitle << "TEST_t" << i << "B"; + Mat convert; + spl[0].convertTo( convert, CV_8UC1 ); + imshow( windowTitle.str().c_str(), convert ); + }*/ // Initially, all pixels are considered as foreground and then we evaluate with the background model highResBFMask.create( image.rows, image.cols, CV_32F ); @@ -243,8 +249,8 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat // Compute the mean of image's block and epslonMatrix's block based on ROI Mat roiImage = image( roi ); Mat roiEpslon = epslonPixelsValue( roi ); - currentPixelValue = mean( roiImage ).val[0]; - currentEpslonValue = mean( roiEpslon ).val[0]; + currentPixelValue = (float) mean( roiImage ).val[0]; + currentEpslonValue = (float) mean( roiEpslon ).val[0]; // scan background model vector for ( int z = 0; z < N_DS; z++ ) @@ -252,8 +258,8 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat // Select the current template 2 channel matrix, select ROI and compute the mean for each channel separately Mat roiTemplate = ( * ( backgroundModel[z] ) )( roi ); Scalar templateMean = mean( roiTemplate ); - currentB = templateMean[0]; - currentC = templateMean[1]; + currentB = (float) templateMean[0]; + currentC = (float) templateMean[1]; if( ( currentC ) > 0 ) //The current template is active { @@ -348,7 +354,7 @@ bool MotionSaliencyBinWangApr2014::templateOrdering() // set new C0 value for current model) swap( bgModel_0P[j][1], bgModel_1P[j][1] ); - bgModel_0P[j][1] = gamma * thetaL; + bgModel_0P[j][1] = (float) gamma * thetaL; } @@ -388,12 +394,12 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, neighborhoodCheck = true; } - float roiSize = 3; // FIXED ROI SIZE, not change until you first appropriately adjust the following controls in the EVALUATION section! + int roiSize = 3; // FIXED ROI SIZE, not change until you first appropriately adjust the following controls in the EVALUATION section! int countNonZeroElements = 0; std::vector mv; - Mat replicateCurrentBAMat( roiSize, roiSize, CV_32FC1 ); - Mat backgroundModelROI( roiSize, roiSize, CV_32FC1 ); - Mat diffResult( roiSize, roiSize, CV_32FC1 ); + Mat replicateCurrentBAMat( roiSize, roiSize, CV_32F ); + Mat backgroundModelROI( roiSize, roiSize, CV_32F ); + Mat diffResult( roiSize, roiSize, CV_32F ); // Scan all pixels of finalBFMask and all pixels of others models (the dimension are the same) const float* finalBFMaskP; @@ -449,9 +455,9 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, if( i > 0 && j > 0 && i < ( backgroundModel[z]->rows - 1 ) && j < ( backgroundModel[z]->cols - 1 ) ) { split( *backgroundModel[z], mv ); - backgroundModelROI = mv[0]( Rect( j - floor( roiSize / 2 ), i - floor( roiSize / 2 ), roiSize, roiSize ) ); + backgroundModelROI = mv[0]( Rect( j - (int) floor( roiSize / 2 ), i - (int) floor( roiSize / 2 ), roiSize, roiSize ) ); } - else if( i == 0 && j == 0 ) // upper left + else if( i == 0 && j == 0 ) // upper leftt { split( *backgroundModel[z], mv ); backgroundModelROI = mv[0]( Rect( j, i, ceil( roiSize / 2 ), ceil( roiSize / 2 ) ) ); @@ -459,37 +465,40 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, else if( j == 0 && i > 0 && i < ( backgroundModel[z]->rows - 1 ) ) // middle left { split( *backgroundModel[z], mv ); - backgroundModelROI = mv[0]( Rect( j, i - floor( roiSize / 2 ), ceil( roiSize / 2 ), roiSize ) ); + backgroundModelROI = mv[0]( Rect( j, i - (int) floor( roiSize / 2 ), ceil( roiSize / 2 ), roiSize ) ); } else if( i == ( backgroundModel[z]->rows - 1 ) && j == 0 ) //down left { split( *backgroundModel[z], mv ); - backgroundModelROI = mv[0]( Rect( j, i - floor( roiSize / 2 ), ceil( roiSize / 2 ), ceil( roiSize / 2 ) ) ); + backgroundModelROI = mv[0]( Rect( j, i - (int) floor( roiSize / 2 ), ceil( roiSize / 2 ), ceil( roiSize / 2 ) ) ); } else if( i == 0 && j > 0 && j < ( backgroundModel[z]->cols - 1 ) ) // upper - middle { split( *backgroundModel[z], mv ); - backgroundModelROI = mv[0]( Rect( ( j - floor( roiSize / 2 ) ), i, roiSize, ceil( roiSize / 2 ) ) ); + backgroundModelROI = mv[0]( Rect( ( j - (int) floor( roiSize / 2 ) ), i, roiSize, (int) ceil( roiSize / 2 ) ) ); } else if( i == ( backgroundModel[z]->rows - 1 ) && j > 0 && j < ( backgroundModel[z]->cols - 1 ) ) //down middle { split( *backgroundModel[z], mv ); - backgroundModelROI = mv[0]( Rect( j - floor( roiSize / 2 ), i - floor( roiSize / 2 ), roiSize, ceil( roiSize / 2 ) ) ); + backgroundModelROI = mv[0]( + Rect( j - (int) floor( roiSize / 2 ), i - (int) floor( roiSize / 2 ), roiSize, (int) ceil( roiSize / 2 ) ) ); } else if( i == 0 && j == ( backgroundModel[z]->cols - 1 ) ) // upper right { split( *backgroundModel[z], mv ); - backgroundModelROI = mv[0]( Rect( j - floor( roiSize / 2 ), i, ceil( roiSize / 2 ), ceil( roiSize / 2 ) ) ); + backgroundModelROI = mv[0]( Rect( j - floor( roiSize / 2 ), i, (int) ceil( roiSize / 2 ), (int) ceil( roiSize / 2 ) ) ); } else if( j == ( backgroundModel[z]->cols - 1 ) && i > 0 && i < ( backgroundModel[z]->rows - 1 ) ) // middle - right { split( *backgroundModel[z], mv ); - backgroundModelROI = mv[0]( Rect( j - floor( roiSize / 2 ), i - floor( roiSize / 2 ), ceil( roiSize / 2 ), roiSize ) ); + backgroundModelROI = mv[0]( + Rect( j - (int) floor( roiSize / 2 ), i - (int) floor( roiSize / 2 ), (int) ceil( roiSize / 2 ), roiSize ) ); } else if( i == ( backgroundModel[z]->rows - 1 ) && j == ( backgroundModel[z]->cols - 1 ) ) // down right { split( *backgroundModel[z], mv ); - backgroundModelROI = mv[0]( Rect( j - floor( roiSize / 2 ), i - floor( roiSize / 2 ), ceil( roiSize / 2 ), ceil( roiSize / 2 ) ) ); + backgroundModelROI = mv[0]( + Rect( j - (int) floor( roiSize / 2 ), i - (int) floor( roiSize / 2 ), (int) ceil( roiSize / 2 ), (int) ceil( roiSize / 2 ) ) ); } /* Check if the value of current pixel BA in potentialBackground model is already contained in at least one of its neighbors' @@ -540,8 +549,8 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, lowResolutionDetection( image.getMat(), lowResBFMask ); /*imshow( "highResBFMask", highResBFMask * 255 ); - imshow( "lowResBFMask", lowResBFMask * 255 ); -*/ + imshow( "lowResBFMask", lowResBFMask * 255 ); + */ // Compute the final background-foreground mask. One pixel is marked as foreground if and only if it is // foreground in both masks (full and low) diff --git a/modules/saliency/src/saliency_init.cpp b/modules/saliency/src/saliency_init.cpp index bdecf1740..aa82d0a9e 100644 --- a/modules/saliency/src/saliency_init.cpp +++ b/modules/saliency/src/saliency_init.cpp @@ -45,11 +45,8 @@ namespace cv { -CV_INIT_ALGORITHM( - StaticSaliencySpectralResidual, - "SALIENCY.SPECTRAL_RESIDUAL", - obj.info()->addParam( obj, "resImWidth", obj.resImWidth); - obj.info()->addParam( obj, "resImHeight", obj.resImHeight)); +CV_INIT_ALGORITHM( StaticSaliencySpectralResidual, "SALIENCY.SPECTRAL_RESIDUAL", + obj.info()->addParam( obj, "resImWidth", obj.resImWidth); obj.info()->addParam( obj, "resImHeight", obj.resImHeight) ); //CV_INIT_ALGORITHM( MotionSaliencySuBSENSE, "SALIENCY.SuBSENSE", ); @@ -57,19 +54,15 @@ 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) ); -CV_INIT_ALGORITHM( - MotionSaliencyBinWangApr2014, - "SALIENCY.BinWangApr2014", - obj.info()->addParam( obj, "imgSize", obj.imgSize, false, - reinterpret_cast( &MotionSaliencyBinWangApr2014::getWsize ), - reinterpret_cast( &MotionSaliencyBinWangApr2014::setWsize ) ) ); +CV_INIT_ALGORITHM( MotionSaliencyBinWangApr2014, "SALIENCY.BinWangApr2014", + obj.info()->addParam( obj, "imageWidth", obj.imageWidth); obj.info()->addParam( obj, "imageHeight", obj.imageHeight) ); bool initModule_saliency( void ) { bool all = true; all &= !StaticSaliencySpectralResidual_info_auto.name().empty(); //all &= !MotionSaliencySuBSENSE_info_auto.name().empty(); -all &= !MotionSaliencyBinWangApr2014_info_auto.name().empty(); + all &= !MotionSaliencyBinWangApr2014_info_auto.name().empty(); all &= !ObjectnessBING_info_auto.name().empty(); return all;