0% found this document useful (0 votes)
8 views19 pages

Appendix

This document contains the code for a C# program that implements various image filtering techniques on bitmap images. It includes functions for mean, median, and max filtering. The mean filter calculates the average red, green, and blue values of pixels in a 3x3 neighborhood. The median filter sorts pixel values in a 3x3 area and uses the median. The max filter selects the maximum red, green, and blue values from the 3x3 neighborhood. The program loads an image, applies a filter, and can save the output. It provides a GUI for users to select options and view the original and filtered images.

Uploaded by

Fitriyani Umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Appendix

This document contains the code for a C# program that implements various image filtering techniques on bitmap images. It includes functions for mean, median, and max filtering. The mean filter calculates the average red, green, and blue values of pixels in a 3x3 neighborhood. The median filter sorts pixel values in a 3x3 area and uses the median. The max filter selects the maximum red, green, and blue values from the 3x3 neighborhood. The program loads an image, applies a filter, and can save the output. It provides a GUI for users to select options and view the original and filtered images.

Uploaded by

Fitriyani Umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

LISTING PROGRAM

1. Form Menu Utama

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Order_Statistic_Filters
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void keluarToolStripMenuItem_Click(object sender, EventArgs e)


{
Application.Exit();
}

private void bukaGambarToolStripMenuItem_Click(object sender, EventArgs e)


{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = " ";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog.ShowDialog())
{
Bitmap aa = new Bitmap(openFileDialog.FileName);
citraAsli.Image = aa;

textBox1.Text = " " + openFileDialog.FileName;


button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;

textBox2.Text = aa.Width.ToString() + " pixel";


textBox3.Text = aa.Height.ToString() + " pixel";

Universitas Sumatera Utara


}

private void simpanGambarToolStripMenuItem_Click(object sender, EventArgs e)


{
Bitmap citra = new Bitmap((Bitmap)this.citraHasil.Image);
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = " ";
saveFileDialog.Filter = "Bitmap Files (*.bmp)|*.bmp | Jpeg files
(*.jpg) | *.jpg";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;

if (DialogResult.OK == saveFileDialog.ShowDialog())
{
citra.Save(saveFileDialog.FileName);
MessageBox.Show("File Hasil Filter disimpan.");
}

if (citraHasil==null)
{
simpanGambarToolStripMenuItem.Enabled = false;

}
}

public class OrderStatisticFilters


{
public static Bitmap Mean(Bitmap citra) // [ Proses Mean Filter ]
{

int x = citra.Width, y = citra.Height;


Bitmap hasil = new Bitmap(x, y);
int merah, hijau, biru;

for (int j = 1; j < y - 1; ++j)


{
for (int i = 1; i < x - 1; ++i)
{

biru = (citra.GetPixel(i - 1, j - 1).B + citra.GetPixel(i, j - 1).B


+ citra.GetPixel(i + 1, j - 1).B + citra.GetPixel(i - 1, j).B
+ citra.GetPixel(i, j).B + citra.GetPixel(i + 1, j).B +
citra.GetPixel(i - 1, j - 1).B + citra.GetPixel(i, j + 1).B +
citra.GetPixel(i + 1, j + 1).B) / 9;

hijau = (citra.GetPixel(i - 1, j - 1).G + citra.GetPixel(i, j - 1).G


+ citra.GetPixel(i + 1, j - 1).G + citra.GetPixel(i - 1, j).G +
citra.GetPixel(i, j).G + citra.GetPixel(i + 1, j).G +
citra.GetPixel(i - 1, j - 1).G + citra.GetPixel(i, j + 1).G +
citra.GetPixel(i + 1, j + 1).G) / 9;

merah = (citra.GetPixel(i - 1, j - 1).R + citra.GetPixel(i, j - 1).R


+ citra.GetPixel(i + 1, j - 1).R + citra.GetPixel(i - 1, j).R +
citra.GetPixel(i, j).R + citra.GetPixel(i + 1, j).R + citra.GetPixel(i
- 1, j - 1).R + citra.GetPixel(i, j + 1).R + citra.GetPixel(i + 1, j +
1).R) / 9;

hasil.SetPixel(i, j, Color.FromArgb(255, merah, hijau, biru));


}

Universitas Sumatera Utara


}
return hasil;
}
}

