Implementasi dan Perbandingan Metode Wiener Filter dan Adaptive Median Filter Untuk Memperbaiki Kualitas Citra Digital

A-1

LISTING PROGRAM

1. Kode Program MainForm
using
using
using
using

System;
System.Collections.Generic;
System.Drawing;
System.Windows.Forms;

namespace skrips
{
///
/// Description of MainForm.
///
public partial class MainForm : Form

{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows
Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the
InitializeComponent() call.
//
}
void Label1Click(object sender, EventArgs e)
{
}
void TestingToolStripMenuItemClick(object sender, EventArgs
e)
{
Testing b1 = new Testing();

b1.ShowDialog();
}
void ExitToolStripMenuItemClick(object sender, EventArgs e)
{
if (MessageBox.Show("Apakah Anda ingin
keluar?","Keluar", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)

Universitas Sumatera Utara

A-2

Application.Exit();
}
void HelpToolStripMenuItemClick(object sender, EventArgs e)
{
Help form3 = new Help();
form3.ShowDialog();
}


2.

Kode Program Form Testing

using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;

System.Data;
System.Diagnostics;
System.Drawing;
System.Drawing.Imaging;
System.IO;
System.Linq;
System.Runtime.InteropServices;
System.Text;
System.Windows.Forms;

namespace skrips
{
///
/// Description of Testing.
///
///
public partial class Testing : Form
{
public Testing()
{

//
// The InitializeComponent() call is required for Windows
Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the
InitializeComponent() call.
//
}
void OpenClick(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image File(.*bmp)|*.bmp";
if(open.ShowDialog() == DialogResult.OK){
long fileSize=new System.IO.FileInfo(open.FileName).Length;

Universitas Sumatera Utara

A-3


Bitmap bit = new Bitmap(open.FileName);
if (bit.Width == bit.Height)
{
gbrAsli.SizeMode = PictureBoxSizeMode.StretchImage;
gbrAsli.ImageLocation = open.FileName;
textBox11.Text=bit.Width.ToString();
textBox12.Text=bit.Height.ToString();
textBox10.Text=open.SafeFileName.ToString();
ukuran.Text = (fileSize / 1000).ToString();
}
else{
MessageBox.Show("Panjang dan lebar citra harus
sama!", "Gagal", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
void BalikClick(object sender, EventArgs e)
{
this.Dispose();

}
double hitungMSE(Bitmap imageAsli, Bitmap imageHasil)
{
double sum = 0;
double pxAsli, pxHasil, MSE;
for (int i = 0; i < imageAsli.Height; i++)
{
for (int j = 0; j < imageAsli.Width; j++)
{
pxAsli = imageAsli.GetPixel(j, i).R;
pxHasil = imageHasil.GetPixel(j, i).R;
sum += Math.Pow((pxAsli - pxHasil), 2);
}
}
MSE = Math.Floor(sum /(imageAsli.Height*imageAsli.Width));
return MSE;
}
double hitungPSNR(double MSE)
{
double PSNR=Math.Round(Math.Log10(10*Math.Pow(255, 2)/MSE));

return PSNR;
}
void Simpan1Click(object sender, EventArgs e)
{
if(gbrHasil.Image != null)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "image Files| *.bmp";
save.FileName = "*.bmp";

Universitas Sumatera Utara

A-4

if (save.ShowDialog() == DialogResult.OK)
{
gbrHasil.Image.Save(save.FileName, System.Drawin
g.Imaging.ImageFormat.Bmp);
MessageBox.Show("Citra berhasil
disimpan", "save", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
else
{
MessageBox.Show("Citra gagal tersimpan");
}
}
}
void Simpan2Click(object sender, EventArgs e)
{
if(gbrHasil2.Image != null)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "image Files| *.bmp";
save.FileName = "*.bmp";
if (save.ShowDialog() == DialogResult.OK)
{
gbrHasil2.Image.Save(save.FileName, System.Drawing.Ima
ging.ImageFormat.Bmp);
MessageBox.Show ("Citra berhasil disimpan", "save",
MessageBoxButtons.OK, MessageBoxIcon.Information);

}
else
{
MessageBox.Show("Citra gagal tersimpan");
}
}
}
void Button3Click(object sender, EventArgs e)
{
if(gbrHasil3.Image != null)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "image Files| *.bmp";
save.FileName = "*.bmp";
if (save.ShowDialog() == DialogResult.OK)
{
gbrHasil3.Image.Save(save.FileName, System.Drawing.Imagi
ng.ImageFormat.Bmp);
MessageBox.Show("Citra berhasil disimpan",
"save", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
else
{

Universitas Sumatera Utara

A-5

MessageBox.Show("Citra gagal tersimpan");
}
}
}
void WienerClick(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
FFTN a = new FFTN(gbrAsli.Image); // kelas wiener filter
a.FFTExecute();
gbrHasil.Image = a.InverseFFT(); //transformasi balik wiener
filter
gbrHasil.SizeMode = PictureBoxSizeMode.StretchImage;
watch.Stop();
textBox3.Text = Math.Round(Convert.ToDecimal
(watch.ElapsedMilliseconds)/1000,4).ToString();
textBox1.Text=hitungMSE((Bitmap)gbrAsli.Image,(Bitmap)gbrHasil.
Image).ToString();
textBox2.Text=hitungPSNR(Convert.ToDouble(textBox1.Text)).ToStr
ing();
MessageBox.Show("Proses Filtering selesai. Tekan tombol Simpan
untuk menyimpan citra", "Berhasil", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
void Button1Click(object sender, EventArgs e) // tombol AMF
{
Stopwatch watch = new Stopwatch();
watch.Start();
gbrHasil2.Image= AdaptiveMedianFilter((Bitmap)gbrAsli.Image,3);
gbrHasil2.SizeMode = PictureBoxSizeMode.StretchImage;
watch.Stop();
textBox6.Text = Math.Round(Convert.ToDecimal
(watch.ElapsedMilliseconds)/1000,4).ToString();
textBox4.Text=hitungMSE((Bitmap)gbrAsli.Image,(Bitmap)gbrHasil2.
Image).ToString();
textBox5.Text=hitungPSNR(Convert.ToDouble(textBox4.Text)).ToStri
ng();
MessageBox.Show("Proses Filtering selesai. Tekan tombol Simpan
untuk menyimpan
citra", "Berhasil", MessageBoxButtons.OK, MessageBoxIcon.Information)
;
}
void Button2Click(object sender, EventArgs e)// tombol Wiener+AMF
{
Stopwatch watch = new Stopwatch();
watch.Start();
gbrHasil3.Image=AdaptiveMedianFilter((Bitmap)gbrHasil.Image,3);
gbrHasil3.SizeMode = PictureBoxSizeMode.StretchImage;
watch.Stop();

Universitas Sumatera Utara

A-6

textBox7.Text = (Math.Round(Convert.ToDecimal
(watch.ElapsedMilliseconds)/1000,4)+decimal.Parse(textBox3.Text)).ToS
tring();
textBox8.Text=hitungMSE((Bitmap)gbrAsli.Image,(Bitmap)gbrHasil3
.Image).ToString();
textBox9.Text=hitungPSNR(Convert.ToDouble(textBox4.Text)).ToStr
ing();
MessageBox.Show("Proses Filtering selesai. Tekan tombol Simpan
untuk menyimpan
citra", "Berhasil", MessageBoxButtons.OK, MessageBoxIcon.Information)
;
}
Bitmap AdaptiveMedianFilter(Bitmap sourceImage, int size)
{
Bitmap bmp;
Bitmap asli = (Bitmap)sourceImage;
bmp = new Bitmap(asli.Width,asli.Height);
Listr = red(sourceImage, size); // array utk
menampung nilai red
Listb = blue(sourceImage, size); // array utk
menampung nilai blue
Listg = green(sourceImage, size); // array untuk
menampung nilai green
for (int i = 0;i < sourceImage.Width;i++){
for (int j =0;j < sourceImage.Height;j++){
bmp.SetPixel(i, j, Color.FromArgb(r[i][j],g[i][j
],b[i][j])); // menampung nilai pixel rgb
}
}
return bmp;
}
List
{
Bitmap asli
int batas =
List r

red(Image sourceImage, int kernelsize)
= (Bitmap)sourceImage;
kernelsize/2;
= new List();

List outr = new List();
Color d;
for (int i = 0;i < sourceImage.Width;i++){
List nilai = new List();
for (int j = 0;j Zmed
- Zmin
int a2 = r[imedian] - r[r.Count-1]; // Zmed Zmax
if (a1 > 0 && a2 < 0) //Jika memenuhi ke
level B
{
int b1 = asli.GetPixel(i, j).R r[0];//Level B --> Zxy - Zmin
int b2 = asli.GetPixel(i, j).R r[r.Count-1]; // Zxy - Zmax
if (b1 > 0 && b2 < 0)
nilai.Add(asli.GetPixel(i, j).R);//ou
tput Zxy
else
nilai.Add(r[imedian]);//output Zmed
r.Clear();
batas = kernelsize/2;
break;
}
else if (batas < kernelsize/2 + 2)//jika
tidak memenuhi persyaratan, ke level B
{
batas += 1;//menambah batas jendela
filter menjadi 5
r.Clear();
continue;//ulangi level A
}
else {
nilai.Add(asli.GetPixel(i, j).R); //
ditampung nilai hasilnya
r.Clear();
batas = kernelsize/2;
break;
}
}
}
outr.Add(nilai);
}
return outr;

Universitas Sumatera Utara

A-8

}
List
{
Bitmap asli
int batas =
List r

green(Image sourceImage, int kernelsize)
= (Bitmap)sourceImage;
kernelsize/2;
= new List();

List outr = new List();
Color d;
for (int i = 0;i < sourceImage.Width;i++){
List nilai = new List();
for (int j = 0;j 0 && a2 < 0){
int b1 = asli.GetPixel(i, j).G - r[0];
int b2 = asli.GetPixel(i, j).G r[r.Count-1];
if (b1 > 0 && b2 < 0)
nilai.Add(asli.GetPixel(i, j).G);
else
nilai.Add(r[imedian]);
r.Clear();
batas = kernelsize/2;
break;
}
else if (batas < kernelsize/2 + 2){
batas += 1;
r.Clear();
continue;
}
else{

Universitas Sumatera Utara

A-9

nilai.Add(asli.GetPixel(i, j).G);
r.Clear();
batas = kernelsize/2;
break;
}
}
}
outr.Add(nilai);
//nilai.Clear();
}
return outr;
}
List
{
Bitmap asli
int batas =
List r

blue(Image sourceImage, int kernelsize)
= (Bitmap)sourceImage;
kernelsize/2;
= new List();

List outr = new List();
Color d;
for (int i = 0;i < sourceImage.Width;i++){
List nilai = new List();
for (int j = 0;j 0 && a2 < 0){
int b1 = asli.GetPixel(i, j).B - r[0];
int b2 = asli.GetPixel(i, j).B r[r.Count-1];
if (b1 > 0 && b2 < 0)
nilai.Add(asli.GetPixel(i, j).B);
else
nilai.Add(r[imedian]);

Universitas Sumatera Utara

A-10

r.Clear();
batas = kernelsize/2;
break;
}
else if (batas < kernelsize/2 + 2){
batas += 1;
r.Clear();
continue;
}
else{
nilai.Add(asli.GetPixel(i, j).B);
r.Clear();
batas = kernelsize/2;
break;
}
}
}
outr.Add(nilai);
}
return outr;
}
void Button4Click(object sender, EventArgs e) // button reset
{
gbrAsli.Image = null;
gbrHasil.Image = null;
gbrHasil2.Image = null;
gbrHasil3.Image = null;
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
textBox4.Text = null;
textBox5.Text = null;
textBox6.Text = null;
textBox7.Text = null;
textBox8.Text = null;
textBox9.Text = null;
textBox10.Text = null;
textBox11.Text = null;
textBox12.Text = null;
ukuran.Text = null;
}

3. Kode Program Class FFTN (Wiener Filter) :
using System;
using System.Drawing;
using System.Windows.Forms;
namespace skrips
{
///

Universitas Sumatera Utara

A-11

/// Description of FFT.
///
public class FFTN
{
public struct Complex{
public double real;
public double imag;
}
Bitmap gbrhasil;
Complex [][] Rgambar, Ggambar, Bgambar, shiftR, shiftG, shift
B;
int nx,ny;
int [,] R,G,B;

=

=
=
=

public FFTN(Image sourceImage)
{
gbrhasil
new Bitmap(sourceImage.Width,sourceImage.Height);
nx = sourceImage.Width;
ny = sourceImage.Height;
Rgambar = new Complex[sourceImage.Width][];
Ggambar = new Complex[sourceImage.Width][];
Bgambar = new Complex[sourceImage.Width][];
shiftR = new Complex[sourceImage.Width][];
shiftG = new Complex[sourceImage.Width][];
shiftB = new Complex[sourceImage.Width][];
for (int i = 0;i < sourceImage.Width;i++){
Rgambar[i] = new Complex[sourceImage.Height];
Ggambar[i] = new Complex[sourceImage.Height];
Bgambar[i] = new Complex[sourceImage.Height];
shiftR[i] = new Complex[sourceImage.Height];
shiftG[i] = new Complex[sourceImage.Height];
shiftB[i] = new Complex[sourceImage.Height];
for (int j = 0;j < sourceImage.Height;j++){
Rgambar[i][j].real
((Bitmap)sourceImage).GetPixel(i, j).R;
Ggambar[i][j].real
((Bitmap)sourceImage).GetPixel(i, j).G;
Bgambar[i][j].real
((Bitmap)sourceImage).GetPixel(i, j).B;
Rgambar[i][j].imag = 0;
Ggambar[i][j].imag = 0;
Bgambar[i][j].imag = 0;
}
}
}
public void FFTExecute(){
ForwardFFT(Rgambar);
ForwardFFT(Ggambar);
ForwardFFT(Bgambar);
FFTShift();

Universitas Sumatera Utara

A-12

R = Plot(shiftR);
G = Plot(shiftG);
B = Plot(shiftB);
RemoveFFTShift();
Hitung();
}
void ForwardFFT(Complex [][] gambar){
//Transform the Rows
double [] real = new double[gambar.Length];
double [] imag = new double[gambar.Length];
for (int i=0; i 4) | ((b & 0x0f0f0f0f) > 8) | ((b & 0x00ff00ff) > 16) | (b > (32 - m);
if (b > a){
double real4 = real[a];
double imag4 = imag[a];
real[a] = real[b];
imag[a] = imag[b];
real[b] = real4;
imag[b] = imag4;
}
}

}
public void FFTShift()
{
int i, j;
for (i = 0; i