Python OpenCV using notes (quad) (Image threshold processing)

Source: Internet
Author: User

    • The image threshold processing generally makes the image pixel value single, the image is simpler. Threshold can be divided into the global nature of the threshold, can also be divided into local properties of the threshold, can be a single threshold value can also be a multi-threshold value. Of course, the more thresholds are more complex. The three threshold methods under OpenCV are described below.
(i) Simple threshold value

The simple threshold is, of course, the simplest, choosing a global threshold and then dividing the entire image into a non-black-and-white two-value image. function is Cv2.threshold ()
This function has four parameters, the first original image, the second to classify the threshold value, the third is higher than the threshold value given when the new value, the fourth is a method to select parameters, commonly used are:
? Cv2. Thresh_binary (black and white two value)
? Cv2. THRESH_BINARY_INV (black and white two value reversal)
? Cv2. Thresh_trunc (The resulting image is a multi-pixel value)
? Cv2. Thresh_tozero
? Cv2. Thresh_tozero_inv
The function has two return values, the first retval (which will be used in the following method), and the second is the image after the threshold value.
An example is as follows:

ImportCv2ImportMatplotlib.pyplot asPltimg = Cv2.imread (' flower.jpg ',0)#直接读为灰度图像RET,THRESH1 = Cv2.threshold (IMG,127,255, Cv2. thresh_binary) RET,THRESH2 = Cv2.threshold (IMG,127,255, Cv2. THRESH_BINARY_INV) RET,THRESH3 = Cv2.threshold (IMG,127,255, Cv2. Thresh_trunc) RET,THRESH4 = Cv2.threshold (IMG,127,255, Cv2. Thresh_tozero) Ret,thresh5 = Cv2.threshold (IMG,127,255, Cv2. THRESH_TOZERO_INV) titles = [' img ',' BINARY ',' BINARY_INV ',' TRUNC ',' Tozero ',' TOZERO_INV ']images = [Img,thresh1,thresh2,thresh3,thresh4,thresh5] forIinchRange6): Plt.subplot (2,3, i+1), Plt.imshow (Images[i],' Gray ') Plt.title (Titles[i]) plt.xticks ([]), Plt.yticks ([]) plt.show ()


You can see that the threshold is set to 127, for binary methods, when the grayscale value in the image is greater than 127, the reset pixel value is 255.

(ii) Adaptive thresholds:

The simple threshold that you see earlier is a global threshold that requires only a threshold value, and the entire image is compared to this threshold value. The adaptive threshold can be regarded as a local threshold value, by defining an area size, comparing the size of the point to the average (or other characteristic) of the pixel within the size of the area to determine whether the pixel is black or white (in case of a two value). The function used is: Cv2.adaptivethreshold ()
The function needs to fill 6 parameters:

    • First original image
    • Upper limit of second pixel value
    • The third Adaptive Approach Adaptive method:
      -cv2. Adaptive_thresh_mean_c: Mean value in the field
      -cv2. Adaptive_thresh_gaussian_c: Weighted sum of pixel points in the field and weights as a Gaussian window
    • Method of assignment of the fourth value: only Cv2. Thresh_binary and Cv2. Thresh_binary_inv
    • Fifth block Size: Specify the field sizes (a square field)
    • The sixth constant C, the threshold equals the mean or the weighted value minus the constant (0 equals the threshold is the field mean or weighted value)
      This method works better theoretically, equivalent to adjusting the threshold of its own pixel in a dynamically adaptive manner, rather than using a threshold value for the entire image.

An example is as follows:

Mport Cv2ImportMatplotlib.pyplot asPltimg = Cv2.imread (' flower.jpg ',0)#直接读为灰度图像ret,th1 = Cv2.threshold (IMG,127,255, Cv2. thresh_binary) Th2 = Cv2.adaptivethreshold (IMG,255, Cv2. Adaptive_thresh_mean_c,cv2. Thresh_binary, One,2)#换行符号 \TH3 = Cv2.adaptivethreshold (IMG,255, Cv2. Adaptive_thresh_gaussian_c,cv2. Thresh_binary, One,2)#换行符号 \Images = [Img,th1,th2,th3]plt.figure () forIinchXrange4): Plt.subplot (2,2, i+1), Plt.imshow (Images[i],' Gray ') Plt.show ()


You can see that the window size used is 11, and the smaller the window, the finer the image gets. Think about it, if you set the window large enough (not to exceed the image size), then the result may be the same as the second image.

(iii) Otsu ' s two value

As we said earlier, the Cv2.threshold function has two return values, the second return value that has been used before, that is, the image after the threshold is processed, then the first return value (the threshold for getting the image) will be used here.
In front of the threshold value of the processing, we choose the threshold is 127, then the actual situation, how to choose this 127? Some images may not have the same threshold value as 127 to get better results. So here we need the algorithm to find a threshold for itself, and Otsu's can find a threshold that they think is the best. And Otsu's is very suitable for image grayscale histogram with Shuangfeng, he will find a value between Shuangfeng as a threshold value, for non-Shuangfeng images, may not be very useful. Then the threshold obtained by Otsu's is the first parameter of the function Cv2.threshold. Because the OTSU ' s method produces a threshold, the second parameter of the function Cv2.threshold (the set threshold) is 0, and the statement Cv2.thresh_otsu is added to the Cv2.threshold method parameter. Then what is Shuangfeng image (only grayscale image only), is the image of the grayscale statistics can be clearly seen only two peaks, such as the following map of the gray histogram can be Shuangfeng diagram:

Okay, now the Otsu ' s threshold is very good for this diagram, and the function Cv2.threshold automatically finds a threshold between the two peaks. An example is as follows:

ImportCv2ImportMatplotlib.pyplot asPltimg = Cv2.imread (' finger.jpg ',0)#直接读为灰度图像#简单滤波ret1,th1 = Cv2.threshold (IMG,127,255, Cv2. Thresh_binary)#Otsu Filteringret2,th2 = Cv2.threshold (IMG,0,255, Cv2. Thresh_binary+cv2. Thresh_otsu)PrintRet2plt.figure () Plt.subplot (221), Plt.imshow (IMG,' Gray ') Plt.subplot (222), Plt.hist (Img.ravel (), the)The #.ravel method transforms a matrix into one-dimensionalPlt.subplot (223), Plt.imshow (Th1,' Gray ') Plt.subplot (224), Plt.imshow (Th2,' Gray ')


The result of print Ret2 is 122. It can be seen that there seems to be no obvious difference between the two results (the material is not very good ~_~!), the main two thresholds (127 and 122) are too close, if the two are very far away then it will be obvious.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python OpenCV using notes (quad) (Image threshold processing)

Related Article

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.