Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class PCAClass
- Dim GlobalCitra(,) As Double
- Dim matrik As New MatriksClass
- Dim totalkelas As Integer
- Dim THETA As Double = 0.5
- Dim fisherface(,) As Double
- Dim RekonstruksiFisherface As New List(Of Double(,))
- Private Function makeoneVektor(ByVal img(,) As Double) As Double(,)
- Dim tmp(img.GetLength(0) - 1, 0) As Double
- For i As Integer = 0 To img.GetLength(0) - 1
- Dim avg As Double = 0
- For j As Integer = 0 To img.GetLength(1) - 1
- avg += img(i, j)
- Next
- Dim fl As String = avg / img.GetLength(1)
- If fl.Contains(".") Then
- Dim hasil() As String = fl.Split(".")
- fl = hasil(1)
- If CDbl(fl.Substring(0, 1)) > 4 Then
- tmp(i, 0) = CDbl(hasil(0)) + 1
- Else
- tmp(i, 0) = CDbl(hasil(0))
- End If
- Else
- tmp(i, 0) = CDbl(fl)
- End If
- Next
- Return tmp
- End Function
- Private Function MergeVektor(ByVal img As List(Of Double(,)), ByVal height As Double) As Double(,)
- Dim tmp(height - 1, img.Count - 1) As Double
- For i As Integer = 0 To tmp.GetLength(1) - 1
- Dim nilai As Double(,) = img.Item(i)
- For j As Integer = 0 To tmp.GetLength(0) - 1
- tmp(j, i) = nilai(j, 0)
- Next
- Next
- Return tmp
- End Function
- Private Function KonversiCitraDatabasekeGrayscale(ByVal img() As Byte) As Double(,)
- Dim ms As New IO.MemoryStream(img)
- Dim pict As New PictureBox
- pict.Image = Image.FromStream(ms)
- Return gambar.GetGrayScale(pict.Image)
- End Function
- Sub New()
- Dim daftarGambar As New List(Of List(Of Double(,)))
- Dim UniteGambar As New List(Of Double(,))
- Dim koneksi As New SqlClient.SqlConnection(My.Settings.koneksiString)
- koneksi.Open()
- Dim dtGambar As New DataTable
- Using da As New SqlClient.SqlDataAdapter("SELECT * From Pengguna", koneksi)
- da.Fill(dtGambar)
- End Using
- For Each Gambar As DataRow In dtGambar.Rows
- Dim pengguna As New List(Of Double(,))
- pengguna.Add(KonversiCitraDatabasekeGrayscale(CType(Gambar("sample1"), Byte())))
- pengguna.Add(KonversiCitraDatabasekeGrayscale(CType(Gambar("sample2"), Byte())))
- pengguna.Add(KonversiCitraDatabasekeGrayscale(CType(Gambar("sample3"), Byte())))
- daftarGambar.Add(pengguna)
- For Each item As Double(,) In pengguna
- UniteGambar.Add(makeoneVektor(item))
- Next
- Next
- 'mengubah seluruh citra PCA ke dalam Vektor
- Dim KumpulanPCA(,) As Double = MergeVektor(UniteGambar, UniteGambar.Item(0).GetLength(0))
- UniteGambar.Clear()
- 'membuat uPCA dari kumpulan citra PCA
- UniteGambar.Add(makeoneVektor(KumpulanPCA))
- Dim CitraPCA(,) As Double
- Dim ST(,) As Double
- Dim Eigenvalue1(,), Eigenvalue2(,) As Double
- 'melakukan pengurang setiap citra PCA dengan UPCA
- CitraPCA = KurangVektor(KumpulanPCA, UniteGambar.Item(0))
- ST = matrik.Perkalian(CitraPCA, matrik.transpose(CitraPCA))
- Dim d1 As Double = SumValue(matrik.eigenvalue(ST))
- Eigenvalue1 = matrik.eigenVektor(ST)
- Dim V(UniteGambar.Item(0).GetLength(0) - 1, (dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count) As Double
- For i As Integer = 0 To UniteGambar.Item(0).GetLength(0) - 1
- For j As Integer = 0 To (dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count
- V(i, j) = Eigenvalue1(i, j)
- Next
- Next
- Dim WPCA(,) As Double = matrik.Perkalian(matrik.transpose(CitraPCA), V)
- Dim inputFLD(,) As Double = matrik.Perkalian(CitraPCA, WPCA)
- Dim UFLD(,) As Double = makeoneVektor(inputFLD)
- Dim SB As Double = 0
- Dim SW As Double = 0
- For Each orang As List(Of Double(,)) In daftarGambar
- Dim rata(,) As Double = makeoneVektor(MergeVektor(orang, orang.Item(0).GetLength(0)))
- SB += matrik.Perkalian(matrik.Perkalian(3, matrik.transpose(matrik.pengurangan(rata, UFLD))), matrik.pengurangan(rata, UFLD))(0, 0)
- For Each gb(,) As Double In orang
- SW += matrik.Perkalian(matrik.transpose(matrik.pengurangan(makeoneVektor(gb), rata)), matrik.pengurangan(makeoneVektor(gb), rata))(0, 0)
- Next
- Next
- Dim rasio = SB / SW
- ''membuat dimensi bari sebesar ukuran citra awal (5x5)
- Dim matRatio(KumpulanPCA.GetLength(0) - 1, KumpulanPCA.GetLength(0) - 1) As Double
- For i As Double = 0 To KumpulanPCA.GetLength(0) - 1
- For j As Double = 0 To KumpulanPCA.GetLength(0) - 1
- matRatio(i, j) = rasio
- Next
- Next
- Dim d2 As Double = SumValue(matrik.eigenvalue(matRatio))
- Eigenvalue2 = matrik.eigenVektor(matRatio)
- '' lalu lakukan pemotongan terhadap baris sebanyak kelas
- ''hasil akhir setelah pemotongan dimensi didapatkan dimensi Z(5x4) maka
- ''dibuat dimensi baru dengan pengurangan baris dari N-C (6-2) = 4
- '' Dimensi awal citra adalah Z(5 x 4 ) maka berubah menjadi Z(4,4)
- Dim VM((UniteGambar.Item(0).GetLength(0) - 1) - dtGambar.Rows.Count, (dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count) As Double
- For i As Integer = 0 To ((UniteGambar.Item(0).GetLength(0) - 1) - dtGambar.Rows.Count) - 1
- For j As Integer = 0 To ((dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count) - 1
- VM(i, j) = Eigenvalue2(i, j)
- Next
- Next
- '' mencari nilai yang lebih kecil dari Theta
- If d1 / d2 > theta Then
- 'maka untuk reduksi nanatinya diambil nilai m
- Dim m As Double = CInt(d1 / d2) Mod 100
- ReDim VM((dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count, m - 1)
- For i As Integer = 0 To (dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count
- For j As Integer = 0 To m - 1
- VM(i, j) = Eigenvalue2(i, j)
- Next
- Next
- End If
- Dim WFLD(,) = VM
- fisherface = matrik.Perkalian(inputFLD, WFLD)
- AvarageFisherFace(daftarGambar)
- End Sub
- Private Sub AvarageFisherFace(ByVal daftar As List(Of List(Of Double(,))))
- For Each orang As List(Of Double(,)) In daftar
- For Each gbr(,) As Double In orang
- RekonstruksiFisherface.Add(pengurangan(gbr, fisherface))
- Next
- Next
- End Sub
- Public ReadOnly Property Rekonstruksi() As List(Of Double(,))
- Get
- Return RekonstruksiFisherface
- End Get
- End Property
- Public Function TestingInput(ByVal img(,) As Double) As Double(,)
- Return pengurangan(img, fisherface)
- End Function
- Public Function pengurangan(ByVal matriksA(,) As Double, ByVal matriksB(,) As Double) As Double(,)
- ReDim Preserve matriksB(matriksA.GetLength(1) - 1, matriksA.GetLength(0) - 1)
- Dim width As Integer = matriksB.GetLength(1) - 1
- Dim height As Integer = matriksB.GetLength(0) - 1
- Dim tmp(height, width) As Double
- ' Dim cmp As Double
- For Posisi_x As Integer = 0 To height
- For Posisi_y As Integer = 0 To width
- tmp(Posisi_x, Posisi_y) = matriksA(Posisi_x, Posisi_y) - matriksB(Posisi_x, Posisi_y)
- Next
- Next
- Return tmp
- End Function
- Private Function KurangVektor(ByVal sample(,) As Double, ByVal koef(,) As Double) As Double(,)
- For i As Integer = 0 To sample.GetLength(1) - 1
- For j As Integer = 0 To sample.GetLength(0) - 1
- sample(j, i) = sample(j, i) - koef(j, 0)
- Next
- Next
- Return sample
- End Function
- Public Sub MakeST()
- 'Buat Rerata Citra
- GlobalCitra = KurangVektor(GlobalCitra, makeoneVektor(GlobalCitra))
- Dim ST(,) As Double = matrik.Perkalian(matrik.transpose(GlobalCitra), GlobalCitra)
- Dim eigen(,) As Double = matrik.eigenVektor(ST)
- Dim tmp(eigen.GetLength(0) - totalkelas - 1, eigen.GetLength(1)) As Double
- For j As Integer = 0 To eigen.GetLength(1) - 1
- For i As Integer = 0 To eigen.GetLength(0) - totalkelas - 1
- tmp(i, j) = GlobalCitra(j, i) - eigen(j, i)
- Next
- Next
- GlobalCitra = matrik.Perkalian(matrik.transpose(GlobalCitra), tmp)
- End Sub
- Public Function SumValue(ByVal x() As Double) As Double
- Dim tmp As Double = 0
- For Each t As Double In x
- tmp += t
- Next
- Return tmp
- End Function
- Public Function Ecludiance(ByVal Citrainput(,) As Double, ByVal CitraDatabase(,) As Double) As Double
- Dim tmp(,) As Double = matrik.Pangkatmatriks(matrik.pengurangan(CitraDatabase, Citrainput))
- Dim total As Double = 0
- For i As Integer = 0 To tmp.GetLength(0) - 1
- For j As Integer = 0 To tmp.GetLength(1) - 1
- total += Math.Abs(tmp(i, j))
- Next
- Next
- Return Math.Sqrt(total / (tmp.GetLength(0) - 1))
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement