Program Pengolahan Citra Dengan VB.Net


Grayscale

Grayscale

Pada artikel kali saya akan membuat program pengolahan citra RGB menjadi Grayscale. Adapun  program yang akan dirancang yaitu menggunakan Visual Basic.Net 2008.

Untuk memulai mengolah gambar atau disebut dengan citra, lakukan langkah dibawah ini:

1. Jalankan Aplikasi Visual Basic.Net 2008
2. Setelah Buat Nama program yang akan dibuat (contoh yang saya buat yaitu : Pengolahan Citra Dengan VB.NET)
3. Klik OK
untuk coding lengkapnya klikMengolah Format RGB Menjadi Grayscale Pada Citra
4. Silahkan design form program vb untuk mengolah citra seperti bentuk di bawah ini.
1
2
5. Untuk properti SizeMode : Normal pada PictureBox1 dan Picturebox2 diganti menjadi StretchImage, sehingga gambar akan menjadi full saat di buka / dijalankan
6. Setelah selesai meancang formnya ketikkah coding dibawah ini

Public Class Form1
Dim gambar As Bitmap
Dim Rt, vM, vH, vB As Double
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
Sub Bersih_Gambar()
'untuk gambar 1
PictureBox1.Image = Nothing
PictureBox1.BackColor = Color.Empty
PictureBox1.Invalidate()
'untuk gambar 2
PictureBox2.Image = Nothing
PictureBox2.BackColor = Color.Empty
PictureBox2.Invalidate()
'untuk gambar 2
PictureBox3.Image = Nothing
PictureBox3.BackColor = Color.Empty
PictureBox3.Invalidate()
'untuk gambar 3
PictureBox3.Image = Nothing
PictureBox3.BackColor = Color.Empty
PictureBox3.Invalidate()
'untuk gambar 4
PictureBox4.Image = Nothing
PictureBox4.BackColor = Color.Empty
PictureBox4.Invalidate()
'untuk gambar 5
PictureBox5.Image = Nothing
PictureBox5.BackColor = Color.Empty
PictureBox5.Invalidate()
'untuk gambar 6
PictureBox6.Image = Nothing
PictureBox6.BackColor = Color.Empty
PictureBox6.Invalidate()
End Sub
Sub open_gambar()
OpenFileDialog1.Filter = "BMP|*.bmp|JPG|*.Jpg"
OpenFileDialog1.ShowDialog()

If OpenFileDialog1.FileName = "" Then Exit Sub
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
gambar = New Bitmap(PictureBox1.Image)
Me.Text = "Citra : " & gambar.Height & " x " & gambar.Width & " pixel"
End Sub
Sub Save_Gambar()
SaveFileDialog1.Filter = "JPG|*.Jpg|BMP|*.bmp"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName = "" Then Exit Sub
If SaveFileDialog1.FilterIndex = 1 Then
gambar.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
If SaveFileDialog1.FilterIndex = 2 Then
gambar.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
End If
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub

Private Sub OpenCitraToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenCitraToolStripMenuItem.Click
Call open_gambar()
End Sub

Private Sub GrayscaleToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GrayscaleToolStripMenuItem1.Click
PictureBox3.Image = gambar
For Pc = 0 To gambar.Height - 1
For Pb = 0 To gambar.Width - 1
vM = gambar.GetPixel(Pb, Pc).R
vH = gambar.GetPixel(Pb, Pc).G
vB = gambar.GetPixel(Pb, Pc).B
Rt = (vM + vH + vB) / 3
gambar.SetPixel(Pb, Pc, Color.FromArgb(Rt, Rt, Rt))
Next
PictureBox3.Invalidate()
PictureBox3.Refresh()
Next
End Sub

Private Sub CitraToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CitraToolStripMenuItem.Click
PictureBox5.Image = gambar
For Pc = 0 To gambar.Height - 1
For Pb = 0 To gambar.Width - 1
vM = 255 - gambar.GetPixel(Pb, Pc).R
vH = 255 - gambar.GetPixel(Pb, Pc).G
vB = 255 - gambar.GetPixel(Pb, Pc).B
If vM < 0 Then vM = 0
If vH < 0 Then vH = 0
If vB < 0 Then vB = 0
gambar.SetPixel(Pb, Pc, Color.FromArgb(vM, vH, vB))
Next
PictureBox5.Invalidate()
PictureBox5.Refresh()
Next
End Sub

