Dalam tutorial C# kali ini akan mengulas mengenai bagaimana merancang project binary threshold dalam digital image processing, pengolahan citra digital, dengan menggunakan framework AForge.
Berikut adalah video tutorialnya.
Sebelumnya perlu diingat untuk install terlebih dahulu AForge, kemudian tambahkan file reference yang diperlukan, yakni dengan cara pilih tab PROJECT kemudian Add Reference, bisa juga pada Solution Explorer pilih References kemudian klik kanan dan Add Reference. Setelah itu akan tampil window pencarian file yang diperlukan. Untuk project ini gunakan AForge.dll dan AForge.Imaging.dll.
Untuk source code yang digunakan ialah sebagai berikut.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
namespace binaryThreshold
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = "Threshold " + hScrollBar1.Value;
}
private void clearChecked()
{
normalToolStripMenuItem.Checked = false;
fitToolStripMenuItem.Checked = false;
stretchedToolStripMenuItem.Checked = false;
centeredToolStripMenuItem.Checked = false;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog(this);
string fileName = fileDialog.FileName;
if (fileName == string.Empty) return;
initialPicture.Image = System.Drawing.Image.FromFile(fileName);
hScrollBar1.Enabled = (initialPicture.Image != null);
resetToolStripMenuItem.Enabled = (initialPicture.Image != null);
}
private void normalToolStripMenuItem_Click(object sender, EventArgs e)
{
clearChecked();
normalToolStripMenuItem.Checked = true;
initialPicture.SizeMode = PictureBoxSizeMode.Normal;
filteredPicture.SizeMode = PictureBoxSizeMode.Normal;
}
private void fitToolStripMenuItem_Click(object sender, EventArgs e)
{
clearChecked();
fitToolStripMenuItem.Checked = true;
initialPicture.SizeMode = PictureBoxSizeMode.Zoom;
filteredPicture.SizeMode = PictureBoxSizeMode.Zoom;
}
private void stretchedToolStripMenuItem_Click(object sender, EventArgs e)
{
clearChecked();
stretchedToolStripMenuItem.Checked = true;
initialPicture.SizeMode = PictureBoxSizeMode.StretchImage;
filteredPicture.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void centeredToolStripMenuItem_Click(object sender, EventArgs e)
{
clearChecked();
centeredToolStripMenuItem.Checked = true;
initialPicture.SizeMode = PictureBoxSizeMode.CenterImage;
filteredPicture.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
label1.Text = "Threshold " + hScrollBar1.Value;
Bitmap image = new Bitmap(initialPicture.Image);
IFilter threshold = new Threshold(hScrollBar1.Value);
image = Grayscale.CommonAlgorithms.RMY.Apply(image);
image = threshold.Apply(image);
filteredPicture.Image = image;
}
private void resetToolStripMenuItem_Click(object sender, EventArgs e)
{
initialPicture.Image = null;
filteredPicture.Image = null;
label1.Text = "Threshold 0";
hScrollBar1.Value = 0;
hScrollBar1.Enabled = false;
resetToolStripMenuItem.Enabled = false;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
namespace binaryThreshold
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = "Threshold " + hScrollBar1.Value;
}
private void clearChecked()
{
normalToolStripMenuItem.Checked = false;
fitToolStripMenuItem.Checked = false;
stretchedToolStripMenuItem.Checked = false;
centeredToolStripMenuItem.Checked = false;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog(this);
string fileName = fileDialog.FileName;
if (fileName == string.Empty) return;
initialPicture.Image = System.Drawing.Image.FromFile(fileName);
hScrollBar1.Enabled = (initialPicture.Image != null);
resetToolStripMenuItem.Enabled = (initialPicture.Image != null);
}
private void normalToolStripMenuItem_Click(object sender, EventArgs e)
{
clearChecked();
normalToolStripMenuItem.Checked = true;
initialPicture.SizeMode = PictureBoxSizeMode.Normal;
filteredPicture.SizeMode = PictureBoxSizeMode.Normal;
}
private void fitToolStripMenuItem_Click(object sender, EventArgs e)
{
clearChecked();
fitToolStripMenuItem.Checked = true;
initialPicture.SizeMode = PictureBoxSizeMode.Zoom;
filteredPicture.SizeMode = PictureBoxSizeMode.Zoom;
}
private void stretchedToolStripMenuItem_Click(object sender, EventArgs e)
{
clearChecked();
stretchedToolStripMenuItem.Checked = true;
initialPicture.SizeMode = PictureBoxSizeMode.StretchImage;
filteredPicture.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void centeredToolStripMenuItem_Click(object sender, EventArgs e)
{
clearChecked();
centeredToolStripMenuItem.Checked = true;
initialPicture.SizeMode = PictureBoxSizeMode.CenterImage;
filteredPicture.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
label1.Text = "Threshold " + hScrollBar1.Value;
Bitmap image = new Bitmap(initialPicture.Image);
IFilter threshold = new Threshold(hScrollBar1.Value);
image = Grayscale.CommonAlgorithms.RMY.Apply(image);
image = threshold.Apply(image);
filteredPicture.Image = image;
}
private void resetToolStripMenuItem_Click(object sender, EventArgs e)
{
initialPicture.Image = null;
filteredPicture.Image = null;
label1.Text = "Threshold 0";
hScrollBar1.Value = 0;
hScrollBar1.Enabled = false;
resetToolStripMenuItem.Enabled = false;
}
}
}
Project ini akan mengubah suatu citra RGB menjadi binary, hitam-putih. Batas nilai hitam-putih ditentukan dari nilai threshold yang diatur dengan menggunakan scroll. Selain itu terdapat pula pengaturan ukuran citra dari Normal, Fit, Stretched, dan Centered.