import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.PixelGrabber;
public class Konvolusi extends Applet
{
Image
img;
int
lebar, tinggi;
int
warna, red, green, blue, alpha, abuabu, tempwarna;
String
tampil;
int
m; int n; //ukuran kernel m X n
double
kernelMean[][];
double
kernelHigh[][];
double
kernelLow[][];
//pembentukan
bitmap
int
bitmapA[][];
int
bitmapR[][];
int
bitmapG[][];
int
bitmapB[][];
int
hasilbitmapA[][];
int
hasilbitmapR[][];
int
hasilbitmapG[][];
int
hasilbitmapB[][];
int
hasilHigh[][];
int
hasilLow[][];
public
void init()
{
img=this.getImage(this.getDocumentBase(),
"mickey.jpg");
//tentukan
ukuran kernel
m
= 3;
n
= 3;
//deklarasi
kernel
kernelMean
= new double [m+1][n+1];
kernelHigh
= new double [m+1][n+1];
kernelLow
= new double [m+1][n+1];
img=this.getImage(this.getDocumentBase(),
"mickey.jpg");
lebar
= img.getWidth(this);
tinggi
= img.getHeight(this);
//isi
kernel Mean
for(int
y=1;y<=n;y++)
{
for(int
x=1;x<=m;x++)
{
kernelMean[x][y]
= 1/(double)(m*n);
}
}
//isi
kernel high
kernelHigh[1][1]=-1;
kernelHigh[1][2]=-1; kernelHigh[1][3]=-1;
kernelHigh[2][1]=-1;
kernelHigh[2][2]=8; kernelHigh[2][3]=-1;
kernelHigh[3][1]=-1;
kernelHigh[3][2]=-1; kernelHigh[3][3]=-1;
//isi
kernel low
kernelLow[1][1]=0; kernelLow[1][2]=1/(double)8;
kernelLow[1][3]=0;
kernelLow[2][1]=1/(double)8;
kernelLow[2][2]=1/(double)2; kernelLow[2][3]=1/(double)8;
kernelLow[3][1]=0; kernelLow[3][2]=1/(double)8;
kernelLow[3][3]=0;
}
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;
}
for(int
x=0;x<=lebar;x++)
{
processedBitmap[x][0]
= nilai;
processedBitmap[x][tinggi+1]
= nilai;
}
}
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,10);
tampil
= String.valueOf(tinggi);
g.drawString("tinggi:
"+tampil,10,30);
//tampilkan
citra dengan drawImage
g.drawImage(img,10,40,this);
//tentukan
panjang array pixels
int
[] pixels = new int[lebar*tinggi];
bitmapA
= new int [lebar+2][tinggi+2];
bitmapR
= new int [lebar+2][tinggi+2];
bitmapG
= new int [lebar+2][tinggi+2];
bitmapB
= new int [lebar+2][tinggi+2];
hasilbitmapA
= new int [lebar+1][tinggi+1];
hasilbitmapR
= new int [lebar+1][tinggi+1];
hasilbitmapG
= new int [lebar+1][tinggi+1];
hasilbitmapB
= new int [lebar+1][tinggi+1];
hasilHigh
= new int [lebar+1][tinggi+1];
hasilLow
= new int [lebar+1][tinggi+1];
g.drawString("Hasil
Pengolahan",lebar+100,10);
g.drawString("Abu-abu",lebar+100,30);
g.drawString("Mean
Filtering",lebar+100+lebar+50,30);
g.drawString("High
Pass Filtering 0",lebar+100+2*lebar+2*50,30);
g.drawString("Min
Filtering",lebar+100,tinggi+70);
g.drawString("Max
Filtering",lebar+100+lebar+50,tinggi+70);
g.drawString("Low
Pass Filtering",lebar+100+2*lebar+2*50,tinggi+70);
g.setColor(Color.black);
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;
}
for(int
y=0;y<tinggi-1;y++)
{
for(int
x=0;x<lebar-1;x++)
{
//menguraikan
warna dengan model RGB
warna=pixels[y*lebar+x];
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;
}
}
//mean
filtering
//isi
filter tepi
setNilaiTepi(bitmapA,
128);
//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, kernelMean);
}
}
for(int
y=1;y<=tinggi;y++)
{
for(int
x=1;x<=lebar;x++)
{
hasilHigh[x][y]
= (int) konvolusi(x, y, m, n, bitmapA, kernelHigh);
}
}
for(int
y=1;y<=tinggi;y++)
{
for(int
x=1;x<=lebar;x++)
{
hasilLow[x][y]
= (int) konvolusi(x, y, m, n, bitmapA, kernelLow);
}
}
int
red1,green1, blue1, kTemp;
//min
filtering grayscale
for(int
y=1;y<tinggi+1;y++)
{
for(int
x=1;x<lebar+1;x++)
{
red1=255;
green1=255;
blue1=255;
for(int
k=-1;k<=1;k++)
{
for(int
l=-1;l<=1;l++)
{
if(red1>bitmapR[x+k][y+l])
red1=bitmapR[x+k][y+l];
if(green1>bitmapG[x+k][y+l])
green1=bitmapG[x+k][y+l];
if(blue1>bitmapB[x+k][y+l])
blue1=bitmapB[x+k][y+l];
}
}
kTemp=(int)(red1+green1+blue1)/3;
g.setColor(new
Color(kTemp, kTemp, kTemp));
g.drawLine(x+lebar+100,
y+80+tinggi, x+lebar+100, y+80+tinggi);
}
}
//max
filtering grayscale
for(int
y=1;y<tinggi+1;y++)
{
for(int
x=1;x<lebar+1;x++)
{
red1=0;
green1=0;
blue1=0;
for(int
k=-1;k<=1;k++)
{
for(int
l=-1;l<=1;l++)
{
if(red1<bitmapR[x+k][y+l])
red1=bitmapR[x+k][y+l];
if(green1<bitmapG[x+k][y+l])
green1=bitmapG[x+k][y+l];
if(blue1<bitmapB[x+k][y+l])
blue1=bitmapB[x+k][y+l];
}
}
kTemp=(int)(red1+green1+blue1)/3;
g.setColor(new
Color(kTemp, kTemp, kTemp));
g.drawLine(x+lebar+100+lebar+50,
y+80+tinggi, x+lebar+100+lebar+50, y+80+tinggi);
}
}
//tampil
hasil proses
for(int
y=1;y<=tinggi;y++)
{
for(int
x=1;x<=lebar;x++)
{
//baris
pertama
//abuabu
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);
//mean
filtering
g.setColor(new
Color(hasilbitmapA[x][y], hasilbitmapA[x][y], hasilbitmapA[x][y]));
g.drawLine(x+lebar+100+lebar+50,
y+40, x+lebar+100+lebar+50, y+40);
//high
pass
g.setColor(new
Color(hasilHigh[x][y], hasilHigh[x][y], hasilHigh[x][y]));
g.drawLine(x+lebar+100+lebar+50+lebar+50,
y+40, x+lebar+100+lebar+50+lebar+50, y+40);
//low
pass
g.setColor(new
Color(hasilLow[x][y], hasilLow[x][y], hasilLow[x][y]));
g.drawLine(x+lebar+100+lebar+50+lebar+50,
y+80+tinggi, x+lebar+100+lebar+50+lebar+50, y+80+tinggi);
}
}
}
public
void repaint (Graphics g)
{
//kosongkan
saja
}
}
0 komentar:
Posting Komentar