From b78f713653a515229260f19b2a8c3429cc4edc64 Mon Sep 17 00:00:00 2001 From: jaco Date: Wed, 23 Jul 2014 16:55:51 +0200 Subject: [PATCH] lowResolutionDetection bug fixed --- modules/saliency/samples/computeSaliency.cpp | 23 +++- .../src/motionSaliencyBinWangApr2014.cpp | 109 +++++++++++++----- 2 files changed, 101 insertions(+), 31 deletions(-) diff --git a/modules/saliency/samples/computeSaliency.cpp b/modules/saliency/samples/computeSaliency.cpp index 56ff6ce17..4b306000a 100644 --- a/modules/saliency/samples/computeSaliency.cpp +++ b/modules/saliency/samples/computeSaliency.cpp @@ -156,10 +156,29 @@ int main( int argc, char** argv ) } else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 ) { + Ptr size= Ptr( new Size( 64, 64 ) ) ; + saliencyAlgorithm.dynamicCast()->setWsize(size); + saliencyAlgorithm.dynamicCast()->init(); + + // Create an fake image test + Mat test( 64, 64, CV_8U ); + RNG rand; + for(int i=0; i=6 && j<12 && j>=6) + test.at(i,j)=255; + else + test.at(i,j)=rand.uniform(40,60); + + } + //imshow("Test", test); + //waitKey(0); + Mat saliencyMap; - if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) ) + if( saliencyAlgorithm->computeSaliency( test, saliencyMap ) ) { - std::cout<<"OKKKK"< namespace cv { @@ -74,11 +76,13 @@ bool MotionSaliencyBinWangApr2014::init() epslonPixelsValue = Mat( imgSize->height, imgSize->width, CV_32F ); 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( NAN ); - - for ( size_t i = 0; i < backgroundModel.size(); i++ ) - backgroundModel[i].setTo( NAN ); + //TODO set to nan + for ( size_t i = 0; i < backgroundModel.size(); i++ ){ + backgroundModel[i].setTo( 0 ); + } epslonPixelsValue.setTo( 48.5 ); // Median of range [18, 80] advised in reference paper. // Since data is even, the median is estimated using two values ​​that occupy @@ -157,7 +161,6 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image, Ma return true; } -//typedef Rect_ Rect; bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat& lowResBFMask ) { float currentPixelValue; @@ -175,13 +178,16 @@ 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.size().height / ( N * N ), image.size().width / ( N * N ), CV_8UC1 ); + //lowResBFMask.create( image.size().height / ( N * N ), image.size().width / ( N * N ), CV_8UC1 ); + //lowResBFMask.setTo( 1 ); + + lowResBFMask.create( image.rows, image.cols, CV_8UC1 ); lowResBFMask.setTo( 1 ); // Scan all the ROI of original matrices that correspond to the pixels of new resized matrices - for ( int i = 0; i < image.rows/( N * N ); i++ ) + for ( int i = 0; i < image.rows / N; i++ ) { - for ( int j = 0; j < image.cols/( N * N ); j++ ) + for ( int j = 0; j < image.cols / N; j++ ) { // Reset and update ROI mask ROIMask.setTo( 0 ); @@ -207,21 +213,23 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat { // The correspondence pixel in the BF mask is set as background ( 0 value) // TODO replace "at" with more efficient matrix access - lowResBFMask.at( i, j ) = 0; + //lowResBFMask.at( i, j ) = 0; + lowResBFMask.setTo( 0, ROIMask ); break; } } } // Shift the ROI from left to right follow the block dimension - roi = roi + Point( 0, N ); + roi = roi + Point( N, 0 ); } //Shift the ROI from up to down follow the block dimension, also bringing it back to beginning of row - roi = roi + Point( N, - ( image.cols - N ) ); + roi.x = 0; + roi.y += N; } // UPSAMPLE the lowResBFMask to the original image dimension, so that it's then possible to compare the results // of lowlResolutionDetection with the fullResolutionDetection - resize( lowResBFMask, lowResBFMask, image.size(), 0, 0, INTER_LINEAR ); + //resize( lowResBFMask, lowResBFMask, image.size(), 0, 0, INTER_LINEAR ); return true; } @@ -246,20 +254,25 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( Mat finalBFMask ) bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, OutputArray saliencyMap ) { + Mat highResBFMask; + Mat lowResBFMask; + Mat t( image.getMat().rows, image.getMat().cols, CV_32FC2 ); + t.setTo( 50 ); + backgroundModel.at( 0 ) = t; - Mat Test( 36, 36, CV_32F ); - Mat Results; + fullResolutionDetection( image.getMat(), highResBFMask ); + lowResolutionDetection( image.getMat(), lowResBFMask ); std::ofstream ofs; - ofs.open( "TEST.txt", std::ofstream::out ); + ofs.open( "highResBFMask.txt", std::ofstream::out ); - for ( int i = 0; i < Test.size().height; i++ ) + for ( int i = 0; i < highResBFMask.rows; i++ ) { - for ( int j = 0; j < Test.size().width; j++ ) + for ( int j = 0; j < highResBFMask.cols; j++ ) { - Test.at( i, j ) = i + j; + //highResBFMask.at( i, j ) = i + j; stringstream str; - str << i + j << " "; + str << (int) highResBFMask.at( i, j ) << " "; ofs << str.str(); } stringstream str2; @@ -268,19 +281,15 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, } ofs.close(); -//blur( Test, Results, Size( 4, 4 ) ); - medianBlur( Test, Results, 3 ); -//pyrDown(Results,Results, Size(Test.size().height/9, Test.size().width/9)); - std::ofstream ofs2; - ofs2.open( "RESULTS.txt", std::ofstream::out ); + ofs2.open( "lowResBFMask.txt", std::ofstream::out ); - for ( int i = 0; i < Results.size().height; i++ ) + for ( int i = 0; i < lowResBFMask.rows; i++ ) { - for ( int j = 0; j < Results.size().width; j++ ) + for ( int j = 0; j < lowResBFMask.cols; j++ ) { stringstream str; - str << Results.at( i, j ) << " "; + str << (int) lowResBFMask.at( i, j ) << " "; ofs2 << str.str(); } stringstream str2; @@ -289,8 +298,50 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, } ofs2.close(); - std::cout << "TEST SIZE: " << Test.size().height << " " << Test.size().width << " RESULTS SIZE: " << Results.size().height << " " - << Results.size().width << std::endl; + /*Mat Test( 16, 16, CV_32F ); + Mat Results; + + std::ofstream ofs; + ofs.open( "TEST.txt", std::ofstream::out ); + + for ( int i = 0; i < Test.size().height; i++ ) + { + for ( int j = 0; j < Test.size().width; j++ ) + { + Test.at( i, j ) = i + j; + stringstream str; + str << i + j << " "; + ofs << str.str(); + } + stringstream str2; + str2 << "\n"; + ofs << str2.str(); + } + ofs.close(); + + //blur( Test, Results, Size( 4, 4 ) ); + medianBlur( Test, Results, 3 ); + //pyrDown(Results,Results, Size(Test.size().height/9, Test.size().width/9)); + + std::ofstream ofs2; + ofs2.open( "RESULTS.txt", std::ofstream::out ); + + for ( int i = 0; i < Results.size().height; i++ ) + { + for ( int j = 0; j < Results.size().width; j++ ) + { + stringstream str; + str << Results.at( i, j ) << " "; + ofs2 << str.str(); + } + stringstream str2; + str2 << "\n"; + ofs2 << str2.str(); + } + ofs2.close(); + + std::cout << "TEST SIZE: " << Test.size().height << " " << Test.size().width << " RESULTS SIZE: " << Results.size().height << " " + << Results.size().width << std::endl; */ return true; }