mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-20 21:40:49 +08:00
sample modified for motion and NAN management
This commit is contained in:
@@ -63,6 +63,24 @@ static void help()
|
|||||||
|
|
||||||
int main( int argc, char** argv )
|
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<Vec2f>(i,j)[0]<<"-"<<A.at<Vec2f>(i,j)[1]<<" " ;
|
||||||
|
}
|
||||||
|
cout<<endl;
|
||||||
|
}
|
||||||
|
exit(0);*/
|
||||||
|
|
||||||
CommandLineParser parser( argc, argv, keys );
|
CommandLineParser parser( argc, argv, keys );
|
||||||
|
|
||||||
String saliency_algorithm = parser.get<String>( 0 );
|
String saliency_algorithm = parser.get<String>( 0 );
|
||||||
@@ -157,7 +175,7 @@ int main( int argc, char** argv )
|
|||||||
else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 )
|
else if( saliency_algorithm.find( "BinWangApr2014" ) == 0 )
|
||||||
{
|
{
|
||||||
// TODO INSERT CAPTURE CICLE FOR MOTION
|
// TODO INSERT CAPTURE CICLE FOR MOTION
|
||||||
//int testSize = 34;
|
//int testSize = 64;
|
||||||
//Ptr<Size> size = Ptr<Size>( new Size( testSize, testSize ) );
|
//Ptr<Size> size = Ptr<Size>( new Size( testSize, testSize ) );
|
||||||
Ptr<Size> size = Ptr<Size>( new Size( image.cols, image.rows ) );
|
Ptr<Size> size = Ptr<Size>( new Size( image.cols, image.rows ) );
|
||||||
saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->setWsize( size );
|
saliencyAlgorithm.dynamicCast<MotionSaliencyBinWangApr2014>()->setWsize( size );
|
||||||
@@ -177,13 +195,33 @@ int main( int argc, char** argv )
|
|||||||
|
|
||||||
}
|
}
|
||||||
} */
|
} */
|
||||||
//imshow("Test", test);
|
|
||||||
//waitKey(0);
|
bool paused=false;
|
||||||
|
while ( true )
|
||||||
|
{
|
||||||
|
if( !paused )
|
||||||
|
{
|
||||||
|
|
||||||
|
cap >> frame;
|
||||||
|
cvtColor(frame, frame, COLOR_BGR2GRAY);
|
||||||
|
|
||||||
Mat saliencyMap;
|
Mat saliencyMap;
|
||||||
if( saliencyAlgorithm->computeSaliency( image /*test*/, saliencyMap ) )
|
if( saliencyAlgorithm->computeSaliency( frame, saliencyMap ) )
|
||||||
{
|
{
|
||||||
std::cout << "motion saliency done" << std::endl;
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@@ -78,12 +78,12 @@ bool MotionSaliencyBinWangApr2014::init()
|
|||||||
potentialBackground = Mat( imgSize->height, imgSize->width, CV_32FC2 );
|
potentialBackground = Mat( imgSize->height, imgSize->width, CV_32FC2 );
|
||||||
backgroundModel = std::vector<Mat>( K + 1, Mat::zeros( imgSize->height, imgSize->width, CV_32FC2 ) );
|
backgroundModel = std::vector<Mat>( K + 1, Mat::zeros( imgSize->height, imgSize->width, CV_32FC2 ) );
|
||||||
//TODO set to nan
|
//TODO set to nan
|
||||||
potentialBackground.setTo( 0 );
|
potentialBackground.setTo( Scalar(NAN,0) );
|
||||||
|
|
||||||
//TODO set to nan
|
//TODO set to nan
|
||||||
for ( size_t i = 0; i < backgroundModel.size(); i++ )
|
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.
|
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;
|
bool backgFlag = false;
|
||||||
|
|
||||||
// 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_8UC1 );
|
highResBFMask.create( image.rows, image.cols, CV_32F );
|
||||||
highResBFMask.setTo( 1 );
|
highResBFMask.setTo( 1 );
|
||||||
|
|
||||||
uchar* pImage;
|
uchar* pImage;
|
||||||
float* pEpslon;
|
float* pEpslon;
|
||||||
uchar* pMask;
|
float* pMask;
|
||||||
|
|
||||||
// Scan all pixels of image
|
// Scan all pixels of image
|
||||||
for ( int i = 0; i < image.rows; i++ )
|
for ( int i = 0; i < image.rows; i++ )
|
||||||
{
|
{
|
||||||
pImage = image.ptr<uchar>( i );
|
pImage = image.ptr<uchar>( i );
|
||||||
pEpslon = epslonPixelsValue.ptr<float>( i );
|
pEpslon = epslonPixelsValue.ptr<float>( i );
|
||||||
pMask = highResBFMask.ptr<uchar>( i );
|
pMask = highResBFMask.ptr<float>( i );
|
||||||
for ( int j = 0; j < image.cols; j++ )
|
for ( int j = 0; j < image.cols; j++ )
|
||||||
{
|
{
|
||||||
backgFlag = false;
|
backgFlag = false;
|
||||||
@@ -185,8 +185,9 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat
|
|||||||
{
|
{
|
||||||
std::vector<Mat> mv;
|
std::vector<Mat> mv;
|
||||||
split( backgroundModel[0], mv );
|
split( backgroundModel[0], mv );
|
||||||
|
|
||||||
//if at least the first template is activated / initialized for all pixels
|
//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 currentPixelValue;
|
||||||
float currentEpslonValue;
|
float currentEpslonValue;
|
||||||
@@ -201,7 +202,7 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat
|
|||||||
Mat currentModel;
|
Mat currentModel;
|
||||||
|
|
||||||
// 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
|
||||||
lowResBFMask.create( image.rows, image.cols, CV_8UC1 );
|
lowResBFMask.create( image.rows, image.cols, CV_32F );
|
||||||
lowResBFMask.setTo( 1 );
|
lowResBFMask.setTo( 1 );
|
||||||
|
|
||||||
// Scan all the ROI of original matrices
|
// Scan all the ROI of original matrices
|
||||||
@@ -248,11 +249,11 @@ bool MotionSaliencyBinWangApr2014::lowResolutionDetection( const Mat& image, Mat
|
|||||||
roi = roi + Point( N, 0 );
|
roi = roi + Point( N, 0 );
|
||||||
if( ( roi.x + ( roi.width - 1 ) ) > ( image.cols - 1 ) && ( roi.y + ( N - 1 ) ) <= ( image.rows - 1 ) )
|
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 ) )
|
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
|
//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;
|
roi.y += N;
|
||||||
if( ( roi.y + ( roi.height - 1 ) ) > ( image.rows - 1 ) )
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lowResBFMask.create( image.rows, image.cols, CV_8UC1 );
|
lowResBFMask.create( image.rows, image.cols, CV_32F );
|
||||||
lowResBFMask.setTo( 1 );
|
lowResBFMask.setTo( NAN );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,9 +339,9 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask,
|
|||||||
split( backgroundModel[0], temp );
|
split( backgroundModel[0], temp );
|
||||||
|
|
||||||
//if at least the first template is activated / initialized for all pixels
|
//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;
|
neighborhoodCheck = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -364,7 +364,7 @@ bool MotionSaliencyBinWangApr2014::templateReplacement( const Mat& finalBFMask,
|
|||||||
for ( int j = 0; j < finalBFMask.cols; j++ )
|
for ( int j = 0; j < finalBFMask.cols; j++ )
|
||||||
{
|
{
|
||||||
/////////////////// MAINTENANCE of potentialBackground model ///////////////////
|
/////////////////// MAINTENANCE of potentialBackground model ///////////////////
|
||||||
if( finalBFMask.at<uchar>( i, j ) == 1 ) // i.e. the corresponding frame pixel has been market as foreground
|
if( finalBFMask.at<float>( 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
|
/* 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*/
|
* 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<Vec2f>( i, j )[0]=potentialBackground.at<Vec2f>( i, j )[0];
|
//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 )[1]= potentialBackground.at<Vec2f>( i, j )[1];
|
||||||
backgroundModel[backgroundModel.size() - 1].at<Vec2f>( i, j ) = potentialBackground.at<Vec2f>( i, j );
|
backgroundModel[backgroundModel.size() - 1].at<Vec2f>( i, j ) = potentialBackground.at<Vec2f>( i, j );
|
||||||
|
//potentialBackground.at<Vec2f>( i, j )[0]=-255;
|
||||||
|
//potentialBackground.at<Vec2f>( i, j )[1]=0;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} // end for backgroundModel size
|
} // end for backgroundModel size
|
||||||
@@ -496,6 +499,42 @@ 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 );
|
||||||
|
|
||||||
@@ -509,8 +548,63 @@ bool MotionSaliencyBinWangApr2014::computeSaliencyImpl( const InputArray image,
|
|||||||
|
|
||||||
templateOrdering();
|
templateOrdering();
|
||||||
templateReplacement( saliencyMap.getMat(), image.getMat() );
|
templateReplacement( saliencyMap.getMat(), image.getMat() );
|
||||||
|
//templateReplacement( highResBFMask, image.getMat() );
|
||||||
templateOrdering();
|
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<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;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user