//load images
        public void imreadall()
        {
            string Location = "C:\\major\\";

            for (int i = 1; i <= trainData; i++)
            {
                int j = 0;

                Bitmap alphaBmp = (Bitmap)Image.FromFile(String.Concat(Location, Convert.ToString(i), ".jpg"));
                Bitmap bmp      = ImageProcessingUtilities.emguCVconvert2Grayscale(alphaBmp);
                ImageProcessingUtilities.hist_equalize(bmp);
                BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
                unsafe
                {
                    byte *p           = (byte *)(void *)bmData.Scan0.ToPointer();
                    int   stopAddress = (int)p + bmData.Stride * bmData.Height;
                    while ((int)p != stopAddress)
                    {
                        // if (i==3) continue;
                        C[j, i - 1] = p[0];
                        //images.SetValue(p[0], j, i - 1);
                        j++;
                        p++;
                    }
                    p = null;
                    j = 0;
                }
                bmp.UnlockBits(bmData);
            }
        }
        public double[,] readImage(Bitmap fileloc)
        {
            //string filename;

            double[,] imgR;
            //try
            {
                //filename = fileloc;
                //Bitmap imgtrec = (Bitmap)Image.FromFile(filename);
                Bitmap imgtrec = fileloc;
                ImageProcessingUtilities.hist_equalize(imgtrec);
                if (imgtrec.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    imgtrec = ImageProcessingUtilities.MakeGrayscale3(imgtrec);
                }
                if (imgtrec.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    imgR = new double[1, imgtrec.Height *imgtrec.Width];
                    BitmapData bmData = imgtrec.LockBits(new Rectangle(0, 0, imgtrec.Width, imgtrec.Height), ImageLockMode.ReadWrite, imgtrec.PixelFormat);
                    //try
                    {
                        unsafe
                        {
                            int   j           = 0;
                            byte *p           = (byte *)(void *)bmData.Scan0.ToPointer();
                            int   stopAddress = (int)p + bmData.Stride * bmData.Height;
                            //while ((int)p != stopAddress)
                            while (j != imgR.GetLength(0) * imgR.GetLength(1))
                            {
                                // if (i==3) continue;
                                imgR[0, j] = p[0];
                                //images.SetValue(p[0], j, i - 1);
                                j++;
                                p++;
                            }
                            p = null;
                            j = 0;
                        }
                        imgtrec.UnlockBits(bmData);
                        double[,] itemp = new double[imgR.GetLength(1), imgR.GetLength(0)];
                        ifeature        = new double[DFLD_Trans.GetLength(0), itemp.GetLength(1)];
                        transpose(ref imgR, ref itemp);
                        matrix_multiply(ref DFLD_Trans, ref itemp, ref ifeature);
                        itemp = new double[ifeature.GetLength(1), ifeature.GetLength(0)];
                        //transpose(ref ifeature, ref itemp);
                        compute_ED(y, ifeature);
                    }
                    //catch
                    {
                        //MessageBox.Show("Error: Load 8 bit image");
                    }
                }
            }
            // catch
            {
                //MessageBox.Show("Error opening image", "Face Recongition", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(ifeature);
        }
예제 #3
0
        public static Bitmap getCroppedFace(Bitmap rawFrame, Rectangle faceRect)
        {
            int newFHeight = ((faceRect.Width) / 3) * 4;
            int gap        = newFHeight - faceRect.Height;
            int top        = 0;

            if ((faceRect.Top - (gap / 2)) > 0)
            {
                top = faceRect.Top - (gap / 2);
            }
            if ((faceRect.Top - (gap / 2) + newFHeight) > 480)
            {
                top = faceRect.Top - gap;
            }

            Rectangle face4isto3 = new Rectangle(faceRect.Left, top, faceRect.Width, newFHeight);
            Bitmap    cFace      = null;

            try
            {
                if (rawFrame != null)
                {
                    Bitmap face_rgb = ImageProcessingUtilities.Resize((ImageProcessingUtilities.Crop(rawFrame, face4isto3)), 76, 101);
                    //note: calling other grayscale convertion function causes recognition algorithm to stop working
                    //      So we call emguCV inbuild grayscale convertion function in ImageProcessingUtilities class
                    //cFace =ImageProcessingUtilities.norm_rgb(ImageProcessingUtilities.emguCVconvert2Grayscale(face_rgb));
                    //cFace = face_rgb;
                    cFace = ImageProcessingUtilities.emguCVconvert2Grayscale(face_rgb);
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
            return(cFace);
        }
        public int recogn(Bitmap imgtrec)
        {
            int indel = 0;


            float tmean = 0; //mean of test image

            //float[,] testim;
            // final vector of the distance

            ImageProcessingUtilities.hist_equalize(imgtrec);
            //if (imgtrec.PixelFormat == PixelFormat.Format8bppIndexed)
            //{
            BitmapData bmData = imgtrec.LockBits(new Rectangle(0, 0, imgtrec.Width, imgtrec.Height), ImageLockMode.ReadWrite, imgtrec.PixelFormat);

            unsafe
            {
                int   j           = 0;
                byte *p           = (byte *)(void *)bmData.Scan0.ToPointer();
                int   stopAddress = (int)p + bmData.Stride * bmData.Height;
                //while ((int)p != stopAddress)
                while ((int)p != stopAddress)
                {
                    testIm[0, j] = p[0];
                    j++;
                    p++;
                }
                p = null;
                j = 0;
            }
            imgtrec.UnlockBits(bmData);

            //}


            /////////Deviation from mean////////////////////////
            //MessageBox.Show("stemp" + stemp.GetLength(0) + "," + stemp.GetLength(1) + "\n" + testIm.GetLength(0) + "," + testIm.GetLength(1));
            for (int i = 0; i < testIm.GetLength(1); i++)
            {
                tmean = tmean + testIm[0, i];
            }

            tmean /= testIm.GetLength(1);
            //MessageBox.Show(tmean.ToString());
            for (int i = 0; i < testIm.GetLength(1); i++)
            {
                testIm[0, i] = testIm[0, i] - tmean;
            }

            ///////// imgR*V   V=stemp///////////////////////

            //ThreadStart m1 = new ThreadStart(th1);
            //Thread mth1 = new Thread(m1);
            //mth1.Start();
            //ThreadStart m2 = new ThreadStart(th2);
            //Thread mth2 = new Thread(m2);
            //mth2.Start();


            //ThreadStart m3 = new ThreadStart(th3);
            //Thread mth3 = new Thread(m3);
            //mth3.Start();

            //testim = this.matmul(ref imgR, ref stemp);
            float sum1 = 0;

            dis_eu = new float[1, trainData / 4];
            for (int j = 0; j < (trainData / 4); j++)
            {
                sum1 = 0;
                for (int k = 0; k < testIm.GetLength(1); k++)
                {
                    sum1 += testIm[0, k] * stemp[k, j];
                }
                dis_eu[0, j] = sum1;
            }

            //MessageBox.Show("coordinate" + dis_eu[0, 0] + "," + dis_eu[0, 1] + "," + dis_eu[0, 2] + "," + dis_eu[0, 3] + ",");



            //this.savp("justy", testim);

            /*
             * double[,] rett = new double[testim.GetLength(0), testim.GetLength(1)];
             * for (int i = 0; i < rett.GetLength(0); i++)
             * {
             *
             *  for (int j = 0; j < rett.GetLength(1); j++)
             *      rett[i, j] = (double)testim[i, j];
             *
             * }
             */
            //MessageBox.Show("rc=(" + rc.GetLength(0) + "," + rc.GetLength(1)+")\n"+trainData);
            ///////////////recognition//////////////////////////
            double devsumsq = 0;

            el = new double[1, trainData];
            for (int i = 0; i < trainData; i++)
            {
                for (int j = 0; j < (trainData / 4); j++)
                {
                    devsumsq += Math.Pow((rc[i, j] - dis_eu[0, j]), 2);
                }
                el[0, i] = Math.Pow(devsumsq, .5);
                devsumsq = 0;
            }
            double min = el[0, 0];

            for (int i = 0; i < trainData; i++)
            {
                if (el[0, i] < min)
                {
                    min   = el[0, i];
                    indel = i;
                }
            }
            minEudDistance = (int)min;


            //MessageBox.Show("Index:"+indel.ToString());



            int classIndex;

            /*
             * if (min > 4500)
             * {
             *  indel = -1;
             *  MessageBox.Show("Unknown Face");
             * }
             */

            //else
            {
                classIndex = indel / 10;
                indel      = 1 + (indel / 10) * 10;
                //MessageBox.Show("min : " + Convert.ToString(min));
                double maxE = findMaxEuclidian();
                //MessageBox.Show("max : " + Convert.ToString(maxE));
                //MessageBox.Show("diff : " + Convert.ToString(maxE - min));
            }
            //this code will set the input vector for lvq

            /*
             * int inputVectorIndex=1;
             * LVQ.setInputVectors(minEudDistance, 0);
             * for (int i = indel; i < (indel + 5); i++)
             * {
             *  LVQ.setInputVectors((int)el[0,i], inputVectorIndex);
             *  inputVectorIndex++;
             * }
             */
            Sorter.BubbleSort(el);
            int[] pat = Sorter.getSortedPatterns();

            FaceRecognition.writeEudPat(pat);

            return(indel);
        }