The binary-Dajing method of gray image

Source: Internet
Author: User
The binary-Dajing method of gray image

The Dajing method is also called the Maximal inter-class variance method. For the image, the T is the threshold of foreground and background, the foreground point is w0, the average gray is u0, the background point is W1, and the average gray is U1. The total average gray level of the image is: U=W0*U0+W1*U1. Traversing t from the minimum grayscale value to the maximum gray value, when T makes the value g=w0* (u0-u) 2 +w1* (u1-u) 2 maximum, T is the best threshold for segmentation. The foreground and background of the threshold T segmentation are composed of the whole image, and the foreground value is u0, the probability is w0, the background value is U1, the probability is W1, the total mean value is u, and the formula is derived according to the definition of variance. Because variance is a measure of the uniformity of gray distribution, the greater the variance value, the greater the two parts of the image, when part of the target fault is divided into background or part of the background error into the target will result in two parts of the difference is smaller, so the maximum variance of the division means the least error probability. The direct application of Dajing method is large, so we use the equivalent formula G=w0*w1* (U0-U1) 2 in the implementation.

Get histogram//1. Pimagedata image Data//2. Nwidth Image Width//3. nheight Image Height//4. Nwidthstep image Row Size//5. Phistogram grayscale histogram, size 256 bool Gethistogram (unsigned char *pimagedata, int nwidth, int nheight, int nwidthstep, int *phistogr AM) {int i = 0; int j = 0; unsigned char *pline = NULL;//Empty Histogram memset (phistogram, 0, sizeof (int) * 256); for (pline = P ImageData, j = 0; J < nheight; J + +, Pline + = Nwidthstep) {for (i = 0; i < nwidth; i++) {phistogram[pline[i]]++;}} return true; }//DAZINFA//1. Pimagedata image Data//2. Nwidth Image Width//3. nheight Image Height//4. Nwidthstep image Row Size//function return threshold int Otsu (unsigned char *pimagedata, int nwidth, int nheight, int nwidthstep) {int i = 0; Int J = 0; int ntotal = 0; int nsum = 0; int A = 0; int B = 0; Double u = 0; Double v = 0; Double dvariance = 0; Double dmaximum = 0; int nthreshold = 0; int nhistogram[256]; Get histogram Gethistogram (Pimagedata, nwidth, nheight, Nwidthstep, Nhistogram); for (i = 0; i < 256 i++) {ntotal = Nhistogram[i]; Nsum + = (nhistogram[i) * i); for (j = 0; J < 256; J + +) {A = 0; B = 0; for (i = 0; i < J; i++) {A + = Nhistogram[i]; B + + (nhistogram[i] * i); } if (A > 0) {u = b/a} else {u = 0;} if (Ntotal-a > 0) {v = (nsum-b)/(ntotal-a);} else {v = 0;} d Variance = A * (ntotal-a) * (u-v) * (U-V); if (Dvariance > Dmaximum) {dmaximum = dvariance; nthreshold = j;}} return nthreshold; }

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.