coding rsp.java :
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.PixelGrabber;
import java.lang.Math;
public class rsp extends Applet
{
Image img;
int lebar,tinggi, parG, parP;
int warna, red, green, blue,alpha, abuabu, tempwarna;
String tampil;
int m; int n;
double kernelRX[][];
double kernelRY[][];
double kernelSX[][];
double kernelSY[][];
double kernelPX[][];
double kernelPY[][];
int bitmapR[][];
int bitmapG[][];
int bitmapB[][];
int bitmapA[][];
int hasilbitmapR[][];
int hasilbitmapG[][];
int hasilbitmapB[][];
int hasilbitmapA[][];
int hasilRobert[][];
int hasilSobel[][];
int hasilPrewitt[][];
public void init()
{
img=this.getImage(this.getDocumentBase(),getParameter("filegambar"));
parG = Integer.parseInt(getParameter("parg"));
parP = Integer.parseInt(getParameter("parp"));
m=3;
n=3;
kernelRX = new double [m+1][n+1];
kernelRY = new double [m+1][n+1];
kernelSX = new double [m+1][n+1];
kernelSY = new double [m+1][n+1];
kernelPX = new double [m+1][n+1];
kernelPY = new double [m+1][n+1];
lebar =img.getWidth(this);
//isi kernelRX
kernelRX[1][1]=1; kernelRX[1][2]=0;
kernelRX[2][1]=0; kernelRX[2][2]=-1;
//isi kernelRY
kernelRY[1][1]=0; kernelRY[1][2]=-1;
kernelRY[2][1]=1; kernelRY[2][2]=0;
//isi kernelSX
kernelSX[1][1]=-1; kernelSX[1][2]=0; kernelSX[1][3]=1;
kernelSX[2][1]=-2; kernelSX[2][2]=0; kernelSX[2][3]=2;
kernelSX[3][1]=-1; kernelSX[3][2]=0; kernelSX[3][3]=1;
//isi kernelSY
kernelSY[1][1]=1; kernelSY[1][2]=2; kernelSY[1][3]=1;
kernelSY[2][1]=0; kernelSY[2][2]=0; kernelSY[2][3]=0;
kernelSY[3][1]=-1; kernelSY[3][2]=-2; kernelSY[3][3]=-1;
//isi kernelPX
kernelPX[1][1]=-1; kernelPX[1][2]=0; kernelPX[1][3]=1;
kernelPX[2][1]=-1; kernelPX[2][2]=0; kernelPX[2][3]=1;
kernelPX[3][1]=-1; kernelPX[3][2]=0; kernelPX[3][3]=1;
//isi kernelPY
kernelPY[1][1]=1; kernelPY[1][2]=1; kernelPY[1][3]=1;
kernelPY[2][1]=0; kernelPY[2][2]=0; kernelPY[2][3]=0;
kernelPY[3][1]=-1; kernelPY[3][2]=-1; kernelPY[3][3]=-1;
}
public double konvolusi(int x, int y, int m, int n, int tempBitmap[][], double kernel[][])
{
double hasil;
hasil=0;
int ht=Math.round(m/2);
int vt=Math.round(n/2);
for(int l=1;l<=m;l++)
{
for(int k=1;k<=m;k++)
{
hasil=hasil+kernel[l][k]*tempBitmap[x+ht-(k-m+ht)][y+vt-(l-n+vt)];
}
}
if(hasil<0) hasil=0;
if(hasil>255) hasil=255;
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;
}
for(int x=0;x<=lebar;x++)
{
processedBitmap[x][0] = nilai;
processedBitmap[x][tinggi+1] = nilai;
}
}
public void paint(Graphics g)
{
//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
bitmapR = new int [lebar+2][tinggi+2];
bitmapG = new int [lebar+2][tinggi+2];
bitmapB = new int [lebar+2] [tinggi+2];
bitmapA = new int [lebar+2][tinggi+2];
hasilbitmapR = new int [lebar+1][tinggi+1];
hasilbitmapG = new int [lebar+1][tinggi+1];
hasilbitmapB = new int [lebar+1][tinggi+1];
hasilbitmapA = new int [lebar+1][tinggi+1];
hasilRobert = new int [lebar+1][tinggi+1];
hasilSobel = new int [lebar+1][tinggi+1];
hasilPrewitt = new int [lebar+1][tinggi+1];
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("abu-abu", 10, tinggi+90);
g.drawString("Robert", lebar+100, tinggi+90);
g.drawString("Sobel", lebar+100+lebar+50, tinggi+90);
g.drawString("Prewitt", lebar+100+2*lebar+2*50, tinggi+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;
bitmapR[x+1][y+1]=red;
bitmapG[x+1][y+1]=green;
bitmapB[x+1][y+1]=blue;
bitmapA[x+1][y+1]=abuabu;
}
}
//deklarasi temp
int tempRX, tempRY, tempSX, tempSY, tempPX, tempPY;
setNilaiTepi(bitmapA, 128);
//operator
for(int y=1;y<=tinggi-1;y++)
{
for(int x=1;x<=lebar-1;x++)
{
//robert
tempRX=(int)konvolusi(x,y,m,n,bitmapA,kernelRX);
tempRY=(int)konvolusi(x,y,m,n,bitmapA,kernelRY);
if(Math.abs(tempRX) > Math.abs(tempRY))
{
hasilRobert[x][y]=Math.abs(tempRX);
} else
{
hasilRobert[x][y]=Math.abs(tempRY);
}
//sobel
tempSX=(int)konvolusi(x,y,m,n,bitmapA,kernelSX);
tempSY=(int)konvolusi(x,y,m,n,bitmapA,kernelSY);
if(Math.abs(tempSX) > Math.abs(tempSY))
{
hasilSobel[x][y]=Math.abs(tempSX);
} else
{
hasilSobel[x][y]=Math.abs(tempSY);
}
//prewitt
tempPX=(int)konvolusi(x,y,m,n,bitmapA,kernelPX);
tempPY=(int)konvolusi(x,y,m,n,bitmapA,kernelPY);
if(Math.abs(tempPX) > Math.abs(tempPY))
{
hasilPrewitt[x][y]=Math.abs(tempPX);
} else
{
hasilPrewitt[x][y]=Math.abs(tempPY);
}
}
}
for (int j=1; j<=tinggi; j++)
{
for (int i=1; i<=lebar; i++)
{
//Abu Abu
g.setColor(new Color(bitmapA[i][j], bitmapA[i][j], bitmapA[i][j]));
g.drawLine(i+10, j+tinggi+100, i+10, j+tinggi+100);
//robert
g.setColor(new Color(hasilRobert[i][j], hasilRobert[i][j], hasilRobert[i][j]));
g.drawLine(i+lebar+100, j+tinggi+100, i+lebar+100, j+tinggi+100);
//sobel
g.setColor(new Color(hasilSobel[i][j], hasilSobel[i][j], hasilSobel[i][j]));
g.drawLine(i+lebar+100+lebar+50, j+tinggi+100, i+lebar+100+lebar+50, j+tinggi+100);
//prewitt
g.setColor(new Color(hasilPrewitt[i][j], hasilPrewitt[i][j], hasilPrewitt[i][j]));
g.drawLine(i+lebar+100+2*lebar+2*50, j+tinggi+100, i+lebar+100+2*lebar+2*50, j+tinggi+100);
}
}
}
public void repaint(Graphics g)
{
//kosongkan saja
}
}
coding rsp.html :
<html>
<head>
<title>Deteksi Tepi</title>
</head>
<body>
<applet code=rsp.class width=1000 height=800>
<PARAM NAME = filegambar VALUE="zayn1.jpg">
<PARAM NAME = parp VALUE=190>
<PARAM NAME = parg VALUE=3>
</applet>
</body>
</html>
Sabtu, 03 Desember 2016
Langganan:
Posting Komentar (Atom)
ada source conya gak kak?
BalasHapustolong bagi ke email dong dvdfernanda@gmail.com Makasih
BalasHapus