Sunteți pe pagina 1din 17

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package praktikum6;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import prak6.prak6Hisogram;

/**
*
* @author riska
*/

public class citra6 extends javax.swing.JFrame {

BufferedImage image=null;
private int width;
private int height;

String input=null,
srcIm="rose.jpg";

/**
* Creates new form citra6
*/
public citra6() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jButton1 = new javax.swing.JButton();


jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();


jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("Load");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Grayscale");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("Histogram");
jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {


jButton3ActionPerformed(evt);
}
});

jButton4.setText("CDF");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setText("H Red");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton6.setText("H Green");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

jButton7.setText("H Blue");

jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});

jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N


jLabel2.setText("Pengolahan Citra \"Histogram Warna\"");

jLabel3.setText("Riska Ulfia ~ 361455401070");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 103,


Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(38, 38, 38)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 503,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(265, 265, 265)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 204,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(26, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 339,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(172, 172, 172))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 332,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 28,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 28,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 28,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton7)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25,
Short.MAX_VALUE)
.addComponent(jLabel3))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton7ActionPerformed


// H blue
File input=new File (srcIm);
width = image.getWidth();
height = image.getHeight();
int [] hist=new int [256];

for (int i = 0; i < hist.length; i++) {


hist[i]=0;
}

for (int i = 0; i <width ; i++) {


for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));

int blue=(int)(w.getGreen());
int xg= (int) (blue+blue+blue)/3;

hist[xg]+=1;
}
}

DefaultCategoryDataset dataset=new DefaultCategoryDataset();


for (int i = 0; i < 256; i++) {
dataset.addValue(hist[i], i+"", "");
}
JFreeChart chart= ChartFactory.createBarChart("Histogram", "blue", "nilai",
dataset,PlotOrientation.VERTICAL,false,true,false);
ChartFrame frame=new ChartFrame("Histogram image", chart);
frame.setSize(600,400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton1ActionPerformed


// load
File input=new File (srcIm);
try{
image=ImageIO.read(input);
}catch (IOException ex)
{
Logger.getLogger(citra6.class.getName()).log(Level.SEVERE,null,ex);
}
ImageIcon imageIcon = new ImageIcon(image);
jLabel1.setIcon(imageIcon);
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton2ActionPerformed


// grayscale
File input=new File (srcIm);
width = image.getWidth();
height = image.getHeight();
try{
image = ImageIO.read(input);

for (int i = 0; i <width ; i++) {


for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));

int red=(int)(w.getRed());

int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
int xg= (int) (red+green+blue)/3;

Color newcolor=new Color(xg,xg,xg);


image.setRGB(i,j,newcolor.getRGB());
}
}
ImageIcon imageIcon1 = new ImageIcon(image);
jLabel1.setIcon(imageIcon1);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(citra6.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton3ActionPerformed


// histogram
File input=new File (srcIm);
width = image.getWidth();
height = image.getHeight();
int [] hist=new int [256];
for (int i = 0; i < hist.length; i++) {
hist[i]=0;
}

for (int i = 0; i <width ; i++) {


for (int j = 0; j < height; j++) {

Color w = new Color(image.getRGB(i, j));

int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
int xg= (int) (red+green+blue)/3;

hist[xg]+=1;
}
}

DefaultCategoryDataset dataset=new DefaultCategoryDataset();


for (int i = 0; i < 256; i++) {
dataset.addValue(hist[i], i+"", "");
}
JFreeChart chart= ChartFactory.createBarChart("Histogram", "grayscale", "nilai",
dataset,PlotOrientation.VERTICAL,false,true,false);
ChartFrame frame=new ChartFrame("Histogram image", chart);
frame.setSize(600,400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton4ActionPerformed


// CDF
File input=new File (srcIm);
width = image.getWidth();

height = image.getHeight();

int [] hist=new int [256];


for (int i = 0; i < hist.length; i++) {
hist[i]=0;
}

for (int i = 0; i <width ; i++) {


for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));

int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
int xg= (int) (red+green+blue)/3;

hist[xg]+=1;
}
}
int[] cdf =new int [256];
cdf[0]=hist[0];
for (int i = 1; i < 256; i++) {
cdf[i]=cdf[i-1]+hist[i];
}
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
for (int i = 0; i < 256; i++) {
dataset.addValue(cdf[i], i+"", "");

}
JFreeChart chart= ChartFactory.createBarChart("Fungsi Distribusi Kumulatif", "grayscale",
"nilai",
dataset,PlotOrientation.VERTICAL,false,true,false);
ChartFrame frame=new ChartFrame("CDF", chart);
frame.setSize(600,400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton5ActionPerformed


// H red
File input=new File (srcIm);
width = image.getWidth();
height = image.getHeight();
int [] hist=new int [256];
for (int i = 0; i < hist.length; i++) {
hist[i]=0;
}

for (int i = 0; i <width ; i++) {


for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));

int red=(int)(w.getRed());
int xg= (int) (red);

hist[xg]+=1;
}
}

DefaultCategoryDataset dataset=new DefaultCategoryDataset();


for (int i = 0; i < 256; i++) {
dataset.addValue(hist[i], i+"", "");
}
JFreeChart chart= ChartFactory.createBarChart("Histogram", "red", "nilai",
dataset,PlotOrientation.VERTICAL,false,true,false);
ChartFrame frame=new ChartFrame("Histogram image", chart);
frame.setSize(600,400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton6ActionPerformed


// H Green
File input=new File (srcIm);
width = image.getWidth();
height = image.getHeight();
int [] hist=new int [256];
for (int i = 0; i < hist.length; i++) {
hist[i]=0;
}

for (int i = 0; i <width ; i++) {

for (int j = 0; j < height; j++) {


Color w = new Color(image.getRGB(i, j));

int green=(int)(w.getGreen());
int xg= (int) (green+green+green)/3;

hist[xg]+=1;
}
}

DefaultCategoryDataset dataset=new DefaultCategoryDataset();


for (int i = 0; i < 256; i++) {
dataset.addValue(hist[i], i+"", "");
}
JFreeChart chart= ChartFactory.createBarChart("Histogram", "green", "nilai",
dataset,PlotOrientation.VERTICAL,false,true,false);
ChartFrame frame=new ChartFrame("Histogram image", chart);
frame.setSize(600,400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}//GEN-LAST:event_jButton6ActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(citra6.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(citra6.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(citra6.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(citra6.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {


new citra6().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration//GEN-END:variables
}

S-ar putea să vă placă și