get BufferedImage Bracketed Color - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

get BufferedImage Bracketed Color

Demo Code

/*/*  www  .j a  v  a  2 s  .c o  m*/
 * (C) Copyright 2000-2011, by Scott Preston and Preston Research LLC
 *
 *  Project Info:  http://www.scottsbots.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.imageio.ImageIO;
import javax.media.jai.Histogram;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import com.scottsbots.core.utils.Utils;

public class Main{
    public static int[] getBracketedColor(BufferedImage srcImg, Color c,
            double threshhold) {

        int h = srcImg.getHeight();
        int w = srcImg.getWidth();
        int count = 0;
        BufferedImage dstImg = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_RGB);
        int bins[] = new int[256];
        int invertBins[] = new int[256];

        for (int i = 0; i < 256; i++) {
            bins[i] = 0;
        }
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                int srcPixel = srcImg.getRGB(x, y);
                Color srcColor = new Color(srcPixel);
                int red = srcColor.getRed();
                int green = srcColor.getGreen();
                int blue = srcColor.getBlue();

                if (c == Color.RED && red > 0) {
                    bins[red] = bins[red] + 1; // red histogram
                    count++;
                }
                if (c == Color.GREEN && green > 0) {
                    bins[green] = bins[green] + 1; // green histogram
                    count++;
                }
                if (c == Color.BLUE && blue > 0) {
                    bins[blue] = bins[blue] + 1; // blue histogram
                    count++;
                }
            }
        }

        int mean = count / 256;
        int min = 0;
        int max = 256;
        int maxBin = 0;

        invertBins = Arrays.copyOf(bins, bins.length);
        Arrays.sort(invertBins);
        int topBins[] = new int[50];
        int tops = 0;
        outer: for (int i = 0; i < 256; i++) {
            for (int k = 0; k < 256; k++) {
                if (invertBins[255 - i] == bins[k]) {
                    if (tops >= 50) {
                        break outer;
                    }
                    // System.out.println("k="+(255-k)+",inverted="+invertBins[255
                    // -i]+", bins="+bins[k]);
                    topBins[tops] = k; // this will be the bin number that has
                    // highest values
                    tops++;
                }
            }
        }
        int bin50Avg = 0;
        for (int i = 0; i < 50; i++) {
            bin50Avg = bin50Avg + topBins[i];
            // System.out.println("topbins="+topBins[i]);
        }
        bin50Avg = (bin50Avg / 50);
        // System.out.println("bin50Avg="+bin50Avg);
        // std dev of top 50
        double devSq50 = 0;
        for (int i = 0; i < 50; i++) {
            int dev50 = bin50Avg - topBins[i];
            int sq50 = dev50 * dev50;
            devSq50 = devSq50 + sq50;
        }
        devSq50 = (devSq50 / 50);
        devSq50 = Math.sqrt(devSq50);
        // System.out.println("std dev 50 = "+devSq50);
        // int maxStdDev = (int) (binAvg + (devSq*2));
        min = (int) (bin50Avg - devSq50 * 2);
        max = (int) (bin50Avg + devSq50 * 2);

        // standard deviation stuff
        // int binAvg = 0;
        // for (int i = 0; i < 256; i++) {
        // binAvg = binAvg + bins[i];
        // }
        // binAvg = (binAvg/256);
        // System.out.println("binAvg="+binAvg);

        // double devSq = 0;
        // for (int i = 0; i < 256; i++) {
        // int dev = binAvg - bins[i];
        // int sq = dev * dev;
        // devSq = devSq + sq;
        // }
        // devSq = (devSq/256);
        // devSq = Math.sqrt(devSq);
        // System.out.println("std dev="+devSq);
        // int maxStdDev = (int) (binAvg + (devSq*2));

        // for (int i = 0; i < 256; i++) {
        // if (c == Color.RED) {
        // System.out.println(i+","+bins[i]);
        // }
        // if (bins[i] > maxBin) {
        // maxBin = bins[i];
        // }
        // }

        // int maxThresh = (int)Math.round(maxBin * threshhold);

        // int maxThresh = mean;

        // for (int i = 1; i < 256; i++) {
        // // System.out.println(i+","+bins[i]+","+mean);
        // if (bins[i] < maxThresh) {
        // min = i;
        // } else {
        // break;
        // }
        // }
        // System.out.println("----");
        // for (int i = 254; i > 0; i--) {
        // // System.out.println(i+","+bins[i]+">"+mean);
        // if (bins[i] > maxThresh) {
        // max = i;
        // break;
        // }
        // }

        // System.out.println(c.toString() + ", min=" + min + ",max=" + max);
        return new int[] { min, max };

    }
}

Related Tutorials