Beispiel #1
0
        List <TemplatePyramid> fromFiles()
        {
            Console.WriteLine("Building templates from files...");

            var list = new List <TemplatePyramid>();

            string resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources", "OpenHandLeft_BW");

            string[] files = Directory.GetFiles(resourceDir, "*.bmp");

            object syncObj = new object();

            Parallel.ForEach(files, delegate(string file)
                             //foreach(var file in files)
            {
                Gray <byte>[,] preparedBWImage = ImageIO.LoadGray(file).Clone();

                try
                {
                    var tp = TemplatePyramid.CreatePyramidFromPreparedBWImage(preparedBWImage, new FileInfo(file).Name /*"OpenHand"*/);
                    lock (syncObj)
                    { list.Add(tp); };
                }
                catch (Exception)
                {}
            });

            //XMLTemplateSerializer<ImageTemplatePyramid, ImageTemplate>.Save(list, "C:/bla.xml");
            return(list);
        }
        public static Dictionary <ModelParams, ITemplate> CreateRange(string imagePath, string extension, IEnumerable <int> scaleRange, IEnumerable <int> rotationRange)
        {
            var fileNames = Directory.GetFiles(imagePath, extension);

            var dict = new Dictionary <ModelParams, ITemplate>();

            int idx = 0;

            foreach (var fileName in fileNames)
            {
                using (var img = ImageIO.LoadGray(fileName))
                {
                    createRange(dict, img.Clone(), idx, scaleRange, rotationRange);
                }
                idx++;
            }

            return(dict);
        }
Beispiel #3
0
        /// <summary>
        /// rotate the image for  <param name="angles"> angles and load them into the <param name="retList"> List.
        /// </summary>
        /// <param name="retList">the list that mean to be loaded with the new templates, create a new one if null is passed in</param>
        /// <param name="image">input image</param>
        /// <param name="angles">the number of angles</param>
        /// <param name="Width">The Width that the orginal image is cropped into, must be less than image.Width()</param>
        /// <param name="Height">The Height that the orginal image is cropped into, must be less than image.Height()</param>
        /// <param name="buildXMLTemplateFile">true to build a xml file with current templates</param>
        /// <param name="maxFeaturesPerLevel">the array of maximum features per pyrimid level, default to 200 in DEFAULT_MAX_FEATURES_PER_LEVEL from ImageTemplatePyramid.cs:56,
        ///                                     increase to increase the precision in expense of detection time-delay</param>
        /// <param name="userFunc">Input User Function for customization and diversity</param>
        /// <returns>nothing.</returns>
        private static void rotateLoad(List <TemplatePyramid> retList, Gray <byte>[,] image, int angles, int Width, int Height, bool buildXMLTemplateFile = false, int[] maxFeaturesPerLevel = null, string name = "DefaultTemplate", Func <TemplatePyramid, Gray <byte> [, ], TemplatePyramid> userFunc = null)
        {
            string resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources");

            retList = (retList == null ? new List <TemplatePyramid>() : retList);
            Width   = (Width > image.Width() ? image.Width() : Width);
            Height  = (Height > image.Height() ? image.Height() : Height);
            //userFunc = (userFunc != null) ? userFunc : (inputList, inputImage) => inputList;

            for (int i = 0; i < angles; i++)
            {
                float  ImageAngle = (float)i * 360 / angles;
                Bitmap bitmap     = image.ToBitmap();

                Bitmap   returnBitmap = new Bitmap(Width, Height);
                Graphics graphics     = Graphics.FromImage(returnBitmap);
                graphics.TranslateTransform((float)Width / 2, (float)Height / 2);
                graphics.RotateTransform(ImageAngle);
                graphics.TranslateTransform(-(float)Width / 2, -(float)Height / 2);
                graphics.DrawImage(bitmap, new System.Drawing.Point(-(image.Width() - Width) / 2, -(image.Height() - Height) / 2));
                bitmap.Dispose();
                returnBitmap.Save("TP.bmp");
                Console.WriteLine(" angle " + ImageAngle);
                Gray <byte>[,] preparedBWImage = ImageIO.LoadGray("TP.bmp").Clone();

                try
                {
                    TemplatePyramid newTemp = TemplatePyramid.CreatePyramidFromPreparedBWImage(
                        preparedBWImage, name, ImageAngle, maxNumberOfFeaturesPerLevel: maxFeaturesPerLevel);
                    if (userFunc != null)
                    {
                        newTemp = userFunc(newTemp, image);
                    }
                    retList.Add(newTemp);
                }
                catch (Exception)
                { }
            }
            if (buildXMLTemplateFile)
            {
                XMLTemplateSerializer <ImageTemplatePyramid <ImageTemplate>, ImageTemplate> .ToFile(retList, Path.Combine(resourceDir, "template" + ".xml"));
            }
        }