private void button2_Click(object sender, EventArgs e)


{
Bitmap citra = new Bitmap((Bitmap)this.citraAsli.Image);
citraHasil.Image = OrderStatisticFilters.Mean(citra);
MessageBox.Show("Prosess Mean Filter Selesai");
simpanGambarToolStripMenuItem.Enabled = true;

// ---------------PROSES MEDIAN FILTER--------------------//


public Bitmap Median()
{
int[,] temp = new int[9, 3];
Bitmap citra = new Bitmap(citraAsli.Image);
int x = citra.Width, y = citra.Height;
Bitmap hasil = new Bitmap(x, y);

int[] sem = new int[3];

for (int j = 1; j < (y - 1); j++)


{
for (int i = 1; i < (x - 1); i++)
{
temp[0, 0] = citra.GetPixel(i - 1, j - 1).B;
temp[0, 1] = citra.GetPixel(i - 1, j - 1).G;
temp[0, 2] = citra.GetPixel(i - 1, j - 1).R;

temp[1, 0] = citra.GetPixel(i, j - 1).B;


temp[1, 1] = citra.GetPixel(i, j - 1).G;
temp[1, 2] = citra.GetPixel(i, j - 1).R;

temp[2, 0] = citra.GetPixel(i + 1, j - 1).B;


temp[2, 1] = citra.GetPixel(i + 1, j - 1).G;
temp[2, 2] = citra.GetPixel(i + 1, j - 1).R;

temp[3, 0] = citra.GetPixel(i - 1, j).B;


temp[3, 1] = citra.GetPixel(i - 1, j).G;
temp[3, 2] = citra.GetPixel(i - 1, j).R;

temp[4, 0] = citra.GetPixel(i, j).B;


temp[4, 1] = citra.GetPixel(i, j).G;
temp[4, 2] = citra.GetPixel(i, j).R;

temp[5, 0] = citra.GetPixel(i + 1, j).B;


temp[5, 1] = citra.GetPixel(i + 1, j).G;
temp[5, 2] = citra.GetPixel(i + 1, j).R;

temp[6, 0] = citra.GetPixel(i - 1, j + 1).B;


temp[6, 1] = citra.GetPixel(i - 1, j + 1).G;
temp[6, 2] = citra.GetPixel(i - 1, j + 1).R;

temp[7, 0] = citra.GetPixel(i, j + 1).B;


temp[7, 1] = citra.GetPixel(i, j + 1).G;
temp[7, 2] = citra.GetPixel(i, j + 1).R;

Universitas Sumatera Utara


temp[8, 0] = citra.GetPixel(i + 1, j + 1).B;
temp[8, 1] = citra.GetPixel(i + 1, j + 1).G;
temp[8, 2] = citra.GetPixel(i + 1, j + 1).R;

for (int a = 0; a < 9; a++)


{
for (int b = a + 1; b < 9; b++)
{
if (Convert.ToInt32
((temp[a, 0] + temp[a, 1] + temp[a, 2]) / 3) >
Convert.ToInt32((temp[b, 0] + temp[b, 1] + temp[b, 2]) / 3))
{

sem[0] = temp[a, 0];


sem[1] = temp[a, 1];
sem[2] = temp[a, 2];

temp[a, 0] = temp[b, 0];


temp[a, 1] = temp[b, 1];
temp[a, 2] = temp[b, 2];

temp[b, 0] = sem[0];
temp[b, 1] = sem[1];
temp[b, 2] = sem[2];

}
}
}
hasil.SetPixel(i, j, Color.FromArgb
(255, temp[4, 2], temp[4, 1], temp[4, 0]));
}
}
return hasil;
}

private void button1_Click(object sender, EventArgs e)


{
citraHasil.Image = Median();
MessageBox.Show("Prosess Median Filter Selesai");
simpanGambarToolStripMenuItem.Enabled = true;

// ---------------PROSES MAX FILTER--------------------//


public Bitmap Max()
{
int[,] temp = new int[9, 3];
Bitmap citra = new Bitmap(citraAsli.Image);
int x = citra.Width, y = citra.Height;

Bitmap hasil = new Bitmap(x, y);

int[] sem = new int[3];

for (int j = 1; j < (y - 1); j++)


{
for (int i = 1; i < (x - 1); i++)
{
temp[0, 0] = citra.GetPixel(i - 1, j - 1).B;
temp[0, 1] = citra.GetPixel(i - 1, j - 1).G;

Universitas Sumatera Utara


temp[0, 2] = citra.GetPixel(i - 1, j - 1).R;

temp[1, 0] = citra.GetPixel(i, j - 1).B;


temp[1, 1] = citra.GetPixel(i, j - 1).G;
temp[1, 2] = citra.GetPixel(i, j - 1).R;

temp[2, 0] = citra.GetPixel(i + 1, j - 1).B;


temp[2, 1] = citra.GetPixel(i + 1, j - 1).G;
temp[2, 2] = citra.GetPixel(i + 1, j - 1).R;

temp[3, 0] = citra.GetPixel(i - 1, j).B;


temp[3, 1] = citra.GetPixel(i - 1, j).G;
temp[3, 2] = citra.GetPixel(i - 1, j).R;

temp[4, 0] = citra.GetPixel(i, j).B;


temp[4, 1] = citra.GetPixel(i, j).G;
temp[4, 2] = citra.GetPixel(i, j).R;

temp[5, 0] = citra.GetPixel(i + 1, j).B;


temp[5, 1] = citra.GetPixel(i + 1, j).G;
temp[5, 2] = citra.GetPixel(i + 1, j).R;

temp[6, 0] = citra.GetPixel(i - 1, j + 1).B;


temp[6, 1] = citra.GetPixel(i - 1, j + 1).G;
temp[6, 2] = citra.GetPixel(i - 1, j + 1).R;

temp[7, 0] = citra.GetPixel(i, j + 1).B;


temp[7, 1] = citra.GetPixel(i, j + 1).G;
temp[7, 2] = citra.GetPixel(i, j + 1).R;

temp[8, 0] = citra.GetPixel(i + 1, j + 1).B;


temp[8, 1] = citra.GetPixel(i + 1, j + 1).G;
temp[8, 2] = citra.GetPixel(i + 1, j + 1).R;

for (int a = 0; a < 9; a++)


{
for (int b = a + 1; b < 9; b++)
{
if (Convert.ToInt32((temp[a, 0] + temp[a, 1] + temp[a, 2]) / 3) >
Convert.ToInt32((temp[b, 0] + temp[b, 1] + temp[b, 2]) / 3))
{

sem[0] = temp[a, 0];


sem[1] = temp[a, 1];
sem[2] = temp[a, 2];

temp[a, 0] = temp[b, 0];


temp[a, 1] = temp[b, 1];
temp[a, 2] = temp[b, 2];

temp[b, 0] = sem[0];
temp[b, 1] = sem[1];
temp[b, 2] = sem[2];
}
}
}

hasil.SetPixel(i, j, Color.FromArgb
(255, temp[8, 2], temp[8, 1], temp[8, 0]));

Universitas Sumatera Utara


}
}
return hasil;
//mengembalikan nilai ke tipe data bitmap
}
private void button3_Click(object sender, EventArgs e)
{
citraHasil.Image = Max();
MessageBox.Show("Prosess Max Filter Selesai");
simpanGambarToolStripMenuItem.Enabled = true;

// ---------------PROSES MIN FILTER--------------------//


public Bitmap Min()
{

int[,] temp = new int[9, 3];


Bitmap citra = new Bitmap(citraAsli.Image);
int x = citra.Width, y = citra.Height;
Bitmap hasil = new Bitmap(x, y);

int[] sem = new int[3];

for (int j = 1; j < (y - 1); j++)


{
for (int i = 1; i < (x - 1); i++)
{
temp[0, 0] = citra.GetPixel(i - 1, j - 1).B;
temp[0, 1] = citra.GetPixel(i - 1, j - 1).G;
temp[0, 2] = citra.GetPixel(i - 1, j - 1).R;

temp[1, 0] = citra.GetPixel(i, j - 1).B;


temp[1, 1] = citra.GetPixel(i, j - 1).G;
temp[1, 2] = citra.GetPixel(i, j - 1).R;

temp[2, 0] = citra.GetPixel(i + 1, j - 1).B;


temp[2, 1] = citra.GetPixel(i + 1, j - 1).G;
temp[2, 2] = citra.GetPixel(i + 1, j - 1).R;

temp[3, 0] = citra.GetPixel(i - 1, j).B;


temp[3, 1] = citra.GetPixel(i - 1, j).G;
temp[3, 2] = citra.GetPixel(i - 1, j).R;

temp[4, 0] = citra.GetPixel(i, j).B;


temp[4, 1] = citra.GetPixel(i, j).G;
temp[4, 2] = citra.GetPixel(i, j).R;

temp[5, 0] = citra.GetPixel(i + 1, j).B;


temp[5, 1] = citra.GetPixel(i + 1, j).G;
temp[5, 2] = citra.GetPixel(i + 1, j).R;

temp[6, 0] = citra.GetPixel(i - 1, j + 1).B;


temp[6, 1] = citra.GetPixel(i - 1, j + 1).G;
temp[6, 2] = citra.GetPixel(i - 1, j + 1).R;

temp[7, 0] = citra.GetPixel(i, j + 1).B;


temp[7, 1] = citra.GetPixel(i, j + 1).G;
temp[7, 2] = citra.GetPixel(i, j + 1).R;

Universitas Sumatera Utara


temp[8, 0] = citra.GetPixel(i + 1, j + 1).B;
temp[8, 1] = citra.GetPixel(i + 1, j + 1).G;
temp[8, 2] = citra.GetPixel(i + 1, j + 1).R;

for (int a = 0; a < 9; a++)


{
for (int b = a + 1; b < 9; b++)
{
if (Convert.ToInt32((temp[a, 0] + temp[a, 1] + temp[a, 2]) / 3) >
Convert.ToInt32((temp[b, 0] + temp[b, 1] + temp[b, 2]) / 3))
{

sem[0] = temp[a, 0];


sem[1] = temp[a, 1];
sem[2] = temp[a, 2];

temp[a, 0] = temp[b, 0];


temp[a, 1] = temp[b, 1];
temp[a, 2] = temp[b, 2];

temp[b, 0] = sem[0];
temp[b, 1] = sem[1];
temp[b, 2] = sem[2];
}
}
}
hasil.SetPixel(i, j, Color.FromArgb
(255, temp[0, 2], temp[0, 1], temp[0, 0]));
}
}
return hasil;
}

private void button4_Click(object sender, EventArgs e)


{
citraHasil.Image = Min();
MessageBox.Show("Prosess Min Filter Selesai");
simpanGambarToolStripMenuItem.Enabled = true;

// ---------------PROSES MIDPOINT FILTER--------------------//


public Bitmap Midpoint()
{
int[,] temp = new int[9, 3];
Bitmap citra = new Bitmap(citraAsli.Image);
int x = citra.Width, y = citra.Height;

Bitmap hasil = new Bitmap(x, y);


int[] sem = new int[3];

for (int j = 1; j < (y - 1); j++)


{
for (int i = 1; i < (x - 1); i++)
{
temp[0, 0] = citra.GetPixel(i - 1, j - 1).B;
temp[0, 1] = citra.GetPixel(i - 1, j - 1).G;
temp[0, 2] = citra.GetPixel(i - 1, j - 1).R;

Universitas Sumatera Utara


temp[1, 0] = citra.GetPixel(i, j - 1).B;
temp[1, 1] = citra.GetPixel(i, j - 1).G;
temp[1, 2] = citra.GetPixel(i, j - 1).R;

temp[2, 0] = citra.GetPixel(i + 1, j - 1).B;


temp[2, 1] = citra.GetPixel(i + 1, j - 1).G;
temp[2, 2] = citra.GetPixel(i + 1, j - 1).R;

temp[3, 0] = citra.GetPixel(i - 1, j).B;


temp[3, 1] = citra.GetPixel(i - 1, j).G;
temp[3, 2] = citra.GetPixel(i - 1, j).R;

temp[4, 0] = citra.GetPixel(i, j).B;


temp[4, 1] = citra.GetPixel(i, j).G;
temp[4, 2] = citra.GetPixel(i, j).R;

temp[5, 0] = citra.GetPixel(i + 1, j).B;


temp[5, 1] = citra.GetPixel(i + 1, j).G;
temp[5, 2] = citra.GetPixel(i + 1, j).R;

temp[6, 0] = citra.GetPixel(i - 1, j + 1).B;


temp[6, 1] = citra.GetPixel(i - 1, j + 1).G;
temp[6, 2] = citra.GetPixel(i - 1, j + 1).R;

temp[7, 0] = citra.GetPixel(i, j + 1).B;


temp[7, 1] = citra.GetPixel(i, j + 1).G;
temp[7, 2] = citra.GetPixel(i, j + 1).R;

temp[8, 0] = citra.GetPixel(i + 1, j + 1).B;


temp[8, 1] = citra.GetPixel(i + 1, j + 1).G;
temp[8, 2] = citra.GetPixel(i + 1, j + 1).R;

for (int a = 0; a < 9; a++)


{
for (int b = a + 1; b < 9; b++)
{
if (Convert.ToInt32
((temp[a, 0] + temp[a, 1] + temp[a, 2]) / 3) >
Convert.ToInt32((temp[b, 0] + temp[b, 1] + temp[b, 2]) / 3))
{

sem[0] = temp[a, 0];


sem[1] = temp[a, 1];
sem[2] = temp[a, 2];

temp[a, 0] = temp[b, 0];


temp[a, 1] = temp[b, 1];
temp[a, 2] = temp[b, 2];

temp[b, 0] = sem[0];
temp[b, 1] = sem[1];
temp[b, 2] = sem[2];

}
}
}

hasil.SetPixel(i, j, Color.FromArgb
(255, (byte)((temp[8, 2] + temp[0, 2]) / 2),
(byte)((temp[8, 1] + temp[0, 1]) / 2),

Universitas Sumatera Utara


(byte)((temp[8, 0] + temp[0, 0]) / 2)));

}
}
return hasil;
}

private void button5_Click(object sender, EventArgs e)


{
citraHasil.Image = Midpoint();
MessageBox.Show("Prosess Midpoint Filter Selesai");
impanGambarToolStripMenuItem.Enabled = true;

private void button7_Click(object sender, EventArgs e)


{
citraHasil.Image = null;
citraAsli.Image = citraAsli.Image;
simpanGambarToolStripMenuItem.Enabled = false;
}
private void programerToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 aa = new Form3();
aa.ShowDialog();
}
private void caraMenggunakanProgramToolStripMenuItem_Click(object sender,
EventArgs e)
{
Form6 aa = new Form6();
aa.ShowDialog();
}
private void button9_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void saltAndPepperToolStripMenuItem_Click(object sender, EventArgs e)
{
Form7 aa = new Form7();
aa.ShowDialog();
}
private void gaussianToolStripMenuItem_Click(object sender, EventArgs e)
{
Form8 aa = new Form8();
aa.ShowDialog();
}
Private void hitungMSEDanPSNRToolStripMenuItem_Click(object sender,EventArgs e)
{
Form9 aa = new Form9();
aa.ShowDialog();
}

2. Form Hitung Nilai MSE dan PSNR

using System;
using System.Collections;
using System.Collections.Generic;

Universitas Sumatera Utara


using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Order_Statistic_Filters
{
public partial class Form9 : Form
{

public Form9()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = " ";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog.ShowDialog())
{
Bitmap aa = new Bitmap(openFileDialog.FileName);
citraAsli.Image = aa;

button2.Enabled = true;

}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = " ";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog.ShowDialog())
{
Bitmap aa = new Bitmap(openFileDialog.FileName);
HasilMedian.Image = aa;

button3.Enabled = true;
}
}

private void button3_Click(object sender, EventArgs e)


{
// Menghitung MSE & PSNR hasil Median Filter
try
{
Bitmap data_citraAsli = new Bitmap(citraAsli.Image);
Bitmap data_citraHasil = new Bitmap(HasilMedian.Image);
double nilai_mse = 0;
int a1, b1, c1, a2, b2, c2;

Universitas Sumatera Utara


int temp1, temp2, temp3, temp4;
if (data_citraAsli.Width == data_citraHasil.Width & data_citraAsli.Height ==
data_citraHasil.Height)
{
for (int i = 0; i < data_citraAsli.Width; i++)
{
for (int j = 0; j < data_citraAsli.Height; j++)
{
a1 = data_citraAsli.GetPixel(i, j).R;
b1 = data_citraAsli.GetPixel(i, j).G;
c1 = data_citraAsli.GetPixel(i, j).B;

a2 = data_citraHasil.GetPixel(i, j).R;
b2 = data_citraHasil.GetPixel(i, j).G;
c2 = data_citraHasil.GetPixel(i, j).B;

temp1 = a1 - a2;
temp2 = b1 - b2;
temp3 = c1 - c2;

temp1 = (temp1 * temp1);


temp2 = (temp2 * temp2);
temp3 = (temp3 * temp3);

if (temp1 < 0)
temp1 = temp1 * (-1);

if (temp2 < 0)
temp2 = temp2 * (-1);

if (temp3 < 0)
temp3 = temp3 * (-1);

temp4 = (temp1 + temp2 + temp3) / 3;

nilai_mse += temp4;
}
}
nilai_mse = nilai_mse / (data_citraAsli.Width * data_citraAsli.Height);

textBox1.Text = nilai_mse.ToString();
}

else
{
MessageBox.Show("Resolusi citra yang di input harus sama !");
}

{
{
double result, cross;
cross = (10 * 255) / Math.Sqrt(nilai_mse);
result = Math.Log(cross, 20);

textBox2.Text = result.ToString();
}
}
}
catch { }

try

Universitas Sumatera Utara


{

// Menghitung MSE & PSNR hasil Mean Filter

Bitmap data_citraAsli1 = new Bitmap(citraAsli.Image);


Bitmap data_mean = new Bitmap(HasilMean.Image);
double nilai_mseMean = 0;
int a, b, c, aa, bb, cc;
int tempa, tempb, tempc, tempd;
if (data_citraAsli1.Width == data_mean.Width & data_citraAsli1.Height ==
data_mean.Height)
{
for (int i = 0; i < data_citraAsli1.Width; i++)
{
for (int j = 0; j < data_citraAsli1.Height; j++)
{
a = data_citraAsli1.GetPixel(i, j).R;
b = data_citraAsli1.GetPixel(i, j).G;
c = data_citraAsli1.GetPixel(i, j).B;

aa = data_mean.GetPixel(i, j).R;
bb = data_mean.GetPixel(i, j).G;
cc = data_mean.GetPixel(i, j).B;

tempa = a - aa;
tempb = b - bb;
tempc = c - cc;

tempa = (tempa * tempa);


tempb = (tempb * tempb);
tempc = (tempc * tempc);

if (tempa < 0)
tempa = tempa * (-1);

if (tempb < 0)
tempb = tempb * (-1);

if (tempc < 0)
tempc = tempc * (-1);

tempd = (tempa + tempb + tempc) / 3;


nilai_mseMean += tempd;
}
}
nilai_mseMean = nilai_mseMean / (data_citraAsli1.Width *
data_citraAsli1.Height);

textBox3.Text = nilai_mseMean.ToString();
}
else
{
MessageBox.Show("Resolusi citra yang di input harus sama !");
}
{
{
double hasil, kali;
kali = (10 * 255) / Math.Sqrt(nilai_mseMean);
hasil = Math.Log(kali, 20);
textBox4.Text = hasil.ToString();

Universitas Sumatera Utara


}

}
}
catch{}

// Menghitung MSE & PSNR hasil Max Filter


try
{
Bitmap data_citraAsli2 = new Bitmap(citraAsli.Image);
Bitmap data_max = new Bitmap(HasilMax.Image);
double nilai_mseMax = 0;
int ei, f, g, ee, ff, gg;
int tempe, tempf, tempg, temph;
if (data_citraAsli2.Width == data_max.Width & data_citraAsli2.Height ==
data_max.Height)
{
for (int i = 0; i < data_citraAsli2.Width; i++)
{
for (int j = 0; j < data_citraAsli2.Height; j++)
{
ei = data_citraAsli2.GetPixel(i, j).R;
f = data_citraAsli2.GetPixel(i, j).G;
g = data_citraAsli2.GetPixel(i, j).B;

ee = data_max.GetPixel(i, j).R;
ff = data_max.GetPixel(i, j).G;
gg = data_max.GetPixel(i, j).B;

tempe = ei - ee;
tempf = f - ff;
tempg = g - gg;

tempe = (tempe * tempe);


tempf = (tempf * tempf);
tempg = (tempg * tempg);

if (tempe < 0)
tempe = tempe * (-1);

if (tempf < 0)
tempf = tempf * (-1);

if (tempg < 0)
tempg = tempg * (-1);

temph = (tempe + tempf + tempg) / 3;


nilai_mseMax += temph;
}

nilai_mseMax = nilai_mseMax / (data_citraAsli2.Width * data_citraAsli2.Height);

textBox5.Text = nilai_mseMax.ToString();
}

else
{
MessageBox.Show("Resolusi citra yang di input harus sama !");
}

Universitas Sumatera Utara


{
{
double hasill, kalii;
kalii = (10 * 255) / Math.Sqrt(nilai_mseMax);
hasill = Math.Log(kalii, 20);
textBox6.Text = hasill.ToString();
}
}
}
catch { }

// Menghitung MSE & PSNR hasil Min Filter


try
{
Bitmap data_citraAsli3 = new Bitmap(citraAsli.Image);
Bitmap data_HasilMin = new Bitmap(HasilMin.Image);
double nilai_mseMin = 0;
int k, l, m, kk, ll, mm;
int tempk, templ, tempm, tempn;
if (data_citraAsli3.Width == data_HasilMin.Width & data_citraAsli3.Height ==
data_HasilMin.Height)
{
for (int i = 0; i < data_citraAsli3.Width; i++)
{
for (int j = 0; j < data_citraAsli3.Height; j++)
{

k = data_citraAsli3.GetPixel(i, j).R;
l = data_citraAsli3.GetPixel(i, j).G;
m = data_citraAsli3.GetPixel(i, j).B;

kk = data_HasilMin.GetPixel(i, j).R;
ll = data_HasilMin.GetPixel(i, j).G;
mm = data_HasilMin.GetPixel(i, j).B;

tempk = k - kk;
templ = l - ll;
tempm = m - mm;

tempk = (tempk * tempk);


templ = (templ * templ);
tempm = (tempm * tempm);

if (tempk < 0)
tempk = tempk * (-1);

if (templ < 0)
templ = templ * (-1);

if (tempm < 0)
tempm = tempm * (-1);

tempn = (tempk + templ + tempm) / 3;


nilai_mseMin += tempn;
}
}

nilai_mseMin = nilai_mseMin / (data_citraAsli3.Width * data_citraAsli3.Height);

textBox7.Text = nilai_mseMin.ToString();

Universitas Sumatera Utara


}

else
{
MessageBox.Show("Resolusi citra yang di input harus sama !");

{
{
double results, crossx;
crossx = (10 * 255) / Math.Sqrt(nilai_mseMin);
results = Math.Log(crossx, 20);
textBox8.Text = results.ToString();
}
}
}
catch { }

// Menghitung MSE & PSNR hasil Midpoint Filter


try
{
Bitmap data_citraAsli4 = new Bitmap(citraAsli.Image);
Bitmap data_HasilMid = new Bitmap(HasilMidpoint.Image);
double nilai_mseMidpoint = 0;
int p, q, r, p2, q2, r2;
int tempp, tempq, tempr, temps;
if (data_citraAsli4.Width == data_HasilMid.Width & data_citraAsli4.Height ==
data_HasilMid.Height)
{
for (int i = 0; i < data_citraAsli4.Width; i++)
{
for (int j = 0; j < data_citraAsli4.Height; j++)
{

p = data_citraAsli4.GetPixel(i, j).R;
q = data_citraAsli4.GetPixel(i, j).G;
r = data_citraAsli4.GetPixel(i, j).B;

p2 = data_HasilMid.GetPixel(i, j).R;
q2 = data_HasilMid.GetPixel(i, j).G;
r2 = data_HasilMid.GetPixel(i, j).B;

tempp = p - p2;
tempq = q - q2;
tempr = r - r2;

tempp = (tempp * tempp);


tempq = (tempq * tempq);
tempr = (tempr * tempr);

if (tempp < 0)
tempp = tempp * (-1);

if (tempq < 0)
tempq = tempq * (-1);

if (tempr < 0)
tempr = tempr * (-1);

Universitas Sumatera Utara


temps = (tempp + tempq + tempr) / 3;
nilai_mseMidpoint += temps;
}

nilai_mseMidpoint = nilai_mseMidpoint / (data_citraAsli4.Width *


data_citraAsli4.Height);

textBox9.Text = nilai_mseMidpoint.ToString();
}
else
{
MessageBox.Show("Resolusi citra yang di input harus sama !");

}
{
{
double hasils, kalis;
kalis = (10 * 255) / Math.Sqrt(nilai_mseMidpoint);
hasils = Math.Log(kalis, 20);
textBox10.Text = hasils.ToString();
}
MessageBox.Show("Perhitungan MSE & PSNR berhasil");
}
}
catch { }
}

private void button4_Click(object sender, EventArgs e)


{
Close();
}

private void button5_Click(object sender, EventArgs e)


{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = " ";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog.ShowDialog())
{
Bitmap aa = new Bitmap(openFileDialog.FileName);
HasilMean.Image = aa;

}
private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = " ";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog.ShowDialog())
{
Bitmap aa = new Bitmap(openFileDialog.FileName);

Universitas Sumatera Utara


HasilMax.Image = aa;
}
}
private void button7_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = " ";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog.ShowDialog())
{
Bitmap aa = new Bitmap(openFileDialog.FileName);
HasilMin.Image = aa;
}
}
private void button8_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = " ";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog.ShowDialog())
{
Bitmap aa = new Bitmap(openFileDialog.FileName);
HasilMidpoint.Image = aa;
}
}
}
}

