绘制图形直方图 [英] Drawing a graphical histogram
问题描述
我正在研究一个项目,我希望显示一个看起来像条形图的单线直方图,条形图中的每一行代表一个像素及其灰度值。
我有一个充满灰度值的数组,我只需要将它们放入这个直方图中并让它显示代表值的行..有点像这样
IMAGE [最小化] [最大化] [关闭]
图片直方图
我
(已加载图片)II
III
IIII
[开放] [保存]
以下是加载数组的代码......我只需要获得一些代码,这些代码将使用这些灰度值并将它们表示为上面的条形。
public void showImage(File fileName){
Scanner scan;
try {
scan = new Scanner(fileName);
typefile = scan.next();
iname = scan.next();
width = scan.nextInt();
height = scan.nextInt();
maxshade = scan.nextInt();
array = new int [width] [height];
for(int r = 0; r< array.length; r ++){
for(int c = 0; c< array [r] .length; c ++){
array [r] [c] = scan.nextInt();
imageArray = array;
repaint();
} catch(FileNotFoundException e){
e.printStackTrace();
}
}
我知道我必须做点什么喜欢......
int hist [] = new int [256];
for(int r = 0; r< array.length; r ++)
for(int c = 0; c< array [r] .length; c ++)
hist [array [R] [C]] ++;
但我不知道从那里去哪里或如何绘制图表。
类似
也许??
<个人而言,我仍然使用 JFreeChart 之类的东西,但这是一个有趣的小练习。 。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
公共类TestHisogram {
//http://stackoverflow.com/a/12520104/714968
public static void main(String [] args){
EventQueue.invokeLater(新的Runnable(){
@覆盖
公共无效的run(){
新TestHisogram();
}
});
}
public TestHisogram(){
//对于这个例子,我只是随机化了一些数据,你需要
//需要自己加载它...
int width = 256;
int height = 256;
int [] [] data = new int [width] [height];
for(int c = 0; c< height; c ++){
for(int r = 0; r< width; r ++){
data [c] [r] = (int)(256 * Math.random());
}
}
地图<整数,整数> mapHistory = new TreeMap< Integer,Integer>();
for(int c = 0; c< data.length; c ++){
for(int r = 0; r< data [c] .length; r ++){
int value = data [c] [r];
int amount = 0;
if(mapHistory.containsKey(value)){
amount = mapHistory.get(value);
金额++;
}其他{
金额= 1;
}
mapHistory.put(value,amount);
}
}
JFrame frame = new JFrame(Test);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new Graph(mapHistory)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected class图扩展JPanel {
protected static final int MIN_BAR_WIDTH = 4;
private Map< Integer,Integer> mapHistory;
public Graph(Map< Integer,Integer> mapHistory){
this.mapHistory = mapHistory;
int width =(mapHistory.size()* MIN_BAR_WIDTH)+ 11;
尺寸minSize = new尺寸(宽度,128);
Dimension prefSize = new Dimension(width,256);
setMinimumSize(minSize);
setPreferredSize(prefSize);
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(mapHistory!= null){
int xOffset = 5;
int yOffset = 5;
int width = getWidth() - 1 - (xOffset * 2);
int height = getHeight() - 1 - (yOffset * 2);
Graphics2D g2d =(Graphics2D)g.create();
g2d.setColor(Color.DARK_GRAY);
g2d.drawRect(xOffset,yOffset,width,height);
int barWidth = Math.max(MIN_BAR_WIDTH,
(int)Math.floor((float)width
/(float)mapHistory.size()));
System.out.println(width =+ width +; size =
+ mapHistory.size()+; barWidth =+ barWidth);
int maxValue = 0;
for(整数键:mapHistory.keySet()){
int value = mapHistory.get(key);
maxValue = Math.max(maxValue,value);
}
int xPos = xOffset;
for(整数键:mapHistory.keySet()){
int value = mapHistory.get(key);
int barHeight = Math.round(((float)value
/(float)maxValue)* height);
g2d.setColor(new Color(key,key,key));
int yPos = height + yOffset - barHeight;
//矩形条=新矩形(xPos,yPos,barWidth,barHeight);
Rectangle2D bar = new Rectangle2D.Float(
xPos,yPos,barWidth,barHeight);
g2d.fill(bar);
g2d.setColor(Color.DARK_GRAY);
g2d.draw(bar);
xPos + = barWidth;
}
g2d.dispose();
}
}
}
}
I am working on a project and I would like to display a single line histogram that looks like a bar graph except each line in the bar graph represents a pixel and its greyscale value.
I have a array full of greyscale values, I just need to put them into this histogram and have it display the lines which will represent the values.. sort of like this
IMAGE [minimize][maximize][close]
picture histogram
I
(Loaded Picture) I I
I I I
I I I I
[open][save]
And below is the code loading the array... I just need to get some code that will use those greyscale values and represent them as bars like above.
public void showImage(File fileName) {
Scanner scan;
try {
scan = new Scanner(fileName);
typefile = scan.next();
iname = scan.next();
width = scan.nextInt();
height = scan.nextInt();
maxshade = scan.nextInt();
array = new int[width][height];
for(int r = 0; r < array.length; r++){
for(int c = 0; c < array[r].length; c++){
array[r][c] = scan.nextInt();
imageArray = array;
repaint();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
I know I have to do something like...
int hist[] = new int[256];
for (int r = 0; r < array.length; r++)
for (int c = 0; c < array[r].length; c++)
hist[array[r][c]]++;
But I don't know where to go from there or how to draw my graph.
Something like
Perhaps??
Personally, I'd still use something like JFreeChart, but this was a fun little exercise...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestHisogram {
//http://stackoverflow.com/a/12520104/714968
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TestHisogram();
}
});
}
public TestHisogram() {
// For this example, I just randomised some data, you would
// Need to load it yourself...
int width = 256;
int height = 256;
int[][] data = new int[width][height];
for (int c = 0; c < height; c++) {
for (int r = 0; r < width; r++) {
data[c][r] = (int) (256 * Math.random());
}
}
Map<Integer, Integer> mapHistory = new TreeMap<Integer, Integer>();
for (int c = 0; c < data.length; c++) {
for (int r = 0; r < data[c].length; r++) {
int value = data[c][r];
int amount = 0;
if (mapHistory.containsKey(value)) {
amount = mapHistory.get(value);
amount++;
} else {
amount = 1;
}
mapHistory.put(value, amount);
}
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new Graph(mapHistory)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected class Graph extends JPanel {
protected static final int MIN_BAR_WIDTH = 4;
private Map<Integer, Integer> mapHistory;
public Graph(Map<Integer, Integer> mapHistory) {
this.mapHistory = mapHistory;
int width = (mapHistory.size() * MIN_BAR_WIDTH) + 11;
Dimension minSize = new Dimension(width, 128);
Dimension prefSize = new Dimension(width, 256);
setMinimumSize(minSize);
setPreferredSize(prefSize);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (mapHistory != null) {
int xOffset = 5;
int yOffset = 5;
int width = getWidth() - 1 - (xOffset * 2);
int height = getHeight() - 1 - (yOffset * 2);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.DARK_GRAY);
g2d.drawRect(xOffset, yOffset, width, height);
int barWidth = Math.max(MIN_BAR_WIDTH,
(int) Math.floor((float) width
/ (float) mapHistory.size()));
System.out.println("width = " + width + "; size = "
+ mapHistory.size() + "; barWidth = " + barWidth);
int maxValue = 0;
for (Integer key : mapHistory.keySet()) {
int value = mapHistory.get(key);
maxValue = Math.max(maxValue, value);
}
int xPos = xOffset;
for (Integer key : mapHistory.keySet()) {
int value = mapHistory.get(key);
int barHeight = Math.round(((float) value
/ (float) maxValue) * height);
g2d.setColor(new Color(key, key, key));
int yPos = height + yOffset - barHeight;
//Rectangle bar = new Rectangle(xPos, yPos, barWidth, barHeight);
Rectangle2D bar = new Rectangle2D.Float(
xPos, yPos, barWidth, barHeight);
g2d.fill(bar);
g2d.setColor(Color.DARK_GRAY);
g2d.draw(bar);
xPos += barWidth;
}
g2d.dispose();
}
}
}
}
这篇关于绘制图形直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!