Commit 8bc385fe authored by jaco's avatar jaco

tuning + morphology operators added + code cleaning

parent c82bd87c
...@@ -208,7 +208,7 @@ int main( int argc, char** argv ) ...@@ -208,7 +208,7 @@ int main( int argc, char** argv )
Mat saliencyMap; Mat saliencyMap;
if( saliencyAlgorithm->computeSaliency( frame, saliencyMap ) ) if( saliencyAlgorithm->computeSaliency( frame, saliencyMap ) )
{ {
std::cout << "motion saliency done" << std::endl; //std::cout << "motion saliency done" << std::endl;
} }
imshow( "image", frame ); imshow( "image", frame );
......
...@@ -43,6 +43,9 @@ ...@@ -43,6 +43,9 @@
//TODO delete highgui include //TODO delete highgui include
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#define thetaA_VAL 200
#define thetaL_VAL 250
namespace cv namespace cv
{ {
...@@ -63,8 +66,8 @@ MotionSaliencyBinWangApr2014::MotionSaliencyBinWangApr2014() ...@@ -63,8 +66,8 @@ MotionSaliencyBinWangApr2014::MotionSaliencyBinWangApr2014()
alpha = 0.01; // Learning rate alpha = 0.01; // Learning rate
L0 = 300; // Upper-bound values for C0 (efficacy of the first template (matrices) of backgroundModel 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 L1 = 200; // Upper-bound values for C1 (efficacy of the second template (matrices) of backgroundModel
thetaL = 30; // T0, T1 swap threshold thetaL = thetaL_VAL; // T0, T1 swap threshold
thetaA = 15; thetaA = thetaA_VAL;
gamma = 3; gamma = 3;
neighborhoodCheck = true; neighborhoodCheck = true;
...@@ -74,7 +77,7 @@ MotionSaliencyBinWangApr2014::MotionSaliencyBinWangApr2014() ...@@ -74,7 +77,7 @@ MotionSaliencyBinWangApr2014::MotionSaliencyBinWangApr2014()
bool MotionSaliencyBinWangApr2014::init() bool MotionSaliencyBinWangApr2014::init()
{ {
epslonPixelsValue = Mat( imgSize->height, imgSize->width, CV_32F, Scalar( 70 ) ); epslonPixelsValue = Mat( imgSize->height, imgSize->width, CV_32F, Scalar( 20 ) );
// Median of range [18, 80] advised in reference paper. // Median of range [18, 80] advised in reference paper.
// Since data is even, the median is estimated using two values ​​that occupy // 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). // the position (n / 2) and ((n / 2) +1) (choose their arithmetic mean).
...@@ -89,7 +92,7 @@ bool MotionSaliencyBinWangApr2014::init() ...@@ -89,7 +92,7 @@ bool MotionSaliencyBinWangApr2014::init()
{ {
Mat* tmpm = new Mat; 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) ); tmpm->setTo( Scalar( NAN, 0 ) );
Ptr<Mat> tmp = Ptr<Mat>( tmpm ); Ptr<Mat> tmp = Ptr<Mat>( tmpm );
backgroundModel[i] = tmp; backgroundModel[i] = tmp;
} }
...@@ -112,22 +115,16 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M ...@@ -112,22 +115,16 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M
float currentEpslonValue; float currentEpslonValue;
bool backgFlag = false; bool backgFlag = false;
Mat test( image.rows, image.cols, CV_8U ); for ( int i = 0; i <= K; i++ )
Mat test1( image.rows, image.cols, CV_8U );
test.setTo( 255 );
for ( int i = 0; i < test.rows; i++ )
{ {
for ( int j = 0; j < test.cols; j++ ) vector<Mat> spl;
{ split( * ( backgroundModel[i] ), spl );
if( backgroundModel[0]->at<Vec2f>( i, j )[1] == 0 ) stringstream windowTitle;
{ windowTitle << "TEST_t" << i << "B";
test.at<uchar>( i, j ) = 0; Mat convert;
} spl.at( 0 ).convertTo( convert, CV_8UC1 );
test1.at<uchar>( i, j ) = (int) backgroundModel[0]->at<Vec2f>( i, j )[0]; imshow( windowTitle.str().c_str(), convert );
}
} }
imshow( "test_T0c", test );
imshow( "test_T0b", test1 );
// Initially, all pixels are considered as foreground and then we evaluate with the background model // Initially, all pixels are considered as foreground and then we evaluate with the background model
highResBFMask.create( image.rows, image.cols, CV_32F ); highResBFMask.create( image.rows, image.cols, CV_32F );
...@@ -154,9 +151,6 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M ...@@ -154,9 +151,6 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M
currentPixelValue = pImage[j]; currentPixelValue = pImage[j];
currentEpslonValue = pEpslon[j]; currentEpslonValue = pEpslon[j];
if( i == 50 && j == 50 )
cout << "currentPixelValue :" << currentPixelValue << endl << "currentEpslonValue :" << currentEpslonValue << endl;
int counter = 0; int counter = 0;
for ( size_t z = 0; z < backgroundModel.size(); z++ ) for ( size_t z = 0; z < backgroundModel.size(); z++ )
{ {
...@@ -175,12 +169,7 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M ...@@ -175,12 +169,7 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M
currentB = & ( backgroundModel.at( z )->at<Vec2f>( i, j )[0] ); currentB = & ( backgroundModel.at( z )->at<Vec2f>( i, j )[0] );
currentC = & ( backgroundModel.at( z )->at<Vec2f>( i, j )[1] ); currentC = & ( backgroundModel.at( z )->at<Vec2f>( i, j )[1] );
if( i == 50 && j == 50 )
{
cout << "zeta:" << z << " currentB :" << *currentB << endl << "currentC :" << *currentC << endl;
}
//continue; //continue;
if( ( *currentC ) > 0 ) //The current template is active if( ( *currentC ) > 0 ) //The current template is active
{ {
//cout<< "DIFFERENCE: "<<abs( currentPixelValue - ( *currentB ) )<<endl; //cout<< "DIFFERENCE: "<<abs( currentPixelValue - ( *currentB ) )<<endl;
...@@ -206,12 +195,6 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M ...@@ -206,12 +195,6 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M
} }
if( i == 50 && j == 50 )
{
cout << "DOPO IF: " << endl;
cout << "zeta:" << z << " currentB_A :" << currentB << endl << "currentC_A :" << currentC << endl;
cout << "zeta:" << z << " currentB :" << *currentB << endl << "currentC :" << *currentC << endl << endl;
}
} // end "for" cicle of template vector } // end "for" cicle of template vector
} }
...@@ -223,8 +206,6 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M ...@@ -223,8 +206,6 @@ bool MotionSaliencyBinWangApr2014::fullResolutionDetection( const Mat& image2, M
} }
} // end "for" cicle of all image's pixels } // end "for" cicle of all image's pixels
//cout<<" STATISTICA :"<<countDec<<"/"<< image.rows*image.cols<< " = "<<(float)countDec/(float)(image.rows*image.cols)*100<<" %"<<endl;
return true; return true;
} }
...@@ -385,7 +366,7 @@ bool MotionSaliencyBinWangApr2014::templateOrdering() ...@@ -385,7 +366,7 @@ bool MotionSaliencyBinWangApr2014::templateOrdering()
} }
bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, const Mat& image ) bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, const Mat& image )
{ {
Mat test( image.rows, image.cols, CV_8U ); /* Mat test( image.rows, image.cols, CV_8U );
for ( int i = 0; i < test.rows; i++ ) for ( int i = 0; i < test.rows; i++ )
{ {
for ( int j = 0; j < test.cols; j++ ) for ( int j = 0; j < test.cols; j++ )
...@@ -394,7 +375,7 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, ...@@ -394,7 +375,7 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask,
test.at<uchar>( i, j ) = (int) potentialBackground.at<Vec2f>( i, j )[0]; test.at<uchar>( i, j ) = (int) potentialBackground.at<Vec2f>( i, j )[0];
} }
} }
imshow( "test_BA", test ); imshow( "test_BA", test );*/
std::vector<Mat> temp; std::vector<Mat> temp;
split( *backgroundModel[0], temp ); split( *backgroundModel[0], temp );
...@@ -403,12 +384,14 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, ...@@ -403,12 +384,14 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask,
if( countNonZero( temp.at( 1 ) ) <= ( temp.at( 1 ).cols * temp.at( 1 ).rows ) / 2 ) if( countNonZero( temp.at( 1 ) ) <= ( temp.at( 1 ).cols * temp.at( 1 ).rows ) / 2 )
{ {
thetaA = 15; thetaA = 15;
thetaL = 40;
neighborhoodCheck = false; neighborhoodCheck = false;
} }
else else
{ {
thetaA = 15; thetaA = thetaA_VAL;
thetaL = thetaL_VAL;
neighborhoodCheck = true; neighborhoodCheck = true;
} }
...@@ -565,42 +548,6 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, ...@@ -565,42 +548,6 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image,
t.setTo( 50 ); t.setTo( 50 );
backgroundModel.at( 0 ) = t; */ 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<int>( i, j ) = i + j;
stringstream str;
str << backgroundModel[0]->at<Vec2f>( 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<int>( i, j ) = i + j;
stringstream str;
str << backgroundModel[0]->at<Vec2f>( i, j )[1] << " ";
ofs5 << str.str();
}
stringstream str2;
str2 << "\n";
ofs5 << str2.str();
}
ofs5.close();
fullResolutionDetection( image.getMat(), highResBFMask ); fullResolutionDetection( image.getMat(), highResBFMask );
lowResolutionDetection( image.getMat(), lowResBFMask ); lowResolutionDetection( image.getMat(), lowResBFMask );
...@@ -620,59 +567,13 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, ...@@ -620,59 +567,13 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image,
//templateReplacement( highResBFMask, image.getMat() ); //templateReplacement( highResBFMask, image.getMat() );
templateOrdering(); templateOrdering();
//highResBFMask.copyTo( saliencyMap ); /* Mat kernel = getStructuringElement( MORPH_RECT, Size( 3, 3 ));
Mat kernel2 = getStructuringElement( MORPH_RECT, Size( 3, 3 ));
std::ofstream ofs; morphologyEx(saliencyMap.getMat(), saliencyMap.getMat(), MORPH_OPEN, kernel);
ofs.open( "highResBFMask.txt", std::ofstream::out ); morphologyEx(saliencyMap.getMat(), saliencyMap.getMat(), MORPH_CLOSE, kernel2);*/
for ( int i = 0; i < highResBFMask.rows; i++ )
{
for ( int j = 0; j < highResBFMask.cols; j++ )
{
//highResBFMask.at<int>( i, j ) = i + j;
stringstream str;
str << highResBFMask.at<float>( 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<float>( i, j ) << " ";
ofs2 << str.str();
}
stringstream str2;
str2 << "\n";
ofs2 << str2.str();
}
ofs2.close();
std::ofstream ofs3; //highResBFMask.copyTo( saliencyMap );
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<float>( i, j ) << " ";
ofs3 << str.str();
}
stringstream str2;
str2 << "\n";
ofs3 << str2.str();
}
ofs3.close();
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment