opencv binary image segmentation - python

1. Binary image

Image binarization is the process of setting the gray value of the pixels on the image to 0 or 255, that is, the process of presenting the entire image with an obvious black and white effect.

2. Global Threshold

cv2.threshold(src, thresholdValue, maxVal, thresholdingTechnique)
src: Input grayscale image array.
thresholdValue: Mentions the value used to classify pixel values.
maxVal: The value given if the pixel value is larger (and sometimes smaller) than the threshold.
thresholdingTechnique: The type of threshold to apply.
There are 5 different simple thresholding techniques are:

cv2.THRESH_BINARY: If the pixel intensity is greater than the set threshold, set the value to 255, otherwise set to 0 (black).
cv2.THRESH_BINARY_INV: Inversion of cv2 or the opposite.
cv2.THRESH_TRUNC: If the pixel intensity value is greater than the threshold, it will be truncated to the threshold. The pixel value is set to be the same as the threshold. All other values ​​remain unchanged.
cv2.THRESH_TOZERO: Pixel intensities are set to 0, for all pixel intensities, less than a threshold.
cv2.THRESH_TOZERO_INV: Inversion of cv2 or the opposite.

import cv2
 
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"
 
    # read/load an image in grayscale mode
    grey_img = cv2.imread(img_path,0)
 
    # show the Input image on the newly created image window
    cv2.imshow('Input',grey_img)
 
    # applying cv2.THRESH_BINARY thresholding techniques
    ret, thresh_img = cv2.threshold(grey_img, 128, 255, cv2.THRESH_BINARY)
 
    # show the Output image on the newly created image window
    cv2.imshow('Output',thresh_img)

insert image description here
ret, th= cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
This method is also called Otsu method (OTSU) It is based on the image grayscale distribution, automatically selects the best threshold, according to Otsu After the image is binarized and segmented using the threshold obtained by the method, the inter-class variance of the foreground and background images is the largest. It is suitable for processing images with a large gap between the foreground image and the background image to be extracted. Its function is also very simple, just set the threshold threshold to 0, and then set the type to cv2.THRESH_BINARY+cv2.THRESH_OTSU, it will automatically return an appropriate threshold.

import cv2
 
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "./image-129.png"
 
    # read/load an image in grayscale mode
    gray_img = cv2.imread(img_path, 0)
 
    # show the Input image on the newly created image window
    print(gray_img.shape)
    cv2.imshow('Input', gray_img)
    cv2.namedWindow("Input", 0)
    cv2.waitKey(0)
 
    # applying cv2.THRESH_BINARY thresholding techniques
    ret, thresh_img= cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
 
    # show the Output image on the newly created image window
    
    cv2.imshow('Output',thresh_img)
    cv2.namedWindow("Output", 0)
    cv2.waitKey(0)
    cv2.imwrite('thresh.png', thresh_img)

insert image description here

3. Local threshold

The color distribution of color images is uneven, and the processing effect of global threshold processing is poor. Usually, local thresholds are used for segmentation. The processing principle of local thresholds is to configure a threshold for each pixel for processing. These thresholds constitute and A matrix with the same dimensions as the original image.

cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None)
src: grayscale image
maxValue: grayscale value that needs to be set for pixels that meet the conditions
adaptiveMethod: adaptive method. There are 2 kinds of mean and Gaussian weighting: ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C, it is recommended to use Gaussian weighted sum.
thresholdType: Binarization method, which can be set to THRESH_BINARY or THRESH_BINARY_INV
blockSize: The size of the area calculated by division, take an odd number
C: constant, and subtract this constant from the calculated threshold of each area as the final threshold of this area, you can negative
dst: output image, optional

import cv2
 
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "./image-129.png"
 
    # read/load an image in grayscale mode
    # gray_img = cv2.imread(img_path, 0)
    color_img = cv2.imread(img_path)
    gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)
 
    # show the Input image on the newly created image window
    print(gray_img.shape)
    cv2.imshow('Input', gray_img)
    cv2.namedWindow("Input", 0)
    cv2.waitKey(0)
 
    # applying cv2.THRESH_BINARY thresholding techniques
    thresh_img = cv2.adaptiveThreshold(gray_img, 127, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, -30)
 
    # show the Output image on the newly created image window
    
    cv2.imshow('Output',thresh_img)
    cv2.namedWindow("Output", 0)
    cv2.waitKey(0)
    cv2.imwrite('thresh.png', thresh_img)

insert image description here

4. Recommendations for use

Grayscale images are segmented using global thresholding: Otsu's method > Ordinary global thresholding
Color images are segmented using local thresholding

Guess you like

Origin blog.csdn.net/CharmsLUO/article/details/123398938