Implementasi Pada Hasil Citra Digital Dengan Menggunakan Perbandingan Metode Laplacian, laplacian of gaussian, dan Difference of Gaussian

A-1

LISTING PROGRAM

1. Form Home
using
using
using
using

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

namespace Program_Skripsi
{
public partial class MainForm : Form
{
public MainForm()
{

InitializeComponent();
}
void ImplementationToolStripMenuItemClick(object sender, EventArgs e)
{
(new FormImplementation()).ShowDialog();
}
void AnaliToolStripMenuItemClick(object sender, EventArgs e)
{
(new FormAnalysis()).ShowDialog();
}
void HelpToolStripMenuItemClick(object sender, EventArgs e)
{
(new FormHelp()).ShowDialog();
}
void AboutToolStripMenuItemClick(object sender, EventArgs e)
{
(new FormAbout()).ShowDialog();
}
}
}


2. Form Analysis
using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Drawing;
System.Drawing.Imaging;
System.Windows.Forms;
System.IO;

System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Linq;
System.Diagnostics;
System.Text;
System.Runtime.InteropServices;

namespace Program_Skripsi {
public partial class FormAnalysis : Form
{
public FormAnalysis ()
{

Universitas Sumatera Utara

A-2
InitializeComponent();
}
void ButtonSweetClick(object sender, EventArgs e)

{
namaefek.Text="Sweet Effect";
citra.ImageLocation =@"D:\ImageSource\bunga sweet.jpg";
}
void ButtonCoolClick(object sender, EventArgs e)
{
namaefek.Text="Cool Effect";
citra.ImageLocation =@"D:\ImageSource\bunga cool.jpg";
}
void ButtonFreshClick(object sender, EventArgs e)
{
namaefek.Text="Fresh Effect";
citra.ImageLocation =@"D:\ImageSource\bunga fresh.jpg";
}
void ButtonGraceClick(object sender, EventArgs e)
{
namaefek.Text="Grace Effect";
citra.ImageLocation =@"D:\ImageSource\bunga grace.jpg";
}
void ButtonSunnyClick(object sender, EventArgs e)

{
namaefek.Text="Sunny Effect";
citra.ImageLocation =@"D:\ImageSource\bunga sunny.jpg";
}
double hitungMSE(Bitmap imageAsli, Bitmap imageHasil)
{
double sum = 0;
double pxAsli;
double pxHasil;
double 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 = sum / (imageAsli.Height * imageAsli.Width);

return MSE;
}
double hitungPSNR (double MSE)
{
double PSNR = Math.Log10(Math.Pow(255, 2)/MSE);
return PSNR;
}
void SimpanGambar(PictureBox pctr){
if(pctr.Image != null)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Image Files | *.jpg";

Universitas Sumatera Utara

A-3
save.FileName = "*.jpg";
if (save.ShowDialog() == DialogResult.OK)
{
pctr.Image.Save(save.FileName, System.Drawing.Imaging.Image

Format.Jpeg);
MessageBox.Show("Image has
saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
void ButtonLaplacianClick(object sender, EventArgs e)
{
if(citra.Image != null){
Stopwatch rt = new Stopwatch();
Bitmap bit = new Bitmap(citra.Image);
Bitmap citraAsli = bit;
rt.Start();
bit = bit.LaplacianFilter(true);
rt.Stop();
pictureBoxLaplacian.Image=bit;
textBoxMSE1.Text = hitungMSE(citraAsli,bit).ToString();
textBoxPSNR1.Text
= hitungPSNR(Convert.ToDouble(textBoxMSE1.Text)).ToString();
textBoxRT1.Text =

Math.Round(Convert.ToDecimal(rt.ElapsedMilliseconds)/1000,4).ToString()+ " s ";
}
else
MessageBox.Show("Pilih gambar yang akan diproses terlebih
dahulu");
}
void ButtonLoGClick(object sender, EventArgs e)
{
if(citra.Image != null){
Stopwatch rt = new Stopwatch();
Bitmap bit = new Bitmap(citra.Image);
Bitmap citraAsli = bit;
rt.Start();
bit = bit.LaplacianOfGaussianFilter();
rt.Stop();
pictureBoxLoG.Image=bit;
textBoxMSE2.Text = hitungMSE(citraAsli,bit).ToString();
textBoxPSNR2.Text
= hitungPSNR(Convert.ToDouble(textBoxMSE2.Text)).ToString();
textBoxRT2.Text =

Math.Round(Convert.ToDecimal(rt.ElapsedMilliseconds)/1000,4).ToString()+ " s ";
}
else
MessageBox.Show("Pilih gambar yang akan diproses terlebih
dahulu");
}
void ButtonDoGClick(object sender, EventArgs e)
{
if(citra.Image != null){
Stopwatch rt = new Stopwatch();
Bitmap bit = new Bitmap(citra.Image);
Bitmap citraAsli = bit;
rt.Start();
bit = bit.DifferenceOfGaussiansFilter(true, false,128);
rt.Stop();
pictureBoxDoG.Image=bit;

Universitas Sumatera Utara

A-4

textBoxMSE3.Text = hitungMSE(citraAsli,bit).ToString();
textBoxPSNR3.Text
= hitungPSNR(Convert.ToDouble(textBoxMSE3.Text)).ToString();
textBoxRT3.Text =
Math.Round(Convert.ToDecimal(rt.ElapsedMilliseconds)/1000,4).ToString()+ " s ";
}
else
MessageBox.Show("Pilih gambar yang akan diproses terlebih
dahulu");
}
void ButtonLoGSaveClick(object sender, EventArgs e)
{
SimpanGambar(pictureBoxLoG);
}
void ButtonLaplacianSaveClick(object sender, EventArgs e)
{
SimpanGambar(pictureBoxLaplacian);
}
void ButtonDoGSaveClick(object sender, EventArgs e)
{

SimpanGambar(pictureBoxDoG);
}
void PictureBoxLoGClick(object sender, EventArgs e)
{
if(pictureBoxLoG.Image != null)
(new DetailImage(pictureBoxLaplacian,pictureBoxLoG,pictureBoxDo
G)).ShowDialog();
}
void PictureBoxLaplacianClick(object sender, EventArgs e)
{
if(pictureBoxLaplacian.Image != null)
(new DetailImage(pictureBoxLaplacian,pictureBoxLoG,pictureBoxDo
G)).ShowDialog();
}
void PictureBoxDoGClick(object sender, EventArgs e)
{
if(pictureBoxDoG.Image != null)
(new DetailImage(pictureBoxLaplacian,pictureBoxLoG,pictureBoxDo
G)).ShowDialog();
}
}
}

Fungsi untuk Input Citra
void ButtonSweetClick(object sender, EventArgs e)
{
namaefek.Text="Sweet Effect";
citra.ImageLocation =@"D:\ImageSource\bunga sweet.jpg";
}
void ButtonCoolClick(object sender, EventArgs e)
{
namaefek.Text="Cool Effect";
citra.ImageLocation =@"D:\ImageSource\bunga cool.jpg";
}

Universitas Sumatera Utara

A-5
void ButtonFreshClick(object sender, EventArgs e)
{
namaefek.Text="Fresh Effect";
citra.ImageLocation =@"D:\ImageSource\bunga fresh.jpg";
}
void ButtonGraceClick(object sender, EventArgs e)
{
namaefek.Text="Grace Effect";
citra.ImageLocation =@"D:\ImageSource\bunga grace.jpg";
}
void ButtonSunnyClick(object sender, EventArgs e)
{
namaefek.Text="Sunny Effect";
citra.ImageLocation =@"D:\ImageSource\bunga sunny.jpg";
}

Fungsi Menyimpan Hasil Deteksi
void SimpanGambar(PictureBox pctr){
if(pctr.Image != null)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Image Files | *.jpg";
save.FileName = "*.jpg";
if (save.ShowDialog() == DialogResult.OK)
{
pctr.Image.Save(save.FileName, System.Drawing.Imaging.Image
Format.Jpeg);
MessageBox.Show("Image has
saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
void ButtonLoGSaveClick(object sender, EventArgs e)
{
SimpanGambar(pictureBoxLoG);
}
void ButtonLaplacianSaveClick(object sender, EventArgs e)
{
SimpanGambar(pictureBoxLaplacian);
}
void ButtonDoGSaveClick(object sender, EventArgs e)
{
SimpanGambar(pictureBoxDoG);
}

Fungsi Deteksi Tepi Laplacian
void ButtonLaplacianClick(object sender, EventArgs e)
{
if(citra.Image != null){
Stopwatch rt = new Stopwatch();
Bitmap bit = new Bitmap(citra.Image);
Bitmap citraAsli = bit;
rt.Start();
bit = bit.LaplacianFilter(true);
rt.Stop();
pictureBoxLaplacian.Image=bit;

Universitas Sumatera Utara

A-6
textBoxMSE1.Text = hitungMSE(citraAsli,bit).ToString();
textBoxPSNR1.Text
= hitungPSNR(Convert.ToDouble(textBoxMSE1.Text)).ToString();
textBoxRT1.Text =
Math.Round(Convert.ToDecimal(rt.ElapsedMilliseconds)/1000,4).ToString()+ " s ";
}
else
MessageBox.Show("Pilih gambar yang akan diproses terlebih
dahulu");
}

Fungsi Deteksi Tepi LoG
void ButtonLoGClick(object sender, EventArgs e)
{
if(citra.Image != null){
Stopwatch rt = new Stopwatch();
Bitmap bit = new Bitmap(citra.Image);
Bitmap citraAsli = bit;
rt.Start();
bit = bit.LaplacianOfGaussianFilter();
rt.Stop();
pictureBoxLoG.Image=bit;
textBoxMSE2.Text = hitungMSE(citraAsli,bit).ToString();
textBoxPSNR2.Text
= hitungPSNR(Convert.ToDouble(textBoxMSE2.Text)).ToString();
textBoxRT2.Text =
Math.Round(Convert.ToDecimal(rt.ElapsedMilliseconds)/1000,4).ToString()+ " s ";
}
else
MessageBox.Show("Pilih gambar yang akan diproses terlebih
dahulu");
}

Fungsi Deteksi Tepi DoG
void ButtonDoGClick(object sender, EventArgs e)
{
if(citra.Image != null){
Stopwatch rt = new Stopwatch();
Bitmap bit = new Bitmap(citra.Image);
Bitmap citraAsli = bit;
rt.Start();
bit = bit.DifferenceOfGaussiansFilter(true, false,128);
rt.Stop();
pictureBoxDoG.Image=bit;
textBoxMSE3.Text = hitungMSE(citraAsli,bit).ToString();
textBoxPSNR3.Text
= hitungPSNR(Convert.ToDouble(textBoxMSE3.Text)).ToString();
textBoxRT3.Text =
Math.Round(Convert.ToDecimal(rt.ElapsedMilliseconds)/1000,4).ToString()+ " s ";
}
else
MessageBox.Show("Pilih gambar yang akan diproses terlebih
dahulu");
}

Fungsi Menghitung Parameter
double hitungMSE(Bitmap imageAsli, Bitmap imageHasil)
{
double sum = 0;

Universitas Sumatera Utara

A-7
double pxAsli;
double pxHasil;
double 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 = sum / (imageAsli.Height * imageAsli.Width);
return MSE;
}
double hitungPSNR (double MSE)
{
double PSNR = Math.Log10(Math.Pow(255, 2)/MSE);
return PSNR;
}

Class ExtBitmap
namespace Program_Skripsi
{
public static class ExtBitmap
{
private static Bitmap ConvolutionFilter(Bitmap sourceBitmap,
double[,] filterMatrix,
double factor = 1,
int bias = 0,
bool grayscale = false)
{
BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0,
sourceBitmap.Width, sourceBitmap.Height),
ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height];
Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
sourceBitmap.UnlockBits(sourceData);
if (grayscale == true)
{
float rgb = 0;
for (int k = 0; k < pixelBuffer.Length; k += 4)
{
rgb = pixelBuffer[k] * 0.11f;
rgb += pixelBuffer[k + 1] * 0.59f;
rgb += pixelBuffer[k + 2] * 0.3f;
pixelBuffer[k] = (byte)rgb;
pixelBuffer[k + 1] = pixelBuffer[k];
pixelBuffer[k + 2] = pixelBuffer[k];
pixelBuffer[k + 3] = 255;
}
}

Universitas Sumatera Utara

A-8
double blue = 0.0;
double green = 0.0;
double red = 0.0;
int filterWidth = filterMatrix.GetLength(1);
int filterHeight = filterMatrix.GetLength(0);
int filterOffset = (filterWidth-1) / 2;
int calcOffset = 0;
int byteOffset = 0;
for (int offsetY = filterOffset; offsetY <
sourceBitmap.Height - filterOffset; offsetY++)
{
for (int offsetX = filterOffset; offsetX <
sourceBitmap.Width - filterOffset; offsetX++)
{
blue = 0;
green = 0;
red = 0;
byteOffset = offsetY *
sourceData.Stride +
offsetX * 4;
for (int filterY = -filterOffset;
filterY 255)
{ green = 255; }
else if (green < 0)

Universitas Sumatera Utara

A-9
{ green = 0; }
if (red
{ red =
else if
{ red =

> 255)
255; }
(red < 0)
0; }

resultBuffer[byteOffset] = (byte)(blue);
resultBuffer[byteOffset + 1] = (byte)(green);
resultBuffer[byteOffset + 2] = (byte)(red);
resultBuffer[byteOffset + 3] = 255;
}
}
Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
BitmapData resultData = resultBitmap.LockBits(new Rectangle(0, 0,
resultBitmap.Width, resultBitmap.Height),ImageLockMode.WriteOnly,
PixelFormat.Format32bppArgb);
Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length);
resultBitmap.UnlockBits(resultData);
return resultBitmap;
}
private static void SubtractImage(this Bitmap subtractFrom,
Bitmap subtractValue, bool invert = false, int bias = 0)
{
BitmapData sourceData = subtractFrom.LockBits(new Rectangle(0, 0,
subtractFrom.Width, subtractFrom.Height), ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb);
byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height];
Marshal.Copy(sourceData.Scan0, resultBuffer, 0, resultBuffer.Length);
BitmapData subtractData = subtractValue.LockBits(new Rectangle(0, 0,
subtractValue.Width, subtractValue.Height), ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
byte[] subtractBuffer = new byte[subtractData.Stride * subtractData.Height];
Marshal.Copy(subtractData.Scan0, subtractBuffer, 0, subtractBuffer.Length);
subtractValue.UnlockBits(subtractData);
int blue = 0;
int green = 0;
int red = 0;
for(int k = 0; k < resultBuffer.Length &&
k < subtractBuffer.Length; k += 4)
{
if (invert == true)
{
blue = 255 - resultBuffer[k] subtractBuffer[k] + bias;
green = 255 - resultBuffer[k + 1] subtractBuffer[k + 1] + bias;
red = 255 - resultBuffer[k + 2] -

Universitas Sumatera Utara

A-10
subtractBuffer[k + 2] + bias;
}
else
{
blue = resultBuffer[k] subtractBuffer[k] + bias;
green = resultBuffer[k + 1] subtractBuffer[k + 1] + bias;
red = resultBuffer[k + 2] subtractBuffer[k + 2] + bias;
}
blue = (blue < 0 ? 0 : (blue > 255 ? 255 : blue));
green = (green < 0 ? 0 : (green > 255 ? 255 : green));
red = (red < 0 ? 0 : (red > 255 ? 255 : red));
resultBuffer[k] = (byte)blue;
resultBuffer[k + 1] = (byte)green;
resultBuffer[k + 2] = (byte)red;
resultBuffer[k + 3] = 255;
}
Marshal.Copy(resultBuffer, 0, sourceData.Scan0,
resultBuffer.Length);
subtractFrom.UnlockBits(sourceData);
}
public static Bitmap LaplacianFilter(this Bitmap sourceBitmap,
bool grayscale = true)
{
Bitmap resultBitmap = ExtBitmap.ConvolutionFilter(sourceBitmap,
Matrix.Laplacian, 1.0, 0, grayscale);
return resultBitmap;
}
public static Bitmap LaplacianOfGaussianFilter(this Bitmap
sourceBitmap)
{
Bitmap resultBitmap = ExtBitmap.ConvolutionFilter(sourceBitmap,
Matrix.Gaussian, 1.0 / 256.0, 0, true);
resultBitmap = ExtBitmap.ConvolutionFilter(resultBitmap,
Matrix.Laplacian, 1.0, 0, false);
return resultBitmap;
}
public static Bitmap DifferenceOfGaussiansFilter(this Bitmap
sourceBitmap, bool grayscale = false, bool invert = false,
int bias = 0)
{
Bitmap bitmap3x3 = ExtBitmap.ConvolutionFilter(sourceBitmap,
Matrix.Gaussian3x3, 1.0 / 16.0, 0, grayscale);
Bitmap bitmap5x5 = ExtBitmap.ConvolutionFilter(sourceBitmap,
Matrix.Gaussian, 1.0 / 159.0, 0, grayscale);
bitmap3x3.SubtractImage(bitmap5x5, invert, bias);

Universitas Sumatera Utara

A-11
return bitmap3x3;
}
}
}

Universitas Sumatera Utara

B-1

Curriculum Vitae

Nama

: Muhammad Irfan

Tanggal Lahir

: Medan / 8 Oktober 1993

Jenis Kelamin

: Laki-laki

Alamat Sekarang

: Jl. Tangkul II Gg.Al-Paeran No.92 Medan

Alamat Orang Tua

: Jl. Tangkul II Gg.Al-Paeran No.92 Medan

Hp

: 0821-6667-7811

Email

: irfanitunamaku@gmail.com

Riwayat Pendidikan
2011 – 2015

: S1 Ilmu Komputer Universitas Sumatera Utara

2008 – 2011

: SMK Swasta Teladan Medan

2005 – 2008

: SMP Negeri 27 Medan

1999 – 2005

: SD Ummi Fatimah Medan

1998 – 1999

: TK Nurul Muslimin Medan

Universitas Sumatera Utara