我使用java swing在netbeans平台上制作应用程序。我创建了特定图像的直方图。但它不像ImageJ软件中那个图像的直方图那样精确。所以,我怎么能得到正确的直方图,或者任何人都可以向我建议我使用哪种方法获得精确的图像像素并绘制直方图?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FinalHistogram extends JPanel {
int[] bins = new int[256];
FinalHistogram(int[] pbins) {
bins = pbins;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
for (int i = 0; i < 256; i++) {
System.out.println("bin[" + i + "]===" + bins[i]);
g.drawLine(200 + i, 300, 200 + i, 300 - (bins[i]) / 70);
}
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame();
frame.setSize(500, 500);
int[] pbins = new int[256];
int[] sbins = new int[256];
PlanarImage image = JAI.create("fileload", "C:\\aimages\\11309.jpg");
BufferedImage bi = image.getAsBufferedImage();
System.out.println("tipe is " + bi.getType());
int[] pixel = new int[3];
int k = 0;
Color c = new Color(k);
Double d = 0.0;
Double d1;
for (int x = 0; x < bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y++) {
pixel = bi.getRaster().getPixel(x, y, new int[3]);
k = (int) ((pixel[0]) + ( pixel[1]) + (pixel[2]));
k=k/3;
sbins[k]++;
}
}
System.out.println("copleted" + d + "--" + k);
frame.add(new FinalHistogram(sbins));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
答案 0 :(得分:0)
用于创建直方图的黑/白图像的伪代码:
lookup_table[256] //integers.
for i in 256 do
lookup_table[i] = 0
for i in image.size
lookup_table[ pixel.value ] = lookup_table[ pixel.value ] + 1;
通常可视化直方图,你必须使用一种称为“binning”的技术,因为在大图像中你有很多像素,灰色阴影和1:1的表现形式都不适合你的屏幕。