Adaptive threshold opencv python
- Image Resizing using OpenCV | Python
- Python OpenCV | cv2.erode() method
- Python | Image blurring using OpenCV
- Python OpenCV | cv2.copyMakeBorder() method
- Python | Grayscaling of Images using OpenCV
- Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
- Erosion and Dilation of images using OpenCV in python
- OpenCV Python Program to analyze an image using Histogram
- Histograms Equalization in OpenCV
- Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)
- Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
- Python | Thresholding techniques using OpenCV | Set-3 (Otsu Thresholding)
- OpenCV: Segmentation using Thresholding
- Python OpenCV | cv2.cvtColor() method
- Filter Color with OpenCV
- Python | Denoising of colored images using opencv
- Python | Visualizing image in different color spaces
- Find Co-ordinates of Contours using OpenCV | Python
- Python | Bilateral Filtering
- Image Inpainting using OpenCV
- Python | Intensity Transformation Operations on Images
- Python | Image Registration using OpenCV
- Python | Background subtraction using OpenCV
- Background Subtraction in an Image using Concept of Running Average
- Python | Foreground Extraction in an Image using Grabcut Algorithm
- Python | Morphological Operations in Image Processing (Opening) | Set-1
- Python | Morphological Operations in Image Processing (Closing) | Set-2
- Python | Morphological Operations in Image Processing (Gradient) | Set-3
- Image segmentation using Morphological operations in Python
- Image Translation using OpenCV | Python
- Image Pyramid using OpenCV | Python
- Python | Program to extract frames using OpenCV
- Displaying the coordinates of the points clicked on the image using Python-OpenCV
- White and black dot detection using OpenCV | Python
- Python | OpenCV BGR color palette with trackbars
- Draw a rectangular shape and extract objects using Python’s OpenCV
- Invisible Cloak using OpenCV | Python Project
- ML | Unsupervised Face Clustering Pipeline
- Saving Operated Video from a webcam using OpenCV
- Face Detection using Python and OpenCV with webcam
- Opening multiple color windows to capture using OpenCV in Python
- Python | Play a video in reverse mode using OpenCV
- Template matching using OpenCV in Python
- Cartooning an Image using OpenCV – Python
- Vehicle detection using OpenCV Python
- Count number of Faces using Python – OpenCV
- Live Webcam Drawing using OpenCV
- Detect and Recognize Car License Plate from a video in real time
- Build GUI Application Pencil Sketch from Photo in Python
- Python OpenCV – Drowsiness Detection
- Face Alignment with OpenCV and Python
- Age Detection using Deep Learning in OpenCV
- Right and Left Hand Detection Using Python
- OpenCV Python: How to detect if a window is closed?
- Save frames of live video with timestamps – Python OpenCV
- Detecting low contrast images with OpenCV, scikit-image, and Python
- Animate image using OpenCV in Python
- Drawing a cross on an image with OpenCV
- Blur and anonymize faces with OpenCV and Python
- Face detection using Cascade Classifier using OpenCV-Python
- Real time object color detection using OpenCV
- Python – Writing to video with OpenCV
- Add image to a live camera feed using OpenCV-Python
- Face and Hand Landmarks Detection using Python – Mediapipe, OpenCV
- Emotion Based Music Player – Python Project
- Realtime Distance Estimation Using OpenCV – Python
- Webcam QR code scanner using OpenCV
- Color Identification in Images using Python – OpenCV
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Opencv Python program for Face Detection
- Image Resizing using OpenCV | Python
- Python OpenCV | cv2.erode() method
- Python | Image blurring using OpenCV
- Python OpenCV | cv2.copyMakeBorder() method
- Python | Grayscaling of Images using OpenCV
- Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
- Erosion and Dilation of images using OpenCV in python
- OpenCV Python Program to analyze an image using Histogram
- Histograms Equalization in OpenCV
- Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)
- Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
- Python | Thresholding techniques using OpenCV | Set-3 (Otsu Thresholding)
- OpenCV: Segmentation using Thresholding
- Python OpenCV | cv2.cvtColor() method
- Filter Color with OpenCV
- Python | Denoising of colored images using opencv
- Python | Visualizing image in different color spaces
- Find Co-ordinates of Contours using OpenCV | Python
- Python | Bilateral Filtering
- Image Inpainting using OpenCV
- Python | Intensity Transformation Operations on Images
- Python | Image Registration using OpenCV
- Python | Background subtraction using OpenCV
- Background Subtraction in an Image using Concept of Running Average
- Python | Foreground Extraction in an Image using Grabcut Algorithm
- Python | Morphological Operations in Image Processing (Opening) | Set-1
- Python | Morphological Operations in Image Processing (Closing) | Set-2
- Python | Morphological Operations in Image Processing (Gradient) | Set-3
- Image segmentation using Morphological operations in Python
- Image Translation using OpenCV | Python
- Image Pyramid using OpenCV | Python
- Python | Program to extract frames using OpenCV
- Displaying the coordinates of the points clicked on the image using Python-OpenCV
- White and black dot detection using OpenCV | Python
- Python | OpenCV BGR color palette with trackbars
- Draw a rectangular shape and extract objects using Python’s OpenCV
- Invisible Cloak using OpenCV | Python Project
- ML | Unsupervised Face Clustering Pipeline
- Saving Operated Video from a webcam using OpenCV
- Face Detection using Python and OpenCV with webcam
- Opening multiple color windows to capture using OpenCV in Python
- Python | Play a video in reverse mode using OpenCV
- Template matching using OpenCV in Python
- Cartooning an Image using OpenCV – Python
- Vehicle detection using OpenCV Python
- Count number of Faces using Python – OpenCV
- Live Webcam Drawing using OpenCV
- Detect and Recognize Car License Plate from a video in real time
- Build GUI Application Pencil Sketch from Photo in Python
- Python OpenCV – Drowsiness Detection
- Face Alignment with OpenCV and Python
- Age Detection using Deep Learning in OpenCV
- Right and Left Hand Detection Using Python
- OpenCV Python: How to detect if a window is closed?
- Save frames of live video with timestamps – Python OpenCV
- Detecting low contrast images with OpenCV, scikit-image, and Python
- Animate image using OpenCV in Python
- Drawing a cross on an image with OpenCV
- Blur and anonymize faces with OpenCV and Python
- Face detection using Cascade Classifier using OpenCV-Python
- Real time object color detection using OpenCV
- Python – Writing to video with OpenCV
- Add image to a live camera feed using OpenCV-Python
- Face and Hand Landmarks Detection using Python – Mediapipe, OpenCV
- Emotion Based Music Player – Python Project
- Realtime Distance Estimation Using OpenCV – Python
- Webcam QR code scanner using OpenCV
- Color Identification in Images using Python – OpenCV
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Opencv Python program for Face Detection
Adaptive threshold opencv python
Note To plot multiple images, we have used the plt.subplot() function. Please checkout the matplotlib docs for more details.
The code yields this result:
Adaptive Thresholding
In the previous section, we used one global value as a threshold. But this might not be good in all cases, e.g. if an image has different lighting conditions in different areas. In that case, adaptive thresholding can help. Here, the algorithm determines the threshold for a pixel based on a small region around it. So we get different thresholds for different regions of the same image which gives better results for images with varying illumination.
In addition to the parameters described above, the method cv.adaptiveThreshold takes three input parameters:
The adaptiveMethod decides how the threshold value is calculated:
- cv.ADAPTIVE_THRESH_MEAN_C: The threshold value is the mean of the neighbourhood area minus the constant C.
- cv.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold value is a gaussian-weighted sum of the neighbourhood values minus the constant C.
The blockSize determines the size of the neighbourhood area and C is a constant that is subtracted from the mean or weighted sum of the neighbourhood pixels.
The code below compares global thresholding and adaptive thresholding for an image with varying illumination:
Otsu’s Binarization
In global thresholding, we used an arbitrary chosen value as a threshold. In contrast, Otsu’s method avoids having to choose a value and determines it automatically.
Consider an image with only two distinct image values (bimodal image), where the histogram would only consist of two peaks. A good threshold would be in the middle of those two values. Similarly, Otsu’s method determines an optimal global threshold value from the image histogram.
In order to do so, the cv.threshold() function is used, where cv.THRESH_OTSU is passed as an extra flag. The threshold value can be chosen arbitrary. The algorithm then finds the optimal threshold value which is returned as the first output.
Check out the example below. The input image is a noisy image. In the first case, global thresholding with a value of 127 is applied. In the second case, Otsu’s thresholding is applied directly. In the third case, the image is first filtered with a 5×5 gaussian kernel to remove the noise, then Otsu thresholding is applied. See how noise filtering improves the result.