Esempio n. 1
0
        // Process new frame
        public void ProcessFrame(ref Bitmap image)
        {
            if (backgroundFrame == null)
            {
                // create initial backgroung image
                backgroundFrame = processingFilter1.Apply(image);

                // get image dimension
                width  = image.Width;
                height = image.Height;

                // just return for the first time
                return;
            }

            Bitmap tmpImage;

            // apply the the first filters sequence
            tmpImage = processingFilter1.Apply(image);

            if (++counter == 2)
            {
                counter = 0;

                // move background towards current frame
                moveTowardsFilter.OverlayImage = tmpImage;
                moveTowardsFilter.ApplyInPlace(backgroundFrame);
            }

            // set backgroud frame as an overlay for difference filter
            differenceFilter.OverlayImage = backgroundFrame;

            // lock temporary image to apply several filters
            bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height),
                                           ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            // apply difference filter
            differenceFilter.ApplyInPlace(bitmapData);
            // apply threshold filter
            thresholdFilter.ApplyInPlace(bitmapData);
            // apply dilatation filter
            Bitmap tmpImage2 = dilatationFilter.Apply(bitmapData);

            // unlock temporary image
            tmpImage.UnlockBits(bitmapData);
            tmpImage.Dispose( );

            // calculate amount of changed pixels
            pixelsChanged = (calculateMotionLevel) ?
                            CalculateWhitePixels(tmpImage2) : 0;

            // find edges
            Bitmap tmpImage2b = edgesFilter.Apply(tmpImage2);

            tmpImage2.Dispose( );

            // extract red channel from the original image
            Bitmap redChannel = extrachChannel.Apply(image);

            //  merge red channel with moving object borders
            mergeFilter.OverlayImage = tmpImage2b;
            Bitmap tmpImage3 = mergeFilter.Apply(redChannel);

            redChannel.Dispose( );
            tmpImage2b.Dispose( );

            // replace red channel in the original image
            replaceChannel.ChannelImage = tmpImage3;
            Bitmap tmpImage4 = replaceChannel.Apply(image);

            tmpImage3.Dispose( );

            image.Dispose( );
            image = tmpImage4;
        }
Esempio n. 2
0
        // Process new frame
        public void ProcessFrame(ref Bitmap image)
        {
            if (backgroundFrame == null)
            {
                // create initial backgroung image
                backgroundFrame = grayscaleFilter.Apply(image);

                // get image dimension
                width  = image.Width;
                height = image.Height;

                // just return for the first time
                return;
            }

            Bitmap tmpImage;

            // apply the grayscale file
            tmpImage = grayscaleFilter.Apply(image);

            // set backgroud frame as an overlay for difference filter
            differenceFilter.OverlayImage = backgroundFrame;

            // apply difference filter
            Bitmap tmpImage2 = differenceFilter.Apply(tmpImage);

            // lock the temporary image and apply some filters on the locked data
            bitmapData = tmpImage2.LockBits(new Rectangle(0, 0, width, height),
                                            ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            // threshold filter
            thresholdFilter.ApplyInPlace(bitmapData);
            // erosion filter
            Bitmap tmpImage3 = erosionFilter.Apply(bitmapData);

            // unlock temporary image
            tmpImage2.UnlockBits(bitmapData);
            tmpImage2.Dispose( );

            // calculate amount of changed pixels
            pixelsChanged = (calculateMotionLevel) ?
                            CalculateWhitePixels(tmpImage3) : 0;

            // dispose old background
            backgroundFrame.Dispose( );
            // set backgound to current
            backgroundFrame = tmpImage;

            // extract red channel from the original image
            Bitmap redChannel = extrachChannel.Apply(image);

            //  merge red channel with moving object
            mergeFilter.OverlayImage = tmpImage3;
            Bitmap tmpImage4 = mergeFilter.Apply(redChannel);

            redChannel.Dispose( );
            tmpImage3.Dispose( );

            // replace red channel in the original image
            replaceChannel.ChannelImage = tmpImage4;
            Bitmap tmpImage5 = replaceChannel.Apply(image);

            tmpImage4.Dispose( );

            image.Dispose( );
            image = tmpImage5;
        }
Esempio n. 3
0
        private int width;// image width

        /// <summary>
        /// The ProcessFrame
        /// </summary>
        /// <param name="image">The image<see cref="Bitmap"/></param>
        public Bitmap ProcessFrame(Bitmap image)
        {
            if (backgroundFrame == null)
            {
                // create initial backgroung image
                backgroundFrame = grayscaleFilter.Apply(image);

                // get image dimension
                width  = image.Width;
                height = image.Height;

                // just return for the first time
                return(null);
            }

            // apply the the grayscale file
            var tmpImage = grayscaleFilter.Apply(image);


            if (++counter == 2)
            {
                counter = 0;

                // move background towards current frame
                moveTowardsFilter.OverlayImage = tmpImage;
                moveTowardsFilter.ApplyInPlace(backgroundFrame);
            }

            // set backgroud frame as an overlay for difference filter
            differenceFilter.OverlayImage = backgroundFrame;

            // lock temporary image to apply several filters
            bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height),
                                           ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            // apply difference filter
            differenceFilter.ApplyInPlace(bitmapData);
            // apply threshold filter
            thresholdFilter.ApplyInPlace(bitmapData);

            pixelsChanged = CalculateWhitePixels(bitmapData);

            var tmpImage2 = openingFilter.Apply(bitmapData);

            // unlock temporary image
            tmpImage.UnlockBits(bitmapData);
            tmpImage.Dispose();

            // apply edges filter
            var tmpImage2b = edgesFilter.Apply(tmpImage2);

            tmpImage2.Dispose();

            // extract red channel from the original image
            var redChannel = extrachChannel.Apply(image);

            //  merge red channel with moving object borders
            mergeFilter.OverlayImage = tmpImage2b;
            var tmpImage3 = mergeFilter.Apply(redChannel);

            redChannel.Dispose();
            tmpImage2b.Dispose();

            // replace red channel in the original image
            replaceChannel.ChannelImage = tmpImage3;
            var tmpImage4 = replaceChannel.Apply(image);

            tmpImage3.Dispose();

            image.Dispose();
            image = tmpImage4;
            return(image);
        }
