Thresholding is a technique in OpenCV, which is the assignment of pixel values in relation to the threshold value provided. In thresholding, each pixel value is compared with the threshold value. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value (generally 255). Thresholding is a very popular segmentation technique, used for separating an object considered as a foreground from its background. A threshold is a value which has two regions on its either side i.e. below the threshold or above the threshold.
In Computer Vision, this technique of thresholding is done on grayscale images. So initially, the image has to be converted in grayscale color space.
If f (x, y) > T then f (x, y) = 0 else f (x, y) = 255 where f (x, y) = Coordinate Pixel Value T = Threshold Value.
In OpenCV with Python, the function cv2.threshold is used for thresholding.
Syntax: cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)
-> source: Input Image array (must be in Grayscale).
-> thresholdValue: Value of Threshold below and above which pixel values will change accordingly.
-> maxVal: Maximum value that can be assigned to a pixel.
-> thresholdingTechnique: The type of thresholding to be applied.
The basic Thresholding technique is Binary Thresholding. For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value.
The different Simple Thresholding Techniques are:
cv2.THRESH_BINARY: If pixel intensity is greater than the set threshold, value set to 255, else set to 0 (black).
cv2.THRESH_BINARY_INV: Inverted or Opposite case of
cv.THRESH_TRUNC: If pixel intensity value is greater than threshold, it is truncated to the threshold. The pixel values are set to be the same as the threshold. All other values remain the same.
cv.THRESH_TOZERO: Pixel intensity is set to 0, for all the pixels intensity, less than the threshold value.
cv.THRESH_TOZERO_INV: Inverted or Opposite case of
Below is the Python code explaining different Simple Thresholding Techniques –
- Python | Thresholding techniques using OpenCV | Set-3 (Otsu Thresholding)
- Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
- OpenCV: Segmentation using Thresholding
- Mahotas - Bernsen local thresholding
- MATLAB | Change the color of background pixels by OTSU Thresholding
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- Looping Techniques in Python
- Short Circuiting Techniques in Python
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Gun Detection using Python-OpenCV
- Python OpenCV: Meanshift
- OpenCV Python Tutorial
- Simple Chat Room using Python
- Simple Keyboard Racing with Python
- Python | Simple GUI calculator using Tkinter
- Simple Plot in Python using Matplotlib
- Python | Simple calculator using Tkinter
- Perspective Transformation - Python OpenCV
- Template matching using OpenCV in Python
- Python OpenCV | cv2.erode() method
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.