Beispiel #4
0
        /// <summary>
        /// build template List from a XML file
        /// </summary>
        /// <param name="filename">input XML file name, the file has to be in the current directory </param>
        /// <param name="buildXMLTemplateFile">true to build a xml file with current templates</param>
        /// <param name="angles">the number of angles, default to 360</param>
        /// <param name="sizes">number of sizes, default to 1</param>
        /// <param name="CropToSqr">decides whether to crop the image to sqrside to prevent edges issues</param>
        /// <param name="maxFeaturesPerLevel">the array of maximum features per pyrimid level, default (null) would evaluate to 200 in DEFAULT_MAX_FEATURES_PER_LEVEL from ImageTemplatePyramid.cs:56,
        ///                                     increase to increase the precision in expense of detection time-delay</param>
        /// <param name="userFunc">Input User Function for customization and diversity</param>
        /// <returns>List of templates.</returns>
        public static List <TemplatePyramid> fromFiles(String[] files, bool buildXMLTemplateFile = false, int angles = 360, int sizes = 1, bool CropToSqr = false, int[] maxFeaturesPerLevel = null, Func <TemplatePyramid, Gray <byte> [, ], TemplatePyramid> userFunc = null)
        {
            Console.WriteLine("Building templates from files...");
            Gray <byte>[,] ResizedtemplatePic;
            var list = new List <TemplatePyramid>();

            string resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources");

            //string[] files = Directory.GetFiles(resourceDir, fileName);
            if (files.Length == 0)
            {
                throw new Exception("NO FILE FOUND");
            }
            object syncObj = new object();


            foreach (var file in files)
            {
                int   shortSide, SqrSide, inputWidth, inputHeight;
                float FileRatio = 1;
                Gray <byte>[,] preparedBWImage;
                for (int j = 0; j < sizes; j++)
                {
                    Console.WriteLine("Size # " + j);
                    try
                    {
                        preparedBWImage = ImageIO.LoadGray(file).Clone();
                    }
                    catch
                    {
                        Console.WriteLine("Error occurs when loading " + file);
                        continue;
                    }

                    FileRatio -= (float)0.01;
                    int width  = (int)(preparedBWImage.Width() * FileRatio);
                    int height = (int)(preparedBWImage.Height() * FileRatio);
                    if (FileRatio < 0)
                    {
                        Console.WriteLine("Too many sizes to build, Max 100");
                        FileRatio = 1;
                        break;
                    }
                    DotImaging.Primitives2D.Size Nsize = new DotImaging.Primitives2D.Size(width, height);
                    ResizedtemplatePic = ResizeExtensions_Gray.Resize(preparedBWImage, Nsize, Accord.Extensions.Imaging.InterpolationMode.NearestNeighbor);

                    try
                    {
                        //var tp = TemplatePyramid.CreatePyramidFromPreparedBWImage(preparedBWImage, new FileInfo(file + "  " + i * 10).Name, ImageAngle, maxNumberOfFeaturesPerLevel: maxFeaturesPerLevel);
                        shortSide   = (preparedBWImage.Width() < preparedBWImage.Height()) ? preparedBWImage.Width() : preparedBWImage.Height();
                        SqrSide     = (int)(shortSide / Math.Sqrt(2));
                        inputWidth  = CropToSqr ? SqrSide : preparedBWImage.Width();
                        inputHeight = CropToSqr ? SqrSide : preparedBWImage.Height();

                        rotateLoad(list, preparedBWImage, angles, inputWidth, inputHeight, false, maxFeaturesPerLevel, file, userFunc);
                    }
                    catch (Exception)
                    { }
                }
                //}
            }
            if (buildXMLTemplateFile)
            {
                XMLTemplateSerializer <ImageTemplatePyramid <ImageTemplate>, ImageTemplate> .ToFile(list, Path.Combine(resourceDir, files[0].Substring(0, files[0].IndexOf(".")) + ".xml"));
            }
            return(list);
        }