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)9c87cff83c3b5e03e6db91aa473f8462.png




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():
  src = cv2.imread("rose.jpg")
  #  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 :


50bde48bda2f4d5bbad73f92c3b24f1f.png


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():
  src = cv2.imread("rose.jpg")
  #  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 :


af7831ec38ccbcd2517b32bb2f5dfff9.png


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():
  src = cv2.imread("rose.jpg")
  #  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 :


520b5d7fe8ffc4e0dabb43b62f3fd770.png


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():
  src = cv2.imread("rose.jpg")
  #  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 :


c4498ab5bffc83a0a90575e8cfca0ea2.png


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():
  src = cv2.imread("rose.jpg")
  #  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 :


79134d4e86ad86b64e665c0e1210949b.png


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 .