diff --git a/modules/saliency/samples/computeSaliency.cpp b/modules/saliency/samples/computeSaliency.cpp index fd9799853..5d3d67132 100644 --- a/modules/saliency/samples/computeSaliency.cpp +++ b/modules/saliency/samples/computeSaliency.cpp @@ -63,6 +63,24 @@ static void help() int main( int argc, char** argv ) { + /* Mat A(3,3,CV_32FC2); + A.setTo(Scalar(NAN,1)); + + Mat B(3,3,CV_32F); + B.setTo(1); + + Mat result; + + bitwise_and( A, B, result ); + + for(int i=0; i< A.rows; i++){ + for( int j=0; j< A.cols; j++){ + cout<< A.at(i,j)[0]<<"-"<(i,j)[1]<<" " ; + } + cout<( 0 ); @@ -157,32 +175,52 @@ int main( int argc, char** argv ) else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 ) { // TODO INSERT CAPTURE CICLE FOR MOTION - //int testSize = 34; + //int testSize = 64; //Ptr size = Ptr( new Size( testSize, testSize ) ); Ptr size = Ptr( new Size( image.cols, image.rows ) ); saliencyAlgorithm.dynamicCast()->setWsize( size ); saliencyAlgorithm.dynamicCast()->init(); /*// Create an fake image test - Mat test( testSize, testSize, CV_8U ); - RNG rand; - for ( int i = 0; i < test.rows; i++ ) - { - for ( int j = 0; j < test.cols; j++ ) - { - if( i < 6 && i >= 0 && j < 6 && j >= 0 ) - test.at < uchar > ( i, j ) = 255; - else - test.at < uchar > ( i, j ) = rand.uniform( 40, 60 ); + Mat test( testSize, testSize, CV_8U ); + RNG rand; + for ( int i = 0; i < test.rows; i++ ) + { + for ( int j = 0; j < test.cols; j++ ) + { + if( i < 6 && i >= 0 && j < 6 && j >= 0 ) + test.at < uchar > ( i, j ) = 255; + else + test.at < uchar > ( i, j ) = rand.uniform( 40, 60 ); - } - } */ - //imshow("Test", test); - //waitKey(0); - Mat saliencyMap; - if( saliencyAlgorithm->computeSaliency( image /*test*/, saliencyMap ) ) + } + } */ + + bool paused=false; + while ( true ) { - std::cout << "motion saliency done" << std::endl; + if( !paused ) + { + + cap >> frame; + cvtColor(frame, frame, COLOR_BGR2GRAY); + + Mat saliencyMap; + if( saliencyAlgorithm->computeSaliency( frame, saliencyMap ) ) + { + std::cout << "motion saliency done" << std::endl; + } + + imshow( "image", frame ); + imshow( "saliencyMap", saliencyMap * 255 ); + } + + char c = (char) waitKey( 2 ); + if( c == 'q' ) + break; + if( c == 'p' ) + paused = !paused; + } } diff --git a/modules/saliency/src/motionSaliencyBinWangApr2014.cpp b/modules/saliency/src/motionSaliencyBinWangApr2014.cpp index ea80dd554..f6f24c184 100644 --- a/modules/saliency/src/motionSaliencyBinWangApr2014.cpp +++ b/modules/saliency/src/motionSaliencyBinWangApr2014.cpp @@ -78,12 +78,12 @@ bool MotionSaliencyBinWangApr2014::init() potentialBackground = Mat( imgSize->height, imgSize->width, CV_32FC2 ); backgroundModel = std::vector( K + 1, Mat::zeros( imgSize->height, imgSize->width, CV_32FC2 ) ); //TODO set to nan - potentialBackground.setTo( 0 ); + potentialBackground.setTo( Scalar(NAN,0) ); //TODO set to nan for ( size_t i = 0; i < backgroundModel.size(); i++ ) { - backgroundModel[i].setTo( 0 ); + backgroundModel[i].setTo( Scalar(NAN, 0) ); } epslonPixelsValue.setTo( 48.5 ); // Median of range [18, 80] advised in reference paper. @@ -114,19 +114,19 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M bool backgFlag = false; // Initially, all pixels are considered as foreground and then we evaluate with the background model - highResBFMask.create( image.rows, image.cols, CV_8UC1 ); + highResBFMask.create( image.rows, image.cols, CV_32F ); highResBFMask.setTo( 1 ); uchar* pImage; float* pEpslon; - uchar* pMask; + float* pMask; // Scan all pixels of image for ( int i = 0; i < image.rows; i++ ) { pImage = image.ptr( i ); pEpslon = epslonPixelsValue.ptr( i ); - pMask = highResBFMask.ptr( i ); + pMask = highResBFMask.ptr( i ); for ( int j = 0; j < image.cols; j++ ) { backgFlag = false; @@ -185,8 +185,9 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat { std::vector mv; split( backgroundModel[0], mv ); + //if at least the first template is activated / initialized for all pixels - if( countNonZero( mv.at( 1 ) ) == ( mv.at( 1 ).cols * mv.at( 1 ).rows ) ) + if( countNonZero( mv.at( 1 ) ) > ( mv.at( 1 ).cols * mv.at( 1 ).rows ) / 2 ) { float currentPixelValue; float currentEpslonValue; @@ -201,7 +202,7 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat Mat currentModel; // Initially, all pixels are considered as foreground and then we evaluate with the background model - lowResBFMask.create( image.rows, image.cols, CV_8UC1 ); + lowResBFMask.create( image.rows, image.cols, CV_32F ); lowResBFMask.setTo( 1 ); // Scan all the ROI of original matrices @@ -248,11 +249,11 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat roi = roi + Point( N, 0 ); if( ( roi.x + ( roi.width - 1 ) ) > ( image.cols - 1 ) && ( roi.y + ( N - 1 ) ) <= ( image.rows - 1 ) ) { - roi = Rect( Point( roi.x, roi.y ), Size( abs( ( image.cols - 1 ) - roi.x )+1, N ) ); + roi = Rect( Point( roi.x, roi.y ), Size( abs( ( image.cols - 1 ) - roi.x ) + 1, N ) ); } else if( ( roi.x + ( roi.width - 1 ) ) > ( image.cols - 1 ) && ( roi.y + ( N - 1 ) ) > ( image.rows - 1 ) ) { - roi = Rect( Point( roi.x, roi.y ), Size( abs( ( image.cols - 1 ) - roi.x )+1, abs( ( image.rows - 1 ) - roi.y )+1 ) ); + roi = Rect( Point( roi.x, roi.y ), Size( abs( ( image.cols - 1 ) - roi.x ) + 1, abs( ( image.rows - 1 ) - roi.y ) + 1 ) ); } } //Shift the ROI from up to down follow the block dimension, also bringing it back to beginning of row @@ -260,17 +261,16 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat roi.y += N; if( ( roi.y + ( roi.height - 1 ) ) > ( image.rows - 1 ) ) { - roi = Rect( Point( roi.x, roi.y ), Size( N, abs( ( image.rows - 1 ) - roi.y )+1 ) ); + roi = Rect( Point( roi.x, roi.y ), Size( N, abs( ( image.rows - 1 ) - roi.y ) + 1 ) ); } - cout << endl << endl; } return true; } else { - lowResBFMask.create( image.rows, image.cols, CV_8UC1 ); - lowResBFMask.setTo( 1 ); + lowResBFMask.create( image.rows, image.cols, CV_32F ); + lowResBFMask.setTo( NAN ); return false; } @@ -339,9 +339,9 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, split( backgroundModel[0], temp ); //if at least the first template is activated / initialized for all pixels - if( countNonZero( temp.at( 1 ) ) != ( temp.at( 1 ).cols * temp.at( 1 ).rows ) ) + if( countNonZero( temp.at( 1 ) ) <= ( temp.at( 1 ).cols * temp.at( 1 ).rows )/2 ) { - thetaA = 20; + thetaA = 2; neighborhoodCheck = false; } @@ -364,7 +364,7 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, for ( int j = 0; j < finalBFMask.cols; j++ ) { /////////////////// MAINTENANCE of potentialBackground model /////////////////// - if( finalBFMask.at( i, j ) == 1 ) // i.e. the corresponding frame pixel has been market as foreground + if( finalBFMask.at( i, j ) == 1 ) // i.e. the corresponding frame pixel has been market as foreground { /* For the pixels with CA= 0, if the current frame pixel has been classified as foreground, its value * will be loaded into BA and CA will be set to 1*/ @@ -467,6 +467,9 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, //backgroundModel[backgroundModel.size()-1].at( i, j )[0]=potentialBackground.at( i, j )[0]; //backgroundModel[backgroundModel.size()-1].at( i, j )[1]= potentialBackground.at( i, j )[1]; backgroundModel[backgroundModel.size() - 1].at( i, j ) = potentialBackground.at( i, j ); + //potentialBackground.at( i, j )[0]=-255; + //potentialBackground.at( i, j )[1]=0; + break; } } // end for backgroundModel size @@ -493,8 +496,44 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, Mat noisePixelsMask; /*Mat t( image.getMat().rows, image.getMat().cols, CV_32FC2 ); - t.setTo( 50 ); - backgroundModel.at( 0 ) = t; */ + t.setTo( 50 ); + backgroundModel.at( 0 ) = t; */ + + std::ofstream ofs4; + ofs4.open( "TEMPLATE_0_B.txt", std::ofstream::out ); + + for ( int i = 0; i < backgroundModel[0].rows; i++ ) + { + for ( int j = 0; j < backgroundModel[0].cols; j++ ) + { + //highResBFMask.at( i, j ) = i + j; + stringstream str; + str << backgroundModel[0].at( i, j )[0] << " "; + ofs4 << str.str(); + } + stringstream str2; + str2 << "\n"; + ofs4 << str2.str(); + } + ofs4.close(); + + std::ofstream ofs5; + ofs5.open( "TEMPLATE_0_C.txt", std::ofstream::out ); + + for ( int i = 0; i < backgroundModel[0].rows; i++ ) + { + for ( int j = 0; j < backgroundModel[0].cols; j++ ) + { + //highResBFMask.at( i, j ) = i + j; + stringstream str; + str << backgroundModel[0].at( i, j )[1] << " "; + ofs5 << str.str(); + } + stringstream str2; + str2 << "\n"; + ofs5 << str2.str(); + } + ofs5.close(); fullResolutionDetection( image.getMat(), highResBFMask ); lowResolutionDetection( image.getMat(), lowResBFMask ); @@ -509,8 +548,63 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, templateOrdering(); templateReplacement( saliencyMap.getMat(), image.getMat() ); + //templateReplacement( highResBFMask, image.getMat() ); templateOrdering(); + //highResBFMask.copyTo(saliencyMap); + + std::ofstream ofs; + ofs.open( "highResBFMask.txt", std::ofstream::out ); + + for ( int i = 0; i < highResBFMask.rows; i++ ) + { + for ( int j = 0; j < highResBFMask.cols; j++ ) + { + //highResBFMask.at( i, j ) = i + j; + stringstream str; + str << highResBFMask.at( i, j ) << " "; + ofs << str.str(); + } + stringstream str2; + str2 << "\n"; + ofs << str2.str(); + } + ofs.close(); + + std::ofstream ofs2; + ofs2.open( "lowResBFMask.txt", std::ofstream::out ); + + for ( int i = 0; i < lowResBFMask.rows; i++ ) + { + for ( int j = 0; j < lowResBFMask.cols; j++ ) + { + stringstream str; + str << lowResBFMask.at( i, j ) << " "; + ofs2 << str.str(); + } + stringstream str2; + str2 << "\n"; + ofs2 << str2.str(); + } + ofs2.close(); + + std::ofstream ofs3; + ofs3.open( "SALMAP.txt", std::ofstream::out ); + + for ( int i = 0; i < saliencyMap.getMat().rows; i++ ) + { + for ( int j = 0; j < saliencyMap.getMat().cols; j++ ) + { + stringstream str; + str << saliencyMap.getMat().at( i, j ) << " "; + ofs3 << str.str(); + } + stringstream str2; + str2 << "\n"; + ofs3 << str2.str(); + } + ofs3.close(); + return true; }