Advertisement
enos

PCAClass.vb

Jul 25th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.39 KB | None | 0 0
  1. Public Class PCAClass
  2.     Dim GlobalCitra(,) As Double
  3.     Dim matrik As New MatriksClass
  4.     Dim totalkelas As Integer
  5.     Dim THETA As Double = 0.5
  6.     Dim fisherface(,) As Double
  7.     Dim RekonstruksiFisherface As New List(Of Double(,))
  8.  
  9.     Private Function makeoneVektor(ByVal img(,) As Double) As Double(,)
  10.         Dim tmp(img.GetLength(0) - 1, 0) As Double
  11.         For i As Integer = 0 To img.GetLength(0) - 1
  12.             Dim avg As Double = 0
  13.             For j As Integer = 0 To img.GetLength(1) - 1
  14.                 avg += img(i, j)
  15.             Next
  16.  
  17.             Dim fl As String = avg / img.GetLength(1)
  18.             If fl.Contains(".") Then
  19.                 Dim hasil() As String = fl.Split(".")
  20.                 fl = hasil(1)
  21.                 If CDbl(fl.Substring(0, 1)) > 4 Then
  22.                     tmp(i, 0) = CDbl(hasil(0)) + 1
  23.                 Else
  24.                     tmp(i, 0) = CDbl(hasil(0))
  25.  
  26.                 End If
  27.  
  28.             Else
  29.                 tmp(i, 0) = CDbl(fl)
  30.             End If
  31.  
  32.         Next
  33.         Return tmp
  34.     End Function
  35.  
  36.    
  37.     Private Function MergeVektor(ByVal img As List(Of Double(,)), ByVal height As Double) As Double(,)
  38.      
  39.         Dim tmp(height - 1, img.Count - 1) As Double
  40.         For i As Integer = 0 To tmp.GetLength(1) - 1
  41.             Dim nilai As Double(,) = img.Item(i)
  42.             For j As Integer = 0 To tmp.GetLength(0) - 1
  43.                 tmp(j, i) = nilai(j, 0)
  44.             Next
  45.         Next
  46.         Return tmp
  47.     End Function
  48.  
  49.  
  50.     Private Function KonversiCitraDatabasekeGrayscale(ByVal img() As Byte) As Double(,)
  51.         Dim ms As New IO.MemoryStream(img)
  52.         Dim pict As New PictureBox
  53.         pict.Image = Image.FromStream(ms)
  54.         Return gambar.GetGrayScale(pict.Image)
  55.  
  56.     End Function
  57.  
  58.     Sub New()
  59.         Dim daftarGambar As New List(Of List(Of Double(,)))
  60.         Dim UniteGambar As New List(Of Double(,))
  61.  
  62.         Dim koneksi As New SqlClient.SqlConnection(My.Settings.koneksiString)
  63.         koneksi.Open()
  64.         Dim dtGambar As New DataTable
  65.  
  66.         Using da As New SqlClient.SqlDataAdapter("SELECT * From Pengguna", koneksi)
  67.             da.Fill(dtGambar)
  68.         End Using
  69.  
  70.  
  71.         For Each Gambar As DataRow In dtGambar.Rows
  72.             Dim pengguna As New List(Of Double(,))
  73.             pengguna.Add(KonversiCitraDatabasekeGrayscale(CType(Gambar("sample1"), Byte())))
  74.             pengguna.Add(KonversiCitraDatabasekeGrayscale(CType(Gambar("sample2"), Byte())))
  75.             pengguna.Add(KonversiCitraDatabasekeGrayscale(CType(Gambar("sample3"), Byte())))
  76.             daftarGambar.Add(pengguna)
  77.  
  78.             For Each item As Double(,) In pengguna
  79.                 UniteGambar.Add(makeoneVektor(item))
  80.             Next
  81.         Next
  82.  
  83.         'mengubah seluruh citra PCA ke dalam Vektor
  84.         Dim KumpulanPCA(,) As Double = MergeVektor(UniteGambar, UniteGambar.Item(0).GetLength(0))
  85.  
  86.         UniteGambar.Clear()
  87.         'membuat uPCA dari kumpulan citra PCA
  88.         UniteGambar.Add(makeoneVektor(KumpulanPCA))
  89.  
  90.         Dim CitraPCA(,) As Double
  91.         Dim ST(,) As Double
  92.  
  93.         Dim Eigenvalue1(,), Eigenvalue2(,) As Double
  94.  
  95.         'melakukan pengurang setiap citra PCA dengan UPCA
  96.         CitraPCA = KurangVektor(KumpulanPCA, UniteGambar.Item(0))
  97.  
  98.         ST = matrik.Perkalian(CitraPCA, matrik.transpose(CitraPCA))
  99.  
  100.         Dim d1 As Double = SumValue(matrik.eigenvalue(ST))
  101.         Eigenvalue1 = matrik.eigenVektor(ST)
  102.  
  103.  
  104.         Dim V(UniteGambar.Item(0).GetLength(0) - 1, (dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count) As Double
  105.  
  106.  
  107.         For i As Integer = 0 To UniteGambar.Item(0).GetLength(0) - 1
  108.             For j As Integer = 0 To (dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count
  109.                 V(i, j) = Eigenvalue1(i, j)
  110.             Next
  111.  
  112.         Next
  113.  
  114.         Dim WPCA(,) As Double = matrik.Perkalian(matrik.transpose(CitraPCA), V)
  115.  
  116.         Dim inputFLD(,) As Double = matrik.Perkalian(CitraPCA, WPCA)
  117.  
  118.         Dim UFLD(,) As Double = makeoneVektor(inputFLD)
  119.  
  120.         Dim SB As Double = 0
  121.         Dim SW As Double = 0
  122.  
  123.         For Each orang As List(Of Double(,)) In daftarGambar
  124.             Dim rata(,) As Double = makeoneVektor(MergeVektor(orang, orang.Item(0).GetLength(0)))
  125.  
  126.             SB += matrik.Perkalian(matrik.Perkalian(3, matrik.transpose(matrik.pengurangan(rata, UFLD))), matrik.pengurangan(rata, UFLD))(0, 0)
  127.  
  128.             For Each gb(,) As Double In orang
  129.                 SW += matrik.Perkalian(matrik.transpose(matrik.pengurangan(makeoneVektor(gb), rata)), matrik.pengurangan(makeoneVektor(gb), rata))(0, 0)
  130.             Next
  131.         Next
  132.  
  133.         Dim rasio = SB / SW
  134.  
  135.  
  136.         ''membuat dimensi bari sebesar ukuran citra awal (5x5)
  137.  
  138.         Dim matRatio(KumpulanPCA.GetLength(0) - 1, KumpulanPCA.GetLength(0) - 1) As Double
  139.  
  140.         For i As Double = 0 To KumpulanPCA.GetLength(0) - 1
  141.             For j As Double = 0 To KumpulanPCA.GetLength(0) - 1
  142.                 matRatio(i, j) = rasio
  143.             Next
  144.         Next
  145.  
  146.         Dim d2 As Double = SumValue(matrik.eigenvalue(matRatio))
  147.         Eigenvalue2 = matrik.eigenVektor(matRatio)
  148.  
  149.         '' lalu lakukan pemotongan terhadap baris sebanyak kelas
  150.  
  151.         ''hasil akhir setelah pemotongan dimensi didapatkan dimensi Z(5x4) maka
  152.         ''dibuat dimensi baru dengan pengurangan baris dari N-C (6-2) = 4
  153.         '' Dimensi awal citra adalah Z(5 x 4 ) maka berubah menjadi Z(4,4)
  154.  
  155.         Dim VM((UniteGambar.Item(0).GetLength(0) - 1) - dtGambar.Rows.Count, (dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count) As Double
  156.  
  157.         For i As Integer = 0 To ((UniteGambar.Item(0).GetLength(0) - 1) - dtGambar.Rows.Count) - 1
  158.             For j As Integer = 0 To ((dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count) - 1
  159.                 VM(i, j) = Eigenvalue2(i, j)
  160.             Next
  161.         Next
  162.  
  163.         '' mencari nilai yang lebih kecil dari Theta
  164.  
  165.         If d1 / d2 > theta Then
  166.             'maka untuk reduksi nanatinya diambil nilai m
  167.             Dim m As Double = CInt(d1 / d2) Mod 100
  168.             ReDim VM((dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count, m - 1)
  169.             For i As Integer = 0 To (dtGambar.Rows.Count * 3 - 1) - dtGambar.Rows.Count
  170.                 For j As Integer = 0 To m - 1
  171.                     VM(i, j) = Eigenvalue2(i, j)
  172.                 Next
  173.             Next
  174.         End If
  175.         Dim WFLD(,) = VM
  176.         fisherface = matrik.Perkalian(inputFLD, WFLD)
  177.         AvarageFisherFace(daftarGambar)
  178.     End Sub
  179.  
  180.     Private Sub AvarageFisherFace(ByVal daftar As List(Of List(Of Double(,))))
  181.         For Each orang As List(Of Double(,)) In daftar
  182.             For Each gbr(,) As Double In orang
  183.                 RekonstruksiFisherface.Add(pengurangan(gbr, fisherface))
  184.             Next
  185.         Next
  186.     End Sub
  187.  
  188.  
  189.     Public ReadOnly Property Rekonstruksi() As List(Of Double(,))
  190.         Get
  191.             Return RekonstruksiFisherface
  192.         End Get
  193.     End Property
  194.  
  195.     Public Function TestingInput(ByVal img(,) As Double) As Double(,)
  196.         Return pengurangan(img, fisherface)
  197.     End Function
  198.  
  199.     Public Function pengurangan(ByVal matriksA(,) As Double, ByVal matriksB(,) As Double) As Double(,)
  200.         ReDim Preserve matriksB(matriksA.GetLength(1) - 1, matriksA.GetLength(0) - 1)
  201.         Dim width As Integer = matriksB.GetLength(1) - 1
  202.         Dim height As Integer = matriksB.GetLength(0) - 1
  203.         Dim tmp(height, width) As Double
  204.         '        Dim cmp As Double
  205.  
  206.         For Posisi_x As Integer = 0 To height
  207.             For Posisi_y As Integer = 0 To width
  208.                 tmp(Posisi_x, Posisi_y) = matriksA(Posisi_x, Posisi_y) - matriksB(Posisi_x, Posisi_y)
  209.             Next
  210.         Next
  211.         Return tmp
  212.     End Function
  213.  
  214.  
  215.     Private Function KurangVektor(ByVal sample(,) As Double, ByVal koef(,) As Double) As Double(,)
  216.         For i As Integer = 0 To sample.GetLength(1) - 1
  217.             For j As Integer = 0 To sample.GetLength(0) - 1
  218.                 sample(j, i) = sample(j, i) - koef(j, 0)
  219.             Next
  220.         Next
  221.         Return sample
  222.     End Function
  223.  
  224.  
  225.  
  226.     Public Sub MakeST()
  227.         'Buat Rerata Citra
  228.         GlobalCitra = KurangVektor(GlobalCitra, makeoneVektor(GlobalCitra))
  229.         Dim ST(,) As Double = matrik.Perkalian(matrik.transpose(GlobalCitra), GlobalCitra)
  230.         Dim eigen(,) As Double = matrik.eigenVektor(ST)
  231.  
  232.         Dim tmp(eigen.GetLength(0) - totalkelas - 1, eigen.GetLength(1)) As Double
  233.         For j As Integer = 0 To eigen.GetLength(1) - 1
  234.             For i As Integer = 0 To eigen.GetLength(0) - totalkelas - 1
  235.                 tmp(i, j) = GlobalCitra(j, i) - eigen(j, i)
  236.             Next
  237.         Next
  238.         GlobalCitra = matrik.Perkalian(matrik.transpose(GlobalCitra), tmp)
  239.     End Sub
  240.  
  241.  
  242.     Public Function SumValue(ByVal x() As Double) As Double
  243.         Dim tmp As Double = 0
  244.         For Each t As Double In x
  245.             tmp += t
  246.         Next
  247.         Return tmp
  248.     End Function
  249.  
  250.     Public Function Ecludiance(ByVal Citrainput(,) As Double, ByVal CitraDatabase(,) As Double) As Double
  251.         Dim tmp(,) As Double = matrik.Pangkatmatriks(matrik.pengurangan(CitraDatabase, Citrainput))
  252.         Dim total As Double = 0
  253.         For i As Integer = 0 To tmp.GetLength(0) - 1
  254.             For j As Integer = 0 To tmp.GetLength(1) - 1
  255.                 total += Math.Abs(tmp(i, j))
  256.             Next
  257.         Next
  258.         Return Math.Sqrt(total / (tmp.GetLength(0) - 1))
  259.     End Function
  260.  End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement