Java 显示图像数据的直方图
我有时需要显示一个的表示。我对访问图像数据的方法特别感兴趣。我熟悉Java 显示图像数据的直方图,java,image,swing,histogram,jfreechart,Java,Image,Swing,Histogram,Jfreechart,我有时需要显示一个的表示。我对访问图像数据的方法特别感兴趣。我熟悉 jFrutsC>/COD>,其中包括直方图支持,但我会考虑其他方法。 < P>下面的示例使用几种技术来创建任意图像的RGB直方图: 方法getSamples()从buffereImage中提取每个色带的值 方法addSeries()将每个波段的计数添加到数据集 如图所示,A将替换默认值 定制提供每个系列所需的颜色;它包含半透明的颜色 讨论了一种用于控制每个波段可见度的变化;显示了使用ChartMouseListener
- 方法
从getSamples()
中提取每个色带的值buffereImage
- 方法
将每个波段的计数添加到addSeries()
数据集
- 如图所示,A将替换默认值
- 定制提供每个系列所需的颜色;它包含半透明的颜色
- 讨论了一种用于控制每个波段可见度的变化;显示了使用
的补充方法ChartMouseListener
- A用于将样本图像转换为灰度,如图和所示
- 嵌套循环在
的像素上迭代,调用buffereImage
方法提取每个像素的值;相应的计数用于构造getRGB()
数据集
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.GradientPaint;
导入java.awt.Graphics2D;
导入java.awt.GridLayout;
导入java.awt.image.buffereImage;
导入java.awt.image.ColorConvertOp;
导入javax.swing.BorderFactory;
导入javax.swing.Icon;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入net.sourceforge.chart2d.Chart2DProperties;
导入net.sourceforge.chart2d.Dataset;
导入net.sourceforge.chart2d.GraphChart2DProperties;
导入net.sourceforge.chart2d.GraphProperties;
导入net.sourceforge.chart2d.LBChart2D;
导入net.sourceforge.chart2d.LegendProperties;
导入net.sourceforge.chart2d.MultiColorsProperties;
导入net.sourceforge.chart2d.Object2DProperties;
/**@见https://stackoverflow.com/q/9964872/230513 */
公共类直方图扩展JPanel{
private buffereImage image=getImage(“OptionPane.warningIcon”);
专用缓冲区图像灰度=getGray(图像);
公共直方图(){
JPanel面板=新JPanel(新网格布局(0,1));
panel.setBorder(BorderFactory.createEmptyByOrder(8,8,8,8));
添加(新JLabel(新图像图标(图像));
添加(新的JLabel(新的图像图标(灰色));
此.setLayout(新的BorderLayout());
此.add(面板,BorderLayout.WEST);
添加(createChart(灰色,20),BorderLayout.CENTER);
}
私有BuffereImage getImage(字符串名称){
Icon-Icon=UIManager.getIcon(名称);
int w=icon.getIconWidth();
inth=icon.getIconHeight();
此.setPreferredSize(新尺寸(w,h));
BuffereImage i=新的BuffereImage(w,h,BuffereImage.TYPE_INT_ARGB);
Graphics2D g2d=(Graphics2D)i.getGraphics();
g2d.setPaint(新GradientPaint(
0,0,Color.blue,w,h,Color.green,true);
g2d.fillRect(0,0,w,h);
icon.paintIcon(null,g2d,0,0);
g2d.dispose();
返回i;
}
私有BuffereImage getGray(BuffereImage图像){
BuffereImage g=新的BuffereImage(
image.getWidth()、image.getHeight()、buffereImage.TYPE_BYTE_GRAY);
ColorConvertOp=新的ColorConvertOp(
image.getColorModel().getColorSpace(),
g、 getColorModel().getColorSpace(),null);
op.filter(图像,g);
返回g;
}
私有LBChart2D createChart(BuffereImage灰色,整数桶){
//图表2D配置
Object2DProperties object2DProps=新的Object2DProperties();
setObjectTitleText(“灰色直方图”);
Chart2DProperties chart2DProps=新的Chart2DProperties();
setChartDataLabelsPrecision(-1);
LegendProperties legendProps=新LegendProperties();
字符串[]legendLabels={“灰色”};
legendProps.SetLegendLabelsText(legendLabels);
GraphChart2Properties GraphChart2Properties=新GraphChart2Properties();
GraphChart2Props.SetLabelAxisTitleText(“灰色”);
graphchart2props.setNumbersAxisTitleText(“计数”);
//数据集
String[]labelsAxisLabels=新字符串[Bucket];
for(int i=0;iimport 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(
"https://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();
});
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import net.sourceforge.chart2d.Chart2DProperties;
import net.sourceforge.chart2d.Dataset;
import net.sourceforge.chart2d.GraphChart2DProperties;
import net.sourceforge.chart2d.GraphProperties;
import net.sourceforge.chart2d.LBChart2D;
import net.sourceforge.chart2d.LegendProperties;
import net.sourceforge.chart2d.MultiColorsProperties;
import net.sourceforge.chart2d.Object2DProperties;
/** @see https://stackoverflow.com/q/9964872/230513 */
public class Histogram extends JPanel {
private BufferedImage image = getImage("OptionPane.warningIcon");
private BufferedImage gray = getGray(image);
public Histogram() {
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
panel.add(new JLabel(new ImageIcon(image)));
panel.add(new JLabel(new ImageIcon(gray)));
this.setLayout(new BorderLayout());
this.add(panel, BorderLayout.WEST);
this.add(createChart(gray, 20), BorderLayout.CENTER);
}
private BufferedImage getImage(String name) {
Icon icon = UIManager.getIcon(name);
int w = icon.getIconWidth();
int h = icon.getIconHeight();
this.setPreferredSize(new Dimension(w, h));
BufferedImage i = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) i.getGraphics();
g2d.setPaint(new GradientPaint(
0, 0, Color.blue, w, h, Color.green, true));
g2d.fillRect(0, 0, w, h);
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
return i;
}
private BufferedImage getGray(BufferedImage image) {
BufferedImage g = new BufferedImage(
image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
ColorConvertOp op = new ColorConvertOp(
image.getColorModel().getColorSpace(),
g.getColorModel().getColorSpace(), null);
op.filter(image, g);
return g;
}
private LBChart2D createChart(BufferedImage gray, int buckets) {
// Chart2D configuration
Object2DProperties object2DProps = new Object2DProperties();
object2DProps.setObjectTitleText("Gray Histogram");
Chart2DProperties chart2DProps = new Chart2DProperties();
chart2DProps.setChartDataLabelsPrecision(-1);
LegendProperties legendProps = new LegendProperties();
String[] legendLabels = {"Gray"};
legendProps.setLegendLabelsTexts(legendLabels);
GraphChart2DProperties graphChart2DProps = new GraphChart2DProperties();
graphChart2DProps.setLabelsAxisTitleText("Gray");
graphChart2DProps.setNumbersAxisTitleText("Count");
// Dataset
String[] labelsAxisLabels = new String[buckets];
for (int i = 0; i < labelsAxisLabels.length; i++) {
labelsAxisLabels[i] = String.valueOf(i * 256 / buckets);
}
graphChart2DProps.setLabelsAxisLabelsTexts(labelsAxisLabels);
int[] counts = new int[buckets];
for (int r = 0; r < gray.getHeight(); r++) {
for (int c = 0; c < gray.getWidth(); c++) {
int v = (gray.getRGB(c, r) & 0xff) * buckets / 256;
counts[v]++;
}
}
Dataset dataset = new Dataset(1, counts.length, 1);
for (int i = 0; i < counts.length; i++) {
dataset.set(0, i, 0, counts[i]);
}
GraphProperties graphProps = new GraphProperties();
MultiColorsProperties multiColorsProps = new MultiColorsProperties();
LBChart2D chart2D = new LBChart2D();
chart2D.setObject2DProperties(object2DProps);
chart2D.setChart2DProperties(chart2DProps);
chart2D.setLegendProperties(legendProps);
chart2D.setGraphChart2DProperties(graphChart2DProps);
chart2D.addGraphProperties(graphProps);
chart2D.addDataset(dataset);
chart2D.addMultiColorsProperties(multiColorsProps);
//Optional validation: Prints debug messages if invalid only.
if (!chart2D.validate(false)) {
chart2D.validate(true);
}
return chart2D;
}
private void display() {
JFrame f = new JFrame("Histogram");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setSize(640, 480);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Histogram().display();
}
});
}
}