This example shows you how to remove the background of an image using depth camera with range filter.
Copy double thresholdValueUp = 530 ;
double thresholdValueDown = 385 ;
char input;
input = cv :: waitKey ( 1 );
if (input == 'p' )
{
if (thresholdValueUp + 5 <= 2000 )
thresholdValueUp += 5 ;
else
thresholdValueUp = 2000 ;
std :: cout << "Threshold Value (Up):" << thresholdValueUp << " (Down):" << thresholdValueDown << std :: endl;
}
else if (input == 'o' )
{
if (thresholdValueUp - 5 >= 0 && thresholdValueUp - 5 >= thresholdValueDown)
thresholdValueUp -= 5 ;
std :: cout << "Threshold Value (Up):" << thresholdValueUp << " (Down):" << thresholdValueDown << std :: endl;
}
else if (input == 'x' )
{
if (thresholdValueDown + 5 <= 2000 && thresholdValueDown + 5 <= thresholdValueUp)
thresholdValueDown += 5 ;
std :: cout << "Threshold Value (Up):" << thresholdValueUp << " (Down):" << thresholdValueDown << std :: endl;
}
else if (input == 'z' )
{
if (thresholdValueDown - 5 >= 0 )
thresholdValueDown -= 5 ;
else
thresholdValueDown = 0 ;
std :: cout << "Threshold Value (Up):" << thresholdValueUp << " (Down):" << thresholdValueDown << std :: endl;
}
Using these two values to create range filter mask image like we did before.
Copy cv :: threshold (thres , thres , thresholdValueDown , 1024 , cv :: THRESH_TOZERO);
cv :: threshold (thres , thres , thresholdValueUp , 1024 , cv :: THRESH_TOZERO_INV);
cv :: threshold (thres , thres , 1 , 1024 , cv :: THRESH_BINARY);
Copy thres . convertTo (thres , CV_8UC1 , 255.0 / 1024.0 );
cv :: Mat res;
cv :: bitwise_and (colorMat , colorMat , res , thres);