Using the Python interface of OPENCV 2.2 to implement the Ostu (Dajing method) to obtain the threshold __python

Source: Internet
Author: User

OpenCV (version 2.2) What's not to say, you know. In fact, this article only uses OPENCV to compute a histogram. You can write Python code implementations entirely.

Python (version 2.7) is not saying anything and you know it.

What is the Dajing method.

Please note that the big "Jin" law is not a big "law" law. When I first saw it, it was a big "law", then also in Baidu and Google slipped a lap, incredibly also found a lot of information, and later in pondering Ostu four letters when found, Ostu is a Japanese county, called Dajing County, so here should be "Dajing law."

Dajing method, also known as the largest inter-class variance method, referred to as Otsu, was proposed by Japanese scholar Otsu in 1979, and is an adaptive threshold determination method. According to the gray character of the image, dividing the image into two parts, called Part 0 and Part 1 (that is, the background and target of the usual meaning), the larger the variance of the two parts, the greater the difference between the two parts of the image, which means the difference between the picture background and the target is larger.

For Image F (x,y), the picture is divided into two parts by the threshold T, and the brightness is less than T, called Part 0, and the brightness greater than or equal to T is called Part 1. The proportion of 0 pixel points to the whole picture is ω0, the average gray level is μ0, the proportion of 1 pixel points to the whole image is ω1, and the average gray level is μ1. The total mean gray level of the image is μ, and the variance between the classes is G.

Then there are

Μ=ω0μ0+ω1μ1

G =ω0 (μ0-μ) 2+ω1 (μ1-μ) 2

To bring the first equation into the second equation, we can get

G=ω0ω1 (μ0-μ1) 2

By traversing all the brightness (generally [0,255]) to get all the T-values corresponding to G, the largest G-occurrence t is the optimal threshold for the Otsu algorithm.

The following Python program shows the best thresholds for computing Otsu using Python 2.7 in OpenCV 2.2, and all Ω uses the w,μ using U instead for easy reading.

Import CV def otsugray (grayimage, debug = 0): # If the picture is a Mat object, convert to the Image object if Type (grayimage) = = Cv.cvmat:grayImage = CV. GetImage (grayimage) # Create hist hist = cv. Createhist ([256],CV. CV_HIST_ARRAY,[[0,256]]) CV. Clearhist (hist) # Calculates the hist CV. Calchist ([grayimage],hist) # Start computing # calculate total Brightness Totalh = 0 for h in range (0,256): v = cv. QUERYHISTVALUE_1D (hist,h) If V = 0:continue Totalh + + v*h if debug > 3:print "t=%d,%d,%d"% (h,totalh,v*h) width = g Rayimage.width height = Grayimage.height Total = width*height If debug > 1:print gross pixels:%d total brightness:%d average brightness:%0.2f "% (Total,tot Alh,totalh/total) # t=0 and t=255 cannot form a split, so start with t=1 compute consistent to t=255 # Initialize v = 0 Gmax = 0.0 tindex = 0 # Temp N0ACC = 0 N1acc = 0 N 0H = 0 n1h = 0 for T in range (1,255): v = cv. QUERYHISTVALUE_1D (hist,t-1) If V = 0:continue N0acc + = v #灰度小于t的像素的数目 n1acc = Total-n0acc #灰度大于等于t的像素的数目 n0h + = (t-1) * V #灰度小于t的像素的总亮度 n1h = totalh-n0h #灰度大于等于t的像素的总亮度 if N0ACC > 0 and n1acc > 0:u0 = n0h/n0acc # Gray level less than t average grayscale u1 = n1h/ N1ACC # Gray level is greater than or equal to t average gray w0 = N0Acc/total # A pixel scale of less than t is w1 = 1.0-w0 # The proportion of pixels with a gray order greater than or t is UD = u0-u1 g = w0 * W1 * UD * or UD if debug > 2:print "T=%3D; u0=%.2f,u1=%.2f,%.2f;n0h=%d,n1h=%d; g=%.2f "/% (T,U0,U1,U0*W0+U1*W1,N0H,N1H,G) if gmax < G:gmax = g Tindex = t if debug >0:print" gmaxvalue=%.2f; t =%d; T_INV =%d "/% (Gmax,tindex,255-tindex) return Tindex

Show the effect of the binary:

The original artwork is as follows:

G Get maximum value when t=102

To save space, just pass an example.

Teachers, if there are errors in the article, I implore you to treatise.

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.