Goal
In this tutorial you will learn how to:
- Perform basic thresholding operations using OpenCV function cv::threshold
Cool Theory
- Note
- The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler. What is
Thresholding?
Types of Thresholding
Threshold Binary
Threshold Binary, Inverted
Truncate
Threshold to Zero
Threshold to Zero, Inverted
Code
The tutorial code's is shown lines below. You can also download it from here
int threshold_value = 0;
int threshold_type = 3;
int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;
Mat src, src_gray, dst;
const char* window_name = "Threshold Demo";
const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
const char* trackbar_value = "Value";
void Threshold_Demo( int, void* );
int main( int argc, char** argv )
{
String imageName(
"../data/stuff.jpg");
if (argc > 1)
{
imageName = argv[1];
}
{ return -1; }
window_name, &threshold_type,
max_type, Threshold_Demo );
window_name, &threshold_value,
max_value, Threshold_Demo );
Threshold_Demo( 0, 0 );
for(;;)
{
if( c == 27 )
{ break; }
}
}
void Threshold_Demo( int, void* )
{
threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
}
Explanation
- Let's check the general structure of the program:
- Load an image. If it is BGR we convert it to Grayscale. For this, remember that we can use the function cv::cvtColor :
String imageName("../data/stuff.jpg");
if (argc > 1)
{
imageName = argv[1];
}
{ return -1; }
- Create a window to display the result
- Create
trackbars for the user to enter user input:
- Type of thresholding: Binary, To Zero, etc...
- Threshold value
window_name, &threshold_type,
max_type, Threshold_Demo );
window_name, &threshold_value,
max_value, Threshold_Demo );
- Wait until the user enters the threshold value, the type of thresholding (or until the program exits)
- Whenever the user changes the value of any of the Trackbars, the function Threshold_Demo is called:
void Threshold_Demo( int, void* )
{
threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
}
As you can see, the function cv::threshold is invoked. We give
parameters:
- src_gray: Our input image
- dst: Destination (output) image
- threshold_value: The
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0074.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0068.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0072.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0065.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0073.png?V=2.7.0)
value with respect to which the thresholding operation is made
- max_BINARY_value: The value used with the Binary thresholding operations (to set the chosen pixels)
- threshold_type: One of the
thresholding operations. They are listed in the comment section of the function above.
Results
After compiling this program, run it giving a path to an image as argument. For instance, for an input image as:
First, we try to threshold our image with a binary threhold inverted. We expect that the pixels brighter than the ![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0074.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0068.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0072.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0065.png?V=2.7.0)
![](https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS/TeX/png/Math/Italic/400/0073.png?V=2.7.0)
will turn dark, which is what actually happens, as we can see in the snapshot below (notice from the original image, that the doggie's tongue and eyes are particularly bright in comparison with the image, this is reflected in the output image).
Now we try with the threshold to zero. With this, we expect that the darkest pixels (below the threshold) will become completely black, whereas the pixels with value greater than the threshold will keep its original value. This is verified by the following snapshot of the output image: