Change language

Python | Threshold Methods Using OpenCV | Set-2 (adaptive threshold)

| | |

In source : Input Image array (Single-channel, 8-bit or floating-point)
-"  maxVal : Maximum value that can be assigned to a pixel.
-"  adaptiveMethod : Adaptive method decides how threshold value is calculated.

  cv2.ADAPTIVE_THRESH_MEAN_C : Threshold Value = (Mean of the neighborhood area values ​​- constant value) ... In other words, it is the mean of the blockSize × blockSize neighborhood of a point minus constant.

cv2.ADAPTIVE_THRESH_GAUSSIAN_C : Threshold Value = (Gaussian-weighted sum of the neighborhood values - constant value). In other words, it is a weighted sum of the blockSize × blockSize neighborhood of a point minus constant.

-"  thresholdType : The type of thresholding to be applied.
-"  blockSize : Size of a pixel neighborhood that is used to calculate a threshold value.
-"  constant : A constant value that is subtracted from the mean or weighted sum of the neighborhood pixels.

Below is the Python implementation:

# Python program for illustration
# adaptive threshold type on image

 
# organization of import

import cv2 

import numpy as np 

 
# specifies the path to the input image and
# the image is loaded using the imread command

image1 = cv2.imread ( ’input1.jpg’

  
# cv2.cvtColor is applied over
# image input with parameters applied
# convert grayscale image

img = cv2.cvtColor (image1, cv2.COLOR_BGR2GRAY)

 
# applying different thresholds
# technique on the input image

thresh1 = cv2.adaptiveThreshold (img, 255 , cv2.ADAPTIVE_THRESH_MEAN_C,

cv2.THRESH_BINARY, 19 9 , 5 )

 

thresh2 = cv2.adaptiveThreshold (img, 255 , cv2.ADAPTIVE_THRESH_GAUSSIAN_C,

cv2.THRESH_BINARY, 199 , 5 )

  
# window with image output
# with the appropriate threshold
# methods applied to the input image

cv2.imshow ( ’Adaptive Mean’ , thresh1)

cv2.imshow ( ’Adaptive Gaussian’ , thresh2)

  

 
# Deselect any associated memory usage

if cv2.waitKey ( 0 ) & amp;  0xff = = 27

cv2.destroyAllWindows () 

Input Image :

Exit :