Skip to main content

Histogram on 16bits images

2 replies [Last post]
cgnev
Offline
Joined: 2010-04-29
Points: 0

Hi everybody,

I'm trying to get an histogram on a 16-bits image but the Histogram operator only returns an array between 0-255.

Here is the code (which is really simple) :
String path = "path_to_16bit_image";
PlanarImage source = JAI.create("fileload", path);
ParameterBlock pb = new ParameterBlock();
pb.addSource(source);

RenderedOp op = JAI.create("histogram", pb, null);
Histogram histogram = (Histogram) op.getProperty("histogram");
// print histogram contents
for( int i = 0; i < histogram.getNumBins(0); i++ ) {
System.out.println("bins[" + i + "] = " + histogram.getBinSize(0, i));
}

When i get the execute this code, the histogram contains value between 0-255. Is there any way to have an histogram with value between 0-65535 (16bit) ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Rafael Santos

CGNev,

This code does 8- and 16-bit histograms.

Rafael

/*
* Part of the Java Image Processing Cookbook, please see
* http://www.lac.inpe.br/~rafael.santos/JIPCookbook.jsp
* for information on usage and distribution.
* Rafael Santos (rafael.santos@lac.inpe.br)
*/
package data.statistics;

import java.awt.image.DataBuffer;
import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.Histogram;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;

/**
* This class creates a Histogram from an image and prints some basic information about it.
* This class can deal with 8- and 16-bits images.
*/
public class SimpleHistogram
{
/**
* Main method.
* @param args the image file name (just one).
*/
public static void main(String[] args)
{
PlanarImage image = JAI.create("fileload",args[0]);
// Is it a 8-bit image?
if (image.getSampleModel().getDataType() == DataBuffer.TYPE_BYTE)
doHistogram(image,256);
// Is it a 16-bit image?
else if ((image.getSampleModel().getDataType() == DataBuffer.TYPE_SHORT) ||
(image.getSampleModel().getDataType() == DataBuffer.TYPE_USHORT))
doHistogram(image,65536);
else // OK, bail out with some information for the user.
{
System.err.println("Sorry, unsupported image type (");
switch(image.getSampleModel().getDataType())
{
case DataBuffer.TYPE_INT: System.err.print("Integer"); break;
case DataBuffer.TYPE_FLOAT: System.err.print("Float"); break;
case DataBuffer.TYPE_DOUBLE: System.err.print("Double"); break;
case DataBuffer.TYPE_UNDEFINED: System.err.print("Undefined"); break;
default: System.err.print("Other -- weird!"); break;
}
System.err.println(")");
}
}

// Auxiliary method to create the histogram and display information about it.
private static void doHistogram(PlanarImage image,int nbins)
{
// Create the histogram.
ParameterBlock pb = new ParameterBlock();
pb.addSource(image);
pb.add(null); // The ROI -- all image.
pb.add(1); pb.add(1); // Sampling -- each and every pixel.
pb.add(new int[]{nbins}); // Bins.
pb.add(new double[]{0}); pb.add(new double[]{nbins}); // Range for inclusion.
RenderedOp dummyImage = JAI.create("histogram", pb);
// Get the histogram from the RenderedOp.
Histogram histogram = (Histogram)dummyImage.getProperty("histogram");
// Print some histogram stats.
System.out.println("Histogram with "+nbins+" bins:");
// For each band...
for(int b=0;b {
System.out.println("Band "+b+":");
System.out.println(" Mean: "+histogram.getMean()[b]);
System.out.println(" StdDev: "+histogram.getStandardDeviation()[b]);
System.out.println(" Entropy: "+histogram.getEntropy()[b]);
System.out.println(" Total Count: "+histogram.getTotals()[b]);
}
}

}

>
> I'm trying to get an histogram on a 16-bits image but the Histogram operator only returns an array between 0-255.
>
> Here is the code (which is really simple) :
> String path = "path_to_16bit_image";
> PlanarImage source = JAI.create("fileload", path);
> ParameterBlock pb = new ParameterBlock();
> pb.addSource(source);
>
> RenderedOp op = JAI.create("histogram", pb, null);
> Histogram histogram = (Histogram) op.getProperty("histogram");
> // print histogram contents
> for( int i = 0; i < histogram.getNumBins(0); i++ ) {
> System.out.println("bins[" + i + "] = " + histogram.getBinSize(0, i));
> }
>
> When i get the execute this code, the histogram contains value between 0-255. Is there any way to have an histogram with value between 0-65535 (16bit) ?
> [Message sent by forum member 'cgnev']
>
> http://forums.java.net/jive/thread.jspa?messageID=399673
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

cgnev
Offline
Joined: 2010-04-29
Points: 0

Hi Rafael,

Thanks for your answer. I've tried it, but it seems to not work as i was expecting.
For example, executing your code on a 16-bits image will return an array of 65536 values, but the array only contains value between 0-255. I have tried it with several 16-bits images and it always given me an array filled between 0-255 and empty between 255-65536.

Do you have any idea about this ?

Thanks in advance,

CG