3. Form Bangkitkan Noise Salt-And-Pepper

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Order_Statistic_Filters
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)


{
try
{

Universitas Sumatera Utara


Bitmap citra = new Bitmap(Gambar.Image);
int x, y, i, x1, y1, pilih, prob;

y = citra.Height;
x = citra.Width;

prob = Convert.ToInt32(persen.Text);

if (prob > 100 || prob < 0)


{
MessageBox.Show(" Probabilitas Noise Hanya mulai dari 1 - 100 %");

}
else
{
prob = Convert.ToInt32(x * y * prob * 0.01);
Random rand = new Random();
for (i = 0; i < prob; i++)
{
x1 = rand.Next(0, x - 1);
y1 = rand.Next(0, y - 1);
pilih = rand.Next(1,10);
if (pilih <=5)
{
citra.SetPixel(x1, y1, Color.FromArgb(255, 0, 0, 0));
}
else
{
citra.SetPixel(x1, y1, Color.FromArgb(255, 255, 255, 255));
}
}
Gambar.Image = citra;
button3.Enabled = true;
MessageBox.Show("Prosess Pembangkitan Noise Berhasil");
}
}
catch
{
MessageBox.Show("Pastikan inputan probabilitas noisenya benar!");
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = " ";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog.ShowDialog())
{
Bitmap aa = new Bitmap(openFileDialog.FileName);
Gambar.Image = aa;
persen.Enabled = true;
}
}
private void button3_Click(object sender, EventArgs e)
{
Bitmap citra = new Bitmap((Bitmap)this.Gambar.Image);
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = " ";

Universitas Sumatera Utara


saveFileDialog.Filter = "Bitmap Files (*.bmp)|*.bmp | Jpeg files
(*.jpg) | *.jpg";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;

if (DialogResult.OK == saveFileDialog.ShowDialog())
{
citra.Save(saveFileDialog.FileName);
MessageBox.Show("Hasil Penambahan Noise pada citra berhasil disimpan.");
}
button4.Enabled = true;
}

private void button4_Click(object sender, EventArgs e)


{
Close();
}
private void persen_TextChanged(object sender, EventArgs e)
{
button2.Enabled = true;
}
}
}

4. Form Bangkitkan Noise Gaussian

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Order_Statistic_Filters
{
public class RestorasiCitra
{
Bitmap citra = null;
IntPtr Iptr = IntPtr.Zero;
BitmapData bmpData = null;
GaussianRandom gaussgen = new GaussianRandom();

public byte[] Pixels { get; set; }


public int Depth { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }

public RestorasiCitra(Bitmap citra)


{
this.citra = citra;
}

public void LockBits()

Universitas Sumatera Utara

You might also like