LISTING PROGRAM
1.
Form Home
using System;using System.Collections.Generic;
using System.Drawing;
using 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 System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
namespace Program_Skripsi {
public partial class FormAnalysis : Form {
save.FileName = "*.jpg";
= hitungPSNR(Convert.ToDouble(textBoxMSE1.Text)).ToString(); textBoxRT1.Text =
Math.Round(Convert.ToDecimal(rt.ElapsedMilliseconds)/1000,4).ToString()+ " s "; }
= hitungPSNR(Convert.ToDouble(textBoxMSE2.Text)).ToString(); textBoxRT2.Text =
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"; }
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();
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 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 {
p
ublic 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;
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 <= filterOffset; filterY++) {
for (int filterX = -filterOffset; filterX <= filterOffset; filterX++) {
calcOffset = byteOffset + (filterX * 4) +
(filterY * sourceData.Stride);
blue += (double)(pixelBuffer[calcOffset]) * filterMatrix[filterY + filterOffset, filterX + filterOffset;
green += (double)(pixelBuffer[calcOffset + 1]) * filterMatrix[filterY + filterOffset, filterX + filterOffset;
red += (double)(pixelBuffer[calcOffset + 2]) * filterMatrix[filterY + filterOffset, filterX + filterOffset]; }
}
blue = factor * blue + bias; green = factor * green + bias; red = factor * red + bias;
if (blue > 255) { blue = 255; } else if (blue < 0) { blue = 0; }
{ green = 0; }
if (red > 255) { red = 255; } else if (red < 0) { red = 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;
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);
return bitmap3x3; }