I am building an application in java using the Swing component and the JAI library. I am making a histogram of a black and white or gray image. Is this a correct histogram method? If correct, then how can I normalize and equalize the histogram in my application in java using the JAI library? My code is below. in my code, I create a BufferedImage object and then create and create a histogram of this image. enter code here
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.*;
public class FinalHistogram extends JPanel {
static int[] bins = new int[256];
static int[] newBins = new int[256];
static int x1 = 0, y1 = 0;
static PlanarImage image = JAI.create("fileload", "alp_finger.tiff");
static BufferedImage bi = image.getAsBufferedImage();
FinalHistogram(int[] pbins) {
for (int i = 0; i < 256; i++) {
bins[i] = pbins[i];
newBins[i] = 0;
}
repaint();
}
@Override
protected void paintComponent(Graphics g) {
for (int i = 0; i < 256; i++) {
g.drawLine(150 + i, 300, 150 + i, 300 - (bins[i] / 300));
if (i == 0 || i == 255) {
String sr = new Integer((i)).toString();
g.drawString(sr, 150 + i, 305);
}
System.out.println("bin[" + i + "]===" + bins[i]);
}
}
public static void main(String[] args) throws IOException {
int[] sbins = new int[256];
int pixel = 0;
int k = 0;
for (int x = 0; x < bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y++) {
pixel = bi.getRaster().getSample(x, y, 0);
k = (int) (pixel / 256);
sbins[k]++;
}
}
JTabbedPane jtp = new JTabbedPane();
jtp.addTab("Histogram", new JScrollPane(new FinalHistogram(sbins)));
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.add(new JScrollPane(jtp));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
source
share