Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Image Thresholding

OpenCV Image Thresholding

In image processing, thresholding is a commonly used technique for converting an image into a binary image (i.e., a black-and-white image). By setting a threshold, pixels in the image can be divided into two categories: pixels above the threshold and pixels below the threshold.

OpenCV provides a variety of thresholding methods. This article will introduce in detail three common thresholding techniques: simple thresholding, adaptive thresholding, and Otsu's binarization.

1. Simple Thresholding (cv2.threshold())

Simple thresholding is the most basic thresholding method. It divides pixels in an image into two categories by setting a fixed threshold.

OpenCV provides the cv2.threshold() function to achieve this.

Function prototype



retval, dst = cv2.threshold(src, thresh, maxval, type)

Parameter Description

- `cv2.THRESH_BINARY`: if the pixel value is greater than the threshold, assign `maxval`, otherwise assign `0`. - `cv2.THRESH_BINARY_INV`: opposite to `cv2.THRESH_BINARY`, if the pixel value is greater than the threshold, assign `0`, otherwise assign `maxval`. - `cv2.THRESH_TRUNC`: if the pixel value is greater than the threshold, assign the threshold, otherwise keep it unchanged. - `cv2.THRESH_TOZERO`: if the pixel value is greater than the threshold, keep it unchanged, otherwise assign `0`. - `cv2.THRESH_TOZERO_INV`: opposite to `cv2.THRESH_TOZERO`, if the pixel value is greater than the threshold, assign `0`, otherwise keep it unchanged.

Return Value

Example

Example

import

cv2

import

numpy

as

np

Read an image

img

=

cv2.

imread

(

'image.jpg'

,

0

)

Simple thresholding

ret

,

thresh1

=

cv2.

threshold

(

img

,

127

,

255

,

cv2.

THRESH_BINARY

)

Display Results

cv2.

imshow

(

'Binary Threshold'

,

thresh1

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)


2. Adaptive Thresholding (cv2.adaptiveThreshold())

In some cases, the brightness distribution of an image is uneven, and using a fixed threshold may not produce ideal results. Adaptive thresholding handles this situation better by computing different thresholds for different regions of the image.

Function prototype



dst = cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)

Parameter Description

- `cv2.ADAPTIVE_THRESH_MEAN_C`: the threshold is the average of the neighborhood minus the constant `C`. - `cv2.ADAPTIVE_THRESH_GAUSSIAN_C`: the threshold is the weighted average of the neighborhood minus the constant `C`, where the weights are determined by a Gaussian function.

Return Value

Example

Example

import

cv2

import

numpy

as

np

Read an image

img

=

cv2.

imread

(

'image.jpg'

,

0

)

Adaptive thresholding

thresh2

=

cv2.

adaptiveThreshold

(

img

,

255

,

cv2.

ADAPTIVE_THRESH_MEAN_C

,

cv2.

THRESH_BINARY

,

11

,

2

)

Display Results

cv2.

imshow

(

'Adaptive Threshold'

,

thresh2

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)

3. Otsu's Binarization (cv2.threshold() with cv2.THRESH_OTSU)

Otsu's binarization is a method for automatically determining the threshold. It finds the optimal global threshold by maximizing the between-class variance, and is suitable for bimodal images (i.e., images whose histogram has two distinct peaks).

Function prototype

retval, dst = cv2.threshold(src, thresh, maxval, type)

Parameter Description

Return Value

Example

Example

import

cv2

import

numpy

as

np

Read an image

img

=

cv2.

imread

(

'image.jpg'

,

0

)

Otsu's binarization

ret

,

thresh3

=

cv2.

threshold

(

img

,

0

,

255

,

cv2.

THRESH_BINARY

THRESH_OTSU

)

Display Results

cv2.

imshow

(

'Otsu's Threshold'

,

thresh3

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)