/// <summary>
        /// Calculates eigen objects
        /// </summary>
        public static void CalcEigenObjects(Bitmap[] input, int maxIteration, double eps, FloatImage[] eigVecs, float[] eigVals, FloatImage avg)
        {
            if (input.Length == 0)
                return;

            int nObjects = input.Length;
            int nEigens = nObjects - 1;

            byte[][] objs = new byte[nObjects][];
            float[][] eigs = new float[nEigens][];
            int obj_step = 0, old_step = 0;
            int eig_step = 0, oldeig_step = 0;
            Size obj_size = avg.Size, old_size = avg.Size, oldeig_size = avg.Size;

            for (var i = 0; i < nObjects; i++)
            {
                Bitmap obj = input[i];
                objs[i] = obj.CopyGrayscaleBitmapToByteArray(out obj_step);
                obj_size = obj.Size;

                if (obj_size != avg.Size || obj_size != old_size)
                    throw new ManagedEigenObjectException("Different sizes of objects");
                if (i > 0 && obj_step != old_step)
                    throw new ManagedEigenObjectException("Different steps of objects");

                old_step = obj_step;
                old_size = obj_size;
            }

            for (var i = 0; i < nEigens; i++)
            {
                FloatImage eig = eigVecs[i];
                eig_step = eig.Step;
                eigs[i] = eig.Data;

                if (eig.Size != avg.Size || eig.Size != oldeig_size)
                    throw new ManagedEigenObjectException("Different sizes of objects");
                if (i > 0 && eig_step != oldeig_step)
                    throw new ManagedEigenObjectException("Different steps of objects");

                oldeig_step = eig.Step;
                oldeig_size = eig.Size;
            }

            CalcEigenObjects(nObjects, objs, obj_step,
                                        eigs, eig_step, obj_size,
                                        maxIteration, eps, avg.Data, avg.Step, eigVals);
        }
        /// <summary>
        /// Caculate the eigen images for the specific traning image
        /// </summary>
        public static void CalcEigenObjects(Bitmap[] trainingImages, int maxIter, double eps, out FloatImage[] eigenImages, out FloatImage avg)
        {
            int width = trainingImages[0].Width;
            int height = trainingImages[0].Height;

            if (maxIter <= 0 || maxIter > trainingImages.Length)
                maxIter = trainingImages.Length;

            int maxEigenObjs = maxIter;

            eigenImages = new FloatImage[maxEigenObjs];
            for (int i = 0; i < eigenImages.Length; i++)
                eigenImages[i] = new FloatImage(width, height);

            avg = new FloatImage(width, height);

            ManagedEigenObjects.CalcEigenObjects(trainingImages, maxIter, eps, eigenImages, null, avg);
        }
        /// <summary>
        /// Caculate the eigen images for the specific traning image
        /// </summary>
        public static void CalcEigenObjects(Bitmap[] trainingImages, int maxIter, double eps, out FloatImage[] eigenImages, out FloatImage avg)
        {
            int width  = trainingImages[0].Width;
            int height = trainingImages[0].Height;

            if (maxIter <= 0 || maxIter > trainingImages.Length)
            {
                maxIter = trainingImages.Length;
            }

            int maxEigenObjs = maxIter;

            eigenImages = new FloatImage[maxEigenObjs];
            for (int i = 0; i < eigenImages.Length; i++)
            {
                eigenImages[i] = new FloatImage(width, height);
            }

            avg = new FloatImage(width, height);

            ManagedEigenObjects.CalcEigenObjects(trainingImages, maxIter, eps, eigenImages, null, avg);
        }
 /// <summary>
 /// Decompose the image as eigen values, using the specific eigen vectors
 /// </summary>
 public static float[] EigenDecomposite(Bitmap src, FloatImage[] eigenImages, FloatImage avg)
 {
     return ManagedEigenObjects.EigenDecomposite(src, eigenImages, avg);
 }
 /// <summary>
 /// Decompose the image as eigen values, using the specific eigen vectors
 /// </summary>
 public static float[] EigenDecomposite(Bitmap src, FloatImage[] eigenImages, FloatImage avg)
 {
     return(ManagedEigenObjects.EigenDecomposite(src, eigenImages, avg));
 }
