Anda di halaman 1dari 6

MODUL 4

PENGELOLAAN CITRA DIGITAL


ERROR DIFFUSION, HISTOGRAM

ALFIAN KURNIAWAN
1741727011
TI-4H

PROGRAM STUDI TEKNIK INFORMATIKA


JURUSAN TEKNOLOGI INFORMASI
POLITEKNIK NEGERI MALANG
2017
1. Error Diffusion
a. Kode program :
private void errorDiffusionToolStripMenuItem_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
MessageBox.Show("Tidak ada citra yang akan diolah");
else
{
int[,] paletteColor = new int[,] { { 0, 0, 0 }, { 255, 0, 0 }, { 0,
255, 0 }, { 255, 255, 0 }, { 0, 0, 255 }, { 255, 0, 255 }, { 255, 255, 255 } };
Bitmap b = new Bitmap((Bitmap)this.pictureBox1.Image);
this.pictureBox2.Image = b;
int merah, hijau, biru;
double baru, errorR, errorG, errorB;
ProgressBar1.Visible = true;
for (int i = 0; i <= b.Width - 2; i++)
{
for (int j = 0; j <= b.Height - 2; j++)
{
Color c1 = b.GetPixel(i, j);
baru = warnaTerdekat(c1.R, c1.G, c1.B);
merah = b.GetPixel(i, j).R;
hijau = b.GetPixel(i, j).G;
biru = b.GetPixel(i, j).B;

b.SetPixel(i, j,
Color.FromArgb(paletteColor[Convert.ToInt16(baru), 0],
paletteColor[Convert.ToInt16(baru), 1], paletteColor[Convert.ToInt16(baru), 2]));
errorR = merah - b.GetPixel(i, j).R;
errorG = hijau - b.GetPixel(i, j).G;
errorB = biru - b.GetPixel(i, j).B;
b.SetPixel(i + 1, j,
Color.FromArgb((int)truncate2(b.GetPixel(i + 1, j).R + ((7.0 / 16.0) * errorR)),
(int)truncate2(b.GetPixel(i + 1, j).G + ((7.0 / 16.0) * errorG)),
(int)truncate2(b.GetPixel(i + 1, j).B + ((7.0 / 16.0) * errorB))));

if (i > 0)
{
b.SetPixel(i - 1, j + 1,
Color.FromArgb((int)truncate2(b.GetPixel(i - 1, j + 1).R + ((3.0 / 16.0) * errorR)),
(int)truncate2(b.GetPixel(i - 1, j + 1).G + ((3.0 / 16.0) * errorG)),
(int)truncate2(b.GetPixel(i - 1, j + 1).B + ((3.0 / 16.0) * errorB))));
}
b.SetPixel(i, j + 1,
Color.FromArgb((int)truncate2(b.GetPixel(i, j + 1).R + ((5.0 / 16.0) * errorR)),
(int)truncate2(b.GetPixel(i, j + 1).G + ((5.0 / 16.0) * errorG)),
(int)truncate2(b.GetPixel(i, j + 1).B + ((5.0 / 16.0) * errorB))));
b.SetPixel(i + 1, j + 1,
Color.FromArgb((int)truncate2(b.GetPixel(i + 1, j + 1).R + ((1.0 / 16.0) * errorR)),
(int)truncate2(b.GetPixel(i + 1, j + 1).G + ((1.0 / 16.0) * errorG)),
(int)truncate2(b.GetPixel(i + 1, j + 1).B + ((1.0 / 16.0) * errorB))));
}
ProgressBar1.Value = Convert.ToInt16(100 * (i + 1) / b.Width);
}
ProgressBar1.Visible = false;
this.pictureBox2.Refresh();
}
}
b. Hasil :

2. Histogram
a. Kode program :
private void inputToolStripMenuItem_Click(object sender, EventArgs e)
{
{
if (pictureBox1.Image == null)
{
MessageBox.Show("None");
}
else
{
double[] HistoR = new double[256];
double[] HistoG = new double[256];
double[] HistoB = new double[256];
Bitmap b = new Bitmap((Bitmap)this.pictureBox1.Image);
Form8 frm8 = new Form8();
Form7 frm7 = new Form7();
for (int i = 0; i < 255; i++)
{
HistoR[i] = 0;
HistoG[i] = 0;
HistoB[i] = 0;
}
for (int i = 0; i <= 255; i++)
{
for (int j = 0; j <= 255; j++)
{
Color c1 = b.GetPixel(i, j);
int merah = c1.R;
int hijau = c1.G;
int biru = c1.B;
HistoR[merah]++;
HistoG[hijau]++;
HistoB[biru]++;
}
ProgressBar1.Value = Convert.ToInt16(100 * (i + 1) /
b.Width);
}
ProgressBar1.Visible = false;

Double sumR = 0;
for (int i = 0; i < 255; i++)
{
if (HistoG[i] == HistoB[i])
{
sumR++;
}
}

if (sumR == 255)
{
frm7.chart1.Series["Series1"].Color = Color.Gray;
frm7.chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled
= false;
frm7.chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Enabled
= false;

foreach (Double HstR in HistoR)


{
for (int i = 0; i <= 255; i++)
{
frm7.chart1.Series["Series1"].Points.AddXY(i,
(HistoR[i] + HistoG[i] + HistoB[i]) / 3);
}
}
frm7.ShowDialog();
}
else
{
frm8.chart1.Series["Series1"].Color = Color.Red;
frm8.chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled
= false;
frm8.chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Enabled
= false;

foreach (Double HstR in HistoR)


{
for (int i = 0; i <= 255; i++)
{
frm8.chart1.Series["Series1"].Points.AddXY(i,
HistoR[i]);
}
}

frm8.chart2.Series["Series1"].Color = Color.Green;
frm8.chart2.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled
= false;
frm8.chart2.ChartAreas["ChartArea1"].AxisY.LabelStyle.Enabled
= false;

foreach (Double HstG in HistoG)


{
for (int i = 0; i <= 255; i++)
{
frm8.chart2.Series["Series1"].Points.AddXY(i,
HistoG[i]);
}
}

frm8.chart3.Series["Series1"].Color = Color.Blue;
frm8.chart3.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled
= false;
frm8.chart3.ChartAreas["ChartArea1"].AxisY.LabelStyle.Enabled
= false;

foreach (Double HstB in HistoB)


{
for (int i = 0; i <= 255; i++)
{
frm8.chart3.Series["Series1"].Points.AddXY(i,
HistoB[i]);
}
}
frm8.ShowDialog();
}

}
}

b. Hasil :

Anda mungkin juga menyukai