PYTHON-OPENCV image binary, Adaptive threshold processing

Source: Internet
Author: User
Tags dashed line

Definition: The two value of the image is to set the gray value of the pixel on the image to 0 or 255, that is, the entire image will be visible only black and white visual effects.

An image includes the target object, background and noise, in order to extract the target object directly from the multi-valued digital image, the common method is to set a threshold T, using t to divide the image data into two parts: a group of pixels larger than t and a pixel group less than T. This is the most special method to study the gray-scale transformation, called the image of the two value (binarization).

Simple threshold value-(global threshold):

The threshold value (threshold) function is provided in PYTHON-OPENCV:

Cv2.threshold ()

Function: The first parameter src refers to the original image, the original image should be a grayscale map.

The second parameter, X, refers to the threshold used to classify the pixel values.

The third parameter, Y, refers to the new pixel value that should be given when the pixel value is above (and sometimes less than) the threshold value.

The fourth parameter, Methods, refers to a different threshold method, which includes:

? cv2. Thresh_binary Chart (1)

? cv2. THRESH_BINARY_INV Chart (2)

? cv2. Thresh_trunc Chart (3)

? cv2. Thresh_tozero Chart (4)

? cv2. THRESH_TOZERO_INV Chart (5)

The broken line is the value that will be threshold, the dashed line is the threshold value

Figure (1)

The grayscale value of a pixel that is greater than the threshold is set to the maximum value (such as a 8-bit grayscale value of up to 255), and the grayscale value of a pixel with a grayscale value less than a threshold is set to 0.

Figure (2)

Pixels that are greater than the threshold value are set to 0, and less than the threshold is set to 255.

Figure (3)

The grayscale value of the pixel points is less than the threshold value, and the pixel of the grayscale value greater than the threshold is set to that threshold value.

Figure (4)

The grayscale value of the pixel points is less than the threshold value, and the grayscale value is all changed to 0, which is greater than the threshold value.

Figure (5)

The grayscale value of the pixel points is greater than the threshold value, and the grayscale value of the pixel is less than the threshold value, and its grayscale value is all changed to 0.

PYTHON+OPENCV Code:

Import Cv2import NumPy asNP fromMatplotlib Import Pyplot aspltimg= Cv2.imread ('1.bmp') Grayimage=Cv2.cvtcolor (Img,cv2. Color_bgr2gray) # median filter grayimage= Cv2.medianblur (Grayimage,5) Ret,th1= Cv2.threshold (Grayimage,127,255, Cv2. thresh_binary) #3for block size, 5 is the param1 value Th2= Cv2.adaptivethreshold (Grayimage,255, Cv2. Adaptive_thresh_mean_c, Cv2. Thresh_binary,3,5) Th3= Cv2.adaptivethreshold (Grayimage,255, Cv2. Adaptive_thresh_gaussian_c, Cv2. Thresh_binary,3,5) Titles= ['Gray Image','Global thresholding (v = 127)','Adaptive Mean thresholding','Adaptive Gaussian thresholding']images=[Grayimage, Th1, Th2, Th3] forIinchXrange4): Plt.subplot (2,2, i+1), Plt.imshow (Images[i],'Gray') Plt.title (Titles[i]) plt.xticks ([]), Plt.yticks ([]) plt.show ()


Adaptive threshold Value:

When the different parts of the same image have different brightness. In this case we need to adopt an adaptive threshold value. At this point the threshold is calculated based on each small area of the image and its corresponding threshold value. So different areas on the same image have different thresholds, so that we can get better results in different brightness situations.

Cv2.adaptivethreshold ()

Function: The first parameter src refers to the original image, the original image should be a grayscale map.

The second parameter, X, refers to the new pixel value that should be given when the pixel value is above (and sometimes less than) the threshold value

The third parameter, Adaptive_method, refers to: Cv_adaptive_thresh_mean_c or Cv_adaptive_thresh_gaussian_c

           fourth parameter     threshold_type     referent threshold type: Must be one of the following                            ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                                  &NBS P                          ,         &NB Sp    ?  cv_thresh_binary,

? Cv_thresh_binary_inv

The fifth parameter, Block_size, refers to the pixel neighborhood size used to calculate thresholds: 3, 5, 7, ...

The sixth parameter, param1, refers to a method-related parameter. For methods Cv_adaptive_thresh_mean_c and Cv_adaptive_thresh_gaussian_c, it is a constant extracted from the mean or weighted mean, although it can be a negative number.

Adaptive threshold: For the method Cv_adaptive_thresh_mean_c, the mean value of the block is calculated first, and then the param1 is reduced.

For the method Cv_adaptive_thresh_gaussian_c, the weighted sum (GAUSSIAN) in the block is calculated first, and then the param1 is lost.

For example:

Using the method Cv_adaptive_thresh_mean_c, the threshold type: cv_thresh_binary, the pixel neighborhood size of the threshold Block_size selected 3, the parameter param1 takes 3 and 5 o'clock:

Partial original image pixel worth parameter param1 is 5 o'clock

Partial original image pixel worth parameter param1 is 7 o'clock

Select the corresponding field (3*3) to calculate the mean, then subtract the value of the parameter param1 as the adaptive threshold. When the mean value is calculated as a decimal, it seems to be rounded and then subtracted from the parameter param1. (Maybe I didn't test accurately, bored when testing, study together)

PYTHON+OPENCV Code:

Import Cv2import NumPy asNP fromMatplotlib Import Pyplot aspltimg= Cv2.imread ('1.bmp') Grayimage=Cv2.cvtcolor (Img,cv2. Color_bgr2gray) # median filter grayimage= Cv2.medianblur (Grayimage,5) Ret,th1= Cv2.threshold (Grayimage,127,255, Cv2. thresh_binary) #3for block size, 5 is the param1 value Th2= Cv2.adaptivethreshold (Grayimage,255, Cv2. Adaptive_thresh_mean_c, Cv2. Thresh_binary,3,5) Th3= Cv2.adaptivethreshold (Grayimage,255, Cv2. Adaptive_thresh_gaussian_c, Cv2. Thresh_binary,3,5) Titles= ['Gray Image','Global thresholding (v = 127)','Adaptive Mean thresholding','Adaptive Gaussian thresholding']images=[Grayimage, Th1, Th2, Th3] forIinchXrange4): Plt.subplot (2,2, i+1), Plt.imshow (Images[i],'Gray') Plt.title (Titles[i]) plt.xticks ([]), Plt.yticks ([]) plt.show ()

PYTHON-OPENCV image binary, Adaptive threshold processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.