Esempio n. 4
0
        public Binarize(UISettings ui, FileData file) : base(ui, file)
        {
            try {
                Invert AFinvert = new Invert();
                switch (ui.ThresholdIndex)         // threshold method selection "Global mean" / "Local adaptive"
                {
                case 0:                            // Global
                    if (ui.ThreshGlobalIsAbsolute) // use absolute
                    {
                        Threshold AFglobalbinary = new Threshold(ui.ThreshGlobalAbsolute);
                        UnmanagedBlackWhite = AFglobalbinary.Apply(UnmanagedGray);
                    }
                    else                                 // use relative
                    {
                        ImageStatistics stats          = new ImageStatistics(UnmanagedGray, AFinvert.Apply(UnmanagedExclude));
                        Threshold       AFglobalbinary = new Threshold(stats.Gray.Center2QuantileValue(1.0d * ui.ThreshGlobalRelative / 255.0d));
                        UnmanagedBlackWhite = AFglobalbinary.Apply(UnmanagedGray);
                    }
                    break;

                case 1:                         // Local
                    BradleyLocalThresholdingX AFlocalbinary = new BradleyLocalThresholdingX()
                    {
                        PixelBrightnessDifferenceLimit = ui.ThreshLocalBrightnessDifference,
                        WindowSize = ui.ThreshLocalWindowSize, UpperLimit = 250
                    };
                    UnmanagedBlackWhite = AFlocalbinary.Apply(UnmanagedGray);
                    break;
                }
                if (ui.FillHoleAirspaceSwitch && ui.FillHoleAirspace != 0)                 // fill holes of airspaces
                {
                    FillHoles AFfillinair = new FillHoles()
                    {
                        CoupledSizeFiltering = true, MaxHoleHeight = ui.FillHoleAirspace, MaxHoleWidth = ui.FillHoleAirspace
                    };
                    //FillHolesArea AFfillinair=new FillHolesArea() { MaxHoleArea=ui.FillHoleAirspace };
                    AFfillinair.ApplyInPlace(UnmanagedBlackWhite);
                }
                UnmanagedBlackWhite = AFinvert.Apply(UnmanagedBlackWhite);
                if (ui.FillHoleTissueSwitch && ui.FillHoleTissue != 0)                 // fill holes of tissue
                {
                    FillHoles AFfillintissue = new FillHoles()
                    {
                        CoupledSizeFiltering = true, MaxHoleHeight = ui.FillHoleTissue, MaxHoleWidth = ui.FillHoleTissue
                    };
                    //FillHolesArea AFfillintissue=new FillHolesArea() { MaxHoleArea=ui.FillHoleTissue };
                    AFfillintissue.ApplyInPlace(UnmanagedBlackWhite);
                }
                if (ui.MorphoDilateSwitch && ui.MorphoDilate != 0)               // Morphological Dilate
                {
                    int n = (Math.Max(ui.MorphoDilate, 0) * 2 + 1); short[,] morphmatrix = new short[n, n];
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            morphmatrix[i, j] = 1;
                        }
                    }
                    Dilatation AFdilate = new Dilatation(morphmatrix);
                    AFdilate.ApplyInPlace(UnmanagedBlackWhite);
                }
                if (ui.MorphoErodeSwitch && ui.MorphoErode != 0)               // Morphological Erode

                {
                    int n = (Math.Max(ui.MorphoErode, 0) * 2 + 1); short[,] morphmatrix = new short[n, n];
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            morphmatrix[i, j] = 1;
                        }
                    }
                    Erosion AFerode = new Erosion(morphmatrix);
                    AFerode.ApplyInPlace(UnmanagedBlackWhite);
                }
                if (ui.ExcludeColorSwitch)
                {
                    NonParen_SumArea = ui.um2px2 * UnmanagedExclude.NonBlackArea();
                }
                if (ui.BlobMinSwitch)
                {
                    Low_Threshold = Math.Pow(10.0d, ui.BlobMin) - 1;
                }
                else
                {
                    Low_Threshold = 0.0d;
                }
                if (ui.BlobMaxSwitch)
                {
                    High_Threshold = Math.Pow(10.0d, ui.BlobMax) - 1;
                }
                else
                {
                    High_Threshold = int.MaxValue;
                }

                if (ui.BlobMinSwitch || ui.BlobMaxSwitch)
                {
                    Merge       AFmerge1  = new Merge(UnmanagedExclude);
                    ExcludeSize AFexcsize = new ExcludeSize()
                    {
                        Low = (int)Math.Round(Low_Threshold / ui.um2px2), High = (int)Math.Round(Math.Min(int.MaxValue, High_Threshold / ui.um2px2))
                    };
                    Merge AFmerge2 = new Merge(AFexcsize.Apply(AFinvert.Apply(AFmerge1.Apply(UnmanagedBlackWhite))));
                    AFmerge2.ApplyInPlace(UnmanagedExclude);
                    Low_SumArea  = ui.um2px2 * AFexcsize.LowCount;
                    High_SumArea = ui.um2px2 * AFexcsize.HighCount;
                }
            } catch { throw new Exception("Error Occured During Binarization"); }
        }
