OpenCV: Segmentation using Thresholding
Last Updated :
15 Jan, 2018
Improve
In this article, a basic technique for object segmentation called Thresholding.
But before moving into anymore detail, below is a brief overview of OpenCV.
OpenCV (Open Source Computer Vision) is a cross platform, open-source library of programming functions, aimed at performing real-time computer vision tasks in a wide variety of fields, such as:
InputArray src: Input Image (Mat, 8-bit or 32-bit)
OutputArray dst: Output Image ( same size as input)
double thresh: Set threshold value
double maxval: maxVal, used in type 1 and 2
int type* :Specifies the type of threshold to be use. (0-4)
*Below a list of thresholding types is given.
Input Image
The input RGB image is first converted to a grayscale image before thresholding is done.
Thresholding types
CPP
- Facial recognition
- Iris Recognition Systems
- Gesture recognition
- Human–computer interaction (HCI)
- Mobile robotics
- Object identification
- Segmentation and recognition
- Stereopsis stereo vision: depth perception from 2 cameras
- Augmented reality
- Pixels having intensity value lower than threshold.
- Pixels having intensity value greater than threshold.


- Binary Threshold(int type=0)
Of the two groups obtained earlier, the group having members with pixel intensity, greater than the set threshold, are assignment “Max_Value”, or in case of a grayscale, a value of 255 (white). The members of the remaining group have their pixel intensities set to 0 (black).
If the pixel intensity value at (x, y) in source image, is greater than threshold, the value in final image is set to “maxVal”.
- Inverted Binary Threshold(int type=1)
Inv. Binary threshold is the same as Binary threshold. The only essential difference being, in Inv.Binary thresholding, the group having pixel intensities greater than set threshold, gets assigned ‘0’, whereas the remaining pixels having intensities, less than the threshold, are set to “maxVal”.
If the pixel intensity value at (x, y) in source image, is greater than threshold, the value in final image is set to “0”, else it is set to “maxVal”.
- Truncate Thresholding(int type=2)
The group having pixel intensities greater than the set threshold, is truncated to the set threshold or in other words, the pixel values are set to be same as the set threshold. All other values remain the same.
If the pixel intensity value at (x, y) in source image, is greater than threshold, the value in final image is set to “threshold”, else it is unchanged.
- Threshold to Zero(int type=3)
A very simple thresholding technique, wherein we set the pixel intensity to ‘0’, for all the pixels of the group having pixel intensity value, less than the threshold.
If the pixel intensity value at (x, y) in source image, is greater than threshold, the value at (x, y) in the final image doesn’t change. All the remaining pixels are set to ‘0’.
- Threshold to Zero, Inverted(int type=4)
Similar to the previous technique, here we set the pixel intensity to ‘0’, for all the pixels of the group having pixel intensity value, greater than the threshold.
If the pixel intensity value at (x, y) in source image, is greater than threshold, the value at (x, y) in the final image is set to ‘0’. All the remaining pixel value are unchanged.
// CPP program to demonstrate segmentation
// thresholding.
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
int main(int argc, char** argv)
{
if (argc != 2)
{
cout << " Usage: <Sourceprogram>"
" <ImageToLoad>" << endl;
return -1;
}
int threshold_value = 0;
// Valid Values: 0, 1, 2, 3, 4
int threshold_type = 2;
// maxVal, useful for threshold_type 1 and 2
int maxVal = 255;
// Source image
Mat src = imread(argv[1], 1);
cvNamedWindow("Original", CV_WINDOW_NORMAL);
imshow("Original", src);
Mat src_gray, dst;
// Convert the image to GrayScale
cvtColor(src, src_gray, CV_BGR2GRAY);
// Create a window to display results
cvNamedWindow("Result", CV_WINDOW_NORMAL);
createTrackbar("Threshold", "Result",
&threshold_value, 255);
while (1)
{
threshold(src_gray, dst, threshold_value,
maxVal, threshold_type);
imshow("Result", dst);
waitKey(1);
}
}