Image Thresholding
Thresholding is a technique in OpenCV, which is the assignment of pixel values about 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).
Operation |
Purpose |
---|---|
cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique) |
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. This can be achieved using the cv2.threshold() method. |
cv2.adaptiveThreshold(source, maxVal, adaptiveMethod, thresholdType, blocksize, constant) | Adaptive thresholding is the method where the threshold value is calculated for smaller regions. This leads to different threshold values for different regions concerning the change in lighting. |
cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique) | In Otsu Thresholding, a value of the threshold isn’t chosen but is determined automatically. A bimodal image (two distinct image values) is considered. The histogram generated contains two peaks. |
Thresholding Techniques:
Thresholding Types |
Description |
---|---|
cv2.THRESH_BINARY | If pixel intensity is greater than the set threshold, the value is set to 255, else set to 0 |
cv2.THRESH_BINARY_INV | Inverted or Opposite case of cv2.THRESH_BINARY |
cv2.THRESH_TRUNC | If the pixel intensity value is greater than the threshold, it is truncated to the threshold. The pixel values are set to be the same as the threshold |
cv2.THRESH_TOZERO | Pixel intensity is set to 0, for all the pixels’ intensity, less than the threshold value |
cv2.THRESH_TOZERO_INV | Inverted or Opposite case of cv2.THRESH_TOZERO |
Simple Threshold
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. This can be achieved using the cv2.threshold() method.
cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)
Adaptive Thresholding
Adaptive thresholding is the method where the threshold value is calculated for smaller regions. This leads to different threshold values for different regions with respect to the change in lighting.
cv2.adaptiveThreshold(source, maxVal, adaptiveMethod, thresholdType, blocksize, constant)
Otsu Thresholding
In Otsu Thresholding, a value of the threshold isn’t chosen but is determined automatically. A bimodal image (two distinct image values) is considered. The histogram generated contains two peaks. So, a generic condition would be to choose a threshold value that lies in the middle of both the histogram peak values.
cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)
Python OpenCV Cheat Sheet
The Python OpenCV Cheat Sheet is your complete guide to mastering computer vision and image processing using Python. It’s designed to be your trusty companion, helping you quickly understand the important ideas, functions, and techniques in the OpenCV library. Whether you’re an experienced developer needing a quick reminder or a newcomer excited to start, this cheat sheet has got you covered.
In this article, we’ve gathered all the vital OpenCV concepts and explained them in simple terms. We’ve also provided practical examples to make things even clearer. You’ll learn everything from how to handle images to using advanced filters, spotting objects, and even exploring facial recognition. It’s all here to help you on your journey of discovering the amazing world of computer vision.
Table of Content
- Python OpenCV Cheat Sheet 2023
- Core Operations
- Drawing Shapes and Text on Images
- Arithmetic Operations on Images
- Morphological Operations on Images
- Geometric Transformations on Image
- Image Thresholding
- Edge/Line Detection (Features)
- Image Pyramids
- Changing the Colorspace of Images
- Smoothing Images
- Working With Videos
- Camera Calibration and 3D Reconstruction