private void MainForm_Load(object sender, EventArgs e) { mainBmp = new Bitmap(Image.FromFile("./plane.bmp")); pb_main.Image = mainBmp; double[,] img = ImageTool.GetGreyScale(mainBmp); ImageTool.SetImage(mainBmp, img); int libCount = LoadLibrary("./imgLib", mainBmp.Width, mainBmp.Height, FACES_PER_PERSON); avg = ImageTool.GetAvg(lib); difLib = ImageTool.GetDifferenceArray(lib, avg); sb_lib.Maximum = libCount; libBmp = new Bitmap(mainBmp.Width, mainBmp.Height); EigenObject eigVects = ImageTool.GetEigen(ImageTool.GetA(lib)); ImageTool.normalize(eigVects.vectors); eigFaces = ImageTool.getEigenFaces(eigVects.vectors, difLib); libWeights = new double[lib.Length][]; for (int i = 0; i < lib.Length; i++) { libWeights[i] = ImageTool.getWeights(eigFaces, lib[i], avg); } double[] weights = ImageTool.getWeights(eigFaces, img, avg); comp = ImageTool.compareWeigths(libWeights, weights); int p = ImageTool.smallestVal(comp); lb_person.Text = "Person: " + p; recon = ImageTool.reconstruct(weights, eigFaces, avg); ImageTool.normalize(recon, 255); pb_lib.Image = libBmp; ImageTool.SetImage(libBmp, lib[p]); sb_lib.Value = p; }
public static double[,] reconstruct(double[] weights, double[][,] eigFaces, double[,] avg) { int length = eigFaces[0].GetLength(0); double[,] result = new double[length, length]; //normalize(weights); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { for (int k = 0; k < eigFaces.Length; k++) { result[i, j] += eigFaces[k][i, j] * weights[k] / eigFaces.Length; } } } ImageTool.normalize(result, 128); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { result[i, j] += avg[i, j]; } } return(result); }
public FaceRecog() { mainBmp = new Bitmap(Image.FromFile("./plane.bmp")); //Would be picturebox of compared image. //pb_main.Image = mainBmp; double[,] img = ImageTool.GetGreyScale(mainBmp); ImageTool.SetImage(mainBmp, img); int libCount = LoadLibrary("./imgLib", mainBmp.Width, mainBmp.Height, FACES_PER_PERSON); avg = ImageTool.GetAvg(lib); difLib = ImageTool.GetDifferenceArray(lib, avg); //Would be scrollbar //sb_lib.Maximum = libCount; libBmp = new Bitmap(mainBmp.Width, mainBmp.Height); EigenObject eigVects = ImageTool.GetEigen(ImageTool.GetA(lib)); ImageTool.normalize(eigVects.vectors); eigFaces = ImageTool.getEigenFaces(eigVects.vectors, difLib); libWeights = new double[lib.Length][]; for (int i = 0; i < lib.Length; i++) { libWeights[i] = ImageTool.getWeights(eigFaces, lib[i], avg); } double[] weights = ImageTool.getWeights(eigFaces, img, avg); comp = ImageTool.compareWeigths(libWeights, weights); int p = ImageTool.smallestVal(comp); //Would be guess of face //lb_person.Text = "Person: " + p; recon = ImageTool.reconstruct(weights, eigFaces, avg); ImageTool.normalize(recon, 255); //would be displaying the image. //pb_lib.Image = libBmp; ImageTool.SetImage(libBmp, lib[p]); //Would be setting scrollbar to correct position //sb_lib.Value = p; }
private void sb_lib_Scroll(object sender, ScrollEventArgs e) { switch (display) { case DIFFERENCE: ImageTool.SetImage(libBmp, difLib[e.NewValue], true); break; case EIGEN: ImageTool.SetImage(libBmp, eigFaces[e.NewValue], true); break; case REGULAR: default: ImageTool.SetImage(libBmp, lib[e.NewValue]); break; } pb_lib.Image = libBmp; lb_distance.Text = "Distance : " + comp[e.NewValue]; }
private int LoadLibrary(string directory, int width, int height, int subSet) { string[] images = Directory.GetFiles(@directory, "*.jpg"); if (subSet < 1) { subSet = 1; } lib = new double[images.Length][, ]; int i = 0; foreach (string image in images) { lib[i++] = ImageTool.GetArray(new Bitmap(image)); } if (subSet > 1) { lib = ImageTool.avgSubsets(lib, subSet); } return(images.Length / subSet); }
private void bt_load_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Jpeg Files (.jpg)|*.jpg|Bitmap Files (.bmp)|*.bmp|All Files (*.*)|*.*"; ofd.FilterIndex = 1; ofd.Multiselect = false; if (ofd.ShowDialog() == DialogResult.OK) { mainBmp = new Bitmap(Image.FromFile(ofd.FileName)); pb_main.Image = mainBmp; double[,] img = ImageTool.GetGreyScale(mainBmp); ImageTool.SetImage(mainBmp, img); double[] weights = ImageTool.getWeights(eigFaces, img, avg); comp = ImageTool.compareWeigths(libWeights, weights); int p = ImageTool.smallestVal(comp); if (comp[p] > SAME_FACE_THRESH) { lb_person.Text = "Person: Unknown"; } else { lb_person.Text = "Person: " + p; } recon = ImageTool.reconstruct(weights, eigFaces, avg); pb_lib.Image = libBmp; ImageTool.SetImage(libBmp, lib[p]); sb_lib.Value = p; lb_distance.Text = "Distance : " + comp[p]; faceSpace = ImageTool.difference(img, recon); lb_faceSpace.Text = "Face Space : " + faceSpace; if (faceSpace > FACE_THRESH) { lb_faceSpace.Text += "\nNot a face"; } } }
public string loadFace(Bitmap mainBmp) { String output; //display image //pb_main.Image = mainBmp; double[,] img = ImageTool.GetGreyScale(mainBmp); ImageTool.SetImage(mainBmp, img); double[] weights = ImageTool.getWeights(eigFaces, img, avg); comp = ImageTool.compareWeigths(libWeights, weights); int p = ImageTool.smallestVal(comp); Debug.WriteLine(comp[p] + "-----------"); if (comp[p] > SAME_FACE_THRESH) { //lb_person.Text = "Person: Unknown"; output = "Person: Unknown"; } else { //lb_person.Text = "Person: " + p; output = "Person: " + p; ImageTool.SetImage(currentFace, lib[p]); } recon = ImageTool.reconstruct(weights, eigFaces, avg); //pb_lib.Image = libBmp; ImageTool.SetImage(libBmp, lib[p]); //ImageTool.SetImage(currentFace, lib[p]); //sb_lib.Value = p; //lb_distance.Text = "Distance : " + comp[p]; faceSpace = ImageTool.difference(img, recon); //lb_faceSpace.Text = "Face Space : " + faceSpace; if (faceSpace > FACE_THRESH) { //lb_faceSpace.Text += "\nNot a face"; output = "Not a face"; } if (faceSpace < 11500) { Debug.WriteLine("--------------------" + candidateAmount); // candidateFace = ImageFunctions.combineImages((Bitmap)candidateFace.Clone(), mainBmp, candidateAmount); for (int i = 0; i < candidates.Length; i++) { if (candidates[i] == null) { candidates[i] = mainBmp; candidateScores[i] = faceSpace; candidateFace = mainBmp; } else { if (faceSpace < candidateScores[i]) { candidates[i] = mainBmp; candidateScores[i] = faceSpace; candidateFace = mainBmp; break; } } } //if (candidateAmount < 10) candidateAmount++; //candidateFace.Save("./imgLib/face"+imageCount+".jpg", ImageFormat.Jpeg); //imageCount++; } return(output); }
private void bt_recon_Click(object sender, EventArgs e) { ImageTool.SetImage(libBmp, recon, false); pb_lib.Image = libBmp; }
private void bt_avg_Click(object sender, EventArgs e) { ImageTool.SetImage(libBmp, avg); pb_lib.Image = libBmp; }