Java – displays a histogram of image data
Sometimes I need to display the representation of image data in the form of a histogram I am particularly interested in the way image data is accessed I am familiar with JfreeChart, including histogram support, but I will consider other methods
Solution
The following example uses several techniques to create an RGB histogram of an arbitrary image:
>Raster method getsamples() extracts the value of each color band from bufferedimage. > The histogramdataset method addseries() adds the count of each band to the dataset. > A standardxybarpainter replaces the default value of chartfactory, as shown here. > Customize the defaultdrawingsupplier to provide the colors required for each series; It contains translucent colors. > The deformation of here is used to control the visibility of each frequency band; The supplementary method of using chartmouselistener is shown here
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JCheck@R_992_2419@;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DefaultDrawingsupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.statistics.HistogramDataset;
/**
* @see https://stackoverflow.com/q/40537278/230513
* @see https://stackoverflow.com/q/11870416/230513
* @see https://stackoverflow.com/a/28519356/230513
*/
public class Histogram {
private static final int BINS = 256;
private final BufferedImage image = getImage();
private HistogramDataset dataset;
private XYBarRenderer renderer;
private BufferedImage getImage() {
try {
return ImageIO.read(new URL(
"http://i.imgur.com/kxXhIH1.jpg"));
} catch (IOException e) {
e.printStackTrace(System.err);
}
return null;
}
private ChartPanel createChartPanel() {
// dataset
dataset = new HistogramDataset();
Raster raster = image.getRaster();
final int w = image.getWidth();
final int h = image.getHeight();
double[] r = new double[w * h];
r = raster.getSamples(0,w,h,r);
dataset.addSeries("Red",r,BINS);
r = raster.getSamples(0,1,r);
dataset.addSeries("Green",2,r);
dataset.addSeries("Blue",BINS);
// chart
JFreeChart chart = ChartFactory.createHistogram("Histogram","Value","Count",dataset,PlotOrientation.VERTICAL,true,false);
XYPlot plot = (XYPlot) chart.getPlot();
renderer = (XYBarRenderer) plot.getRenderer();
renderer.setBarPainter(new StandardXYBarPainter());
// translucent red,green & blue
Paint[] paintArray = {
new Color(0x80ff0000,true),new Color(0x8000ff00,new Color(0x800000ff,true)
};
plot.setDrawingsupplier(new DefaultDrawingsupplier(
paintArray,DefaultDrawingsupplier.DEFAULT_FILL_PAINT_SEQUENCE,DefaultDrawingsupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,DefaultDrawingsupplier.DEFAULT_stroke_SEQUENCE,DefaultDrawingsupplier.DEFAULT_OUTLINE_stroke_SEQUENCE,DefaultDrawingsupplier.DEFAULT_SHAPE_SEQUENCE));
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
return panel;
}
private JPanel createControlPanel() {
JPanel panel = new JPanel();
panel.add(new JCheck@R_992_2419@(new VisibleAction(0)));
panel.add(new JCheck@R_992_2419@(new VisibleAction(1)));
panel.add(new JCheck@R_992_2419@(new VisibleAction(2)));
return panel;
}
private class VisibleAction extends AbstractAction {
private final int i;
public VisibleAction(int i) {
this.i = i;
this.putValue(NAME,(String) dataset.getSeriesKey(i));
this.putValue(SELECTED_KEY,true);
renderer.setSeriesVisible(i,true);
}
@Override
public void actionPerformed(ActionEvent e) {
renderer.setSeriesVisible(i,!renderer.getSeriesVisible(i));
}
}
private void display() {
JFrame f = new JFrame("Histogram");
f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
f.add(createChartPanel());
f.add(createControlPanel(),BorderLayout.soUTH);
f.add(new JLabel(new ImageIcon(image)),BorderLayout.WEST);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokelater(() -> {
new Histogram().display();
});
}
}
