Reputation: 1025
I had load buffered image and need to plot histogram? please suggest me next steps to to plot RGB histogram. if it can done using jai please suggest me the way to do it. I hadtried alot and also googled alot but dint found any right solution. here is how i had load my image please provide me next steps
BufferedImage image= ImageIO.read(new File("C:\\Images\\Sunset.jpg"));
ParameterBlock pb = new ParameterBlock();
int[] bins = { 256 };
double[] low = { 0.0D };
double[] high = { 256.0D };
pb.addSource(image);
pb.add(null);
pb.add(1);
pb.add(1);
pb.add(bins);
pb.add(low);
pb.add(high);
RenderedOp op = JAI.create("histogram", pb, null);
Histogram histogram = (Histogram) op.getProperty("histogram");
Upvotes: 2
Views: 9846
Reputation: 1265
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.IOException;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.*;
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])/1000);
}
}
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", "image12.tiff");
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]);
d=(0.2125*pixel[0])+(0.7154*pixel[1])+(0.072*pixel[2]);
k=(int) (d/256);
sbins[k]++;
}
}
System.out.println("copleted" + d + "--" + k);
JTabbedPane jtp=new JTabbedPane();
ImageIcon im= new ImageIcon(bi);
jtp.addTab("Histogram",new FinalHistogram(sbins));
frame.add(jtp);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
Upvotes: 2
Reputation: 11245
This is a really basic Histogram and to be honest I don't know how correct it is... but I hope it can be of some use to you
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Histogram extends JPanel {
private int SIZE = 256;
// Red, Green, Blue
private int NUMBER_OF_COLOURS = 3;
public final int RED = 0;
public final int GREEN = 1;
public final int BLUE = 2;
private int[][] colourBins;
private volatile boolean loaded = false;
private int maxY;
/**
*
* @param Path
* of image to create Histogram of.
*/
public Histogram() {
colourBins = new int[NUMBER_OF_COLOURS][];
for (int i = 0; i < NUMBER_OF_COLOURS; i++) {
colourBins[i] = new int[SIZE];
}
loaded = false;
}
public void load(String path) throws IOException {
BufferedImage bi = ImageIO.read(new File(path));
// Reset all the bins
for (int i = 0; i < NUMBER_OF_COLOURS; i++) {
for (int j = 0; j < SIZE; j++) {
colourBins[i][j] = 0;
}
}
for (int x = 0; x < bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y++) {
Color c = new Color(bi.getRGB(x, y));
colourBins[RED][c.getRed()]++;
colourBins[GREEN][c.getGreen()]++;
colourBins[BLUE][c.getBlue()]++;
}
}
maxY = 0;
for (int i = 0; i < NUMBER_OF_COLOURS; i++) {
for (int j = 0; j < SIZE; j++) {
if (maxY < colourBins[i][j]) {
maxY = colourBins[i][j];
}
}
}
loaded = true;
}
@Override
public void paint(Graphics g) {
if (loaded) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setStroke(new BasicStroke(2));
int xInterval = (int) ((double)getWidth() / ((double)SIZE+1));
g2.setColor(Color.black);
for (int i = 0; i < NUMBER_OF_COLOURS; i++) {
// Set the graph
if (i == RED) {
g2.setColor(Color.red);
} else if (i == GREEN) {
g2.setColor(Color.GREEN);
} else if (i == BLUE) {
g2.setColor(Color.blue);
}
// draw the graph for the spesific colour.
for (int j = 0; j < SIZE - 1 ; j++) {
int value = (int) (((double)colourBins[i][j] / (double)maxY) * getHeight());
int value2 = (int) (((double)colourBins[i][j+1] / (double)maxY) * getHeight());
g2.drawLine(j * xInterval, getHeight() - value, (j+1)*xInterval, getHeight() - value2);
}
}
} else {
super.paint(g);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Debug Frame");
frame.setSize(200, 200);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Histogram his = new Histogram();
try {
his.load("c:/scratch/andriod.png");
} catch (IOException e) {
e.printStackTrace();
}
frame.add(his,BorderLayout.CENTER);
frame.setVisible(true);
}
}
Upvotes: 2