LAMPIRAN A : LISTING PROGRAM

  

LAMPIRAN A : LISTING PROGRAM

  using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Eigenfaces.Kelas { class EigenValueDecompositionSymm { virtual public double[] EigenValues { get { return (eigenValues); } } virtual public double[][] EigenVectors { get { return (eigenVectors); } } private double[] eigenValues; private double[][] eigenVectors; public EigenValueDecompositionSymm(double[][] matrix) { int m = Matrix.getNumOfRows(matrix); int n = Matrix.getNumOfColumns(matrix); eigenVectors = Matrix.clone(matrix); eigenValues = Vector.newVector(m); double[] accu = Vector.newVector(m); for (int i = 0; i < m; i++) { eigenValues[i] = eigenVectors[m - 1][i]; } for (int i = m - 1; i > 0; i--) { double sc = 0.0; double h1 = 0.0; for (int j = 0; j < i; j++) { sc = sc + System.Math.Abs(eigenValues[j]); } if (sc == 0.0) { accu[i] = eigenValues[i - 1]; for (int j = 0; j < i; j++) { eigenValues[j] = eigenVectors[i - 1][j]; eigenVectors[i][j] = 0.0; eigenVectors[j][i] = 0.0; } } else { for (int j = 0; j < i; j++) { double evj = eigenValues[j] / sc; eigenValues[j] = evj; h1 += evj * evj; } double f = eigenValues[i - 1]; double g = System.Math.Sqrt(h1); if (f > 0) { g = -g; } accu[i] = sc * g; h1 = h1 - f * g; eigenValues[i - 1] = f - g; for (int j = 0; j < i; j++) { accu[j] = 0.0; } for (int j = 0; j < i; j++) { f = eigenValues[j]; eigenVectors[j][i] = f; g = accu[j] + eigenVectors[j][j] * f; for (int k = j + 1; k <= i - 1; k++) { g += eigenVectors[k][j] * eigenValues[k]; accu[k] += eigenVectors[k][j] * f; } accu[j] = g; } f = 0.0; for (int j = 0; j < i; j++) { accu[j] /= h1; f += accu[j] * eigenValues[j]; } double h2 = f / (h1 + h1); for (int j = 0; j < i; j++) { accu[j] -= h2 * eigenValues[j]; } for (int j = 0; j < i; j++) { f = eigenValues[j]; g = accu[j]; for (int k = j; k <= i - 1; k++) { eigenVectors[k][j] -= (f * accu[k] + g * eigenValues[k]); } eigenValues[j] = eigenVectors[i - 1][j]; eigenVectors[i][j] = 0.0; } } eigenValues[i] = h1;

  } for (int i = 0; i < m - 1; i++) { eigenVectors[m - 1][i] = eigenVectors[i][i]; eigenVectors[i][i] = 1.0; double h = eigenValues[i + 1]; if (h != 0.0) { for (int j = 0; j <= i; j++) { eigenValues[j] = eigenVectors[j][i + 1] / h; } for (int j = 0; j <= i; j++) { double g = 0.0; for (int k = 0; k <= i; k++) { g += eigenVectors[k][i + 1] * eigenVectors[k][j]; } for (int k = 0; k <= i; k++) { eigenVectors[k][j] -= g * eigenValues[k]; } } } for (int j = 0; j <= i; j++) { eigenVectors[j][i + 1] = 0.0; } } for (int i = 0; i < m; i++) { eigenValues[i] = eigenVectors[m - 1][i]; eigenVectors[m - 1][i] = 0.0; } eigenVectors[m - 1][m - 1] = 1.0; accu[0] = 0.0; double a = 0.0; double piv = 0.0; double eps = 1.0e-16; for (int i = 1; i < n; i++) { accu[i - 1] = accu[i]; } accu[n - 1] = 0.0; for (int i = 0; i < n; i++) { piv = System.Math.Max(piv, System.Math.Abs(eigenValues[i]) + System.Math.Abs(accu[i])); int l = i; while (l < n) { if (System.Math.Abs(accu[l]) <= eps * piv) { break; } l++; } if (l > i)

  { int iter = 0; do { iter = iter + 1; double f = eigenValues[i]; double g = (eigenValues[i + 1] - f) / (2.0 * accu[i]); double r = dist(g, 1.0); if (g < 0) { r = -r; } eigenValues[i] = accu[i] / (g + r); eigenValues[i + 1] = accu[i] * (g + r); double ev1 = eigenValues[i + 1]; double h = f - eigenValues[i]; for (int j = i + 2; j < n; j++) { eigenValues[j] -= h; } a = a + h; g = eigenValues[l]; double ac1 = accu[i + 1]; double b1 = 1.0; double b2 = 1.0; double b3 = 1.0; double s1 = 0.0; double s2 = 0.0; for (int j = l - 1; j >= i; j--) { b3 = b2; b2 = b1; s2 = s1; f = b1 * accu[j]; h = b1 * g; r = dist(g, accu[j]); accu[j + 1] = s1 * r; s1 = accu[j] / r; b1 = g / r; g = b1 * eigenValues[j] - s1 * f; eigenValues[j + 1] = h + s1 * (b1 * f + s1 * eigenValues[j]); for (int k = 0; k < n; k++) { h = eigenVectors[k][j + 1]; eigenVectors[k][j + 1] = s1 * eigenVectors[k][j] + b1 * h; eigenVectors[k][j] = b1 * eigenVectors[k][j] - s1 * h; } } g = (-s1) * s2 * b3 * ac1 * accu[i] / ev1; accu[i] = s1 * g; eigenValues[i] = b1 * g; } while (System.Math.Abs(accu[i]) > eps * piv); } eigenValues[i] = eigenValues[i] + a; accu[i] = 0.0; } } private static double dist(double a, double b) { double r; if (System.Math.Abs(a) > System.Math.Abs(b)) { r = b / a; return (System.Math.Abs(a) * System.Math.Sqrt(1 + r * r)); } else if (b != 0) { r = a / b; return (System.Math.Abs(b) * System.Math.Sqrt(1 + r * r)); } else { return (0.0); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Eigenfaces { class EuclideanDistance { public int dMax(int[] nilai1, int[] nilai2) { int d = 0; int dmax = 0; int temp = 0; for (int i = 0; i < nilai1.Length; i++) { d = (int)Math.Pow((nilai1[i] - nilai2[i]), 2); double c = Math.Sqrt(d); if (c > dmax) { dmax = (int)c; } temp += d; } return (int)Math.Sqrt(temp); } public int dMin(double[] nilai1, double[] nilai2) { int d = 0; int dmin = 0; int temp = 0; for (int i = 0; i < nilai1.Length; i++) { d = (int)Math.Pow((nilai1[i] - nilai2[i]), 2); if (i==0) { dmin = d; } if (d < dmin) { dmin = d; } temp += d; } return dmin; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace Eigenfaces.Kelas { class Koneksi { SqlConnection con; public bool test() { try { con = new SqlConnection("Data Source=COMPAQ510\\SQLEXPRESS; Initial Catalog=EIGEN; Integrated Security=SSPI;"); con.Open(); } catch { return false; } return true; } public SqlConnection getCon() { return con; } public SqlConnection tutupKoneksi() { con.Close(); return con; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Eigenfaces.Kelas { class PCA { virtual public double[] EigenValues { get { return (eigenValues); } } virtual public double[][] EigenVectors { get { return (eigenVectors); } } virtual public double[] MeanValues { get { return (meanValues); } } virtual public double[][] VectorsZeroMean { get { return (vectorsZeroMean); } } virtual public double[][] ResultingVectors { get { return (resVectors); } } private double[] meanValues; private double[][] vectorsZeroMean; private double[][] covarianceMatrix; private double[] eigenValues; private double[][] eigenVectors; private double[][] resVectors;

public PCA(double[][] inVectors) { meanValues = calcMeanValues(inVectors); vectorsZeroMean = Vector.addVecToSet(inVectors, Vector.scale(-1.0, meanValues)); covarianceMatrix = Matrix.scale(Matrix.square(vectorsZeroMean), 1.0 / Matrix.getNumOfColumns(inVectors)); EigenValueDecompositionSymm eigenDeco = new EigenValueDecompositionSymm(covarianceMatrix); eigenVectors = eigenDeco.EigenVectors; eigenValues = eigenDeco.EigenValues; resVectors = Matrix.mult(Matrix.transpose(eigenVectors), vectorsZeroMean); } private static double[] calcMeanValues(double[][] inVectors) { int m = Matrix.getNumOfRows(inVectors); int n = Matrix.getNumOfColumns(inVectors); double[] meanValues = Vector.newVector(m); for (int i = 0; i < m; ++i) { meanValues[i] = 0.0; for (int j = 0; j < n; ++j) { meanValues[i] += inVectors[i][j]; } meanValues[i] /= n; } return (meanValues); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Eigenfaces.Kelas { class PCA { virtual public double[] EigenValues { get { return (eigenValues); } } virtual public double[][] EigenVectors { get { return (eigenVectors); } } virtual public double[] MeanValues { get { return (meanValues); } } virtual public double[][] VectorsZeroMean { get { return (vectorsZeroMean); } } virtual public double[][] ResultingVectors { get { return (resVectors); } } private double[] meanValues; private double[][] vectorsZeroMean; private double[][] covarianceMatrix; private double[] eigenValues; private double[][] eigenVectors; private double[][] resVectors;

public PCA(double[][] inVectors) { meanValues = calcMeanValues(inVectors); vectorsZeroMean = Vector.addVecToSet(inVectors, Vector.scale(-1.0, meanValues)); covarianceMatrix = Matrix.scale(Matrix.square(vectorsZeroMean), 1.0 / Matrix.getNumOfColumns(inVectors)); EigenValueDecompositionSymm eigenDeco = new EigenValueDecompositionSymm(covarianceMatrix); eigenVectors = eigenDeco.EigenVectors; eigenValues = eigenDeco.EigenValues; resVectors = Matrix.mult(Matrix.transpose(eigenVectors), vectorsZeroMean); } private static double[] calcMeanValues(double[][] inVectors) { int m = Matrix.getNumOfRows(inVectors); int n = Matrix.getNumOfColumns(inVectors); double[] meanValues = Vector.newVector(m); for (int i = 0; i < m; ++i) { meanValues[i] = 0.0; for (int j = 0; j < n; ++j) { meanValues[i] += inVectors[i][j]; } meanValues[i] /= n; } return (meanValues); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace Eigenfaces.Kelas { class Preprocessing { public Bitmap grayScale(Bitmap bmp) { Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height); for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { Double c = (bmp.GetPixel(x, y).R * 0.21) + (bmp.GetPixel(x, y).G * 0.71) + (bmp.GetPixel(x, y).B * 0.07); bmp2.SetPixel(x, y, Color.FromArgb((int)c, (int)c, (int)c)); } } return bmp2; } public Bitmap resampling(Bitmap bmp, int resolusi) { int w = bmp.Width; int h = bmp.Height; Bitmap bmp2 = new Bitmap(w, h); int w1 = resolusi; int h1 = resolusi; double sh = (double)w1 / (double)w; double sv = (double)h1 / (double)h; for (int x1 = 0; x1 < w1; x1++) { for (int y1 = 0; y1 < h1; y1++) { double xAsal = x1 / sh; double yAsal = y1 / sv; if (Math.Floor(xAsal) < 0 || Math.Ceiling(xAsal) > w - 1 || Math.Floor(yAsal) < 0 || Math.Ceiling(yAsal) > h - 1) { bmp2.SetPixel(x1, y1, Color.FromArgb(255, 255, 255)); } else { int xL = (int)Math.Floor(xAsal); int xR = (int)Math.Ceiling(xAsal); int yT = (int)Math.Floor(yAsal); int yB = (int)Math.Ceiling(yAsal); int wxL = xR - (int)xAsal; int wyT = yB - (int)yAsal; double hasil = (wxL * wyT * bmp.GetPixel(xL, yT).R

  • (1 - wxL) * wyT * bmp.GetPixel(xR, yT).R
  • wxL * (1 - wyT) * bmp.GetPixel(xL, yB).R
  • (1 - wxL) * (1 - wyT) * bmp.GetPixel(xR, yB).R); if (hasil > 255) { hasil = 255; } bmp2.SetPixel(x1, y1, Color.FromArgb((int)hasil, (int)hasil, (int)hasil)); } }

  } return bmp2; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Eigenfaces.Kelas { class Vector { public static bool equals(double[] vec1, double[] vec2) { if (vec1.Length != vec2.Length) { return (false); } for (int i = 0; i < vec1.Length; ++i) { if (vec1[i] != vec2[i]) { return (false); } } return (true); } private static System.String fillString(System.String in_Renamed, int len) { System.String out_Renamed = new System.Text.StringBuilder(in_Renamed).ToString(); while (out_Renamed.Length < len) { out_Renamed = " " + out_Renamed; } return (out_Renamed); } public static System.String toString(double[] vector) { System.String result = ""; for (int i = 0; i < vector.Length; ++i) { result += (fillString(vector[i].ToString(), 24) + "\n"); } return (result); } public static double[] newVector(int m) { return (new double[m]); } public static double[] newVector(int m, double val) { double[] res = new double[m]; for (int i = 0; i < m; ++i) { res[i] = val; } return (res); }

public static double[] scale(double fac, double[] vector) { int n = vector.Length; double[] res = new double[n]; for (int i = 0; i < n; ++i) { res[i] = fac * vector[i]; } return (res); } public static double dot(double[] vec1, double[] vec2) { int n = vec1.Length; double res = 0.0; for (int i = 0; i < n; ++i) { res += vec1[i] * vec2[i]; } return (res); } public static double[] add(double[] vec1, double[] vec2) { int m = vec1.Length; double[] res = new double[m]; for (int i = 0; i < m; ++i) { res[i] = vec1[i] + vec2[i]; } return (res); } public static double[] sub(double[] vec1, double[] vec2) { int m = vec1.Length; double[] res = new double[m]; for (int i = 0; i < m; ++i) { res[i] = vec1[i] - vec2[i]; } return (res); } public static double[] clone(double[] vector) { int m = vector.Length; double[] res = new double[m]; for (int i = 0; i < m; ++i) { res[i] = vector[i]; } return (res); } public static double[] random(int m) { double[] res = new double[m]; for (int i = 0; i < m; ++i) {

  //res[i] = SupportClass.Random.NextDouble(); } return (res); } public static double[][] addVecToSet(double[][] vecSet, double[] addVec) { int m = Matrix.getNumOfRows(vecSet); int n = Matrix.getNumOfColumns(vecSet); double[][] res = Matrix.newMatrix(m, n); for (int i = 0; i < m; ++i) { double add = addVec[i]; for (int j = 0; j < n; ++j) { res[i][j] = vecSet[i][j] + add; } } return (res); } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Emgu.CV.Util; using Emgu.CV.CvEnum; using Emgu.CV.Structure; using Emgu.CV; using Emgu.Util; using AForge.Video; using Accord.Vision.Detection; using Accord.Vision.Detection.Cascades; using System.Runtime.InteropServices; using System.Windows.Threading; using System.Data.SqlClient; using System.IO; using System.Collections; namespace Eigenfaces { public partial class Pengenalan2 : Form { //Inisialisasi Variabel Kamera public Capture capture; DispatcherTimer timer; Image<Bgr, Byte> imgFrame; Bitmap gambar; public Pengenalan2() { InitializeComponent(); } void timer_Tick(object sender, EventArgs e) { imgFrame = capture.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC); pictureBox1.Image = imgFrame.ToBitmap(); HaarObjectDetector detector; FaceHaarCascade cascade = new FaceHaarCascade(); detector = new HaarObjectDetector(cascade, 30); detector.SearchMode = ObjectDetectorSearchMode.Average; detector.ScalingFactor = 1.5f; detector.ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller; detector.UseParallelProcessing = true; detector.Suppression = 3; Rectangle[] faceObjects = detector.ProcessFrame((Bitmap)pictureBox1.Image); Graphics g = Graphics.FromImage(pictureBox1.Image); Pen pen = new Pen(Brushes.Black, 10); foreach (System.Drawing.Rectangle face in faceObjects) { Bitmap cropImage = new Bitmap(face.Width, face.Height); using (Graphics grp = Graphics.FromImage(cropImage)) { grp.DrawImage(pictureBox1.Image, new Rectangle(0, 0, face.Width, face.Height), face, GraphicsUnit.Pixel); gambar = (Bitmap)cropImage;

  } g.DrawRectangle(Pens.Red, face); } g.Dispose(); this.pictureBox1.Invalidate(); } private void bersih() { txtNama.Clear(); txtAlamat.Clear(); txtNoTelp.Clear(); pictureBox1.Image = null; pictureBox3.Image = null; } private void button1_Click(object sender, EventArgs e) { if (pictureBox1.Image == null || pictureBox2.Image == null) { MessageBox.Show("Gambar belum dimasukkan!"); } else { Kelas.Koneksi koneksi = new Kelas.Koneksi(); if (koneksi.test()) { SqlConnection con1 = koneksi.getCon(); SqlCommand cmd1 = new SqlCommand(); double[][] inVectors = new double[1][]; int z = 0; Kelas.Preprocessing preproses = new Kelas.Preprocessing(); Bitmap gambars = (Bitmap)pictureBox2.Image; gambars = preproses.grayScale(gambars); //gambars = preproses.resampling(gambars, 120); inVectors[0] = new double[gambars.Height * gambars.Width]; for (int y = 0; y < gambars.Height; y++) { for (int x = 0; x < gambars.Width; x++) { inVectors[0][z] = gambars.GetPixel(x, y).R; z++; } } Kelas.PCA eigen = new Kelas.PCA(inVectors); EuclideanDistance euclideanD = new EuclideanDistance(); double[] eigenValues = eigen.EigenValues; double[] eigenValues2 = new double[1]; ArrayList nama = new ArrayList(); ArrayList alamat = new ArrayList(); ArrayList noTelp = new ArrayList(); ArrayList foto = new ArrayList(); ArrayList nilai = new ArrayList(); cmd1.Connection = con1; cmd1.CommandText = "Select * from datatrain"; SqlDataReader dataReader = cmd1.ExecuteReader(); while (dataReader.Read()) { eigenValues2[0] = Convert.ToDouble(dataReader[4]); nama.Add((String)dataReader[0]); alamat.Add((String)dataReader[1]); noTelp.Add((String)dataReader[2]); foto.Add((Byte[])dataReader[5]); nilai.Add(euclideanD.dMin(eigenValues, eigenValues2)); } dataReader.Close(); int penanda = 100000; int indekss = 0; for (int i = 0; i < nama.Count; i++) { //if ((int)nilai[i] <= 500) //{ if ((int)nilai[i] < penanda) { penanda = (int)nilai[i]; indekss = i; } //} } if (penanda != 100000) { txtNama.Text = nama[indekss].ToString(); txtAlamat.Text = alamat[indekss].ToString(); txtNoTelp.Text = noTelp[indekss].ToString(); byte[] gambar2 = (byte[])(foto[indekss]); if (gambar2 == null) pictureBox2.Image = null; else { MemoryStream ms = new MemoryStream(gambar2); pictureBox3.Image = Image.FromStream(ms); pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox2.Image = null; } } if (txtNama.Text == "") { MessageBox.Show("Data tidak ditemukan!"); bersih(); } } } } private void btnAmbilGambar_Click(object sender, EventArgs e) { pictureBox2.Image = gambar; bersih(); } private void Pengenalan2_Load_1(object sender, EventArgs e) { capture = new Capture(); timer = new DispatcherTimer(); timer.Tick += new EventHandler(timer_Tick); timer.Interval = new TimeSpan(0, 0, 0, 0, 1); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage; timer.Start(); } private void Form_closed(object sender, EventArgs e) { timer.Stop(); capture.Stop(); capture.Dispose(); } } }

  Percobaan pertama Percobaan ke 2

  Percobaan ke 3 (berhasil) Percobaan ke 4(dengan user ke 2)

  Percobaan ke lima PERCOBAAN ke enam:

  Percobaan ke7 (berhasil) Percobaan ke 8

  Percobqqn ke 9 Percobaan ke 10 (berhasil)

  Struktur Database Table DATATRAIN Isi table Database Table DATATRAIN using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Emgu.CV.Util; using Emgu.CV.CvEnum; using Emgu.CV.Structure; using Emgu.CV; using Emgu.Util; using AForge.Video; using Accord.Vision.Detection; using Accord.Vision.Detection.Cascades; using System.Runtime.InteropServices; using System.Windows.Threading; using System.Data.SqlClient; using System.IO; namespace Eigenfaces { public partial class Training2 : Form { //Inisialisasi Variabel Kamera public Capture capture; DispatcherTimer timer; Image<Bgr, Byte> imgFrame; Bitmap gambar; public Training2() { InitializeComponent(); } void timer_Tick(object sender, EventArgs e) { imgFrame = capture.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC); pictureBox1.Image = imgFrame.ToBitmap(); HaarObjectDetector detector; FaceHaarCascade cascade = new FaceHaarCascade(); detector = new HaarObjectDetector(cascade, 30); detector.SearchMode = ObjectDetectorSearchMode.Average; detector.ScalingFactor = 1.5f; detector.ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller; detector.UseParallelProcessing = true; detector.Suppression = 3; Rectangle[] faceObjects = detector.ProcessFrame((Bitmap)pictureBox1.Image); Graphics g = Graphics.FromImage(pictureBox1.Image); Pen pen = new Pen(Brushes.Black, 10); foreach (System.Drawing.Rectangle face in faceObjects) { Bitmap cropImage = new Bitmap(face.Width, face.Height); using (Graphics grp = Graphics.FromImage(cropImage)) { grp.DrawImage(pictureBox1.Image, new Rectangle(0, 0, face.Width, face.Height), face, GraphicsUnit.Pixel);

gambar = (Bitmap)cropImage; } g.DrawRectangle(Pens.Red, face); } g.Dispose(); this.pictureBox1.Invalidate(); } private void Training2_Load(object sender, EventArgs e) { capture = new Capture(); timer = new DispatcherTimer(); timer.Tick += new EventHandler(timer_Tick); timer.Interval = new TimeSpan(0, 0, 0, 0, 1); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; timer.Start(); } private void training_closed(object sender, EventArgs e) { timer.Stop(); capture.Stop(); capture.Dispose(); } private void btnAmbilGambar_Click(object sender, EventArgs e) { pictureBox2.Image = gambar; } private void bersih() { txtNama.Clear(); txtAlamat.Clear(); txtNoTelp.Clear(); } private void btnTrain_Click(object sender, EventArgs e) { DialogResult hasil = MessageBox.Show("Apakah anda yakin? ", "Jawab.", MessageBoxButtons.YesNo); if (hasil == DialogResult.Yes) { Kelas.Koneksi koneksi = new Kelas.Koneksi(); if (koneksi.test()) { if (txtNama.Text != "" || txtAlamat.Text != "" || txtNoTelp.Text != "" || pictureBox2.Image == null) { capture.Pause(); SqlConnection con1 = koneksi.getCon(); SqlCommand cmd1 = new SqlCommand(); //Bentuk inVector dari semua image di dalam folder double[][] inVectors = new double[1][]; int z = 0; Kelas.Preprocessing preproses = new Kelas.Preprocessing(); Bitmap gambar2 = (Bitmap)pictureBox2.Image; gambar2 = preproses.grayScale(gambar2); //gambar2 = preproses.resampling(gambar2, 120); inVectors[0] = new double[gambar2.Height * gambar2.Width]; for (int y = 0; y < gambar2.Height; y++) {

for (int x = 0; x < gambar2.Width; x++) { inVectors[0][z] = gambar2.GetPixel(x, y).R; z++; } } Kelas.PCA eigen = new Kelas.PCA(inVectors); double[] eigenValues = eigen.EigenValues; cmd1.Connection = con1; //buat string fleksibel untuk query ke SQL String temp2 = eigenValues[0].ToString(); //ubah gambar menjadi byte agar bisa disimpan kedalam database gambar2.Save("temp.bmp"); FileStream fs = new FileStream("temp.bmp", FileMode.Open, FileAccess.Read); Byte[] picByte = new Byte[fs.Length]; fs.Read(picByte, 0, Convert.ToInt32(fs.Length)); fs.Close(); //buat string sql cmd1.CommandText = "INSERT INTO DATATRAIN values('" + txtNama.Text + "','" + txtAlamat.Text + "','" + txtNoTelp.Text + "','" + "temp.bmp" + "','" + temp2 + "', @pic)"; SqlParameter picParameter = new SqlParameter(); picParameter.SqlDbType = SqlDbType.Image; picParameter.ParameterName = "pic"; picParameter.Value = picByte; cmd1.Parameters.Add(picParameter); cmd1.ExecuteNonQuery(); MessageBox.Show("Berhasil!"); bersih(); capture.Start(); } } } } } }