Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Nama : Huda Pratama Aji Saputra

NIM : 2003040070
Matkul :Pengolahan Citra Digital A2

Latihan dan Praktikum 6


1. Source Code Konversi Citra Biner

private void button11_Click(object sender, EventArgs e)


{
Bitmap bmp = new Bitmap(pictureBox2.Image);
int height = bmp.Height; //mendapatkan tinggi citra
int width = bmp.Width; //mendapatkan lebar citra
Color p;
int i;
//thresholding / binerissasi (mendapatkan citra biner)
for (int y = 0; y < height; y++)
{
for (int x = 0; x < height; x++)
{
p = bmp.GetPixel(x, y);
i = p.B;
if (i < 150)
{
bmp.SetPixel(x, y, Color.FromArgb(255, 255,
255));
}
else
{
bmp.SetPixel(x, y, Color.FromArgb(0, 0, 0));
}
}
}
pictureBox3.Image = bmp;
}
2. Source Code Deteksi Tepi
private void button12_Click(object sender, EventArgs e)
{
Bitmap bmp3 = new Bitmap(pictureBox3.Image);//citra biner
Bitmap bmp4 = new Bitmap(pictureBox3.Image);//citra
biner
int height = bmp3.Height;
int width = bmp3.Width;
Color p8, p0, p1, p2, p3, p4, p5, p6, p7;
int r8 = 0, r0, r1, r2, r3, r4, r5, r6, r7;
int sigma = 0;
for (int q = 1; q < width - 1; q++)
{
for (int s = 1; s < height - 1; s++)
{
p8 = bmp3.GetPixel(q, s);
p0 = bmp3.GetPixel(q, s + 1);
p1 = bmp3.GetPixel(q - 1, s);
p2 = bmp3.GetPixel(q - 1, s);
p3 = bmp3.GetPixel(q - 1, s);
p4 = bmp3.GetPixel(q, s - 1);
p5 = bmp3.GetPixel(q + 1, s - 1);
p6 = bmp3.GetPixel(q + 1, s);
p7 = bmp3.GetPixel(q + 1, s + 1);
r8 = p8.R;
r0 = p0.R;
r1 = p1.R;
r2 = p2.R;
r3 = p3.R;
r4 = p4.R;
r5 = p5.R;
r6 = p6.R;
r7 = p7.R;
sigma = r0 + r1 + r2 + r3 + r4 + r5 + r6 + r7;
if (sigma == 2040)// nilai 2040 artinya piksel
tetangga bernilai 255
{
bmp4.SetPixel(q, s, Color.FromArgb(0, 0, 0));
}
else
{
bmp4.SetPixel(q, s, Color.FromArgb(r8, r8,
r8));
}
}
}
pictureBox4.Image = bmp4;
}
3. Source Code Cari luas, perimeter, kebulatan, dan kekompakan
private void button13_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox3.Image);
Bitmap bmp1 = new Bitmap(pictureBox4.Image);
int height = bmp.Height;
int width = bmp.Width;
Color p;
#region perimeter/keliling
int perimeter = 0;
int a;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
p = bmp1.GetPixel(x, y);
a = p.R;
if (a == 255)
{
perimeter = perimeter + 1;
}
}
}
Console.WriteLine("perimeter : " + perimeter);
#endregion
#region luas/area
int luas = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
p = bmp.GetPixel(x, y);
a = p.R;
if (a == 255)
{
luas = luas + 1;
}
}
}
Console.WriteLine("luas area : " + luas);
#endregion
#region compactness
//compactness
double compactness = 0.0;
compactness = 1 - ((4 * (22.0 / 7.0) * luas) /
Math.Pow(perimeter, 2));
Console.WriteLine("compactness:" + compactness);
#endregion
#region roundness
//roundness
double roundness = 0.0;
roundness = (4 * (22.0 / 7.0)) * (luas /
Math.Pow(perimeter, 2));
Console.WriteLine("roundness : " + roundness);
#endregion
}
4. Screenshoot form
5. Screenshot Output

You might also like