Making a RGB Histogram

More Topics

Applets and Native Methods.

Java Javascript interaction.

Undo / Redo for image processing

As you know a histogram provides information about the statistical distribution of color in an image. Most digital cameras nowadays have a historgam view built in. However even in some expensives DSLRs the histogram is monochrome. One of the Java Advanced Imaging (JAI) demonstrations shows you how to build a histogram but that too is monochrome. But creating a three color histogram with the Java Advanced Imaging API is a simple task.

Before we draw the chart, we need to select a file, for that we will use the familar old JFileChooser. We will load the selected file into memory using one of the JAI methods.


        PlanarImage image = JAI.create("fileload",file.getAbsolutePath());


This approach gives us a PlanarImage, rather than the familiar BufferedImage. There are no terrors in PlanarImage and it even has a getAsBufferedImage() method. In fact you can generate a historgram with a BufferedImage using JAI. Even if you don't want to mix JAI and Java 2D you can still create a histogram for a BufferedImage by simply counting the colors.


        ParameterBlock pb = new ParameterBlock();
        
        int[] bins = { 256 };
        double[] low = { 0.0D };
        double[] high = { 256.0D };
        
        pb.addSource(image);
        pb.add(null);
        pb.add(1);
        pb.add(1);
        pb.add(bins);
        pb.add(low);
        pb.add(high);
        
        RenderedOp op = JAI.create("histogram", pb, null);
        Histogram histogram = (Histogram) op.getProperty("histogram");


The usual practice with JAI is to call the create() factory method in the JAI class to make objects that we need. We pass parameters to the the create() method using the ParameterBlock class (which is part AWT package). For most operations, JAI returns a RenderedOp and the Histogram class doesn't actually implement that interface, so we need to get hold of a Histogram object by calling the getProperty() method in RenderedOp. In the next page we will see how the plot is made.

Digg!