PENGOLAHAN CITRA - Erosi dan Dilasi
Coding erosi.java
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.PixelGrabber;
public class erosi extends Applet
{
Image img;
int lebar,tinggi;
int latar,ambang,iterasi,periksa,nilai,periksa1;
int warna, red, green, blue,alpha, abuabu, tempwarna;
String tampil;
public void init()
{
img=this.getImage(this.getDocumentBase(),getParameter("filegambar"));
lebar =img.getWidth(this);
latar = Integer.parseInt(getParameter("latar"));
ambang = Integer.parseInt(getParameter("ambang"));
iterasi = Integer.parseInt(getParameter("iterasi"));
}
public void paint(Graphics g)
{
//beri warna pada panel
g.setColor(Color.lightGray);
g.fillRect(0,0,2800,800);
//ambil lebar dan tinggi citra
lebar = img.getWidth(this);
tinggi = img.getHeight(this);
//tampilkan informasi lebar dan tinggi citra
g.setColor(Color.black);
tampil = String.valueOf(lebar);
g.drawString("lebar: "+tampil,10,20);
tampil = String.valueOf(tinggi);
g.drawString("tinggi: "+tampil,10,40);
g.drawImage(img,10,50,this); //tampilkan citra dengan drawImage
int [] pixels = new int[lebar*tinggi]; //Tentukan panjang array pixels
int [][] bitmapA = new int [lebar+2][tinggi+2];
int [][] bitmapB = new int [lebar+2][tinggi+2];
int [][] bitmapTemp = new int[lebar+2] [tinggi+2];
int [][] resultBitmap =new int [lebar][tinggi];
int [][] bitmapA1 = new int [lebar+2][tinggi+2];
int [][] bitmapB1 = new int [lebar+2][tinggi+2];
int [][] bitmapTemp1 = new int[lebar+2] [tinggi+2];
int [][] resultBitmap1 =new int [lebar][tinggi];
PixelGrabber pg = new PixelGrabber(img,0,0,lebar,tinggi, pixels,0,lebar);
try
{
pg.grabPixels(); //grab piksel citra ke object pg.
}
catch(InterruptedException ie)
{
System.out.println("Terjadi kesalahan saat mengambil data pixels");
ie.printStackTrace();
return;
}
g.setColor(Color.black);
g.drawString("Hasil Pengolahan", lebar+100, 10);
g.drawString("Grayscale", lebar+100, 30);
g.drawString("Biner", lebar+100+lebar+50, 30);
g.drawString("Erosi", lebar+100, lebar+90);
g.drawString("Dilasi", lebar+100+lebar+50, lebar+90);
g.setColor(Color.white);
for(int y=0; y<tinggi; y++)
{
for(int x=0; x<lebar; x++)
{
//Menguraikan warna dengan model RGB
warna = pixels[y*lebar+x];
red = (warna >> 16) &0xff;
green = (warna >> 8) &0xff;
blue = (warna) & 0xff;
abuabu = (red + green + blue) /3;
//isi bitmap dengan nilai intensitasnya
bitmapA[x+1][y+1] = (int) abuabu;
bitmapA1[x+1][y+1] = (int) abuabu;
if(abuabu<=ambang)
{
bitmapB[x+1][y+1] = 0;
bitmapTemp[x+1][y+1] = 0;
bitmapB1[x+1][y+1] = 0;
bitmapTemp1[x+1][y+1] = 0;
}else
{
bitmapB[x+1][y+1] = 1;
bitmapTemp[x+1][y+1] = 1;
bitmapB1[x+1][y+1] = 1;
bitmapTemp1[x+1][y+1] = 1;
}
}
}
for(int i=1;i<=iterasi;i++)
{
//isi nilai tepi
for(int y=0;y<=tinggi+1;y++)
{
bitmapB[0][y] = bitmapB[1][y];
bitmapB[lebar+1][y] = bitmapB[lebar][y];
bitmapB1[0][y] = bitmapB1[1][y];
bitmapB1[lebar+1][y] = bitmapB1[lebar][y];
}
for(int x=0;x<=lebar+1;x++)
{
bitmapB[x][0] = bitmapB[x][1];
bitmapB[x][tinggi+1] = bitmapB[x][tinggi];
bitmapB1[x][0] = bitmapB1[x][1];
bitmapB1[x][tinggi+1] = bitmapB1[x][tinggi];
}
//erosi
for(int y=0;y<tinggi;y++)
{
for(int x=0;x<lebar;x++)
{
resultBitmap[x][y] = 0;
periksa=0;
for(int l=-1;l<=1;l++)
{
for(int k=-1;k<=1;k++)
{
periksa=periksa + + (int)(bitmapB[x+1+(k*-1)][y+1+(l*-1)]);
}
}
if(latar==0)
{
//untuk latar=0,jika semua adalah 1 maka set dengan 1,jika tidak set dengan 0
if(periksa==9)
resultBitmap[x][y] = 1;
else
resultBitmap[x][y] = 0;
}else
{
//untuk latar=1,jika semua adalah 0 maka set dengan 0,jika tidak set dengan 1
if(periksa==0)
resultBitmap[x][y] = 0;
else
resultBitmap[x][y] = 1;
}
}
}
//dilasi
for (int y=0;y<tinggi;y++)
{
for (int x=0;x<lebar;x++)
{
resultBitmap1[x][y] = 1;
periksa1 = 0;
for (int l=-1;l<=1;l++)
{
for (int k=-1;k<=1;k++)
{
periksa1 = periksa1 + + (int) (bitmapB1[x+1+(k*-1)][y+1+(l*-1)]);
}
}
if (latar == 0)
{
if (periksa1 == 0)
resultBitmap1[x][y] = 0;
else
resultBitmap1[x][y] = 1;
}
else
{
if (periksa1 == 9)
resultBitmap1[x][y] = 1;
else
resultBitmap1[x][y] = 0;
}
}
}
if(i<iterasi)
{
for(int y=0;y<tinggi;y++)
{
for(int x=0;x<lebar;x++)
{
bitmapB[x+1][y+1] = resultBitmap[x][y];
bitmapB1[x+1][y+1] = resultBitmap1[x][y];
}
}
}
}
//Tampilkan Hasil Proses
bitmapB = bitmapTemp;
bitmapB1 = bitmapTemp1;
for (int y=0; y<=tinggi-1; y++)
{
for (int x=0; x<=lebar-1; x++)
{
//Baris Pertama
//Abu Abu
g.setColor(new Color(bitmapA[x+1][y+1], bitmapA[x+1][y+1], bitmapA[x+1][y+1]));
g.drawLine(x+lebar+100, y+40,x+lebar+100, y+40);
//biner
//sesuaikan nilai
if(bitmapB[x][y] == 1)
nilai = 255;
else
nilai=0;
g.setColor(new Color(nilai, nilai, nilai));
g.drawLine(x+2*lebar+100+50, y+40,x+2*lebar+100+50, y+40);
//erosi
if(resultBitmap[x][y] == 1)
nilai=255;
else
nilai=0;
g.setColor(new Color(nilai, nilai, nilai));
g.drawLine(x+lebar+100, y+lebar+100,x+lebar+100, y+lebar+100);
//dilasi
if(resultBitmap1[x][y] == 1)
nilai=255;
else
nilai=0;
g.setColor(new Color(nilai, nilai, nilai));
g.drawLine(x+2*lebar+100+50, y+lebar+100,x+2*lebar+100+50, y+lebar+100);
}
}
}
public void repaint(Graphics g)
{
//kosongkan saja
}
}
coding erosi.html
<html>
<head>
<title>Erosi</title>
</head>
<body>
<applet code=erosi.class width=1000 height=800>
<PARAM NAME = filegambar VALUE="love.png">
<PARAM NAME = latar VALUE=1>
<PARAM NAME = ambang VALUE=127>
<PARAM NAME = iterasi VALUE=5>
</applet>
</body>
</html>
0 komentar:
Posting Komentar