Ejemplo n.º 1
0
        /// <summary>
        /// IF options include split screen style then apply the applicable cropping else return the source
        /// </summary>
        /// <param name="source"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private Bitmap SplitSourceImage(Bitmap source, TemplateMatchOptions options)
        {
            if (options.SplitScreenPanel != Panel.All)
            {
                Crop f = new Crop(GetCrop(source, options));

                //var ss = f.Apply(source);
                //ss.Save("croptest.png", ImageFormat.Png);
                //throw new Exception();
                try
                {
                    var output = f.Apply(source);
                    if (options.SavePath != "")
                    {
                        output.Save(options.SavePath.UnCleanString(), ImageFormat.Png);
                    }
                    return(output);
                }
                catch
                {
                    Compiler.ExceptionListener.Throw("[53]There was an unexpected issue getting Options for your source image cropping. Please " +
                                                     "double check your additional options!");
                    return(source);
                }
            }
            else
            {
                return(source);
            }
        }
Ejemplo n.º 2
0
        private Rectangle GetCrop(Bitmap source, TemplateMatchOptions options)
        {
            switch (options.SplitScreenPanel)
            {
            case (Panel.Top):
                return(new Rectangle(0, 0, source.Width, (source.Height / 2)));

            case (Panel.Bottom):
                return(new Rectangle(0, (source.Height / 2), source.Width, (source.Height / 2)));

            case (Panel.Left):
                return(new Rectangle(0, 0, (source.Width / 2), (source.Height)));

            case (Panel.Right):
                return(new Rectangle((source.Width / 2), 0, (source.Width / 2), (source.Height)));

            case (Panel.Q1):
                return(new Rectangle(0, 0, (source.Width / 2), (source.Height / 2)));

            case (Panel.Q2):
                return(new Rectangle((source.Width / 2), 0, (source.Width / 2), (source.Height / 2)));

            case (Panel.Q3):
                return(new Rectangle(0, (source.Height / 2), (source.Width / 2), (source.Height / 2)));

            case (Panel.Q4):
                return(new Rectangle((source.Width / 2), (source.Height / 2), (source.Width / 2), (source.Height / 2)));

            case (Panel.Custom):
                return(new Rectangle(options.CustomX, options.CustomY, options.CustomWidth, options.CustomHeight));

            default:
                throw new Exception("borked");
            }
        }
Ejemplo n.º 3
0
 public static string[] SetStaticOptions(TemplateMatchOptions opt)
 {
     DefaultThreshold        = opt.Threshold;
     DefaultReduction        = opt.Reduction;
     DefaultSplitScreenPanel = opt.SplitScreenPanel;
     DefaultCustomX          = opt.CustomX;
     DefaultCustomY          = opt.CustomY;
     DefaultCustomWidth      = opt.CustomWidth;
     DefaultCustomHeight     = opt.CustomHeight;
     return(new string[] { DefaultThreshold.ToString(), DefaultReduction.ToString(), DefaultSplitScreenPanel.ToString(),
                           DefaultCustomX.ToString(), DefaultCustomY.ToString(), DefaultCustomWidth.ToString(), DefaultCustomHeight.ToString() });
 }
Ejemplo n.º 4
0
        private double[] GetCoords(TemplateMatchOptions options, TemplateMatch match, int templateWidth, int templateHeight, int screenx, int screeny)
        {
            var reduction = options.Reduction / 100f;
            var matchx    = match.Rectangle.Location.X / reduction;
            var matchy    = match.Rectangle.Location.Y / reduction;

            if (options.SplitScreenPanel != Panel.All)
            {
                switch (options.SplitScreenPanel)
                {
                case (Panel.Top):
                    break;

                case (Panel.Bottom):
                    matchy = matchy + (screeny / 2);
                    break;

                case (Panel.Left):
                    break;

                case (Panel.Right):
                    matchx = matchx + (screenx / 2);
                    break;

                case (Panel.Q1):
                    break;

                case (Panel.Q2):
                    matchx = matchx + (screenx / 2);
                    matchy = matchy + (screeny / 2);
                    break;

                case (Panel.Q3):
                    matchy = matchy + (screeny / 2);
                    break;

                case (Panel.Q4):
                    matchx = matchx + (screenx / 2);
                    matchy = matchy + (screeny / 2);
                    break;

                case (Panel.Custom):
                    matchx = matchx + (options.CustomX);
                    matchy = matchy + (options.CustomY);
                    break;
                }
            }
            var centerx = matchx + ((templateWidth / reduction) / 2);
            var centery = matchy + ((templateHeight / reduction) / 2);

            return(new double[] { centerx, centery });
        }
