Review the knowledge

Before binarization, let’s take a look at the basics of images. This is also a small review before me, there are excerpts from the big guy’s wonderful explanation also have my own point of humble opinion.

The color of a pixel is represented by three values of RGB, so a pixel matrix corresponds to three color vector matrices, which are R matrix, G matrix and B matrix, and they are all matrices of the same size. In image processing, RGB three components (R: Red, G: Green, B: Blue), namely Red, Green and Blue, are used to represent true color. The value range of R component, G component and B component is 0~255. For example, the value of three components of a Red pixel on the computer screen is 255, 0, 0. After understanding that an image is composed of a matrix of pixels, we know that our processing of the image is the operation of the matrix of pixels. To change the color of a pixel, we just need to find the position of the pixel in the matrix of pixels, such as row x, column Y, So the pixel position in the pixel matrix can be expressed as (x, y), because a pixel color with red, green, blue three color variable said, so we pass to the three variable assignment, to change the pixel color, such as to red (0, 255), can be expressed as (x, y, (R = 255, G=0, B=0)). Image graying is to make every pixel in the pixel matrix satisfy the following relationship: R=G=B binarization is to make the gray value of every pixel in the pixel matrix of the image be 0 (black) or 255 (white), that is, to make the whole image show only black and white effect. The grayscale value ranges from 0 to 255 in the grayscale image, and from 0 to 255 in the binarization image.Copy the code

Image binarization

Definition: Image binarization, that is, the gray value of the pixel on the image is set to 0 or 255, that is, the whole image is obviously only black and white visual effect. An image includes the target object, background and noise. To extract the target object directly from the multi-value digital image, a common method is to set a threshold value T, and use T to divide the image data into two parts: the pixel group greater than T and the pixel group less than T. This is the most special method to study the gray transform, called the image Binarization (Binarization).Copy the code

(Baidu Dafa)

Realization of image binarization

Global threshold

Threshold () the first parameter SRC refers to the original image, which should be grayscale. The second parameter, x, refers to the threshold used to classify pixel values. The third parameter y refers to the new pixel value that should be assigned if the pixel value is above (and sometimes below) the threshold. The fourth parameter Methods refers to a different threshold methodCopy the code
def threshold_demo(image):# Image binarization gray= cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    ret,binary=  cv.threshold(gray,0.255,cv.THRESH_BINARY|cv.THRESH_OTSU)
    print("%s"%ret)
    cv.imshow("binary",binary)

Copy the code

Here with the CV. THRESH_BINARY | CV. THRESH_OTSU global threshold method, the results:We change a way (CV. THRESH_BINARY | CV. THRESH_TRIANGLE triangle threshold method)

def threshold_demo(image):# Image binarization gray= cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    ret,binary=  cv.threshold(gray,0.255,cv.THRESH_BINARY|cv.THRESH_TRIANGLE)
    print("%s"%ret)
    cv.imshow("binary",binary)
Copy the code

It is also possible to use a custom THRESH_BINARY with the second argument as a threshold, and the fourth argument only using cv.THRESH_BINARY, without any automatic THRESH_BINARY.

def threshold_demo(image):# Image binarization gray= cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    ret,binary=  cv.threshold(gray,144.255,cv.THRESH_BINARY)
    print("%s"%ret)
    cv.imshow("binary",binary)
Copy the code

The local threshold

When different parts of the same image have different brightness. In this case we need to use local thresholds. In this case, the threshold value is calculated according to each small area on the image. So different thresholds are used for different regions of the same image, which allows us to get better results in different brightness situations.Copy the code
Cv2.adaptivethreshold () The first parameter SRC refers to the original image, which should be grayscale. The second parameter x refers to the new pixel value that should be assigned if the pixel value is above (and sometimes below) the threshold. The third parameter adaptive_method is either CV_ADAPTIVE_THRESH_MEAN_C or CV_ADAPTIVE_THRESH_GAUSSIAN_C • CV_THRESH_BINARY_INV • CV_THRESH_BINARY_INV • block_size specifies the size of the threshold neighborhood: 3, 5, 7,... The sixth parameter param1 is the parameter related to the method. For methods CV_ADAPTIVE_THRESH_MEAN_C and CV_ADAPTIVE_THRESH_GAUSSIAN_C,Copy the code

Code:

def local_threshold(image): # Image binarization with 2 local threshold gray= cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    binary =  cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,25.10)

    cv.imshow("binary", binary)
Copy the code

Operation effect: