LISTING PROGRAM
Kode Program Menu Utama:
using System;
using System.Collections.Generic; using System.Drawing;
using System.Windows.Forms; using System.Data;
using System.Linq;
using System.ComponentModel; using System.Text;
namespace projek {
public partial class MainForm : Form {
public MainForm() {
Windows Forms designer support. InitializeComponent() call. }
void DdfToolStripMenuItem_Click(object sender, EventArgs e) {
}
void LinkLabel1_LinkClicked(object sender, LinkLabelLinkCli ckedEventArgs e)
{ }
void LinkLabel4_LinkClicked(object sender,LinkLabelLinkClick edEventArgs e)
void ContextMenuStrip1_Opening(object sender,System.Componen tModel.CancelEventArgs e)
{ }
void MenuStrip1_ItemClicked(object sender,ToolStripItemClick edEventArgs e)
{ }
void BANTUANToolStripMenuItem_Click(object sender, EventA rgs e)
{
Form6 frm6 = new Form6(); frm6.ShowDialog(); }
void KELUARToolStripMenuItem_Click(object sender, EventArgs e) {
this.Close(); }
void TENTANGToolStripMenuItem_Click(object sender, EventArgs e) {
Form5 frm5 = new Form5(); frm5.ShowDialog();
}
void DETEKSITEPIToolStripMenuItem_Click(object sender, EventArgs e)
{
Hide();
Form2 frm2 = new Form2(); frm2.ShowDialog();
}
void HOMEToolStripMenuItem_Click(object sender, EventArgs e)
{
MainForm mf = new MainForm(); mf.ShowDialog();
} } }
Kode Program Menu Deteksi Tepi:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Linq; using System.Text;
using System.Collections; using System.IO;
using System.Runtime.InteropServices; using System.Diagnostics;
namespace projek {
public partial class Form2 : Form {
public Form2() {
}
public double hitungMSE(Bitmap gambar_asli, Bitmap gambar_Hasil) {
double sum = 0;
double r1, r2, g1, g2, b1, b2, MSE;
for (int i = 0; i < gambar_asli.Height; i++) {
for (int j = 0; j < gambar_asli.Width; j++) {
g1 = gambar_asli.GetPixel(j, i).G; g2 = gambar_Hasil.GetPixel(j, i).G; sum += Math.Pow(g1 - g2, 2);
b1 = gambar_asli.GetPixel(j, i).B; b2 = gambar_Hasil.GetPixel(j, i).B; sum += Math.Pow(b1 - b2, 2);
} }
MSE = sum / (gambar_asli.Height * gambar_asli.Width); return MSE;
}
public double hitungPSNR(double MSE) {
double PSNR = Math.Log10(Math.Pow(255, 2) / MSE); return PSNR;
}
private ArrayList konvolusi(int xPos, int yPos, Bitmap bitmap) {
// inisialisasi variabel untuk menampung nilai ArrayList neighboursList = new ArrayList();
int xStart, xFinish, yStart, yFinish;
int pixel;
// menentukan posisi awal dan akhir koordinat dalam // ukuran mask 3 x 3
xStart = xPos - 1; xFinish = xPos + 1;
yStart = yPos - 1; yFinish = yPos + 1;
// loop sejumlah 3 x 3 perluasan pixel tetangga for (int y = yStart; y <= yFinish; y++)
{
for (int x = xStart; x <= xFinish; x++) {
anggota tetangga
// bila posisi x dan y tidak valid maka isi list dengan 0
// tidak valid : nilai negatif atau lebih dari batas citra
if (x < 0 || y < 0 || x > (bitmap.Width 1) || y > (bitmap.Height - 1))
{
// menambahkan data ke list dengan nilai 0
// 0D : artinya nilai 0 dengan tipe double (D)
neighboursList.Add(0); }
else {
// menampung nilai pixel pada titik (x,y) pada variabel pixel
pixel = bitmap.GetPixel(x, y).R;
// menambahkan data ke list dengan nilai pixel
neighboursList.Add(pixel); }
} }
// nilai kembalian berupa array list return konvolusi;
}
private int getGradienValue(ArrayList neighboursList, String maskType)
{
// sobel X : mask dari sobel X // sobel Y : mask dari sobel Y int result = 0;
int a;
int[,] sobelX = { {-1,0,1}, {-2,0,2}, {-1,0,1} };
int[,] sobelY = { { 1,2,1 }, { 0,0,0 }, { -1,-2,-1 } }; int[,] Frei_ChenX = { {-1, 0,1}, {-a,0,a}, {-1,0,1} }; int[,] Frei_ChenY= { {1,a,1}, { 0,0,0 }, {-1,-a,-1} }; // count : digunakan untuk menunjukkan index pada list int count = 0;
// kondisi untuk mask type, bila X maka lakukan sobel X // tetapi jika Y maka lakukan sobel Y
if (maskType.Equals("sX")) {
// looping untuk menghitung nilai sobel X pada titik (x,y) for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++) {
// perhitungan sobel X
result += sobelX[x, y] * Convert.ToInt16(neighboursList[count])); // increment count yang digunakan untuk index neighboursList count++;
} } }
else if (maskType.Equals("sY")) {
// looping untuk menghitung nilai sobel Y pada titik (x,y) for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++) {
// perhitungan sobel Y
result+=(sobelY[x, y] * Convert.ToInt16(neighboursList[count])); // increment count yang digunakan untuk index neighboursList count++;
} } }
// looping untuk menghitung nilai sobel Y pada titik (x,y) for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++) {
// perhitungan FREI-CHEN X result
+=(Frei_ChenX[x, y] * Convert.ToInt16(neighboursList[count])) // increment count yang digunakan untuk index neighboursList count++;
} }
}
else if (maskType.Equals("fY")) {
// looping untuk menghitung nilai sobel Y pada titik (x,y) for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++) {
// perhitungan fREI-CHEN Y result =
result + (Frei_ChenY[x, y] * Convert.ToInt16(neighboursList[count ]));
// increment count yang digunakan untuk index neighboursList count++;
} } }
// nilai kembalian hasil sobel X atau sobel Y pada titik (x,y) pada citra
return result; }
private int getMorfoValue(ArrayList neighbourlist, String maskType)
{
int maks = 0; int count=0; int min = 255;
if (maskType.Equals("D")) {
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
result = Convert.ToInt16(neighbourlist[count]); if (maks < result)
{
maks= result;
} count++;
} }
return maks; }
else if (maskType.Equals("E")) {
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
result = Convert.ToInt16(neighbourlist[count]); if (result < min)
{
min = result; }
// increment count yang digunakan untuk index neighboursList count++;
} } }
return min; }
{
void SobelToolStripMenuItem_Click(object sender, EventArgs e) {
Form2 frm2 = new Form2(); frm2.ShowDialog();
}
void Button4_Click(object sender, EventArgs e) {
Bitmap gambar_awal = new Bitmap(pictureBox1.Image); Bitmap gambar = new Bitmap(gambar_awal);
Bitmap bitmap = new Bitmap(gambar); Bitmap hasil = new Bitmap(gambar); Stopwatch sw = new Stopwatch(); int result;
int count = 0; int resultFx; int resultFy;
// inisialisasi array list untuk menampung pixel tetangga
ArrayList neighboursList = new ArrayList(); // mengosongkan list sobel X
sw.Start();
// nested looping untuk scanline citra secara horizontal
for (int y = 0; y < bitmap.Height; y++) {
for (int x = 0; x < bitmap.Width; x++) {
// mengosongkan list neighboursList.Clear();
// menampung list tetangga dengan perluasan 3 x 3
neighboursList = konvlusi(x, y, bitmap); resultFx
= getGradienValue(neighboursList,"fX"); resultFy
result =
Convert.ToInt16(Math.Sqrt(Math.Pow(resultFx, 2) + Math.Pow(result Fy, 2))); // kondisi untuk filter nilai harus dalam range 0 - 255
if (result < 0) {
result = 0; }
else if (result > 255) {
result = 255; }
// set nilai pixel baru setelah dikenakan sobel mask X pada titik (x,y)
hasil.SetPixel(x, y, Color.FromArgb(result, r esult, result));
count++; }
} sw.Stop();
pictureBox3.Image=hasil;
double MSE = hitungMSE(gambar, hasil); textBox6.Text= MSE.ToString();
textBox7.Text=hitungPSNR(MSE).ToString();
textBox8.Text=Math.Round(Convert.ToDecimal(sw.Elapsed Milliseconds)/ 1000, 4).ToString();
}
void Button6_Click(object sender, EventArgs e) {
if (pictureBox2.Image != null) {
SaveFileDialog simpan = new SaveFileDialog(); simpan.Filter = "Image Files|*.bmp|Image Files|*.jpg";
simpan.FileName = "*.jpg";
{
pictureBox2.Image.Save(simpan.FileName, Syste m.Drawing.Imaging.ImageFormat.Jpeg);
string filename =
simpan.FileName.Substring(0, simpan.FileName.Length 4) + ".txt";
FileStream fstream
= new FileStream(filename, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fstream); SeekOrigin seekorigin = new SeekOrigin(); sw.BaseStream.Seek(0, seekorigin);
sw.Flush(); sw.Close();
MessageBox.Show("Citra Hasil Deteksi Tepi Sobel Telah
Disimpan", "Simpan", MessageBoxButtons.OK, MessageBoxIcon.Informa tion);
} } else {
MessageBox.Show("Citra Hasil Deteksi Tepi Belum Ada");
} }
void Button7_Click(object sender, EventArgs e) {
// TODO: Implement Button7_Click if (pictureBox3.Image != null) {
SaveFileDialog simpan = new SaveFileDialog(); simpan.Filter = "Image Files|*.bmp|Image Files|*.jpg";
simpan.FileName = "*.jpg";
pictureBox3.Image.Save(simpan.FileName, Syste m.Drawing.Imaging.ImageFormat.Jpeg);
string filename =
simpan.FileName.Substring(0, simpan.FileName.Length 4) + ".txt";
FileStream fstream
= new FileStream(filename, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fstream); SeekOrigin seekorigin = new SeekOrigin(); sw.BaseStream.Seek(0, seekorigin);
sw.Flush(); sw.Close();
MessageBox.Show("Citra Hasil Deteksi Tepi Sobel Telah
Disimpan", "Simpan", MessageBoxButtons.OK, MessageBoxIcon.Informa tion);
} } else {
MessageBox.Show("Citra Hasil Deteksi Tepi Belum Ada");
} }
void Button5_Click(object sender, EventArgs e) {
Bitmap gambar_awal = new Bitmap(pictureBox1.Image); Bitmap gambar = new Bitmap(gambar_awal);
Bitmap bitmap = new Bitmap(gambar); Bitmap hasil = new Bitmap(gambar); Stopwatch sw = new Stopwatch(); int result;
int count = 0; int resultDilasi; int resultErosi;
tetangga
ArrayList neighboursList = new ArrayList(); // mengosongkan list sobel X
sw.Start();
// nested looping untuk scanline citra secara horizontal
for (int y = 0; y < bitmap.Height; y++) {
for (int x = 0; x < bitmap.Width; x++) {
// mengosongkan list neighboursList.Clear();
// menampung list tetangga dengan perluasan 3 x 3
neighboursList = konvolusi(x, y, bitmap); resultDilasi
= getMorfoValue(neighboursList,"D"); resultErosi
= getMorfoValue(neighboursList, "E"); result = Convert.ToInt32(resultDilasi-resultErosi);
// kondisi untuk filter nilai harus dalam range 0 - 255
if (result < 0) {
result = 0; }
else if (result > 255) {
result = 255; }
// set nilai pixel baru setelah dikenakan sobel mask X pada titik (x,y)
hasil.SetPixel(x, y, Color.FromArgb(result, r esult, result));
} sw.Stop();
pictureBox4.Image=hasil;
double MSE = hitungMSE(gambar, hasil); textBox9.Text= MSE.ToString();
textBox10.Text=hitungPSNR(MSE).ToString();
textBox11.Text=Math.Round(Convert.ToDecimal(sw.Elapse dMilliseconds)/ 1000, 4).ToString();
}
void Button8_Click(object sender, EventArgs e) {
if (pictureBox4.Image != null) {
SaveFileDialog simpan = new SaveFileDialog(); simpan.Filter = "Image Files|*.bmp|Image Files|*.jpg";
simpan.FileName = "*.jpg";
if (simpan.ShowDialog() == DialogResult.OK) {
pictureBox4.Image.Save(simpan.FileName, Syste m.Drawing.Imaging.ImageFormat.Jpeg);
string filename =
simpan.FileName.Substring(0, simpan.FileName.Length 4) + ".txt";
FileStream fstream
= new FileStream(filename, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fstream); SeekOrigin seekorigin = new SeekOrigin(); sw.BaseStream.Seek(0, seekorigin);
sw.Flush(); sw.Close();
MessageBox.Show("Citra Hasil Deteksi Tepi Sobel Telah
Disimpan", "Simpan", MessageBoxButtons.OK, MessageBoxIcon.Informa tion);
else {
MessageBox.Show("Citra Hasil Deteksi Tepi Belum Ada");
} }
void Button3_Click(object sender, EventArgs e) {
Bitmap gambar_awal = new Bitmap(pictureBox1.Image); Bitmap gambar = new Bitmap(gambar_awal);
Bitmap bitmap = new Bitmap(gambar); Bitmap hasil = new Bitmap(gambar); Stopwatch sw = new Stopwatch(); int result;
int count = 0; int resultSx; int resultSy;
// inisialisasi array list untuk menampung pixel tetangga
ArrayList neighboursList = new ArrayList(); // mengosongkan list
sw.Start();
// nested looping untuk scanline citra secara horizontal
for (int y = 0; y < bitmap.Height; y++) {
for (int x = 0; x < bitmap.Width; x++) {
// mengosongkan list neighboursList.Clear();
// menampung list tetangga dengan perluasan 3 x 3
neighboursList = Konvolusi (x, y, bitmap); resultSx
= getGradienValue(neighboursList,"sX"); resultSy
result =
Convert.ToInt16(Math.Sqrt(Math.Pow(resultSx, 2) + Math.Pow(result Sy, 2))); // kondisi untuk filter nilai harus dalam range 0 - 255
if (result < 0) {
result = 0; }
else if (result > 255) {
result = 255; }
// set nilai pixel baru setelah dikenakan sobel mask X pada titik (x,y)
hasil.SetPixel(x, y, Color.FromArgb(result, r esult, result));
count++; }
} sw.Stop();
pictureBox2.Image=hasil;
double MSE = hitungMSE(gambar, hasil); textBox3.Text= MSE.ToString();
textBox4.Text=hitungPSNR(MSE).ToString();
textBox5.Text=Math.Round(Convert.ToDecimal(sw.Elapsed Milliseconds)/ 1000, 4).ToString();
}
void KeluarToolStripMenuItem_Click(object sender, EventAr gs e)
{
Close(); }
void Button1_Click(object sender, EventArgs e) {
{
OpenFileDialog open = new OpenFileDialog(); open.Filter = "Image Files(*.bmp)|*.bmp|Image Files(*.jpg)|*.jpg|All Files(*.*)|*.*";
if (open.ShowDialog() == DialogResult.OK) {
Bitmap gambar
= new Bitmap(open.FileName.ToString());
Bitmap gray= new Bitmap(gambar); int rata2=0;
for(int y =0; y< gambar.Height; y++) {
for (int x=0; x< gambar.Width; x++) {
rata2=(gambar.GetPixel(x, y).R + gambar.GetP ixel(x, y).G + gambar.GetPixel(x, y).B)/3;
gray.SetPixel(x, y, Color.FromArgb(rata2,rata 2,rata2));
} }
pictureBox1.Image= gray;
textBox13.Text = open.FileName.ToString(); textBox2.Text = gambar.Width.ToString(); textBox12.Text = gambar.Height.ToString(); long fileSize
= new System.IO.FileInfo(open.FileName).Length; if (fileSize / 1000 < 1) {
textBox1.Text = fileSize.ToString(); lblSize.Text = "Byte";
} else {
textBox1.Text = (fileSize / 1000).ToString();
} }
catch (Exception) {
throw new ApplicationException("Failed loading image");
} }
void DeteksiTepiToolStripMenuItem_Click(object sender, Ev entArgs e)
{
Hide();
Form2 frm2 = new Form2(); frm2.ShowDialog();
}
void Form2_Load(object sender, EventArgs e) {
}
void Button9_Click(object sender, EventArgs e) {
Bitmap gambar_awal = new Bitmap(pictureBox2.Image); Bitmap gambar_utama= new Bitmap(pictureBox1.Image); Bitmap gambar = new Bitmap(gambar_awal);
Bitmap bitmap = new Bitmap(gambar); Bitmap hasil = new Bitmap(gambar); Stopwatch sw = new Stopwatch(); Bitmap gambar2= new Bitmap(gambar); int result;
int count = 0; int resultDilasi; int resultErosi;
// inisialisasi array list untuk menampung pixel tetangga
sw.Start();
// nested looping untuk scanline citra secara horizontal
for (int y = 0; y < bitmap.Height; y++) {
for (int x = 0; x < bitmap.Width; x++) {
// mengosongkan list neighboursList.Clear();
// menampung list tetangga dengan perluasan 3 x 3
neighboursList = Konvolusi(x, y, bitmap); resultDilasi
= getMorfoValue(neighboursList,"D"); resultErosi = getMorfoValue(neighboursList,"E");
result = Convert.ToInt32(resultDilasi-resultErosi);
// kondisi untuk filter nilai harus dalam range 0 - 255
if (result < 0) {
result = 0; }
else if (result > 255) {
result = 255; }
// set nilai pixel baru setelah dikenakan sobel mask X pada titik (x,y)
hasil.SetPixel(x, y, Color.FromArgb(result, r esult, result));
count++;
} }
sw.Stop(); pictureBox5.Image=hasil;
textBox16.Text= MSE.ToString();
textBox15.Text=hitungPSNR(MSE).ToString();
textBox14.Text=Math.Round(Convert.ToDecimal(sw.Elapse dMilliseconds)/ 1000, 4).ToString();
}
void Button11_Click(object sender, EventArgs e) {
// TODO: Implement Button11_Click
Bitmap gambar_awal = new Bitmap(pictureBox3.Image); Bitmap gambar_utama= new Bitmap(pictureBox1.Image); Bitmap gambar = new Bitmap(gambar_awal);
Bitmap bitmap = new Bitmap(gambar); Bitmap hasil = new Bitmap(gambar); Stopwatch sw = new Stopwatch(); Bitmap gambar2= new Bitmap(gambar); int result;
int count = 0; int resultDilasi; int resultErosi;
// inisialisasi array list untuk menampung pixel tetangga
ArrayList neighboursList = new ArrayList(); // mengosongkan list
sw.Start();
// nested looping untuk scanline citra secara horizontal
for (int y = 0; y < bitmap.Height; y++) {
for (int x = 0; x < bitmap.Width; x++) {
// mengosongkan list neighboursList.Clear();
// menampung list tetangga dengan perluasan 3 x 3
neighboursList = konvolusi(x, y, bitmap); resultDilasi
= getMorfoValue(neighboursList,"E");
result = Convert.ToInt32(resultDilasi-resultErosi);
// kondisi untuk filter nilai harus dalam range 0 - 255
if (result < 0) {
result = 0; }
else if (result > 255) {
result = 255; }
// set nilai pixel baru setelah dikenakan sobel mask X pada titik (x,y)
hasil.SetPixel(x, y, Color.FromArgb(result, r esult, result));
count++; } }
sw.Stop(); pictureBox6.Image=hasil;
double MSE = hitungMSE(gambar_utama,hasil); textBox19.Text= MSE.ToString();
textBox18.Text=hitungPSNR(MSE).ToString();
textBox17.Text=Math.Round(Convert.ToDecimal(sw.Elapse dMilliseconds)/ 1000, 4).ToString();
}
void Button2_Click(object sender, EventArgs e) {
if (pictureBox5.Image != null) {
SaveFileDialog simpan = new SaveFileDialog(); simpan.Filter = "Image Files|*.bmp|Image Files|*.jpg";
simpan.FileName = "*.jpg";
pictureBox3.Image.Save(simpan.FileName, Syste m.Drawing.Imaging.ImageFormat.Jpeg);
string filename =
simpan.FileName.Substring(0, simpan.FileName.Length 4) + ".txt";
FileStream fstream
= new FileStream(filename, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fstream); SeekOrigin seekorigin = new SeekOrigin(); sw.BaseStream.Seek(0, seekorigin);
sw.Flush(); sw.Close();
MessageBox.Show("Citra Hasil Deteksi Tepi Telah
Disimpan", "Simpan", MessageBoxButtons.OK, MessageBoxIcon.Informa tion);
} } else {
MessageBox.Show("Citra Hasil Deteksi Tepi Belum Ada");
} }
void Button10_Click(object sender, EventArgs e) {
if (pictureBox6.Image != null) {
SaveFileDialog simpan = new SaveFileDialog(); simpan.Filter = "Image Files|*.bmp|Image Files|*.jpg";
simpan.FileName = "*.jpg";
if (simpan.ShowDialog() == DialogResult.OK) {
string filename =
simpan.FileName.Substring(0, simpan.FileName.Length 4) + ".txt";
FileStream fstream
= new FileStream(filename, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fstream); SeekOrigin seekorigin = new SeekOrigin(); sw.BaseStream.Seek(0, seekorigin);
sw.Flush(); sw.Close();
MessageBox.Show("Citra Hasil Deteksi Tepi Telah
Disimpan", "Simpan", MessageBoxButtons.OK, MessageBoxIcon.Informa tion);
} } else {
MessageBox.Show("Citra Hasil Deteksi Tepi Belum Ada");
}
}
void HomeToolStripMenuItem_Click(object sender, EventArgs e) {
Hide();
MainForm mf = new MainForm(); mf.ShowDialog();
}
void BantuanToolStripMenuItem_Click(object sender, EventA rgs e)
{
Form6 frm6 = new Form6(); frm6.ShowDialog();
}
void TentangToolStripMenuItem_Click(object sender, EventA rgs e)
Form5 frm5 = new Form5(); frm5.ShowDialog();
} } }
Kode Program Menu Bantuan:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace projek {
public partial class Form6 : Form {
public Form6() {
InitializeComponent(); }
} }
Kode Program Menu Tentang:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace projek {
public partial class Form5 : Form {
public Form5() {
InitializeComponent();