Ejemplo n.º 5
0
        private double[] TemplateMatch(TemplateMatchOptions options)
        {
            Stopwatch watch = Stopwatch.StartNew();

            if (_screen == null)
            {
                return(null);
            }
            var   originScreenX   = _screen.Width;
            var   originScreenY   = _screen.Height;
            float threshold       = options.Threshold / 100f;
            float reductionAmount = options.Reduction / 100f;

            //Console.WriteLine("Cloning " + watch.ElapsedMilliseconds);
            System.Drawing.Bitmap template = AForge.Imaging.Image.Clone(options.Template, PixelFormat.Format24bppRgb);
            //Console.WriteLine("Splitting source " + watch.ElapsedMilliseconds);
            System.Drawing.Bitmap sourceImage = SplitSourceImage(_screen, options);
            //Console.WriteLine("resizing imgs " + watch.ElapsedMilliseconds);
            if (sourceImage == null)
            {
                throw new Exception();
            }
            sourceImage = AForge.Imaging.Image.Clone(sourceImage, PixelFormat.Format24bppRgb);
            sourceImage = new ResizeBicubic((int)(sourceImage.Width * reductionAmount), (int)(sourceImage.Height * reductionAmount)).Apply(sourceImage);
            template    = new ResizeBicubic((int)(template.Width * reductionAmount), (int)(template.Height * reductionAmount)).Apply(template);

            //Console.WriteLine("exhausting " + watch.ElapsedMilliseconds);
            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(threshold);

            // find all matchings with specified above similarity
            TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template);
            //Console.WriteLine("done " + watch.ElapsedMilliseconds);
            if (matchings.Length == 0)
            {
                return(null);
            }
            else if (matchings.Length > 0)
            {
                return(GetCoords(options, matchings[0], template.Width, template.Height, originScreenX, originScreenY));
            }
            Compiler.ExceptionListener.ThrowSilent(new ExceptionHandler($"[IMG RECOGNITION] More than one match found: {matchings.Length}"));
            return(null);
        }
Ejemplo n.º 6
0
        public static string[] SetDefaultTemplateOptions(string[] prop)
        {
            var opt = GetOptionsFromStringArray(prop, "");

            return(TemplateMatchOptions.SetStaticOptions(opt));
        }
Ejemplo n.º 7
0
        private static TemplateMatchOptions GetOptionsFromStringArray(string[] prop, string template)
        {
            var opt = new TemplateMatchOptions(template);

            if (prop == null)
            {
                return(opt);
            }
            var thresh    = prop.ElementAtOrDefault(0);
            var reduction = prop.ElementAtOrDefault(1);
            var ssp       = prop.ElementAtOrDefault(2);
            var x         = prop.ElementAtOrDefault(3);
            var y         = prop.ElementAtOrDefault(4);
            var width     = prop.ElementAtOrDefault(5);
            var height    = prop.ElementAtOrDefault(6);
            var savepath  = prop.ElementAtOrDefault(7);
            var threshint = 0;
            var reductint = 0;
            var sspenum   = Panel.All;
            var xint      = 0;
            var yint      = 0;
            var widthint  = 0;
            var heightint = 0;

            if (thresh != null)
            {
                int.TryParse(thresh, out threshint);
            }
            if (reduction != null)
            {
                int.TryParse(reduction, out reductint);
            }
            if (ssp != null)
            {
                Enum.TryParse <Panel>(ssp, out sspenum);
            }
            if (x != null)
            {
                int.TryParse(x, out xint);
            }
            if (y != null)
            {
                int.TryParse(y, out yint);
            }
            if (width != null)
            {
                int.TryParse(width, out widthint);
            }
            if (height != null)
            {
                int.TryParse(height, out heightint);
            }
            if (savepath != null)
            {
                opt.SavePath = savepath;
            }
            if (threshint != 0)
            {
                opt.Threshold = threshint;
            }
            if (reductint != 0)
            {
                opt.Reduction = reductint;
            }
            if (sspenum != Panel.All)
            {
                opt.SplitScreenPanel = sspenum;
            }
            if (xint != 0)
            {
                opt.CustomX = xint;
            }
            if (yint != 0)
            {
                opt.CustomY = yint;
            }
            if (heightint != 0)
            {
                opt.CustomHeight = heightint;
            }
            if (widthint != 0)
            {
                opt.CustomWidth = widthint;
            }
            return(opt);
        }