示例#6
0
        /// <summary>
        /// Calculates eigen objects
        /// </summary>
        public static void CalcEigenObjects(Bitmap[] input, int maxIteration, double eps, FloatImage[] eigVecs, float[] eigVals, FloatImage avg)
        {
            if (input.Length == 0)
            {
                return;
            }

            int nObjects = input.Length;
            int nEigens  = nObjects - 1;

            byte[][]  objs = new byte[nObjects][];
            float[][] eigs = new float[nEigens][];
            int       obj_step = 0, old_step = 0;
            int       eig_step = 0, oldeig_step = 0;
            Size      obj_size = avg.Size, old_size = avg.Size, oldeig_size = avg.Size;

            for (var i = 0; i < nObjects; i++)
            {
                Bitmap obj = input[i];
                objs[i]  = obj.CopyGrayscaleBitmapToByteArray(out obj_step);
                obj_size = obj.Size;

                if (obj_size != avg.Size || obj_size != old_size)
                {
                    throw new ManagedEigenObjectException("Different sizes of objects");
                }
                if (i > 0 && obj_step != old_step)
                {
                    throw new ManagedEigenObjectException("Different steps of objects");
                }

                old_step = obj_step;
                old_size = obj_size;
            }

            for (var i = 0; i < nEigens; i++)
            {
                FloatImage eig = eigVecs[i];
                eig_step = eig.Step;
                eigs[i]  = eig.Data;

                if (eig.Size != avg.Size || eig.Size != oldeig_size)
                {
                    throw new ManagedEigenObjectException("Different sizes of objects");
                }
                if (i > 0 && eig_step != oldeig_step)
                {
                    throw new ManagedEigenObjectException("Different steps of objects");
                }

                oldeig_step = eig.Step;
                oldeig_size = eig.Size;
            }

            CalcEigenObjects(nObjects, objs, obj_step,
                             eigs, eig_step, obj_size,
                             maxIteration, eps, avg.Data, avg.Step, eigVals);
        }
        /// <summary>
        /// Calculates eigen decomposite
        /// </summary>
        public static float[] EigenDecomposite(Bitmap obj, FloatImage[] eigInput, FloatImage avg)
        {
            var nEigObjs = eigInput.Length;
            var coeffs = new float[nEigObjs];

            int i;

            int obj_step;
            byte[] obj_data = obj.CopyGrayscaleBitmapToByteArray(out obj_step);
            Size obj_size = obj.Size;

            /*cvGetImageRawData( avg, (uchar **) & avg_data, &avg_step, &avg_size );
            if( avg->depth != IPL_DEPTH_32F )
                CV_ERROR( CV_BadDepth, cvUnsupportedFormat );
            if( avg->nChannels != 1 )
                CV_ERROR( CV_BadNumChannels, cvUnsupportedFormat );

            cvGetImageRawData( obj, &obj_data, &obj_step, &obj_size );
            if( obj->depth != IPL_DEPTH_8U )
                CV_ERROR( CV_BadDepth, cvUnsupportedFormat );
            if( obj->nChannels != 1 )
                CV_ERROR( CV_BadNumChannels, cvUnsupportedFormat );*/

            if (obj_size != avg.Size)
                throw new ManagedEigenObjectException("Different sizes of objects");

            float[][] eigs = new float[nEigObjs][];
            int eig_step = 0, old_step = 0;
            Size eig_size = avg.Size, old_size = avg.Size;

            for (i = 0; i < nEigObjs; i++)
            {
                FloatImage eig = eigInput[i];
                eig_step = eig.Step;
                eigs[i] = eig.Data;

                if (eig_size != avg.Size || eig_size != old_size)
                    throw new ManagedEigenObjectException("Different sizes of objects");
                if (i > 0 && eig_step != old_step)
                    throw new ManagedEigenObjectException("Different steps of objects");

                old_step = eig.Step;
                old_size = eig.Size;
            }

            EigenDecomposite(obj_data, obj_step, nEigObjs, eigs, eig_step, avg.Data, avg.Step, obj_size, coeffs);

            return coeffs;
        }