예제 #1
0
        //-------------------------------------------------------------

        //----------------SubtractAvgImage()---------------------
        public void SubtractAvgImage(MyImage img1)
        {
            // subtract average image from given image
            for (int i = 0; i < img1.ImgVector.Length; i++) //goes through length beause we have vectors images #pixes x 1
            {
                img1.ImgVectorAdjM[i] = img1.ImgVector[i] - this.AvgImage.ImgVector[i];
            }
        }
예제 #2
0
 public double[] FindCorrelation(MyImage unkIm)
 {
 
     int i, j, k;
     double rsum = 0; double avgunk;
     double[] avgi = new double[ImageList.Count];
     //---------Calculates the unknown face space average---------//
     //------------------ Y' ---------------------------------//
     for (k = 0; k < EigenFaceList.Count; k++)
     {
         rsum = rsum + unkIm.FSV[k];
     }
     avgunk = rsum / EigenFaceList.Count;
     //---------Calculates the known face space average---------//
     //------------------ X' ---------------------------------//
     for (i = 0; i < ImageList.Count; i++)
     {
         rsum = 0.0;
         MyImage im = (MyImage)ImageList[i];
         for (k = 0; k < EigenFaceList.Count; k++)//review
         {
             rsum = rsum + im.FSV[k];
         }
         avgi[i] = rsum / EigenFaceList.Count;
     }
     //----Calculate the Numerator of the Correlation Equation----//
     double[] rtop = new double[ImageList.Count];
     for (i = 0; i < ImageList.Count; i++)
     {
         rtop[i] = 0.0;
         for (j = 0; j < EigenFaceList.Count; j++)
         {
             MyImage im = (MyImage)ImageList[i];
             rtop[i] = rtop[i] + (im.FSV[j] - avgi[i]) * (unkIm.FSV[j] - avgunk);
         }
     }
     //---Calculate the Denominator of the Correlation Equation----//
     double[] rbot1 = new Double[ImageList.Count];
     double[] rbot2 = new Double[ImageList.Count];
     for (i = 0; i < ImageList.Count; i++)
     {
         rbot1[i] = 0.0; rbot2[i] = 0.0;
         for (j = 0; j < EigenFaceList.Count; j++)
         {
             MyImage im = (MyImage)ImageList[i];
             rbot1[i] = rbot1[i] + (im.FSV[j] - avgi[i]) * (im.FSV[j] - avgi[i]);
             rbot2[i] = rbot2[i] + (unkIm.FSV[j] - avgunk) * (unkIm.FSV[j] - avgunk);
         }
     }
     //----Calculate the final Correlation Equation----//
     double[] corr = new double[ImageList.Count];
     for (i = 0; i < ImageList.Count; i++)
         corr[i] = rtop[i] / Math.Sqrt(rbot1[i] * rbot2[i]);
     return corr;
 }
예제 #3
0
        public int CompareTo(object obj)
        {
            MyImage im = (MyImage)obj;

            if (imgCompareMode == ImgComparison.CORRELATION)
            {
                return(im.CorrError.CompareTo(this.CorrError)); // index 0 is best
            }
            else
            {
                return(im.EuclideanError.CompareTo(this.EuclideanError));
            }
        }
예제 #4
0
        //-------------------------------------------------

        //-----------Finds the LSE ------------------------
        public void FindLSE()
        {
            // ResultVec = new Double[ImageList.Count];
            for (int i = 0; i < ImageList.Count; i++)
            {
                double rsum = 0.0;
                MyImage im = (MyImage)ImageList[i];
                for (int k = 0; k < EigenFaceList.Count; k++)//review
                {
                    rsum = rsum + (im.FSV[k] - unkIm.FSV[k]) * (im.FSV[k] - unkIm.FSV[k]);
                }
            }
        }
예제 #5
0
        //-------------------------------------------------

        //----------ComputeFaceSpace()---------------------
        public void ComputeFaceSpace(MyImage im)
        {
            int i, j;
            double rsum;
            for (i = 0; i < EigenFaceList.Count; i++)
            {
                rsum = 0.0;
                for (j = 0; j < imageNumPixels; j++)
                {
                    EigenFace ef = (EigenFace)EigenFaceList[i];
                    rsum = rsum + im.ImgVectorAdjM[j] * ef.EF[j];
                }
                im.FSV[i] = rsum;
            }
        }
예제 #6
0
        //------------------------------------------------------------------

        public object Clone()
        {
            MyImage clone = (MyImage)this.MemberwiseClone();

            if (this.FSV != null)
            {
                clone.FSV = (double[])(this.FSV.Clone());
            }
            if (this.ImgVector != null)
            {
                clone.ImgVector = (double[])(this.ImgVector.Clone());
            }
            if (this.ImgVectorAdjM != null)
            {
                clone.ImgVectorAdjM = (double[])(this.ImgVectorAdjM.Clone());
            }
            return(clone);
        }
예제 #7
0
 private void btn_Accuracy_Click(object sender, EventArgs e)
 {
     try
     { // loop through test images folder to see if the
       // test image matches correctly to known images
         string        testImagesFolder = @"C:\Users\ivans_000\Desktop\MASTER\Spring2019\Computer Vision\Assignment8_ComputerVision_SANGINES\ATTFaceDataSet\Testing";
         DirectoryInfo dirInfo          = new DirectoryInfo(testImagesFolder);
         if (!dirInfo.Exists)
         {
             throw new DirectoryNotFoundException(testImagesFolder + " folder does not exist,");
         }
         int accuracyCount = 0;
         int count         = 0;
         foreach (FileInfo nextFile in dirInfo.GetFiles())
         {
             if (nextFile.Extension.ToUpper() == ".JPG")
             {
                 MyImage mimg = new MyImage(nextFile.FullName, nextFile.Name, width, height, ImgFormat.TwentyFourBit, ImgComparison.CORRELATION);
                 //ef.SubtractAvgImage(mimg);
                 MatchResult[] bestMatches  = new MatchResult[ef.ImageList.Count];
                 double        reconstError = 0;
                 MyImage       reconImg     = ef.GetMatchedAndReconstructedImages(mimg, ref reconstError, ref bestMatches);
                 if (bestMatches[0].ImageName.ToString().Substring(0, 3) == mimg.FileNameShort.Substring(0, 3))
                 {
                     accuracyCount++;
                 }
                 else
                 {
                     Console.WriteLine(mimg.FileNameShort + ":" + bestMatches[0].ImageName);
                 }
                 count++;
             }
         }
         MessageBox.Show("Accuracy = " + ((double)accuracyCount / count * 100).ToString() + "%");
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message + "Error creating the ImageList..");
     }
 }
예제 #8
0
 public MatchResult(int Imnum, double err, MyImage img)
 {
     ImageNum = Imnum;
     if (img.FileNameShort.Length > 10)
     {
         ImageName = img.FileNameShort.Substring(img.FileNameShort.Length - 10,
                                                 9);
     }
     else
     {
         ImageName = img.FileNameShort;
     }
     mImage = img;
     if (img.ImgCompareMode == ImgComparison.CORRELATION)
     {
         Correlation = err;
     }
     else
     {
         EucledianDist = err;
     }
 }
예제 #9
0
        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);
            }
        }
예제 #10
0
        //---------------------------------------------------

        //--------------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;
        }