Pro-Test Effective! A perfect dynamic threshold white balance algorithm Java implementation.

Source: Internet
Author: User

A few years have not been issued, re-pick up the technology!

Recently do image processing, to automatically deal with color balance problems, a lot of what histogram optimization and so on, are not perfect. So I found this predecessor's article in the blog Park.

Http://www.cnblogs.com/Imageshop/archive/2013/04/20/3032062.html#commentform

Principle, realization and effect of automatic white balance algorithm based on Gray world, perfect reflection and dynamic threshold

Unfortunately, this article, first there is no source code, followed by some of the computational process problems. So I looked directly at the original text, as well as some mapping formulas, and now share the Java implementation version:

Core algorithms:

BufferedImage img = Imageio.read (NewFile ("Model3.jpg")); intPixelsize = Img.getwidth () *img.getheight (); Double[][][] YCbCr =New Double[Img.getwidth ()] [Img.getheight ()] [3]; DoubleMr = 0, Mb = 0, Ymax = 0;  for(inti = 0; I < img.getwidth (); i++) {             for(intj = 0; J < Img.getheight (); J + +) {Ycbcr[i][j]=toycbcr (Img.getrgb (i, j)); Mr+ = Ycbcr[i][j][2]; Mb+ = Ycbcr[i][j][1]; Ymax= Math.max (Ymax, ycbcr[i][j][0]); }} Mr/=pixelsize; Mb/=pixelsize; DoubleDr = 0, Db = 0;  for(inti = 0; i < ycbcr.length; i++) {             for(intj = 0; J < Ycbcr[i].length; J + +) {Db+ = Math.Abs (Ycbcr[i][j][1]-Mb); Dr+ = Math.Abs (ycbcr[i][j][2]-Mr); }} Dr/=pixelsize; Db/=pixelsize; Double[] Y =New Double[Img.getwidth ()][img.getheight ()]; Double[] Yhistogram =New Double[256]; DoubleYsum = 0;  for(inti = 0; i < y.length; i++) {             for(intj = 0; J < Y[i].length; J + +) {                intValue = (Math.Abs (ycbcr[i][j][1]-(MB + Db * Math.signum (MB))) < 1.5 * Db) &//(Math.Abs (Ycbcr[i][j][2])-(1.5 * Mr + DR * MATH.SIGNUM (MR)) < 1.5 * Dr? 1:0; if(Value <= 0)                    Continue; Doubley = ycbcr[i][j][0]; Y[I][J]=y; yhistogram[(int) y[i][j]]++; Ysum++; }        }        DoubleYhistogramsum = 0; Doubleymin = 0;  for(inti = yhistogram.length-1; I >= 0; i--) {yhistogramsum+=Yhistogram[i]; if(Yhistogramsum > 0.1 *ysum) {ymin=i;  Break; }        }        DoubleRaver = 0, Gaver = 0, Baver = 0; DoubleAversum = 0;  for(inti = 0; i < y.length; i++) {             for(intj = 0; J < Y[i].length; J + +) {                if(Y[i][j] >ymin) {                    intcolor =Img.getrgb (i, j); intr = (color >>) & 0xFF; intg = (color >> 8) & 0xFF; intb = Color & 0xFF; Raver+=R; Gaver+=G; Baver+=b; Aversum++; } }} raver/=aversum; Gaver/=aversum; Baver/=aversum; DoubleRgain = ymax/raver, Ggain = ymax/gaver, Bgain = Ymax/Baver;  for(inti = 0; I < img.getwidth (); i++) {             for(intj = 0; J < Img.getheight (); J + +) {color color=NewColor (Img.getrgb (i, j)); intR = Ensurecolor ((int) Math.floor (color.getred () *rgain)); intg = Ensurecolor ((int) Math.floor (Color.getgreen () *ggain)); intb = Ensurecolor ((int) Math.floor (Color.getblue () *bgain)); Img.setrgb (i, J,NewColor (R, G, b). getRGB ()); }} imageio.write (IMG,"JPG",NewFile ("xxx.jpg"));

The algorithm for calculating YCRCB is as follows:

    // HTTPS://MATHEMATICA.STACKEXCHANGE.COM/QUESTIONS/29786/HOW-TO-CONVERT-RGB-TO-YCBCR    Private Double[] TOYCBCR (intcolor) {        intr = (color >>) & 0xFF; intg = (color >> 8) & 0xFF; intb = Color & 0xFF; DoubleY = + (65.481 * r/255 + 128.553 * g/255 + 24.966 * b/255); DoubleCb = + ( -37.797 * r/255-74.203 * g/255 + b/255 *); DoubleCr = + (r/255-93.786 * g/255-18.214 * b/255); return New Double[] {Y, Cb, Cr}; }

Finally, there is a pixel range detection:

    Private int ensurecolor (double  color) {        if (color < 0)            return 0;         if (Color > 255)             return 255;         return (int) color;    }

Actual effect:

It works very well. I also read the original author's question, it should be calculated YCRCB error.

This share is complete! For several years did not post in the blog Park, say the recent situation. The first time to enter the blog Park is more than 10 years ago, in Shanghai Jiao Tong University graduate students of a poor child. After graduating from graduate school has been a bump in entrepreneurship, to now 36, still in business. Perhaps the future of entrepreneurship, these blogs can become inspirational experience. If you don't succeed, keep trying.

Recently, I was engaged in the public number, but also small achievements, made the country's largest Lego public number. I hope to be able to do something someday.

Thank you friends of the reading, I hope this article is helpful!

Pro-Test Effective! A perfect dynamic threshold white balance algorithm Java implementation.

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.