Kohonen Neural Network Dalam Pendeteksian Duplikasi Image Digital Melalui Deteksi Garis Tepi
LAMPIRAN
LISTING PROGRAM
Imports System Imports System.IO Imports System.Math Public Class frmUtama Dim diCari As DirectoryInfo Dim flPath As String, srcPath As String Dim asNode As Double, dsNode As Double, ksNode As Double, ssNode As Double Dim atNode() As Double, dtNode() As Double, ktNode() As Double, stNode() As Double Dim tmpTarget As PictureBox Dim LT As Long, HT As Long Dim MaxErr As Double, LRate As Double Dim outDev() As Double, outKur() As Integer, outSkew() As Integer Dim inDev As Double, inKur As Integer, inSkew As Integer Dim FinProses As Boolean Dim posX As Single, posY As Single Private Sub DoReset() ClearPictureBox(pbSource) ClearPictureBox(pbPreview) btnSource.Text = "Load" btnSource.Enabled = True btnProses.Enabled = False lblFolder.Text = "" txtLT.Text = "10" txtHT.Text = "250" txtErr.Text = "0.05" txtInDev.Text = "" txtInKur.Text = "" txtInSkew.Text = "" lstDeviasi.Items.Clear() lstKurtosis.Items.Clear() lstSkewness.Items.Clear() lstHasil.Items.Clear() lstDaftar.Items.Clear() End Sub Private Sub ClearPictureBox(ByVal pb As PictureBox) pb.Image = Nothing pb.BackColor = Color.Empty pb.Invalidate() End Sub Private Sub btnSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSource.Click Select Case btnSource.Text Case "Load" FinProses = False ofdPicture.Title = "Pilih File Gambar" ofdPicture.Filter = "BMP|*.bmp" ofdPicture.FileName = "" ofdPicture.ShowDialog() srcPath = ofdPicture.FileName If Len(srcPath) < 1 Then Exit Sub pbSource.Image = LoadPic(Image.FromFile(srcPath), 150, 150) btnSource.Text = "Deteksi Tepi" Case "Deteksi Tepi" intVector = pbSource.Width * pbSource.Height pbSource.Image = DoEdgeLinking(pbSource, True) If Len(lblFolder.Text) < 1 Then btnSource.Enabled = False Else btnSource.Enabled = True End If btnSource.Text = "Segmentasi Kohonen" Case "Segmentasi Kohonen" LT = txtLT.Text HT = txtHT.Text ReDim srcNode(intVector - 1) pbSource.Image = DoSegment(pbSource, LT, HT, True, 0) asNode = AvgNode(True, 0) dsNode = DevNode(asNode, True, 0) ksNode = KurNode(asNode, dsNode, True, 0) ssNode = SkewNode(asNode, True, 0) ReDim trgNode(lstDaftar.Items.Count - 1, intVector - 1) ReDim atNode(lstDaftar.Items.Count - 1) ReDim dtNode(lstDaftar.Items.Count - 1) ReDim ktNode(lstDaftar.Items.Count - 1) ReDim stNode(lstDaftar.Items.Count - 1) tmpTarget = New PictureBox tmpTarget.Width = 150 tmpTarget.Height = 150 For i = 0 To lstDaftar.Items.Count - 1 lstDaftar.SelectedIndex = i tmpTarget.Image = LoadPic(Image.FromFile(flPath & "\" & lstDaftar.Items(i)), 150, 150) tmpTarget.Image = DoEdgeLinking(tmpTarget, False) tmpTarget.Image = DoSegment(tmpTarget, LT, HT, False, i) atNode(i) = AvgNode(False, i) dtNode(i) = DevNode(atNode(i), False, i) ktNode(i) = KurNode(atNode(i), dtNode(i), False, i) stNode(i) = SkewNode(atNode(i), False, i) Next btnProses.Enabled = True btnSource.Text = "Load" End Select End Sub Private Sub btnFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFolder.Click fbdFolder.Description = "Pilih Lokasi Folder" fbdFolder.ShowDialog() flPath = fbdFolder.SelectedPath If Len(flPath) < 1 Then Exit Sub lblFolder.Text = flPath
diCari = New DirectoryInfo(flPath) lstDaftar.Items.Clear() For Each fi In diCari.GetFiles("*.bmp") lstDaftar.Items.Add(fi.Name) Next If btnSource.Enabled = False Then btnSource.Enabled = True End Sub Private Sub lstDaftar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstDaftar.MouseDown If e.Button = Windows.Forms.MouseButtons.Right And FinProses Then cmsAksi.Show(Me.lstDaftar, New Point(Me.posX, Me.posY)) End If End Sub Private Sub lstDaftar_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstDaftar.MouseMove posX = e.X posY = e.Y End Sub Private Sub lstDaftar_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDaftar.SelectedIndexChanged Using fs As New System.IO.FileStream(flPath & "\" & lstDaftar.Items(lstDaftar.SelectedIndex), IO.FileMode.Open,IO.FileAccess.Read) pbPreview.Image = LoadPic(Image.FromStream(fs), 150, 150) End Using If FinProses Then lstDeviasi.SelectedIndex = lstDaftar.SelectedIndex lstKurtosis.SelectedIndex = lstDaftar.SelectedIndex lstSkewness.SelectedIndex = lstDaftar.SelectedIndex lstHasil.SelectedIndex = lstDaftar.SelectedIndex Using fs As New System.IO.FileStream(flPath & "\" & lstDaftar.Items(lstDaftar.SelectedIndex), IO.FileMode.Open,
IO.FileAccess.Read) pbPreview.Image = LoadPic(Image.FromStream(fs), 150, 150) End Using pbPreview.Image = DoEdgeLinking(pbPreview, False) pbPreview.Image = DoSegment(pbPreview, LT, HT, False, lstDaftar.SelectedIndex) End If End Sub Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click DoReset() End Sub Private Sub frmUtama_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load DoReset() End Sub Private Sub btnProses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProses.Click Dim tmpKur As Double, dDev As Double
Dim CekDev As Boolean, CekKur As Boolean, CekSkew As Boolean Dim Hasil() As String MaxErr = txtErr.Text inDev = dsNode tmpKur = ksNode - 3 If tmpKur < 0 Then inKur = -1 ElseIf tmpKur = 0 Then inKur = 0 Else inKur = 1 End If If ssNode < 0 Then inSkew = 0 Else inSkew = 1 End If txtInDev.Text = inDev txtInKur.Text = inKur txtInSkew.Text = inSkew ReDim outDev(lstDaftar.Items.Count - 1) ReDim outKur(lstDaftar.Items.Count - 1) ReDim outSkew(lstDaftar.Items.Count - 1) ReDim Hasil(lstDaftar.Items.Count - 1) lstDeviasi.Items.Clear() lstKurtosis.Items.Clear() lstSkewness.Items.Clear() lstHasil.Items.Clear() For i = 0 To lstDaftar.Items.Count - 1 outDev(i) = dtNode(i) tmpKur = ktNode(i) - 3 If tmpKur < 0 Then outKur(i) = -1 ElseIf tmpKur = 0 Then outKur(i) = 0 Else outKur(i) = 1 End If If stNode(i) < 0 Then outSkew(i) = 0 Else outSkew(i) = 1 End If dDev = Abs(inDev - outDev(i)) * 100 If dDev <= MaxErr Then CekDev = True Else CekDev = False End If If inKur = outKur(i) Then CekKur = True Else CekKur = False End If If inSkew = outSkew(i) Then CekSkew = True Else CekSkew = False End If
If CekDev And CekKur And CekSkew Then Hasil(i) = "Mirip" Else Hasil(i) = "Tidak Mirip" End If lstDeviasi.Items.Add(outDev(i)) lstKurtosis.Items.Add(outKur(i)) lstSkewness.Items.Add(outSkew(i)) lstHasil.Items.Add(Hasil(i)) Next FinProses = True End Sub Private Sub cmsAksi_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles cmsAksi.ItemClicked Dim strNewFile As String, strOldFile As String Dim tmpCheck As Boolean strOldFile = flPath & "\" & lstDaftar.Items(lstDaftar.SelectedIndex) If e.ClickedItem.Name = "tsmHapus" Then If MsgBox("Anda Ingin Menghapus File Gambar " & lstDaftar.Items(lstDaftar.SelectedIndex) & " Dari Media Penyimpanan Data?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Konfirmasi Hapus File") = MsgBoxResult.Yes Then tmpCheck = False Do Try If File.Exists(strOldFile) Then FileSystem.Kill(strOldFile) MsgBox("File Berhasil Di Hapus", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Hapus File Sukses") tmpCheck = True lstDaftar.Items.Clear() For Each fi In diCari.GetFiles("*.bmp") lstDaftar.Items.Add(fi.Name) Next End If Catch ex As Exception tmpCheck = False End Try Loop Until tmpCheck = True End If Else If MsgBox("Ubah Nama File Gambar " & lstDaftar.Items(lstDaftar.SelectedIndex) & "?", MsgBoxStyle.Question
- MsgBoxStyle.YesNo, "Konfirmasi Ubah Nama File") = MsgBoxResult.Yes Then strNewFile = InputBox("Input Nama File Pengganti") If Len(strNewFile) < 1 Then Exit Sub strNewFile = flPath & "\" & strNewFile & ".bmp" Do Try If File.Exists(strOldFile) Then FileSystem.Rename(strOldFile, strNewFile) MsgBox("Nama File Berhasil Di Ubah", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "File Rename Sukses")
tmpCheck = True lstDaftar.Items.Clear()
For Each fi In diCari.GetFiles("*.bmp") lstDaftar.Items.Add(fi.Name) Next End If Catch ex As Exception tmpCheck = False End Try Loop Until tmpCheck = True End If End If End Sub Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click frmAbout.ShowDialog(Me) End Sub Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click Me.Close() End Sub Private Sub tsmHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmHapus.Click End Sub End Class Imports System Imports System.Drawing Imports System.Drawing.Imaging Imports System.Math Module mdlUtama Public intVector As Long, trgVector As Long Public srcNode() As Double, trgNode(0, 0) As Double Public Bobot As Double Public Function LoadPic(ByVal tmpPic As Image, ByVal THeight As Integer, ByVal TWidth As Integer) As Image Return New Bitmap(tmpPic, THeight, TWidth) End Function Public Function DoEdgeLinking(ByVal tmpPic As PictureBox, ByVal blMode As Boolean) As Image Dim bmpIn As New Bitmap(tmpPic.Image) Dim bmpOut As New Bitmap(tmpPic.Image) Dim tmpR As Long, tmpG As Long, tmpB As Long Dim Op_X(0 To 2, 0 To 2) As Integer, Op_Y(0 To 2, 0 To 2) As Integer Dim Itensity As Long, GradX As Long, GradY As Long, Grad As Long Grad = 0 Op_X(0, 0) = -1
Op_X(1, 0) = -(Sqrt(2)) Op_X(2, 0) = -1 Op_X(0, 1) = 0 Op_X(1, 1) = 0 Op_X(2, 1) = 0 Op_X(0, 2) = 1 Op_X(1, 2) = (Sqrt(2)) Op_X(2, 2) = 1 Op_Y(0, 0) = -1 Op_Y(1, 0) = 0 Op_Y(2, 0) = 1 Op_Y(0, 1) = -(Sqrt(2)) Op_Y(1, 1) = 0 Op_Y(2, 1) = (Sqrt(2)) Op_Y(0, 2) = -1 Op_Y(1, 2) = 0 Op_Y(2, 2) = 1 For X = 0 To bmpIn.Height - 1 For Y = 0 To bmpIn.Width - 1 GradX = 0 GradY = 0 Grad = 0 If X = 0 Or Y = 0 Or X = bmpIn.Width - 1 Or Y = bmpIn.Height - 1 Then Grad = 0 Else For I = 0 To 2 For J = 0 To 2 tmpR = bmpIn.GetPixel(Y + J - 1, X + I - 1).R tmpG = bmpIn.GetPixel(Y + J - 1, X + I - 1).G tmpG = bmpIn.GetPixel(Y + J - 1, X + I - 1).B Itensity = (tmpR + tmpG + tmpB) / 3 GradX = GradX + (Itensity * Op_X(J, I)) GradY = GradY + (Itensity * Op_Y(J, I)) Next J Next I Grad = Round(Sqrt(Abs(GradX * GradX) + Abs(GradY
- GradY))) End If If Grad < 0 Then Grad = 0 ElseIf Grad > 255 Then Grad = 255 End If bmpOut.SetPixel(Y, X, Color.FromArgb(Grad, Grad, Grad)) Next
Next DoEdgeLinking = bmpOut End Function Public Function DoSegment(ByVal tmpPic As PictureBox, ByVal iTL As Long, ByVal iHL As Long, ByVal blMode As Boolean, ByVal Pos As Long) As Image Dim bmpIn As New Bitmap(tmpPic.Image) Dim bmpOut As New Bitmap(tmpPic.Image)
Dim tmpR As Long, tmpG As Long, tmpB As Long Dim tmpPos As Long Dim Node As Boolean tmpPos = 0 For X = 0 To bmpIn.Height - 1 For Y = 0 To bmpIn.Width - 1 tmpR = bmpIn.GetPixel(Y, X).R If tmpR < iTL Then tmpR = 0 tmpG = 0 tmpB = 0 Node = 0 ElseIf tmpR >= iTL And tmpR <= iHL Then tmpR = 255 tmpG = 255 tmpB = 255 Node = 1 Else tmpR = 0 tmpG = 0 tmpB = 0 Node = 0 End If If blMode Then srcNode(tmpPos) = Node Else trgNode(Pos, tmpPos) = Node End If bmpOut.SetPixel(Y, X, Color.FromArgb(tmpR, tmpG, tmpB)) tmpPos += 1 Next Next DoSegment = bmpOut End Function Public Function AvgNode(ByVal blMode As Boolean, ByVal Pos As Long) As Double Dim tmpTotal As Double tmpTotal = 0 For i = 0 To intVector - 1 If blMode Then tmpTotal = tmpTotal + srcNode(i) Else tmpTotal = tmpTotal + trgNode(Pos, i) End If Next AvgNode = tmpTotal / intVector End Function Public Function DevNode(ByVal avNode As Double, ByVal blMode As Boolean, ByVal Pos As Long) As Double Dim tmpTotal As Double, tmpDist As Double tmpTotal = 0 For i = 0 To intVector - 1 If blMode Then tmpDist = srcNode(i) - avNode tmpTotal = tmpTotal + (tmpDist ^ 2) Else tmpDist = trgNode(Pos, i) - avNode tmpTotal = tmpTotal + (tmpDist ^ 2) End If Next tmpTotal = tmpTotal / (intVector - 1) DevNode = tmpTotal ^ 0.5 End Function Public Function KurNode(ByVal avNode As Double, ByVal dvNode As Double, ByVal blMode As Boolean, ByVal Pos As Long) As Double Dim tmpTotal As Double, tmpMoment As Double, tmpSD As Double tmpTotal = 0 For i = 0 To intVector - 1 If blMode Then tmpMoment = Abs(srcNode(i) - avNode) ^ 4 tmpSD = Abs(srcNode(i) - avNode) ^ 2 tmpTotal = tmpTotal + (tmpMoment / (tmpSD ^ 2)) Else tmpMoment = Abs(trgNode(Pos, i) - avNode) ^ 4 tmpSD = Abs(trgNode(Pos, i) - avNode) ^ 2 tmpTotal = tmpTotal + (tmpMoment / (tmpSD ^ 2)) End If Next KurNode = tmpTotal / intVector End Function Public Function SkewNode(ByVal avNode As Double, ByVal blMode As Boolean, ByVal Pos As Long) As Double Dim tmpM3 As Double, tmpM2 As Double tmpM3 = 0 tmpM2 = 0 For i = 0 To intVector - 1 If blMode Then tmpM3 = tmpM3 + ((srcNode(i) - avNode) ^ 3) tmpM2 = tmpM2 + ((srcNode(i) - avNode) ^ 2) Else tmpM3 = tmpM3 + ((trgNode(Pos, i) - avNode) ^ 3) tmpM2 = tmpM2 + ((trgNode(Pos, i) - avNode) ^ 2) End If Next tmpM3 = tmpM3 / intVector tmpM2 = (tmpM2 / intVector) ^ (3 / 2) SkewNode = tmpM3 / tmpM2 End Function End Module