private void btn_Test_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { FileInfo fi = new FileInfo(ofd.FileName); Bitmap bmp = new Bitmap(ofd.FileName); bmp = new Bitmap(bmp, new Size(width, height)); MyImage mimg = null; if (fi.Extension.ToUpper() == ".GIF") { mimg = new MyImage(ofd.FileName, fi.Name, width, height, ImgFormat.EightBit, ImgComparison.CORRELATION); } if (fi.Extension.ToUpper() == ".JPG") { mimg = new MyImage(ofd.FileName, fi.Name, width, height, ImgFormat.TwentyFourBit, ImgComparison.CORRELATION); } NormalizeDataAndShowFace(mimg.ImgVector, width, height, pic0); lblCheck.Text = mimg.FileNameShort; ef.SubtractAvgImage(mimg); // mean adjusted image NormalizeDataAndShowFace(mimg.ImgVectorAdjM, width, height, picAdjust); double selfReconsError = 0; MatchResult[] bestMatches = new MatchResult[ef.ImageList.Count]; MyImage reconImg = ef.GetMatchedAndReconstructedImages(mimg, ref selfReconsError, ref bestMatches); NormalizeDataAndShowFace(reconImg.ImgVectorAdjM, width, height, picRecons); //lblReconstructedError.Text = selfReconsError.ToString() + ":" + bestMatches[0].ImageName.ToString(); if (bestMatches[0].EucledianDist < 50) { lblBest.BackColor = System.Drawing.Color.Green; } else { lblBest.BackColor = System.Drawing.Color.Red; } NormalizeDataAndShowFace(((MyImage)ef.ImageList[bestMatches[0].ImageNum]).ImgVector, width, height, picBest1); lblBest.Text = "D=" + bestMatches[0].EucledianDist.ToString() + "\n" + bestMatches[0].Correlation.ToString();//.Substring(0, 5); NormalizeDataAndShowFace(((MyImage)ef.ImageList[bestMatches[1].ImageNum]).ImgVector, width, height, picBest2); lblBest2.Text = "D=" + bestMatches[1].EucledianDist.ToString() + "\n" + bestMatches[1].Correlation.ToString().Substring(0, 5); NormalizeDataAndShowFace(((MyImage)ef.ImageList[bestMatches[2].ImageNum]).ImgVector, width, height, picBest3); lblBest3.Text = "D=" + bestMatches[2].EucledianDist.ToString() + "\n" + bestMatches[2].Correlation.ToString().Substring(0, 5); NormalizeDataAndShowFace(((MyImage)ef.ImageList[bestMatches[3].ImageNum]).ImgVector, width, height, picBest4); lblBest4.Text = "D=" + bestMatches[3].EucledianDist.ToString() + "\n" + bestMatches[3].Correlation.ToString().Substring(0, 5); NormalizeDataAndShowFace(((MyImage)ef.ImageList[bestMatches[4].ImageNum]).ImgVector, width, height, picBest5); lblBest5.Text = "D=" + bestMatches[4].EucledianDist.ToString() + "\n" + bestMatches[4].Correlation.ToString().Substring(0, 5); NormalizeDataAndShowFace(((MyImage)ef.ImageList[bestMatches[5].ImageNum]).ImgVector, width, height, picBest6); lblBest6.Text = "D=" + bestMatches[5].EucledianDist.ToString() + "\n" + bestMatches[5].Correlation.ToString().Substring(0, 5); NormalizeDataAndShowFace(((MyImage)ef.ImageList[bestMatches[6].ImageNum]).ImgVector, width, height, picBest7); lblBest7.Text = "D=" + bestMatches[6].EucledianDist.ToString() + "\n" + bestMatches[6].Correlation.ToString().Substring(0, 5); NormalizeDataAndShowFace(((MyImage)ef.ImageList[bestMatches[7].ImageNum]).ImgVector, width, height, picBest8); lblBest8.Text = "D=" + bestMatches[7].EucledianDist.ToString() + "\n" + bestMatches[7].Correlation.ToString().Substring(0, 5); } }
//--------------------------------------------------- //--------------GetReconstructedFaceSpaceImage(MyImage img)------------ public MyImage GetMatchedAndReconstructedImages(MyImage inputImg,ref double selfReconstError, ref MatchResult[] bestMatches) { // assumes data is in Imagevector MyImage recImage = new MyImage(); recImage.ImgVectorAdjM = new double[inputImg.ImgVectorAdjM.Length]; // subtract mean image from input image SubtractAvgImage(inputImg); //-------FSV for input image----------------- ComputeFaceSpace(inputImg); double[] fsvData = inputImg.FSV; // Reconstruct the input image double[] recData = new double[inputImg.ImgVectorAdjM.Length]; for (int j = 0; j < inputImg.ImgVectorAdjM.Length; j++) { recData[j] = 0; for (int i = 0; i < NumEigenFaces; i++) { recData[j] += fsvData[i] * ((EigenFace)EigenFaceList[i]).EF[j]; } } // normalize the reconstructed image to 255 range double max1 = (from n in recData select n).Max(); double min = (from n in recData select n).Min(); double diff = max1 - min; for (int i = 0; i < inputImg.ImgVectorAdjM.Length; i++) { recData[i] = recData[i] - min; recData[i] = (recData[i] / diff) * 255; recData[i] = recData[i]; if (recData[i] < 0) recData[i] = 0; if (recData[i] > 255) recData[i] = 255; } // add mean image for (int i = 0; i < recData.Length; i++) recImage.ImgVectorAdjM[i] = recData[i] + this.AvgImage.ImgVector[i]; // readjust the reconstructed image to 0-255 range max1 = (from n in recImage.ImgVectorAdjM select n).Max(); min = (from n in recImage.ImgVectorAdjM select n).Min(); diff = max1 - min; for (int i = 0; i < recImage.ImgVectorAdjM.Length; i++) { recImage.ImgVectorAdjM[i] = recImage.ImgVectorAdjM[i] - min; recImage.ImgVectorAdjM[i] = (recImage.ImgVectorAdjM[i] / diff) * 255; if (recImage.ImgVectorAdjM[i] < 0) recImage.ImgVectorAdjM[i] = 0; if (recImage.ImgVectorAdjM[i] > 255) recImage.ImgVectorAdjM[i] = 255; } selfReconstError = 0; for (int i = 0; i < inputImg.ImgVectorAdjM.Length; i++) selfReconstError = selfReconstError + (inputImg.ImgVector[i] - recImage.ImgVectorAdjM[i]) * (inputImg.ImgVector[i] - recImage.ImgVectorAdjM[i]); selfReconstError = Math.Sqrt(selfReconstError); //-----------find best match---------------------------- MatchResult[] MR = new MatchResult[ImageList.Count]; for (int i = 0; i < ImageList.Count; i++) { MR[i] = new MatchResult(i, 0, (MyImage)((MyImage)ImageList[i]).Clone()); double dist = 0; for (int j = 0; j < NumEigenFaces; j++) dist += ((MR[i].mImage.FSV[j] - inputImg.FSV[j]) * (MR[i].mImage.FSV[j] - inputImg.FSV[j])); MR[i].EucledianDist = Math.Sqrt(dist); } //--------------find correlation-------------------- double[] corr = FindCorrelation(inputImg); for (int i = 0; i < ImageList.Count; i++) MR[i].Correlation = corr[i]; Array.Sort(MR); bestMatches = MR; return recImage; }