Implementasi Algoritma Edge Detection Operator Sobel pada Proses Perbaikan Kualitas Citra Teks
Exit Sub End If
End Class
MsgBox("Pilih dulu gambar yang akan diproses", MsgBoxStyle.Critical
System.EventArgs) Handles cmdProses.Click cmdLoad.Enabled = False cmdProses.Enabled = False If PicAda = False Then
End Sub Private Sub cmdProses_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdLoad.Click OpenFileDialog1.Filter = "Images|*.JPG;*.BMP" OpenFileDialog1.ShowDialog() If OpenFileDialog1.FileName = "" Then Exit Sub PicAda = True Pic1.Image = Image.FromFile(OpenFileDialog1.FileName) Pic1.Width = Pic1.Height * Pic1.Image.Width / Pic1.Image.Height NmFIle = OpenFileDialog1.FileName lblNmFile.Text = NmFIle Dim fileDetails As System.IO.FileInfo = New System.IO.FileInfo(NmFIle) lblSize.Text = fileDetails.Length.ToString cmdProses.Enabled = True
Public Class frmEdges1 Dim bmap As Bitmap 'deklarasi gambar/image dengan tipe Bitmap Dim PicAda As Boolean Dim NmFIle As String Dim nmFILE1 As String Private displayBM As Bitmap Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As
2. EdgeDetect
As System.EventArgs) Handles ToolStripMenuItem1.Click frmEdges1.Show() End Sub
LISTING PROGRAM
ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click frmAbt.Show() End Sub Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e
End Sub Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click End
ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click frmHelp.Show() End Sub Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object,
Public Class frmMenu Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object,
1. Menu
- MsgBoxStyle.OkOnly, "Error Proses")
For X = DX To .Height - DX - 1 For Y = DY To .Width - DY - 1
Red = CInt(.GetPixel(Y, X).R) Green = CInt(.GetPixel(Y, X).G) Blue = CInt(.GetPixel(Y, X).B) Grey = (Red + Green + Blue) / 3 Dim clr As Color = bmap.GetPixel(Y, X) If (Grey < 128) Then
Red = 0 Green = 0 Blue = 0
Else Red = 255 Blue = 255 Green = 255
End If bmap.SetPixel(Y, X, Color.FromArgb(Red, Green, Blue)) Next If X Mod 10 = 0 Then
Pic2.Invalidate() Me.Text = "Progres Proses Biner : " & Int(100 * X / _
(Pic2.Image.Height - 2)).ToString & "%" ProgressBar1.Value = Int(100 * X / (Pic2.Image.Height - 2)) Pic2.Refresh()
End If Next
End With Pic2.Refresh() Me.Text = "Pengolahan Citra : Proses Biner berhasil" Call SobelH() Call SobelV()
End Sub Sub SobelH()
If PicAda = False Then MsgBox("Pilih dulu gambar yang akan diproses", _
MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Proses") Exit Sub
End If Dim MF(2, 2) As Double MF(0, 0) = -1 'a MF(0, 1) = -2 'b MF(0, 2) = -1 'c MF(1, 0) = 0 'd MF(1, 1) = 0 'e MF(1, 2) = 0 'f MF(2, 0) = 1 'g MF(2, 1) = 2 'h MF(2, 2) = 1 'i bmap = New Bitmap(Pic2.Image) Pic3.Image = bmap Dim tempbmp As New Bitmap(Pic3.Image) Dim DX As Integer = 1 Dim DY As Integer = 1 Dim Red As Integer, Green As Integer, Blue As Integer Dim i As Integer, j As Integer Dim k As Integer, l As Integer 'untuk mask border ProgressBar1.Width = Pic3.Width ProgressBar1.Show() With bmap
For i = DX To .Height - DX - 1 For j = DY To .Width - DY - 1
End If If j + 1 > .Width - DY - 1 And i - 1 > 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(1, 0)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(1, 0)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(1, 0))
MF(0, 2)) End If If j - 1 < 1 Then
MF(0, 2)) Blue = Blue + (CInt(.GetPixel(j + 1, i - 1).B) * _
MF(0, 2)) Green = Green + (CInt(.GetPixel(j + 1, i - 1).G) * _
Else Red = Red + (CInt(.GetPixel(j + 1, i - 1).R) * _
Red = Red + (CInt(.GetPixel(j, i).R) * MF(0, 2)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(0, 2)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(0, 2))
MF(0, 1)) Blue = Blue + (CInt(.GetPixel(j, i - 1).B) * MF(0, 1))
'proses matriks filter 'point(j,i)*e --> titik tengah Red = CInt(.GetPixel(j, i).R) * MF(1, 1) Green = CInt(.GetPixel(j, i).G) * MF(1, 1) Blue = CInt(.GetPixel(j, i).B) * MF(1, 1) 'proses titik tetangga 'point(j-1,i-1)*a--> MF(0,0)--> titik kiri atas If j - 1 < 1 And i - 1 < 1 Then
Else Green = Green + (CInt(.GetPixel(j, i - 1).G) * _
Red = Red + (CInt(.GetPixel(j, i).R) * MF(0, 1)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(0, 1)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(0, 1))
MF(0, 0)) End If If i - 1 < 1 Then
MF(0, 0)) Blue = Blue + (CInt(.GetPixel(j - 1, i - 1).B) * _
MF(0, 0)) Green = Green + (CInt(.GetPixel(j - 1, i - 1).G) * _
Else Red = Red + (CInt(.GetPixel(j - 1, i - 1).R) * _
Red = Red + (CInt(.GetPixel(j, i).R) * MF(0, 0)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(0, 0)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(0, 0))
Else Red = Red + (CInt(.GetPixel(j - 1, i).R) * MF(1, 0))
Green = Green + (CInt(.GetPixel(j - 1, i).G) * _ MF(1, 0))
Blue = Blue + (CInt(.GetPixel(j - 1, i).B) * MF(1, 0)) End If If j + 1 > .Width - DY - 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(1, 2)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(1, 2)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(1, 2))
Else Red = Red + (CInt(.GetPixel(j + 1, i).R) * MF(1, 2)) Green = Green + (CInt(.GetPixel(j + 1, i).G) * _
MF(1, 2)) Blue = Blue + (CInt(.GetPixel(j + 1, i).B) * MF(1, 2))
End If If j - 1 < 1 And i + 1 > .Height - DX - 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(2, 0)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(2, 0)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(2, 0))
Else Red = Red + (CInt(.GetPixel(j - 1, i + 1).R) * _
MF(2, 0)) Green = Green + (CInt(.GetPixel(j - 1, i + 1).G) * _
MF(2, 0)) Blue = Blue + (CInt(.GetPixel(j - 1, i + 1).B) * _
MF(2, 0)) End If If i + 1 > .Height - DX - 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(2, 1)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(2, 1)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(2, 1))
Else Red = Red + (CInt(.GetPixel(j, i + 1).R) * MF(2, 1)) Green = Green + (CInt(.GetPixel(j, i + 1).G) * _
MF(2, 1)) Blue = Blue + (CInt(.GetPixel(j, i + 1).B) * MF(2, 1))
End If If j + 1 > .Width - DY - 1 And i + 1 > .Height - DX – 1
Then Green = Green + (CInt(.GetPixel(j, i).G) * MF(2, 2)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(2, 2))
Else Red = Red + (CInt(.GetPixel(j + 1, i + 1).R) * _
MF(2, 2)) Green = Green + (CInt(.GetPixel(j + 1, i + 1).G) _
- MF(2, 2)) Blue = Blue + (CInt(.GetPixel(j + 1, i + 1).B) * _
MF(2, 2)) End If If Red < 0 Then
Red = 0 Else
If Red > 255 Then Red = 255
End If End If If Green < 0 Then
Green = 0 Else
If Green > 255 Then Green = 255
End If End If If Blue < 0 Then
Blue = 0 Else
If Blue > 255 Then Blue = 255
End If End If bmap.SetPixel(j, i, Color.FromArgb(Red, Green, Blue))
Next If i Mod 10 = 0 Then
Pic3.Invalidate() Me.Text = Int(100 * i / (Pic3.Image.Height - 2))_
.ToString & "%" ProgressBar1.Value = Int(100 * i / (Pic3.Image.Height - 2)) Pic3.Refresh()
End If Next
End With ProgressBar1.Hide() Pic3.Refresh()
End Sub Sub SobelV()
If PicAda = False Then MsgBox("Pilih dulu gambar yang akan diproses", _
MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Proses") Exit Sub
End If Dim MF(2, 2) As Double MF(0, 0) = 1 'a MF(0, 1) = 0 'b MF(0, 2) = -1 'c MF(1, 0) = 2 'd MF(1, 1) = 0 'e MF(1, 2) = -2 'f MF(2, 1) = 0 'h MF(2, 2) = -1 'i bmap = New Bitmap(Pic2.Image) Pic4.Image = bmap Dim tempbmp As New Bitmap(Pic4.Image) Dim DX As Integer = 1 Dim DY As Integer = 1 Dim Red As Integer, Green As Integer, Blue As Integer Dim i As Integer, j As Integer Dim k As Integer, l As Integer 'untuk kernel border ProgressBar1.Width = Pic4.Width ProgressBar1.Show() With bmap
For i = DX To .Height - DX - 1 For j = DY To .Width - DY - 1
Red = CInt(.GetPixel(j, i).R) * MF(1, 1) Green = CInt(.GetPixel(j, i).G) * MF(1, 1) Blue = CInt(.GetPixel(j, i).B) * MF(1, 1) If j - 1 < 1 And i - 1 < 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(0, 0)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(0, 0))
Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(0, 0)) Else
Red = Red + (CInt(.GetPixel(j - 1, i - 1).R) * _ MF(0, 0))
Green = Green + (CInt(.GetPixel(j - 1, i - 1).G) * _ MF(0, 0))
Blue = Blue + (CInt(.GetPixel(j - 1, i - 1).B) * _ MF(0, 0))
End If If i - 1 < 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(0, 1)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(0, 1)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(0, 1))
Else Red = Red + (CInt(.GetPixel(j, i - 1).R) * MF(0, 1)) Green = Green + (CInt(.GetPixel(j, i - 1).G) * _
MF(0, 1)) Blue = Blue + (CInt(.GetPixel(j, i - 1).B) * MF(0, 1))
End If If j + 1 > .Width - DY - 1 And i - 1 > 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(0, 2)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(0, 2)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(0, 2))
Else Red = Red + (CInt(.GetPixel(j + 1, i - 1).R) * _
MF(0, 2)) Green = Green + (CInt(.GetPixel(j + 1, i - 1).G) * _
MF(0, 2)) Blue = Blue + (CInt(.GetPixel(j + 1, i - 1).B) * _
MF(0, 2)) End If If j - 1 < 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(1, 0)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(1, 0)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(1, 0))
Else Red = Red + (CInt(.GetPixel(j - 1, i).R) * MF(1, 0)) Green = Green + (CInt(.GetPixel(j - 1, i).G) * _ Blue = Blue + (CInt(.GetPixel(j - 1, i).B) * MF(1, 0))
End If If j + 1 > .Width - DY - 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(1, 2)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(1, 2)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(1, 2))
Else Red = Red + (CInt(.GetPixel(j + 1, i).R) * MF(1, 2)) Green = Green + (CInt(.GetPixel(j + 1, i).G) * _
MF(1, 2)) Blue = Blue + (CInt(.GetPixel(j + 1, i).B) * MF(1, 2))
End If If j - 1 < 1 And i + 1 > .Height - DX - 1 Then
Red = Red + (CInt(.GetPixel(j, i).R) * MF(2, 0)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(2, 0)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(2, 0))
Else Red = Red + (CInt(.GetPixel(j - 1, i + 1).R) * _
MF(2, 0)) Green = Green + (CInt(.GetPixel(j - 1, i + 1).G) _
- MF(2, 0)) Blue = Blue + (CInt(.GetPixel(j - 1, i + 1).B) * _
- MF(2, 2)) Blue = Blue + (CInt(.GetPixel(j + 1, i + 1).B) * _
MF(2, 0)) End If If i + 1 > .Height - DX - 1 Then
If Green > 255 Then Green = 255
End With ProgressBar1.Hide() Pic4.Refresh() cmdSimpan.Enabled = True
End If Next
& "%" ProgressBar1.Value = Int(100 * i / (Pic4.Image.Height - 2)) Pic4.Refresh()
Pic4.Invalidate() Me.Text = Int(100 * i / (Pic4.Image.Height - 2)).ToString_
Next If i Mod 10 = 0 Then
End If End If bmap.SetPixel(j, i, Color.FromArgb(Red, Green, Blue))
If Blue > 255 Then Blue = 255
Blue = 0 Else
End If If Blue < 0 Then
Green = 0 Else
Red = Red + (CInt(.GetPixel(j, i).R) * MF(2, 1)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(2, 1)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(2, 1))
End If End If If Green < 0 Then
If Red > 255 Then Red = 255
Red = 0 Else
MF(2, 2)) End If If Red < 0 Then
MF(2, 2)) Green = Green + (CInt(.GetPixel(j + 1, i + 1).G) _
Else Red = Red + (CInt(.GetPixel(j + 1, i + 1).R) * _
Then Red = Red + (CInt(.GetPixel(j, i).R) * MF(2, 2)) Green = Green + (CInt(.GetPixel(j, i).G) * MF(2, 2)) Blue = Blue + (CInt(.GetPixel(j, i).B) * MF(2, 2))
End If If j + 1 > .Width - DY - 1 And i + 1 > .Height - DX – 1 _
MF(2, 1)) Blue = Blue + (CInt(.GetPixel(j, i + 1).B) * MF(2, 1))
Else Red = Red + (CInt(.GetPixel(j, i + 1).R) * MF(2, 1)) Green = Green + (CInt(.GetPixel(j, i + 1).G) * _
End Sub
Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdKeluar.Click
Close() End Sub Private Sub cmdBersih_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles cmdBersih.Click Pic1.Image = Nothing Pic2.Image = Nothing Pic3.Image = Nothing Pic4.Image = Nothing lblNmFile.Text = "" lblNmFile1.Text = "" lblSize.Text = "" lblSize1.Text = "" cmdSimpan.Enabled = False cmdLoad.Enabled = True
End Sub Private Sub cmdSimpan_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles cmdSimpan.Click cmdSimpan.Enabled = False Dim i As Integer Dim str As String For i = 1 To 1000
If i < 10 Then str = "Picture_00" & i.ToString & ".jpg" If i > 9 And i < 100 Then str = "Picture_0" & i.ToString & ".jpg" If i > 99 Then str = "Picture_" & i.ToString & ".jpg" If i > 900 Then MsgBox("err") If Not System.IO.File.Exists(str) Then
Try Pic3.Image.Save(str, _
System.Drawing.Imaging.ImageFormat.Jpeg) System.Drawing.Imaging.ImageFormat.Png) lblNmFile1.Text = Application.StartupPath & "\" & str
Catch Ex As Exception MsgBox("Gagal")
End Try End If
Next Dim fileDetails As System.IO.FileInfo = New _
System.IO.FileInfo(lblNmFile1.Text) lblSize1.Text = fileDetails.Length.ToString MsgBox("Gambar berhasil di simpan .....", MsgBoxStyle.OkOnly, _
"Save Sukses") cmdLoad.Enabled = True End Sub Private Sub frmEdges1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load cmdProses.Enabled = False cmdSimpan.Enabled = False cmdLoad.Focus()
End Sub End Class