coding contur.java :
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.PixelGrabber;
public class contur extends Applet
{
Image img, img2;
int lebar, tinggi, posX, posY, filter, tepi;
int lebar2, tinggi2, posX2, posY2, filter2, tepi2;
int warna, alpha, red, green, blue, abuabu, tempwarna;
int warna2, alpha2, red2, green2, blue2, abuabu2, tempwarna2;
String tampil, tampil2, rantai, rantai2, kon, kon2;
int m, n;
double kernelK[][];
public void init()
{
img=this.getImage(this.getDocumentBase(),getParameter("filegambar"));
lebar=img.getWidth(this);
filter=Integer.parseInt(getParameter("filter"));
tepi=Integer.parseInt(getParameter("tepi"));
img2=this.getImage(this.getDocumentBase(),getParameter("filegambar2"));
lebar2=img2.getWidth(this);
filter2=Integer.parseInt(getParameter("filter"));
tepi2=Integer.parseInt(getParameter("tepi"));
m=3;
n=3;
kernelK=new double [m+1][n+1];
//isi kernel kontur
kernelK[1][1]=-1; kernelK[1][2]=-1; kernelK[1][3]=-1;
kernelK[2][1]=-1; kernelK[2][2]=8; kernelK[2][3]=-1;
kernelK[3][1]=-1; kernelK[3][2]=-1; kernelK[3][3]=-1;
}
public double konvolusi (int x, int y, int m, int n, int tempBitmap[][], double kernel[][])
{
double hasil;
hasil=0;
//rumus konvolusi
for(int i=-1;i<2;i++)
{
for(int j=-1;j<2;j++)
{
hasil = hasil + (tempBitmap[x+i][y+j] * kernel[m-1+i][n-1+j]);
}
}
//tresholding atau ada yang menyebutnya cliping
if(hasil <0) hasil = 0;
if (hasil > 255) hasil = 255;
//return hasil
return hasil;
}
public void setNilaiTepi (int processedBitmap[][], int nilai)
{
//nilai tepi
for(int y=0;y<=tinggi;y++)
{
processedBitmap[0][y] = nilai;
processedBitmap[lebar+1][y] = nilai;
processedBitmap[lebar2+1][y] = nilai;
}
for(int x=0;x<=lebar;x++)
{
processedBitmap[x][0] = nilai;
processedBitmap[x][tinggi+1] = nilai;
processedBitmap[x][tinggi2+1] = nilai;
}
}
public void cari(int nextX, int nextY, int bitmapRantai[][])
{
//cari kode rantai 1
if(bitmapRantai[nextX+1][nextY] == 0)
{
rantai = rantai + "1";
bitmapRantai[nextX+1][nextY] = 1;
cari(nextX+1, nextY, bitmapRantai);
}
//cari kode rantai 2
if(bitmapRantai[nextX+1][nextY-1] == 0)
{
rantai = rantai + "2";
bitmapRantai[nextX+1][nextY-1] =2;
cari(nextX+1, nextY-1, bitmapRantai);
}
//cari kode rantai 3
if(bitmapRantai[nextX][nextY-1] == 0)
{
rantai = rantai + "3";
bitmapRantai[nextX][nextY-1] = 3;
cari(nextX, nextY-1, bitmapRantai);
}
//cari kode rantai 4
if(bitmapRantai[nextX-1][nextY-1] == 0)
{
rantai = rantai + "4";
bitmapRantai[nextX-1][nextY-1] = 4;
cari(nextX-1, nextY, bitmapRantai);
}
//cari kode rantai 5
if(bitmapRantai[nextX-1][nextY] == 0)
{
rantai = rantai + "5";
bitmapRantai[nextX-1][nextY] = 5;
cari(nextX-1, nextY, bitmapRantai);
}
//cari kode rantai 6
if(bitmapRantai[nextX+1][nextY+1] == 0)
{
rantai = rantai + "6";
bitmapRantai[nextX+1][nextY+1] = 6;
cari(nextX+1, nextY+1, bitmapRantai);
}
//cari kode rantai 7
if(bitmapRantai[nextX][nextY+1] == 0)
{
rantai = rantai + "7";
bitmapRantai[nextX][nextY+1] = 7;
cari(nextX, nextY+1, bitmapRantai);
}
//cari kode rantai 8
if(bitmapRantai[nextX+1][nextY+1] == 0)
{
rantai = rantai + "8";
bitmapRantai[nextX+1][nextY+1] = 8;
cari(nextX+1, nextY+1, bitmapRantai);
}
}
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);
lebar2 = img2.getWidth(this);
tinggi2 = img2.getHeight(this);
//tampilkan informasi lebar dan tinggi citra
g.setColor(Color.black);
tampil = String.valueOf(lebar);
g.drawString("lebar: "+tampil,10,10);
tampil = String.valueOf(tinggi);
g.drawString("tinggi: "+tampil,10,30);
tampil2 = String.valueOf(lebar2);
g.drawString("lebar: "+tampil2,10,170);
tampil2 = String.valueOf(tinggi2);
g.drawString("tinggi: "+tampil2,10,190);
//tampilkan citra dengan drawImage
g.drawImage(img,10,40,this);
g.drawImage(img2,10,200,this);
//tentukan panjang array pixels
int [] pixels = new int[lebar*tinggi];
int [] pixels2 = new int[lebar2*tinggi2];
int [][] bitmapA = new int [lebar+2][tinggi+2];
int [][] bitmapR = new int [lebar+2][tinggi+2];
int [][] bitmapG = new int [lebar+2][tinggi+2];
int [][] bitmapB = new int [lebar+2][tinggi+2];
int [][] hasilbitmapA = new int [lebar+1][tinggi+1];
int [][] hasilK = new int [lebar+1][tinggi+1];
int [][] bitmapA2 = new int [lebar2+2][tinggi2+2];
int [][] bitmapR2 = new int [lebar2+2][tinggi2+2];
int [][] bitmapG2 = new int [lebar2+2][tinggi2+2];
int [][] bitmapB2 = new int [lebar2+2][tinggi2+2];
int [][] hasilbitmapA2 = new int [lebar2+1][tinggi2+1];
int [][] hasilK2 = new int [lebar2+1][tinggi2+1];
g.drawString("Hasil Pengolahan",lebar+100,10);
g.drawString("Grayscale",lebar+100,30);
g.drawString("Deteksi Tepi",lebar+100+lebar+50,30);
g.drawString("Kontur",lebar+100+2*lebar+2*50,30);
g.drawString("Grayscale",lebar+100,tinggi+70);
g.drawString("Deteksi Tepi",lebar+100+lebar+50,tinggi+70);
g.drawString("Kontur",lebar+100+2*lebar+2*50,tinggi+70);
g.setColor(Color.black);
PixelGrabber pg = new PixelGrabber(img,0,0,lebar,tinggi, pixels,0,lebar);
PixelGrabber pg2 = new PixelGrabber(img2,0,0,lebar2,tinggi2, pixels2,0,lebar2);
try
{
pg.grabPixels(); //grab piksel citra ke object pg.
pg2.grabPixels();
}
catch (InterruptedException ie)
{
System.out.println("Terjadi kesalahan saat mengambil data pixels");
ie.printStackTrace();
return;
}
for(int y=0;y<tinggi;y++)
{
for(int x=0;x<lebar;x++)
{
//menguraikan warna dengan model RGB
warna=pixels[y*lebar+x];
alpha = (warna>>24) & 0xff;
red=(warna >> 16) & 0xff;
bitmapR[x+1][y+1]=red;
green=(warna >> 8) & 0xff;
bitmapG[x+1][y+1]=green;
blue=(warna) & 0xff;
bitmapB[x+1][y+1]=blue;
abuabu = (red+green+blue)/3;
bitmapA[x+1][y+1]=abuabu;
}
}
for(int y=0;y<tinggi2;y++)
{
for(int x=0;x<lebar2;x++)
{
//menguraikan warna dengan model RGB
warna2=pixels2[y*lebar2+x];
alpha2 = (warna2>>24) & 0xff;
red2=(warna2 >> 16) & 0xff;
bitmapR2[x+1][y+1]=red2;
green2=(warna2 >> 8) & 0xff;
bitmapG2[x+1][y+1]=green2;
blue2=(warna2) & 0xff;
bitmapB2[x+1][y+1]=blue2;
abuabu2 = (red2+green2+blue2)/3;
bitmapA2[x+1][y+1]=abuabu2;
}
}
//isi filter tepi
setNilaiTepi(bitmapA, 255);
setNilaiTepi(bitmapA2, 255);
for(int y=1;y<=tinggi;y++)
{
for(int x=1;x<=lebar;x++)
{
hasilK[x][y] = 0;
}
}
for(int y=1;y<=tinggi2;y++)
{
for(int x=1;x<=lebar2;x++)
{
hasilK2[x][y] = 0;
}
}
//konvolusi
for(int y=1;y<=tinggi;y++)
{
for(int x=1;x<=lebar;x++)
{
hasilbitmapA[x][y] = (int) konvolusi(x, y, m, n, bitmapA, kernelK);
hasilK[x][y]=255-hasilbitmapA[x][y];
}
}
for(int y=1;y<=tinggi2;y++)
{
for(int x=1;x<=lebar2;x++)
{
hasilbitmapA2[x][y] = (int) konvolusi(x, y, m, n, bitmapA2, kernelK);
hasilK2[x][y]=255-hasilbitmapA2[x][y];
}
}
//tampil hasil proses
for(int y=1;y<=tinggi;y++)
{
for(int x=1;x<=lebar;x++)
{
//abu-abu
g.setColor(new Color(bitmapA[x][y],bitmapA[x][y],bitmapA[x][y]));
g.drawLine(x+lebar+100,y+40,x+lebar+100,y+40);
//deteksi tepi
g.setColor(new Color(hasilbitmapA[x][y],hasilbitmapA[x][y],hasilbitmapA[x][y]));
g.drawLine(x+lebar+100+lebar+40,y+40,x+lebar+100+lebar+40,y+40);
//kontur
g.setColor(new Color(hasilK[x][y],hasilK[x][y],hasilK[x][y]));
g.drawLine(x+lebar+100+lebar+40+lebar+40,y+40,x+lebar+100+lebar+40+lebar+40,y+40);
}
}
posX = -1; posY = -1;
for(int y =1; y<lebar;y++)
{
for(int x=1;x<tinggi;x++)
{
if(hasilK[x][y] == 0)
{
if(posX <0)
{
posX = x;
posY = y;
}
}
}
}
g.setColor(Color.blue);
g.drawString("Posisi awal gambar 1 X = "+posY+" Y = "+posX,10,3*tinggi+50);
//kode rantai gambar 1
rantai = "Mulai ";
cari(posX,posY, hasilK);
kon = rantai + " Selesai";
g.drawString("Kode rantai 1 = "+ kon,10,3*tinggi+70);
for(int y=1;y<=tinggi2;y++)
{
for(int x=1;x<=lebar2;x++)
{
//abu-abu
g.setColor(new Color(bitmapA2[x][y],bitmapA2[x][y],bitmapA2[x][y]));
g.drawLine(x+lebar2+100,y+90+tinggi2,x+lebar2+100,y+90+tinggi2);
//deteksi tepi
g.setColor(new Color(hasilbitmapA2[x][y],hasilbitmapA2[x][y],hasilbitmapA2[x][y]));
g.drawLine(x+lebar2+100+lebar2+40,y+90+tinggi2,x+lebar2+100+lebar2+40,y+90+tinggi2);
//kontur
g.setColor(new Color(hasilK2[x][y],hasilK2[x][y],hasilK2[x][y]));
g.drawLine(x+lebar2+100+lebar2+40+lebar2+40,y+90+tinggi2,x+lebar2+100+lebar2+40+lebar2+40,y+90+tinggi2);
}
}
posX2 = -1; posY2 = -1;
for(int y =1; y<lebar2;y++)
{
for(int x=1;x<tinggi2;x++)
{
if(hasilK2[x][y] == 0)
{
if(posX2 <0)
{
posX2 = x;
posY2 = y;
}
}
}
}
g.setColor(Color.blue);
g.drawString("Posisi awal gambar 2 X = "+posY2+" Y = "+posX2,10,3*tinggi2+92);
//kode rantai gambar2
rantai ="";
rantai = "Mulai ";
cari(posX2,posY2,hasilK2);
kon2 = rantai + " Selesai";
g.drawString("Kode rantai 2 = "+ kon2,10,3*tinggi2+114);
if(kon.equals(kon2))
{
g.setColor(new Color(0,0,0));
g.drawString("Kedua gambar mempunyai bentuk sama",10,10+2*tinggi+50+180);
} else
{
g.setColor(new Color(255,0,0));
g.drawString("Kedua gambar mempunyai bentuk berbeda",10,10+2*tinggi+50+180);
}
}
public void repaint (Graphics g)
{
//kosongkan saja
}
}
coding contur.html :
<html>
<head>
<title>Kontur</title>
</head>
<body>
<applet code=contur.class width=1000 height=800>
<PARAM NAME = filegambar VALUE="hex1.jpg">
<PARAM NAME = filegambar2 VALUE="pen1.jpg">
<PARAM NAME = filter VALUE=-1>
<PARAM NAME = tepi VALUE=255>
<PARAM NAME = filter2 VALUE=-1>
<PARAM NAME = tepi2 VALUE=255>
</applet>
</body>
</html>
Sabtu, 03 Desember 2016
Langganan:
Posting Komentar (Atom)
bisa kirim programnya RAR ke jambuu.2ja@gmail.com .. pliss.. thanks
BalasHapus