Kelas : 4IA11
Dina Amalia (50408283)
Risti Mustika Brilianti (50408732)
sobel = {{-1,-2,-1},
{0 , 0, 0},
{1 , 2, 1}};
vertikal:
sobel = {{-1,0,1},
{-2,0,2},
{-1,0,1}};
Contoh EDGE DETECTION dengan menggunakan aplikasi program JAVA dan sudah berbasis GUI dalam pembahasan presentasi klompok saya seperti dibawah ini:
- menggunakan bentuk sobel horizontal:
kurang jelas? coba saya lampirkan source code dari sobel yang digunakan:
int c = 1;
double Gx=0, Gy=0, G;
boolean[][] b = new boolean[width][height];
int[][] array = new int[3][3];
int[][] sobel = {{-1,-2,-1},{0,0,0},{1,2,1}};
keseluruhan dari source code program ini :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* EdgeDetections.java
*
* Created on Nov 22, 2011, 5:02:27 AM
*/
//membuat folder edgedection//
package edgedetection;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
*
*
*
*
* @author mondemonci
*/
public class EdgeDetections extends javax.swing.JFrame {
JFileChooser fc;
File file;
/** Creates new form EdgeDetections */
public EdgeDetections() {
initComponents();
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
DateFormat wk = new SimpleDateFormat("E, dd MMM yyyy");
DateFormat wk2 = new SimpleDateFormat("HH:mm:ss");
String si = wk.format(Calendar.getInstance().getTime());
jLabel5.setText(si);
Timer time = new Timer(1000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String jam, menit, detik;
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND); if(h<10) jam = "0"+h;
else jam = String.valueOf(h);
if(m<10) menit = "0"+m;
else menit = String.valueOf(m);
if(s<10) detik = "0"+s;
else detik = String.valueOf(s);
jLabel6.setText(jam + ":" + menit + ":" + detik);
}
});
time.start();
}
/** 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")
//
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLayeredPane1 = new javax.swing.JLayeredPane();
jLayeredPane2 = new javax.swing.JLayeredPane();
jLabel2 = new javax.swing.JLabel();
jLayeredPane3 = new javax.swing.JLayeredPane();
jLabel3 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Edge Detection - Sobel");
jLayeredPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLayeredPane2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("No Picture Selected");
jLabel2.setBounds(10, 10, 320, 380);
jLayeredPane2.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane2.setBounds(20, 50, 340, 400);
jLayeredPane1.add(jLayeredPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("No Picture Selected");
jLabel3.setBounds(10, 10, 330, 380);
jLayeredPane3.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane3.setBounds(370, 50, 350, 400);
jLayeredPane1.add(jLayeredPane3, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLabel1.setText("Select a picture");
jLabel1.setBounds(10, 14, 120, 20);
jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jTextField1.setBounds(100, 10, 230, 30);
jLayeredPane1.add(jTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jButton1.setText("Browse");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton1.setBounds(340, 10, 90, 30);
jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jButton3.setText("Clear");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton3.setBounds(440, 10, 80, 30);
jLayeredPane1.add(jButton3, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLabel5.setText("Load..");
jLabel5.setBounds(10, 480, 120, 14);
jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLabel6.setText("Wait..");
jLabel6.setBounds(120, 480, 110, 14);
jLayeredPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);
jButton2.setText("Edge Detection");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton2.setBounds(550, 460, 170, 23);
jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int nilai = fc.showOpenDialog(this);
if(nilai==JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
jTextField1.setText(file.getName().toUpperCase());
if (jTextField1.getText().endsWith(".JPG")||jTextField1.getText().endsWith(".JPEG") || jTextField1.getText().endsWith(".PNG") || jTextField1.getText().endsWith(".GIF") || jTextField1.getText().endsWith(".BMP")) {
jTextField1.setText(file.getName());
ImageIcon icon = new ImageIcon(file.getPath());
Image img = icon.getImage();
Image newImg = img.getScaledInstance(300, 300, java.awt.Image.SCALE_SMOOTH);
ImageIcon icon2 = new ImageIcon(newImg);
jLabel2.setText("");
jLabel2.setIcon(icon2);
}
else{
jTextField1.setText("anda tidak memilih file Image");
}
}else{
jTextField1.setText("anda tidak memilih file");
}
jButton1.setEnabled(false);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField1.setText("");
jLabel2.setIcon(null);
jLabel2.setText("No Picture Selected");
jLabel3.setIcon(null);
jLabel3.setText("No Picture Selected");
jButton1.setEnabled(true);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
String nama = file.getName();
int threshold = 70;
String path = file.getPath();
BufferedImage bi = ImageIO.read(new File(path));
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage finalThresholdImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ;
int red = 0;
int green = 0;
int blue = 0;
int c = 1;
double Gx=0, Gy=0, G;
boolean[][] b = new boolean[width][height];
int[][] array = new int[3][3];
int[][] sobel = {{-1,-2,-1},{0,0,0},{1,2,1}};
for (int x = 0; x < width; x++) {
try {
for (int y = 0; y < height; y++) {
int color = bi.getRGB(x, y);
red = EdgeDetections.getRed(color);
green = EdgeDetections.getGreen(color);
blue = EdgeDetections.getBlue(color);
if((red+green+green)/3 < threshold) {
finalThresholdImage.setRGB(x,y,EdgeDetections.mixColor(0, 0,0));
}
else {
finalThresholdImage.setRGB(x,y,EdgeDetections.mixColor(255, 255,255));
}
}
} catch (Exception e) {
e.getMessage();
}
}
ImageIcon icon = new ImageIcon(finalThresholdImage);
Image newImg = icon.getImage().getScaledInstance(300, 300, java.awt.Image.SCALE_SMOOTH);
ImageIcon icon2 = new ImageIcon(newImg);
jLabel3.setText("");
jLabel3.setIcon(icon2);
} catch (IOException ex) {
Logger.getLogger(EdgeDetections.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static int mixColor(int red, int green, int blue) {
return red<<16|green<<8|blue;
}
public static int getRed(int color) {
return (color & 0x00ff0000) >> 16;
}
public static int getGreen(int color) {
return (color & 0x0000ff00) >> 8;
}
public static int getBlue(int color) {
return (color & 0x000000ff) >> 0;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(EdgeDetections.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EdgeDetections.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EdgeDetections.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EdgeDetections.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EdgeDetections().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JLayeredPane jLayeredPane2;
private javax.swing.JLayeredPane jLayeredPane3;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
- tampilan EDGE DETECTION pada saat tampilan picture belum dimasukkan layar 1 digunakan untuk pengambilan picture asli dan layar 2 digunakan untuk setelah perubahan menjadi EDGE DETECTION.
· pada saat browse picture, kemudian pilih open
- setelah dibrowse maka akan tampil seperti contoh picture dibawah ini pada layar 1.
- kemudian pada tahap ini, dilakukan proses perubahan dari picture asli ke dalam EDGE DETECTION-nya.
catatan:
-kelemahan dari sobel itu sendiri adalah tidak bisa terlalu detail/rumit dalam suatu picture.
-kelebihannya dari sobel itu sendiri adalah sangat praktis dan mudah digunakan.