## Python image thresholding processing and algorithm comparison example analysis

osc_ r2ntyjuu 2020-11-15 11:48:32
python image thresholding processing algorithm

Binarization or thresholding of images （Binarization） It aims to extract the object from the image , Distinguish the background from the noise . A threshold is usually set T, adopt T The pixels of the image are divided into two categories ： Greater than T The pixel group of and less than T The pixel group of .

In the image after gray conversion processing , Each pixel has only one gray value , Its size indicates the degree of light and shade . Binarization can divide the pixels in an image into two kinds of colors , Commonly used binarization algorithms such as formulas 1 Shown ：

{Y=0,gray<TY=255,gray>=T {Y=0,gray<TY=255,gray>=T

When gray Gray Less than threshold T when , Its pixels are set to 0, According to black ; When gray Gray Greater than or equal to the threshold T when , Its Y The value is 255, Said the white .

Python OpenCV Threshold functions are provided in threshold() Realize binary processing , The formula and parameters are shown in the figure below ：

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

Binary thresholding

In this method, a specific threshold value must be selected first , such as 127

1) Greater than or equal to 127 The gray value of the pixel is set to the maximum value

2) The gray value is less than 127 The gray value of the pixel is set to 0

for example : 156->255 89->0

Keyword is cv2.THRESH_BINARY, The complete code is as follows

``````import cv2
def test22():
#  Gray image conversion
GrayImage = cv2.cvtColor(src, cv2.COLOR_BGR2BGRA)
#  Binary threshold processing
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_BINARY)
#  Display images
cv2.imshow("src", src)
cv2.imshow("result", b)

if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
test22()``````

The effect is as follows :

Anti binary thresholding

This method is similar to the binary thresholding method , First select a specific gray value as the threshold , such as 127

1) Greater than 127 The gray value of the pixel is set to 0

2) The gray value less than the threshold is set to 255

for example ：156->0 89->255

Keyword is cv2.THRESH_BINARY_INV

The code is as follows :

``````import cv2
def test22():
#  Gray image conversion
GrayImage = cv2.cvtColor(src, cv2.COLOR_BGR2BGRA)
#  Binary threshold processing
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_BINARY_INV)
#  Display images
cv2.imshow("src", src)
cv2.imshow("result", b)

if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
test22()``````

The effect is as follows :

Truncation thresholding

This method needs to select a threshold , Pixels larger than the threshold in the image are set as the threshold , Less than this threshold remains unchanged .

1) Greater than or equal to 127 The gray value of pixel is set as the threshold value 127

2) The gray value less than the threshold remains unchanged

for example : 163-> 127 89->89

keyword cv2.THRESH_TRUNC, The complete code is as follows

``````import cv2
def test22():
#  Gray image conversion
GrayImage = cv2.cvtColor(src, cv2.COLOR_BGR2BGRA)
#  Binary threshold processing
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_TRUNC)
#  Display images
cv2.imshow("src", src)
cv2.imshow("result", b)

if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
test22()``````

The effect is as follows :

Reverse thresholding to 0

This method first selects a threshold , such as 127

(1) Greater than or equal to the threshold 127 The pixels of the image become 0 (2) The pixel value less than the threshold remains unchanged

for example : 128->0 89->89

Keyword is cv2.THRESH_TOZERO_INV, The complete code is as follows ：

``````import cv2
def test22():
#  Gray image conversion
GrayImage = cv2.cvtColor(src, cv2.COLOR_BGR2BGRA)
#  Binary threshold processing
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_TOZERO_INV)
#  Display images
cv2.imshow("src", src)
cv2.imshow("result", b)
if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
test22()``````

The effect is as follows :

The threshold for 0

This method first selects a threshold , such as 127

(1) Greater than or equal to the threshold 127 Pixels of , The value remains the same

(2) The pixel value less than the threshold is set to 0

for example : 163->163 102->0

Keyword is cv2.THRESH_TOZERO, The complete code is as follows ：

``````import cv2
def test22():
#  Gray image conversion
GrayImage = cv2.cvtColor(src, cv2.COLOR_BGR2BGRA)
#  Binary threshold processing
r, b = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_TOZERO)
#  Display images
cv2.imshow("src", src)
cv2.imshow("result", b)

if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
test22()``````

The effect is as follows :

The above is the whole content of this paper , I hope it will be helpful for your study , I also hope that you can support .