Skip to content

Instantly share code, notes, and snippets.

@ardianta
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardianta/e1319141ca7887f3f510 to your computer and use it in GitHub Desktop.
Save ardianta/e1319141ca7887f3f510 to your computer and use it in GitHub Desktop.
/*
Pastikan sudah memasukan pustaka berikut ini ke dalam project:
+ jfreechart-1.0.19.jar
+ jcommon-1.0.23.jar
*/
// Method untuk membuat histogram
public void bikinHistogram(BufferedImage gmbr, String judul) {
int lebar = gmbr.getWidth();
int tinggi = gmbr.getHeight();
int count[][] = new int[4][0x100];
int merah = 0;
int biru = 1;
int hijau = 2;
int total = lebar * tinggi;
try {
for (int x = 0; x < lebar; x++) {
for (int y = 0; y < tinggi; y++) {
int rgb = gmbr.getRGB(x, y);
int merahGrey = (rgb >> 16) & 0xff;
int hijauGrey = (rgb >> 8) & 0xff;
int biruGrey = (rgb >> 0) & 0xff;
int grey = (merahGrey + hijauGrey + biruGrey) / 3;
count[merah][merahGrey]++;
count[biru][biruGrey]++;
count[hijau][hijauGrey]++;
count[3][grey]++;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
// membuat dataset dan mengisinya dengan perulangan
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int f = 0; f < 0x100; f++) {
dataset.addValue(count[0][f], "Red", new Integer(f));
dataset.addValue(count[2][f], "Blue", new Integer(f));
dataset.addValue(count[1][f], "Green", new Integer(f));
dataset.addValue(count[3][f], "Black", new Integer(f));
}
JFreeChart histo = ChartFactory.createBarChart("Histogram Citra", "Nilai", "Frekuensi", dataset,
PlotOrientation.VERTICAL, true, true, false);
ChartFrame frame = new ChartFrame("Histogram Citra", histo);
histo.setBackgroundPaint(Color.white);
final CategoryPlot plot = (CategoryPlot) histo.getPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeMinorGridlinePaint(Color.white);
frame.setSize(500, 300);
frame.setLocation(380, 200);
frame.setTitle(judul);
frame.setVisible(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment