Goal
- In this tutorial, you will learn Simple thresholding, Adaptive thresholding, Otsu's thresholding etc.
- You will learn these functions : cv.threshold, cv.adaptiveThreshold etc.
Simple Thresholding
Here, the matter is straight forward. If pixel value is greater than a threshold value, it is assigned one value (may be white), else it is assigned another value (may be black).
We use the function: cv.threshold (src, dst, thresh, maxval, type)
- Parameters
-
src input array. dst output array of the same size and type and the same number of channels as src. thresh threshold value. maxval maximum value to use with the cv.THRESH_BINARY and cv.THRESH_BINARY_INV thresholding types. type thresholding type(see cv.ThresholdTypes).
thresholding type - OpenCV provides different styles of thresholding and it is decided by the fourth parameter of the function. Different types are:
- cv.THRESH_BINARY
- cv.THRESH_BINARY_INV
- cv.THRESH_TRUNC
- cv.THRESH_TOZERO
- cv.THRESH_OTSU
- cv.THRESH_TRIANGLE
- Note
- Input image should be single channel only in case of cv.THRESH_OTSU or cv.THRESH_TRIANGLE flags
Try it
Adaptive Thresholding
In the previous section, we used a global value as threshold value. But it may not be good in all the conditions where image has different lighting conditions in different areas. In that case, we go for adaptive thresholding. In this, the algorithm calculate the threshold for a small regions of the image. So we get different thresholds for different regions of the same image and it gives us better results for images with varying illumination.
We use the function: cv.adaptiveThreshold (src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C)
- Parameters
-
src source 8-bit single-channel image. dst destination image of the same size and the same type as src. maxValue non-zero value assigned to the pixels for which the condition is satisfied adaptiveMethod adaptive thresholding algorithm to use. thresholdType thresholding type that must be either cv.THRESH_BINARY or cv.THRESH_BINARY_INV. blockSize size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on. C constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.
adaptiveMethod - It decides how thresholding value is calculated:
Try it