java 图像直方图_java – 显示图像数据的直方图

该示例展示了如何用Java创建一个任意图像的RGB直方图。通过使用Raster的getSamples()方法从BufferedImage中提取颜色带值,然后将这些值添加到HistogramDataset中。进一步定制了JFreeChart的渲染和颜色,以呈现半透明的红色、绿色和蓝色直方图。
摘要由CSDN通过智能技术生成

下面的示例使用几种技术来创建任意图像的RGB直方图:

> Raster方法getSamples()从BufferedImage中提取每个色带的值.

> HistogramDataset方法addSeries()将每个波段的计数添加到数据集.

> A StandardXYBarPainter替换ChartFactory默认值,如here所示.

>定制DefaultDrawingSupplier提供每个系列所需的颜色;它包含半透明的颜色.

> here的变形here用于控制每个频带的可见度;使用ChartMouseListener的补充方法如here所示.

QPZmt.png

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.JCheckBox;

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, 0, w, h, 0, r);

dataset.addSeries("Red", r, BINS);

r = raster.getSamples(0, 0, w, h, 1, r);

dataset.addSeries("Green", r, BINS);

r = raster.getSamples(0, 0, w, h, 2, r);

dataset.addSeries("Blue", r, BINS);

// chart

JFreeChart chart = ChartFactory.createHistogram("Histogram", "Value",

"Count", dataset, PlotOrientation.VERTICAL, true, 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, true),

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 JCheckBox(new VisibleAction(0)));

panel.add(new JCheckBox(new VisibleAction(1)));

panel.add(new JCheckBox(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();

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值