Private Sub BinerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BinerToolStripMenuItem.Click
Dim Pb, Pc As Integer
Dim Rt, vM, vH, vB As Double
With gambar
For Pb = 0 To .Height - 1
For Pc = 0 To .Width - 1
vM = .GetPixel(Pc, Pb).R + 5
vH = .GetPixel(Pc, Pb).G + 5
vB = .GetPixel(Pc, Pb).B + 5
Rt = (vM + vH + vB) / 3
If (Rt 255 Then vM = 255
If vB > 255 Then vB = 255
If vH > 255 Then vH = 255
Rt = (vM + vH + vB) / 3
.SetPixel(Pc, Pb, Color.FromArgb(vM, vH, vB))
Next
PictureBox2.Image = gambar
PictureBox2.Refresh()
Next
End With
End Sub

Private Sub X05r02g03bToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles X05r02g03bToolStripMenuItem.Click
Call Bersih_Gambar()
Call open_gambar()
PictureBox2.Image = gambar
For Pc = 0 To gambar.Height - 1
For Pb = 0 To gambar.Width - 1
vM = gambar.GetPixel(Pb, Pc).R
vH = gambar.GetPixel(Pb, Pc).G
vB = gambar.GetPixel(Pb, Pc).B
Rt = (0.5 * vM) + (0.2 * vH) + (0.3 * vB)
gambar.SetPixel(Pb, Pc, Color.FromArgb(Rt, Rt, Rt))
Next
PictureBox2.Invalidate()
PictureBox2.Refresh()
Next
End Sub

Private Sub ResetCitraToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetCitraToolStripMenuItem.Click
Call Bersih_Gambar()
End Sub

Private Sub X02r05g03bToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles X02r05g03bToolStripMenuItem.Click
PictureBox3.Image = gambar
For Pc = 0 To gambar.Height - 1
For Pb = 0 To gambar.Width - 1
vM = gambar.GetPixel(Pb, Pc).R
vH = gambar.GetPixel(Pb, Pc).G
vB = gambar.GetPixel(Pb, Pc).B
Rt = (0.2 * vM) + (0.5 * vH) + (0.3 * vB)
gambar.SetPixel(Pb, Pc, Color.FromArgb(Rt, Rt, Rt))
Next
PictureBox3.Invalidate()
PictureBox3.Refresh()
Next
End Sub

Private Sub X02r02g05bToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles X02r02g05bToolStripMenuItem.Click
PictureBox4.Image = gambar
For Pc = 0 To gambar.Height - 1
For Pb = 0 To gambar.Width - 1
vM = gambar.GetPixel(Pb, Pc).R
vH = gambar.GetPixel(Pb, Pc).G
vB = gambar.GetPixel(Pb, Pc).B
Rt = (0.2 * vM) + (0.2 * vH) + (0.3 * vB)
gambar.SetPixel(Pb, Pc, Color.FromArgb(Rt, Rt, Rt))
Next
PictureBox4.Invalidate()
PictureBox4.Refresh()
Next
End Sub

Private Sub X05r05g0bToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles X05r05g0bToolStripMenuItem.Click
PictureBox5.Image = gambar
For Pc = 0 To gambar.Height - 1
For Pb = 0 To gambar.Width - 1
vM = gambar.GetPixel(Pb, Pc).R
vH = gambar.GetPixel(Pb, Pc).G
vB = gambar.GetPixel(Pb, Pc).B
Rt = (0.5 * vM) + (0.5 * vH) + (0 * vB)
gambar.SetPixel(Pb, Pc, Color.FromArgb(Rt, Rt, Rt))
Next
PictureBox5.Invalidate()
PictureBox5.Refresh()
Next
End Sub

Private Sub X05r0g05bToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles X05r0g05bToolStripMenuItem.Click
PictureBox6.Image = gambar
For Pc = 0 To gambar.Height - 1
For Pb = 0 To gambar.Width - 1
vM = gambar.GetPixel(Pb, Pc).R
vH = gambar.GetPixel(Pb, Pc).G
vB = gambar.GetPixel(Pb, Pc).B
Rt = (0.5 * vM) + (0 * vH) + (0.5 * vB)
gambar.SetPixel(Pb, Pc, Color.FromArgb(Rt, Rt, Rt))
Next
PictureBox6.Invalidate()
PictureBox6.Refresh()
Next
End Sub

Private Sub SaveCitraToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCitraToolStripMenuItem.Click
Call Save_Gambar()
End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Call open_gambar()
End Sub

Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
Call Save_Gambar()
End Sub

Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
End
End Sub
End Class

7. Jalankan program yang baru saja di buat yaitu dengan tekan F5
8. Setelah dijalankan hasilnya adalah sebagai berikut :

Grayscale

thresholding

Advertisements

2 responses to “Program Pengolahan Citra Dengan VB.Net

  1. saya sangat tertarik…
    jika apa boleh saya minta mentahan di visual studionya..

    maklum saya masih sangat pemula…
    mbuat masih step by step lihat petunjuk….

  2. Saya kira dah lengkap tu kode dan petunjuknya

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s