Leave That Thing Alone Blog

Viewing By Entry / Main
10 Jun. 2006

Here is an example of how to create a histogram image from a JPG. This example uses ImageJ an open source image processing and analysis in Java.

View example output histograms

Here is the ColdFusion Code:

<cfscript>
//image
photo = "#expandPath("yourImage.jpg")#";
//read in file
jpgInputStream = createObject("java", "java.io.FileInputStream").init(photo);
//jpeg codec
jpegCodec = createObject("java", "com.sun.image.codec.jpeg.JPEGCodec");
//decode the jpeg
decoder = jpegCodec.createJPEGDecoder(jpgInputStream);
//buffered image
bufferedJPG = decoder.decodeAsBufferedImage();

//JavaLoader: set the path for JavaLoader
paths = ArrayNew(1);
//JavaLoader:ij.jar location
paths[1] = expandPath("ij.jar");
//JavaLoader:create the loader
loader = createObject("component", "JavaLoader").init(paths);
//JavaLoader:create ij ImagePlus
ImagePlus = loader.create("ij.ImagePlus").init("Test Image",bufferedJPG);
//JavaLoader:creat ij HistogramWindow
HistogramWindow = loader.create("ij.gui.HistogramWindow").init(ImagePlus);

//output JPG stream
jpgOutputStream = createObject("java", "java.io.FileOutputStream").init(expandPath("histogram.jpg"));
//encode histogram
encoder = jpegCodec.createJPEGEncoder(jpgOutputStream);
//encode histogram image
encoder.encode(HistogramWindow.getImagePlus().getImage().getBufferedImage());
//close jpgOutputStream
jpgOutputStream.close();
</cfscript>

<img src="histogram.jpg" />

<cfdump var="#HistogramWindow.getHistogram()#" label="Histogram" />

To get this example to work you will need:

Comments

Very cool! Thanks for the walkthrough.


I'm not a huge programmer, but I had a theory question for ya and your article was the first clue i could find to what I want to do.

Is there some way you can implement this (Or maybe something that is already out there) - that could take a small sized image (like at most 20x20.jpg) and could output the most frequent color used in a picture that size?

I've been researching it for weeks but I'm not a smart programmer... I only know some basic stuff but had an idea for months now that could use this or something similar.

I could get into it more but it's probably disrepectful to just start asking you a million questions without knowing you.... so I guess I'll finish my query here unless i hear back from you. Great article though :)