Vous êtes sur la page 1sur 18

/*

* 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 praktikum7;

import Praktikum3.citra3;
import Praktikum5.citra5;
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.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
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 praktikum6.citra6;

/**

*
* @author Acer
*/
public class Citra7 extends javax.swing.JFrame {

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

String input=null,
srcIm="flower1.jpg";
private Icon ImageIcon;
private int[] cdf;
private int[] hist;
private ImageIcon imageIcon;

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

// private JFileChooser chooser = new JFileChooser();

/**
* 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();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = 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("HisEqual");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

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

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


jLabel3.setText("Pengolahan Citra \"Histogram Equalization\"");

jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N


jLabel4.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()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 98,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 98,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 98,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 98,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 98,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 98,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(114, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 347,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 233,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(242, 242, 242))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 480,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(130, 130, 130))))


);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 350,
Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13,
Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())
);

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

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


// load

/* int jes=chooser.showOpenDialog(this);
try {
if(jes==chooser.APPROVE_OPTION){
File input=chooser.getSelectedFile();
image = ImageIO.read(input);
}
} catch (IOException ex) {
java.util.logging.Logger.getLogger(Citra7.class.getName()).log(Level.SEVERE, null, ex);
}
ImageIcon imageIcon = new ImageIcon(image);
jLabel1.setIcon(imageIcon); */

File input=new File (srcIm);


try{
image=ImageIO.read(input);
}catch (IOException ex)
{
Logger.getLogger(Citra7.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);

jLabel2.setIcon(imageIcon1);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(Citra7.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 jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton6ActionPerformed


// autolevel
File input = new File(srcIm);
width = image.getWidth();
height = image.getHeight();

try{
image = ImageIO.read(input);
int xgmax=0;
int xgmin=255;
for (int i = 0; i <width ; i++) {
for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));
// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB digabungkan
int xg = (int) (red+green+blue)/3;
if (xg < xgmin) {
xgmin = xg;
}
if (xg > xgmax) {
xgmax = xg;
}
}
}

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


for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));
// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB digabungkan
int xg = (int) (red+green+blue)/3;

int xb=(int) (255*(xg-xgmin)/(xgmax-xgmin));


Color newcolor=new Color(xb,xb,xb);
image.setRGB(i,j,newcolor.getRGB());
}
}
ImageIcon imageIcon = new ImageIcon(image);
jLabel2.setIcon(imageIcon);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton6ActionPerformed

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


// HistQual
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];
}
int nx=width;
int ny=height;

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;

int xb= (int)(255*cdf[xg])/(nx*ny);

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


image.setRGB(i,j,newcolor.getRGB());
}
}
imageIcon = new ImageIcon(image);
jLabel2.setIcon (imageIcon);

}//GEN-LAST:event_jButton5ActionPerformed

/**
* @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(Citra7.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Citra7.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Citra7.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Citra7.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 Citra7().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.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
// End of variables declaration//GEN-END:variables
}