Esempio n. 5
0
        static void OnNewGameState(GameState gs)
        {
            try
            {
                bool DEBUGING = true;

                if (gs.Map.GameState == DOTA_GameState.Undefined)
                {
                    return;
                }

                if (gs.Previously.Map.GameState == GS_WAITING && gs.Map.GameState == GS_DRAFTING)
                {
                    var client   = new HGV.Basilius.MetaClient();
                    var heroKeys = client.GetADHeroes().ToDictionary(_ => _.Key, _ => _.Id);

                    Console.WriteLine("Drafting");
                    Console.WriteLine("Hero {0}:{1}", gs.Hero.ID, gs.Hero.Name);

                    Thread.Sleep(4000); // Wait for ability draft screen to load.

                    var captureImage = ScreenCapture.CaptureApplication();

                    if (DEBUGING == true)
                    {
                        captureImage.Save(".//output//capture.png");
                    }

                    Bitmap overlay;
                    Bitmap source;
                    {
                        // create filter
                        var colorFiltering = new ColorFiltering();
                        // set channels' ranges to keep
                        colorFiltering.Red   = new IntRange(50, 52);
                        colorFiltering.Green = new IntRange(50, 52);
                        colorFiltering.Blue  = new IntRange(50, 52);
                        // apply the filter
                        var image = colorFiltering.Apply(captureImage);

                        var gfilter = Grayscale.CommonAlgorithms.BT709;
                        source = gfilter.Apply(image);
                    }

                    {
                        // create filter
                        var colorFiltering = new ColorFiltering();
                        // set channels' ranges to keep
                        colorFiltering.Red   = new IntRange(194, 204);
                        colorFiltering.Green = new IntRange(211, 221);
                        colorFiltering.Blue  = new IntRange(165, 170);
                        // apply the filter
                        var image = colorFiltering.Apply(captureImage);

                        var gfilter = Grayscale.CommonAlgorithms.BT709;
                        overlay = gfilter.Apply(image);
                    }

                    // create filter
                    var mergeFilter = new Merge(overlay);
                    var resultImage = mergeFilter.Apply(source);

                    if (DEBUGING == true)
                    {
                        resultImage.Save("./output/bounds.png");
                    }

                    var shapeChecker = new SimpleShapeChecker();

                    var bc = new BlobCounter();
                    bc.FilterBlobs = true;
                    bc.MinHeight   = 5;
                    bc.MinWidth    = 5;
                    bc.ProcessImage(resultImage);

                    var blobs = bc.GetObjectsInformation();

                    var size = new Size();

                    var left   = 0;
                    var top    = 0;
                    var right  = 0;
                    var height = 0;

                    foreach (var blob in blobs)
                    {
                        var points = bc.GetBlobsEdgePoints(blob);
                        if (shapeChecker.IsCircle(points))
                        {
                            right = blob.Rectangle.Left;
                        }
                        else if (shapeChecker.IsQuadrilateral(points))
                        {
                            left   = blob.Rectangle.Right;
                            top    = blob.Rectangle.Top;
                            height = blob.Rectangle.Height;

                            const double HERO_RATIO = 0.76;
                            size.Width  = (int)Math.Round(height * HERO_RATIO);
                            size.Height = height;
                        }
                    }

                    var bounds  = new Rectangle(left, top, right - left, height);
                    var spacing = (bounds.Width - (size.Width * 10)) / 11;
                    bounds.X     += spacing;
                    bounds.Width -= (spacing * 2);

                    if (DEBUGING == true)
                    {
                        Crop filter      = new Crop(bounds);
                        var  imageHeader = filter.Apply(captureImage);
                        imageHeader.Save("./output/header.png");
                    }

                    var collection = new List <Bitmap>();
                    for (int i = 0; i < 10; i++)
                    {
                        var x = (spacing * i) + (size.Width * i) + bounds.X;

                        Crop cropFilter  = new Crop(new Rectangle(x, bounds.Y, size.Width, size.Height));
                        var  cropedImage = cropFilter.Apply(captureImage);
                        collection.Add(cropedImage);

                        if (DEBUGING == true)
                        {
                            cropedImage.Save($"./output/hero[{i}].png");
                        }
                    }

                    var templates    = new List <TemplatePackage>();
                    var resizeFilter = new ResizeBicubic(size.Width, size.Height);
                    var files        = Directory.GetFiles(".//heroes");
                    foreach (var file in files)
                    {
                        var key    = Path.GetFileNameWithoutExtension(file);
                        var heroId = 0;
                        heroKeys.TryGetValue(key, out heroId);

                        var template        = Bitmap.FromFile(file) as Bitmap;
                        var resizedTemplate = resizeFilter.Apply(template);
                        var package         = new TemplatePackage()
                        {
                            Key   = key,
                            Hero  = heroId,
                            Image = resizedTemplate,
                        };
                        templates.Add(package);
                    }

                    var tm   = new ExhaustiveTemplateMatching(0.8f);
                    var data = new List <DataPackage>();
                    for (int i = 0; i < collection.Count; i++)
                    {
                        var image = collection[i];

                        foreach (var template in templates)
                        {
                            var matchings = tm.ProcessImage(image, template.Image);
                            foreach (var match in matchings)
                            {
                                var package = new DataPackage()
                                {
                                    Similarity = match.Similarity,
                                    Bounds     = match.Rectangle,
                                    Key        = template.Key,
                                    Hero       = template.Hero,
                                    Index      = i,
                                };
                                data.Add(package);
                            }
                        }
                    }

                    var heroes = new List <int>();
                    var groups = data.GroupBy(_ => _.Index).OrderBy(_ => _.Key).ToList();
                    foreach (var group in groups)
                    {
                        var item = group.OrderByDescending(_ => _.Similarity).Take(1).FirstOrDefault();

                        heroes.Add(item.Hero);

                        if (DEBUGING == true)
                        {
                            Console.WriteLine("{0}:{1} ({2})", item.Hero, item.Key, item.Similarity);
                        }
                    }

                    Console.WriteLine("Launching Drafter");

                    var roster = string.Join(",", heroes.ToArray());
                    Process.Start("https://hgv-desolator.azurewebsites.net/#/draft?roster=" + roster);

                    Console.WriteLine("");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error");
                // Next Time Gadget. NEXT TIME!
            }
        }