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:
- 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
It also includes a robust statistical machine learning library, that contains a number of different classifiers used to support the above areas.
To use OpenCV, simply import or include the required libraries and start making use of the myriad of available functions.
Thresholding is a very popular segmentation technique, used for separating an object from its background. In the article below, I have described various techniques used to threshold grayscale images(8-bit).
The process of thresholding involves, comparing each pixel value of the image (pixel intensity) to a specified threshold. This divides all the pixels of the input image into 2 groups:
- Pixels having intensity value lower than threshold.
- Pixels having intensity value greater than threshold.
These 2 groups are now given different values, depending on various segmentation types.
OpenCV supports 5 different thresholding schemes on Grayscale(8-bit) images using the function :
Double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
*Below a list of thresholding types is given.
The input RGB image is first converted to a grayscale image before thresholding is done.
- 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.
To compile OpenCV programs, you need to have OpenCV library installed on your system. I will be posting a simple tutorial for the same, in the coming days.
If you have already installed OpenCV, run the below code with the input image of your choice.
- Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
- Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)
- Python | Thresholding techniques using OpenCV | Set-3 (Otsu Thresholding)
- MATLAB | Change the color of background pixels by OTSU Thresholding
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- How to find Segmentation Error in C & C++ ? (Using GDB)
- Image segmentation using Morphological operations in Python
- Introduction to OpenCV
- OpenCV - Overview
- Histograms Equalization in OpenCV
- OpenCV | Loading Video
- OpenCV | Saving an Image
- Filter Color with OpenCV
- Image Inpainting using OpenCV
- OpenCV | Displaying an Image
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.