Commit b2f4affb authored by jaco's avatar jaco

templateReplacement function completed

parent 9a196a9c
...@@ -120,7 +120,7 @@ class CV_EXPORTS_W MotionSaliencyBinWangApr2014 : public MotionSaliency ...@@ -120,7 +120,7 @@ class CV_EXPORTS_W MotionSaliencyBinWangApr2014 : public MotionSaliency
// Background model maintenance functions // Background model maintenance functions
bool templateOrdering(); bool templateOrdering();
bool templateReplacement( Mat finalBFMask, Mat image ); bool templateReplacement( const Mat& finalBFMask, const Mat& image );
// Decision threshold adaptation and Activity control function // Decision threshold adaptation and Activity control function
//bool activityControl(vector<Mat> noisePixelMask); //bool activityControl(vector<Mat> noisePixelMask);
......
...@@ -156,29 +156,33 @@ int main( int argc, char** argv ) ...@@ -156,29 +156,33 @@ int main( int argc, char** argv )
} }
else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 ) else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 )
{ {
Ptr<Size> size= Ptr<Size>( new Size( 32, 32 ) ) ; // TODO INSERT CAPTURE CICLE FOR MOTION
saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->setWsize(size); //int testSize = 34;
//Ptr<Size> size = Ptr<Size>( new Size( testSize, testSize ) );
Ptr<Size> size = Ptr<Size>( new Size( image.cols, image.rows ) );
saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->setWsize( size );
saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->init(); saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->init();
// Create an fake image test /* // Create an fake image test
Mat test( 32, 32, CV_8U ); Mat test( testSize, testSize, CV_8U );
RNG rand; RNG rand;
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++ )
if(i<12 && i>=6 && j<12 && j>=6) {
test.at<uchar>(i,j)=255; if( i < 6 && i >= 0 && j < 34 && j >= 28 )
else test.at<uchar>( i, j ) = 255;
test.at<uchar>(i,j)=rand.uniform(40,60); else
test.at<uchar>( i, j ) = rand.uniform( 40, 60 );
}
}
} */
//imshow("Test", test); //imshow("Test", test);
//waitKey(0); //waitKey(0);
Mat saliencyMap; Mat saliencyMap;
if( saliencyAlgorithm->computeSaliency( test, saliencyMap ) ) if( saliencyAlgorithm->computeSaliency( image /*test*/, saliencyMap ) )
{ {
std::cout<<"motion saliency done"<<std::endl; std::cout << "motion saliency done" << std::endl;
} }
} }
......
...@@ -40,8 +40,6 @@ ...@@ -40,8 +40,6 @@
//M*/ //M*/
#include "precomp.hpp" #include "precomp.hpp"
//TODO delete highgui include
#include <opencv2/highgui.hpp>
namespace cv namespace cv
{ {
...@@ -200,7 +198,7 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat ...@@ -200,7 +198,7 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat
currentEpslonValue = mean( epslonPixelsValue, ROIMask ).val[0]; currentEpslonValue = mean( epslonPixelsValue, ROIMask ).val[0];
// scan background model vector // scan background model vector
for ( size_t z = 0; z < N_DS; z++ ) for ( int z = 0; z < N_DS; z++ )
{ {
// Select the current template 2 channel matrix, select ROI and compute the mean for each channel separately // Select the current template 2 channel matrix, select ROI and compute the mean for each channel separately
...@@ -298,13 +296,14 @@ bool MotionSaliencyBinWangApr2014::templateOrdering() ...@@ -298,13 +296,14 @@ bool MotionSaliencyBinWangApr2014::templateOrdering()
return true; return true;
} }
bool MotionSaliencyBinWangApr2014::templateReplacement( Mat finalBFMask, Mat image ) bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask, const Mat& image )
{ {
int roiSize = 3; float roiSize = 3; // FIXED ROI SIZE, not change until you first appropriately adjust the following controls in the EVALUATION section!
int countNonZeroElements = NAN; int countNonZeroElements = NAN;
Mat replicateCurrentBAMat( roiSize, roiSize, CV_8U ); std::vector<Mat> mv;
Mat backgroundModelROI( roiSize, roiSize, CV_32F ); Mat replicateCurrentBAMat( roiSize, roiSize, CV_32FC1 );
Mat diffResult( roiSize, roiSize, CV_32F ); Mat backgroundModelROI( roiSize, roiSize, CV_32FC1 );
Mat diffResult( roiSize, roiSize, CV_32FC1 );
// Scan all pixels of finalBFMask and all pixels of others models (the dimension are the same) // Scan all pixels of finalBFMask and all pixels of others models (the dimension are the same)
for ( int i = 0; i < finalBFMask.rows; i++ ) for ( int i = 0; i < finalBFMask.rows; i++ )
...@@ -332,76 +331,91 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( Mat finalBFMask, Mat ima ...@@ -332,76 +331,91 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( Mat finalBFMask, Mat ima
{ {
potentialBackground.at<Vec2f>( i, j )[1] -= 1; potentialBackground.at<Vec2f>( i, j )[1] -= 1;
} }
} /////////////////// END of potentialBackground model MAINTENANCE/////////////////// /*}*/ /////////////////// END of potentialBackground model MAINTENANCE///////////////////
/////////////////// EVALUATION of potentialBackground values ///////////////////
/////////////////// EVALUATION of potentialBackground values /////////////////// if( potentialBackground.at<Vec2f>( i, j )[1] > thetaA )
if( potentialBackground.at<Vec2f>( i, j )[1] > thetaA )
{
// replicate currentBA value
replicateCurrentBAMat.setTo( potentialBackground.at<Vec2f>( i, j )[0] );
for ( size_t z = 1; z < backgroundModel.size(); z++ )
{ {
// Neighborhood of current pixel in the current background model template. // replicate currentBA value
// The ROI is centered in the pixel coordinates replicateCurrentBAMat.setTo( potentialBackground.at<Vec2f>( i, j )[0] );
// TODO border check
if( ( i - floor( roiSize / 2 ) >= 0 ) && ( j - floor( roiSize / 2 ) >= 0 )
&& ( i + floor( roiSize / 2 ) <= ( backgroundModel[z].rows - 1 ) ) && ( j + floor( roiSize / 2 ) <= ( backgroundModel[z].cols - 1 ) ) )
{
backgroundModelROI = backgroundModel[z]( Rect( i - floor( roiSize / 2 ), j - floor( roiSize / 2 ), roiSize, roiSize ) );
}
else if( i == 0 && j == 0 ) // upper left
{
backgroundModelROI = backgroundModel[z]( Rect( i, j, round( roiSize / 2 ), round( roiSize / 2 ) ) );
}
else if( j == 0 && ( i - floor( roiSize / 2 ) >= 0 ) ) // middle left
{
backgroundModelROI = backgroundModel[z]( Rect( i - floor( roiSize / 2 ), j, roiSize, round( roiSize / 2 ) ) );
}
else if( i == ( backgroundModel[z].rows - 1 ) && j == 0 ) //down left
{
backgroundModelROI = backgroundModel[z]( Rect( i - floor( roiSize / 2 ), j, round( roiSize / 2 ), round( roiSize / 2 ) ) );
}
else if( i == 0 && ( j - floor( roiSize / 2 ) >= 0 ) ) // upper - middle
{
backgroundModelROI = backgroundModel[z]( Rect( i, j - floor( roiSize / 2 ), round( roiSize / 2 ), roiSize ) );
}
else if( i == ( backgroundModel[z].rows - 1 ) && ( j - floor( roiSize / 2 ) >= 0 ) ) //down middle
{
backgroundModelROI = backgroundModel[z]( Rect( i - floor( roiSize / 2 ), j - floor( roiSize / 2 ), round( roiSize / 2 ), roiSize ) );
}
else if( i == 0 && j == ( backgroundModel[z].cols - 1 ) ) // upper right
{
backgroundModelROI = backgroundModel[z]( Rect( i, j - floor( roiSize / 2 ), roiSize, round( roiSize / 2 ) ) );
}
else if( j == ( backgroundModel[z].cols - 1 ) && ( i - floor( roiSize / 2 ) >= 0 ) ) // middle - right
{
backgroundModelROI = backgroundModel[z]( Rect( i - floor( roiSize / 2 ), j - floor( roiSize / 2 ), round( roiSize / 2 ), roiSize ) );
}
else if( i == ( backgroundModel[z].rows - 1 ) && j == ( backgroundModel[z].cols - 1 ) ) // down right
{
backgroundModelROI = backgroundModel[z](
Rect( i - floor( roiSize / 2 ), j - floor( roiSize / 2 ), round( roiSize / 2 ), round( roiSize / 2 ) ) );
}
/* Check if the value of current pixel BA in potentialBackground model is already contained in at least one of its neighbors' for ( size_t z = 0; z < backgroundModel.size(); z++ )
* background model
*/
absdiff( replicateCurrentBAMat, backgroundModelROI, diffResult );
threshold( diffResult, diffResult, epslonPixelsValue.at<float>( i, j ), 255, THRESH_BINARY_INV );
countNonZeroElements = countNonZero( diffResult );
if( countNonZeroElements > 0 )
{ {
/////////////////// REPLACEMENT of backgroundModel template /////////////////// // Neighborhood of current pixel in the current background model template.
//replace TA with current TK // The ROI is centered in the pixel coordinates
//backgroundModel[backgroundModel.size()-1].at<Vec2f>( i, j )[0]=potentialBackground.at<Vec2f>( i, j )[0];
//backgroundModel[backgroundModel.size()-1].at<Vec2f>( i, j )[1]= potentialBackground.at<Vec2f>( i, j )[1]; /*if( ( i - floor( roiSize / 2 ) >= 0 ) && ( j - floor( roiSize / 2 ) >= 0 )
backgroundModel[backgroundModel.size() - 1].at<Vec2f>( i, j ) = potentialBackground.at<Vec2f>( i, j ); && ( i + floor( roiSize / 2 ) <= ( backgroundModel[z].rows - 1 ) )
break; && ( j + floor( roiSize / 2 ) <= ( backgroundModel[z].cols - 1 ) ) ) */
} if( i > 0 && j > 0 && i < ( backgroundModel[z].rows - 1 ) && j < ( backgroundModel[z].cols - 1 ) )
} // end for backgroundModel size {
} // close if of EVALUATION split( backgroundModel[z], mv );
backgroundModelROI = mv.at( 0 )( Rect( j - floor( roiSize / 2 ), i - floor( roiSize / 2 ), roiSize, roiSize ) );
}
else if( i == 0 && j == 0 ) // upper left
{
split( backgroundModel[z], mv );
backgroundModelROI = mv.at( 0 )( Rect( j, i, ceil( roiSize / 2 ), ceil( roiSize / 2 ) ) );
}
else if( j == 0 && i > 0 && i < ( backgroundModel[z].rows - 1 ) ) // middle left
{
split( backgroundModel[z], mv );
backgroundModelROI = mv.at( 0 )( Rect( j, i - floor( roiSize / 2 ), ceil( roiSize / 2 ), roiSize ) );
}
else if( i == ( backgroundModel[z].rows - 1 ) && j == 0 ) //down left
{
split( backgroundModel[z], mv );
backgroundModelROI = mv.at( 0 )( Rect( j, i - 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.at( 0 )( Rect( ( j - floor( roiSize / 2 ) ), i, roiSize, 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.at( 0 )( Rect( j - floor( roiSize / 2 ), i - floor( roiSize / 2 ), roiSize, ceil( roiSize / 2 ) ) );
}
else if( i == 0 && j == ( backgroundModel[z].cols - 1 ) ) // upper right
{
split( backgroundModel[z], mv );
backgroundModelROI = mv.at( 0 )( Rect( j - floor( roiSize / 2 ), i, ceil( roiSize / 2 ), 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.at( 0 )( Rect( j - floor( roiSize / 2 ), i - floor( roiSize / 2 ), ceil( roiSize / 2 ), roiSize ) );
}
else if( i == ( backgroundModel[z].rows - 1 ) && j == ( backgroundModel[z].cols - 1 ) ) // down right
{
split( backgroundModel[z], mv );
backgroundModelROI = mv.at( 0 )( Rect( j - floor( roiSize / 2 ), i - floor( roiSize / 2 ), ceil( roiSize / 2 ), ceil( roiSize / 2 ) ) );
}
/* Check if the value of current pixel BA in potentialBackground model is already contained in at least one of its neighbors'
* background model
*/
resize( replicateCurrentBAMat, replicateCurrentBAMat, Size( backgroundModelROI.cols, backgroundModelROI.rows ), 0, 0, INTER_LINEAR );
resize( diffResult, diffResult, Size( backgroundModelROI.cols, backgroundModelROI.rows ), 0, 0, INTER_LINEAR );
absdiff( replicateCurrentBAMat, backgroundModelROI, diffResult );
threshold( diffResult, diffResult, epslonPixelsValue.at<float>( i, j ), 255, THRESH_BINARY_INV );
countNonZeroElements = countNonZero( diffResult );
if( countNonZeroElements > 0 )
{
/////////////////// REPLACEMENT of backgroundModel template ///////////////////
//replace TA with current TK
//TODO CHECK BACKGROUND MODEL COUNTER ASSIGNEMENT
//backgroundModel[backgroundModel.size()-1].at<Vec2f>( i, j )[0]=potentialBackground.at<Vec2f>( i, j )[0];
//backgroundModel[backgroundModel.size()-1].at<Vec2f>( i, j )[1]= potentialBackground.at<Vec2f>( i, j )[1];
backgroundModel[backgroundModel.size() - 1].at<Vec2f>( i, j ) = potentialBackground.at<Vec2f>( i, j );
break;
}
} // end for backgroundModel size
} // close if of EVALUATION
} // end of if( finalBFMask.at<uchar>( i, j ) == 1 ) // i.e. the corresponding frame pixel has been market as foreground
} // end of second for } // end of second for
} // end of first for } // end of first for
...@@ -415,25 +429,21 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image, ...@@ -415,25 +429,21 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image,
Mat highResBFMask; Mat highResBFMask;
Mat lowResBFMask; Mat lowResBFMask;
Mat not_lowResBFMask; Mat not_lowResBFMask;
Mat finalBFMask;
Mat noisePixelsMask; Mat noisePixelsMask;
/*Mat t( image.getMat().rows, image.getMat().cols, CV_32FC2 );
t.setTo( 50 );
backgroundModel.at( 0 ) = t; */
fullResolutionDetection( image.getMat(), highResBFMask ); fullResolutionDetection( image.getMat(), highResBFMask );
lowResolutionDetection( image.getMat(), lowResBFMask ); lowResolutionDetection( image.getMat(), lowResBFMask );
// Compute the final background-foreground mask. One pixel is marked as foreground if and only if it is // 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) // foreground in both masks (full and low)
bitwise_and( highResBFMask, lowResBFMask, finalBFMask ); bitwise_and( highResBFMask, lowResBFMask, saliencyMap );
// Detect the noise pixels (i.e. for a given pixel, fullRes(pixel) = foreground and lowRes(pixel)= background) // Detect the noise pixels (i.e. for a given pixel, fullRes(pixel) = foreground and lowRes(pixel)= background)
bitwise_not( lowResBFMask, not_lowResBFMask ); bitwise_not( lowResBFMask, not_lowResBFMask );
bitwise_and( highResBFMask, not_lowResBFMask, noisePixelsMask ); bitwise_and( highResBFMask, not_lowResBFMask, noisePixelsMask );
templateOrdering(); templateOrdering();
templateReplacement( finalBFMask, image.getMat() ); templateReplacement( saliencyMap.getMat(), image.getMat() );
templateOrdering(); templateOrdering();
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