Beispiel #1
0
        private void imageLoad_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog
            {
                Filter = "Pliki graficzne|*.bmp; *.png; *.jpg; *.gif; *.jpeg"
            };
            Nullable <bool> result = dlg.ShowDialog();

            if (result == true)
            {
                Bitmap    b         = new Bitmap(dlg.FileName);
                Grayscale filterG   = new Grayscale(0.2125, 0.7154, 0.0721);
                Bitmap    grayImage = b;
                if (b.PixelFormat != PixelFormat.Format8bppIndexed)
                {
                    grayImage = filterG.Apply(b);
                    IterativeThreshold filter = new IterativeThreshold(2, 128);
                    filter.ApplyInPlace(grayImage);
                }
                image.Source = BitmapToImageSource(grayImage);

                var path      = Path.GetDirectoryName(dlg.FileName);
                var filename  = Path.GetFileNameWithoutExtension(dlg.FileName);
                var extension = Path.GetExtension(dlg.FileName);
                imageFileName = new string[] { path, filename, extension };

                Bitmap[] shares = null;

                if (twoPixels.IsChecked == true)
                {
                    shares = twoPixelsF(grayImage);
                }
                else if (fourPixels.IsChecked == true)
                {
                    shares = fourPixelsF(grayImage);
                }
                else
                {
                    showAlert("Nieoczekiwany błąd. Pole obok nie zostało zaznaczone.");
                }
                showAlert("Udziały zostały automatycznie zapisane w folderze: " + imageFileName[0]);
                if (shares != null)
                {
                    firstShare.Source  = BitmapToImageSource(shares[0]);
                    secondShare.Source = BitmapToImageSource(shares[1]);
                }
            }
        }
        private System.Drawing.Rectangle?FindHealthbarRect()
        {
            var healthArea = CaptureApexWindow();
            // locating objects
            var blobCounter = new BlobCounter();

            blobCounter.CoupledSizeFiltering = true;
            blobCounter.FilterBlobs          = true;
            blobCounter.MinHeight            = 6;
            blobCounter.MinWidth             = 100;
            blobCounter.MaxHeight            = 15;

            //grayscale
            var bmp = Grayscale.CommonAlgorithms.BT709.Apply(healthArea);
            //Invert invert = new Invert();
            //bmp = invert.Apply(bmp);

            var filter = new IterativeThreshold(2, 4);

            // apply the filter
            filter.ApplyInPlace(bmp);
            blobCounter.ProcessImage(bmp);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            // check for rectangles
            var shapeChecker = new SimpleShapeChecker();

            shapeChecker.AngleError  = 5.0f;
            shapeChecker.LengthError = 0.5f;

            foreach (var blob in blobs)
            {
                List <AForge.IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                List <AForge.IntPoint> cornerPoints;

                try
                {
                    // use the shape checker to extract the corner points
                    if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                    {
                        PolygonSubType subType = shapeChecker.CheckPolygonSubType(cornerPoints);
                        if (subType == PolygonSubType.Trapezoid)
                        {
                            // here i use the graphics class to draw an overlay, but you
                            // could also just use the cornerPoints list to calculate your
                            // x, y, width, height values.
                            List <System.Drawing.Point> Points = new List <System.Drawing.Point>();
                            foreach (var point in cornerPoints)
                            {
                                Points.Add(new System.Drawing.Point(point.X, point.Y));
                            }
                            var boundingBox = GetBoundingBox(Points);
                            var ratio       = (boundingBox.Width / (float)boundingBox.Height);
                            if (ratio > 21.0f && ratio < 24.0f)
                            {
                                return(boundingBox);
                            }
                        }
                    }
                }
                catch (Exception e) { }
            }

            return(null);
        }