Berikut ini ialah contoh design form window project Visual Studio C#.
Untuk component yang digunakan adalah sebagai berikut ini.
- openButton - Button - untuk membuka file image
- convertButton - Button - untuk mengubah initial image ke binary level
- initialPicture - PictureBox - menampilkan image awal
- convertedPicture - PictureBox - menampilkan image yang telah diubah ke grayscale
- OpenFileDialog - dialog window saat mencari file image yang akan dibuka
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog(this);
string imageFile = fileDialog.FileName;
if (imageFile == string.Empty) return;
initialPicture.Image = Image.FromFile(imageFile);
fileDialog.ShowDialog(this);
string imageFile = fileDialog.FileName;
if (imageFile == string.Empty) return;
initialPicture.Image = Image.FromFile(imageFile);
Tambahkan class untuk menangani proses pengolahan citra dari RGB ke binary level. Pada tab PROJECT pilih Add Class, untuk contoh ini beri nama class tersebut process. Berikut source code untuk class process.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace rgbToGrayscale
{
class process
{
public bool grayLevel(Bitmap b)
{
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
byte red, green, blue;
int nOffset = stride - b.Width * 3;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
blue = p[0];
green = p[1];
red = p[2];
p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
}
}
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace rgbToGrayscale
{
class process
{
public bool grayLevel(Bitmap b)
{
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
byte red, green, blue;
int nOffset = stride - b.Width * 3;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
blue = p[0];
green = p[1];
red = p[2];
p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
}
}
Tambahkan code berikut ini pada bagian convertButton, untuk mengubah image awal menjadi grayscale.
process toGray = new process();
Bitmap image = new Bitmap(initialPicture.Image);
toGray.grayLevel(image);
convertedPicture.Image = image;
Bitmap image = new Bitmap(initialPicture.Image);
toGray.grayLevel(image);
convertedPicture.Image = image;
Untuk Build Debug dan Release, atur Properties dari Project ini untuk akses Allow unsafe code.
0 Response to "Tutorial Image Processing dengan Visual Studio C# RGB to Grayscale"
Post a Comment