private async Task <IList <Rect> > GetPlateRectanglesAsync(Bitmap image)
        {
            var rectanglePoints = await Task.Factory.StartNew(() =>
            {
                var blobCounter = new BlobCounter
                {
                    FilterBlobs = true,
                    MinHeight   = 5,
                    MinWidth    = 5
                };

                blobCounter.ProcessImage(image);

                var blobs        = blobCounter.GetObjectsInformation();
                var shapeChecker = new SimpleShapeChecker();
                var rectPoints   = new List <List <AForgePoint> >();

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

                    if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                    {
                        var polygonType = shapeChecker.CheckPolygonSubType(cornerPoints);

                        if (polygonType == PolygonSubType.Rectangle ||
                            polygonType == PolygonSubType.Parallelogram)
                        {
                            foreach (var point in cornerPoints)
                            {
                                points.Add(new AForgePoint(point.X, point.Y));
                            }

                            rectPoints.Add(points);
                        }
                    }
                }

                return(rectPoints);
            });

            var rects          = rectanglePoints.Select(points => GetRect(points)).ToList();
            var images         = new List <WriteableBitmap>();
            var cadidatesRects = new List <Rect>();

            foreach (var rect in rects)
            {
                var ratio = rect.Height / rect.Width;

                if (ratio >= MinPlateRatio && ratio <= MaxPlateRatio)
                {
                    cadidatesRects.Add(rect);
                }
            }

            return(cadidatesRects);
        }
Beispiel #2
0
        private UMat findSquares(UMat img)
        {
            // Open your image
            string path  = "test.png";
            Bitmap image = img.Bitmap; //(Bitmap)Bitmap.FromFile(path);

            // locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = false;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;

            blobCounter.ProcessImage(image);
            Blob[] blobs = blobCounter.GetObjectsInformation();

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


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

                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners form a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        // 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 <AForge.Point> Points = new List <AForge.Point>();
                        foreach (var point in cornerPoints)
                        {
                            Points.Add(new AForge.Point(point.X, point.Y));
                        }

                        PointF[] P = new PointF[Points.Count];
                        for (int i = 0; i < P.Length; i++)
                        {
                            P[i] = new PointF(Points[i].X, Points[i].Y);
                        }
                        Bitmap   newBitmap = new Bitmap(image.Width, image.Height);
                        Graphics g         = Graphics.FromImage(newBitmap);
                        g.DrawImage(image, 0, 0);
                        //Graphics g = Graphics.FromImage(image);
                        g.DrawPolygon(new Pen(Color.Red, 5.0f), P);

                        image = newBitmap;

                        //image.Save("result.png");
                    }
                }
            }
            return((new Image <Bgr, Byte>(image)).ToUMat());
        }
Beispiel #3
0
        public void GetDisplayCorner(ManagedImage m_processedImage, out List <IntPoint> displaycornerPoints)
        {
            // get display corner position
            //Process Image to 1bpp to increase SNR
            Bitmap m_orig = m_processedImage.bitmap.Clone(new Rectangle(0, 0, m_processedImage.bitmap.Width, m_processedImage.bitmap.Height), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
            // only support the 32bppArgb for Aforge Blob Counter
            Bitmap processbmp = m_orig.Clone(new Rectangle(0, 0, m_orig.Width, m_orig.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            BlobCounter bbc = new BlobCounter();

            bbc.FilterBlobs = true;
            bbc.MinHeight   = 5;
            bbc.MinWidth    = 5;

            bbc.ProcessImage(processbmp);

            Blob[]             blobs        = bbc.GetObjectsInformation();
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            foreach (var blob in blobs)
            {
                List <IntPoint> edgePoints = bbc.GetBlobsEdgePoints(blob);
                List <IntPoint> cornerPoints;
                int             count = 0;

                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners from a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        flagPoints = cornerPoints;
                        continue;
                    }
                    else
                    {
                        MessageBox.Show("Cannot Find the Display");
                        flagPoints = null;
                        //                       picturebox_test.Image = m;
                        count++;
                        if (count < 3)
                        {
                            continue;
                        }
                        else
                        {
                            MessageBox.Show("Cannot Find the Display for 3 times. Quit Program");
                            Environment.ExitCode = -1;
                            Application.Exit();
                        }
                    }
                }
            }
            displaycornerPoints = flagPoints;
        }
        /// <summary>
        /// Use AForge to automatically detect the image borders, and crop it to size
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutoFitImage_Click(object sender, RoutedEventArgs e)
        {
            if (_LocalPersistentObject.bitmapProcessingImage == null)
            {
                return;
            }

            WriteableBitmap bmp = _LocalPersistentObject.bitmapProcessingImage as WriteableBitmap;

            bmp = bmp.Resize(1024, 1024, WriteableBitmapExtensions.Interpolation.Bilinear);
            Bitmap image = AForge.Imaging.Image.Clone((Bitmap)bmp, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 50;
            blobCounter.MinWidth    = 50;

            blobCounter.ProcessImage(image);
            Blob[] blobs = blobCounter.GetObjectsInformation();

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

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

                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners form a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Square)
                    {
                        // 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 <AForge.Point> Points = new List <AForge.Point>();

                        bmp = bmp.Crop(cornerPoints[0].X,
                                       cornerPoints[0].Y,
                                       cornerPoints[2].X,
                                       cornerPoints[2].Y);
                    }
                }
            }

            bmp = bmp.Resize(640, 640, WriteableBitmapExtensions.Interpolation.Bilinear);
            _LocalPersistentObject.bitmapProcessingImage = (WriteableBitmap)bmp;

            MainImageFrame.Navigate(typeof(ImageLoadedView), _LocalPersistentObject, new SuppressNavigationTransitionInfo());
        }
Beispiel #5
0
        public void GetDisplayCornerfrombmp(Bitmap processbmp, out List <IntPoint> displaycornerPoints)
        {
            BlobCounter bbc = new BlobCounter();

            bbc.FilterBlobs = true;
            bbc.MinHeight   = 5;
            bbc.MinWidth    = 5;

            bbc.ProcessImage(processbmp);

            Blob[]             blobs        = bbc.GetObjectsInformation();
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

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


                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners from a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        flagPoints = cornerPoints;
                        List <IntPoint> tmpPoints = new List <IntPoint>(flagPoints);
                        int             max = new int[] { tmpPoints[0].X, tmpPoints[1].X, tmpPoints[2].X, tmpPoints[3].X }.Max();
                        int             min = new int[] { tmpPoints[0].X, tmpPoints[1].X, tmpPoints[2].X, tmpPoints[3].X }.Min();

                        if (max - min > 500)
                        {
                            break;
                        }

                        continue;
                        //break;
                    }
                    else
                    {
                        flagPoints = null;
                        continue;
                    }
                }
            }

            if (flagPoints == null)
            {
                MessageBox.Show("Cannot Find the Display");
            }

            displaycornerPoints = flagPoints;
        }
Beispiel #6
0
        private void button7_Click(object sender, EventArgs e)
        {
            Bitmap image = (Bitmap)pictureBox6.Image;

            // create instance of blob counter

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;
            blobCounter.MaxWidth    = 350;
            blobCounter.MaxHeight   = 350;
            //process the dilated image
            blobCounter.ProcessImage(image);
            // get info about detected objects
            Blob[] blobs = blobCounter.GetObjectsInformation();

            // create a graphics object to draw on the image and a pen
            Graphics g      = Graphics.FromImage(image);
            Pen      Redpen = new Pen(Color.Red, 4);

            SimpleShapeChecker shapechecker = new SimpleShapeChecker();

            //check in image and draw around the object found as rectangle
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgepoints = blobCounter.GetBlobsEdgePoints(blobs[i]);


                List <Point> Points = new List <Point>();
                if (shapechecker.IsQuadrilateral(edgepoints, out cornerpoints))
                {
                    if (shapechecker.CheckPolygonSubType(cornerpoints) == PolygonSubType.Rectangle)
                    {
                        g.DrawPolygon(Redpen, cornerpoints.Select(p => new System.Drawing.Point(p.X, p.Y)).ToArray());
                    }
                }
                foreach (var point in cornerpoints)
                {
                    Crop filter = new Crop(new Rectangle(point.X, point.Y, image.Width, image.Height));
                    newImage = filter.Apply(image);
                }
            }


            Redpen.Dispose();
            g.Dispose();


            pictureBox7.Image = newImage;
        }
        private void checkShape(SimpleShapeChecker shapeChecker, List <IntPoint> edgePts)
        {
            AForge.Point    center;
            float           radius;
            List <IntPoint> corners;

            if (edgePts.Count > 4 && shapeChecker.IsCircle(edgePts, out center, out radius))
            {
                //shape is a circle
                drawEllipse(center, radius);
                shapeCount[0]++;
            }
            else if (edgePts.Count > 3 && shapeChecker.IsConvexPolygon(edgePts, out corners))
            {
                PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                if (subType == PolygonSubType.Unknown)
                {
                    shapeCount[4]++;
                }
                else if (subType == PolygonSubType.Square)
                {
                    shapeCount[2]++;
                }
                else if (subType == PolygonSubType.Rectangle)
                {
                    shapeCount[3]++;
                }
                else if (subType == PolygonSubType.Trapezoid)
                {
                    shapeCount[5]++;
                }
                else if (subType == PolygonSubType.EquilateralTriangle)
                {
                    shapeCount[1]++;
                }
                else if (subType == PolygonSubType.IsoscelesTriangle)
                {
                    shapeCount[1]++;
                }

                drawPolygon(corners);
            }
            else if (shapeChecker.IsTriangle(edgePts))
            {
                //triangle
                shapeCount[1]++;
            }
        }
Beispiel #8
0
        private Tuple <Bitmap, IntPoint, IntPoint, IntPoint, IntPoint> ShapeCheck(Bitmap image)
        {
            IntPoint p1 = new IntPoint();
            IntPoint p2 = new IntPoint();
            IntPoint p3 = new IntPoint();
            IntPoint p4 = new IntPoint();

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 500;
            blobCounter.MinWidth    = 400;
            blobCounter.ProcessImage(image);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

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

                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        var points = SortCorners(cornerPoints, image);
                        p1 = points.Item1;
                        p2 = points.Item2;
                        p3 = points.Item3;
                        p4 = points.Item4;

                        lblInfo.Content = "p1: " + p1.ToString() + " | p2: " + p2.ToString() + " | p3: " + p3.ToString() + " | p4:  " + p4.ToString();
                        List <IntPoint> Points = new List <IntPoint>();

                        foreach (var point in cornerPoints)
                        {
                            Points.Add(new IntPoint(point.X, point.Y));
                        }

                        Graphics g = Graphics.FromImage(image);
                        g.DrawPolygon(new Pen(Color.Red, 5.0f), Points.Select(p => new System.Drawing.Point(p.X, p.Y)).ToArray());
                    }
                }
            }

            return(Tuple.Create(image, p1, p2, p3, p4));
        }
Beispiel #9
0
        public static Bitmap buscaRectangulos(Bitmap bitmap)
        {
            Blob[]    blobs;
            Rectangle area = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            // lock image
            BitmapData bitmapData = bitmap.LockBits(area, ImageLockMode.ReadWrite, bitmap.PixelFormat);
            // Filtramos la imagen para determinar hasts cuando de ROJO, VERDE Y AZUL se permitiráanalizar
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red              = new IntRange(0, 104);
            colorFilter.Green            = new IntRange(0, 104);
            colorFilter.Blue             = new IntRange(0, 104);
            colorFilter.FillOutsideRange = false; colorFilter.ApplyInPlace(bitmapData);
            // Localizamos objetos en la imagen
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 15;
            blobCounter.MinWidth    = 15;
            blobCounter.ProcessImage(bitmapData);
            blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);
            // step 3 -check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            Graphics           g            = Graphics.FromImage(bitmap);
            Pen yellowPen = new Pen(Color.Yellow, 2);

            // Para cada figuura encontrada
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List <IntPoint> corners;
                // Cuadrado
                if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
                {//Obtenemos cuantas esquinas tiene el shape
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
                    //Pintamos las áreas detectadas
                    areaInteres(g, corners);
                }
            }
            //yellowPen.Dispose();
            g.Dispose();
            // put new image to clipboard
            Clipboard.SetDataObject(bitmap);
            // and to picture box
            return(bitmap);
        }
Beispiel #10
0
        // =========================================================
        private Bitmap DrawRectanglesFunct_OLD(Bitmap image)
        {
            // step 1 - turn background to black (done)

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 3;
            blobCounter.MinWidth    = 3;
            blobCounter.ProcessImage(image);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            // step 3 - check objects' type and do what you do:
            Graphics g   = Graphics.FromImage(image);
            Pen      pen = new Pen(Color.DarkOrange, 2);

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                SimpleShapeChecker ShapeChecker = new SimpleShapeChecker();
                List <IntPoint>    edgePoints   = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List <IntPoint>    cornerPoints;

                // fine tune ShapeChecker
                ShapeChecker.AngleError              = 15;   // default 7
                ShapeChecker.LengthError             = 0.3F; // default 0.1 (10%)
                ShapeChecker.MinAcceptableDistortion = 0.9F; // in pixels, default 0.5
                ShapeChecker.RelativeDistortionLimit = 0.2F; // default 0.03 (3%)

                // use the Outline checker to extract the corner points
                if (ShapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners form a rectangle
                    if (ShapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        List <IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
                        g.DrawPolygon(pen, ToPointsArray(corners));
                    }
                }
            }
            return(image);
        }
Beispiel #11
0
        public void GetDisplayCornerfrombmp(Bitmap processbmp, out List <IntPoint> displaycornerPoints)
        {
            BlobCounter bbc = new BlobCounter();

            bbc.FilterBlobs = true;
            bbc.MinHeight   = 5;
            bbc.MinWidth    = 5;

            bbc.ProcessImage(processbmp);

            Blob[]             blobs        = bbc.GetObjectsInformation();
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

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


                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners from a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        flagPoints = cornerPoints;
                        continue;
                    }
                    else
                    {
                        MessageBox.Show("Cannot Find the Display");
                        flagPoints = null;
                        //                       picturebox_test.Image = m;
                        continue;
                    }
                }
            }
            displaycornerPoints = flagPoints;
        }
Beispiel #12
0
        private bool IsValid(Bitmap image, SimpleShapeChecker shapeChecker, List <IntPoint> blobEdgePoints,
                             out List <IntPoint> cornerPoints)
        {
            if (!shapeChecker.IsQuadrilateral(blobEdgePoints, out cornerPoints))
            {
                return(false);
            }
            if (shapeChecker.CheckPolygonSubType(cornerPoints) != PolygonSubType.Rectangle)
            {
                return(false);
            }
            if (!IsSameSizeAsLogo(cornerPoints))
            {
                return(false);
            }
            if (!IsWhiteRectangle(image, cornerPoints))
            {
                return(false);
            }

            return(true);
        }
Beispiel #13
0
        public List <IntPoint> GetDisplayCorner(Bitmap bitmap)
        {
            BlobCounter bbc = new BlobCounter();

            bbc.FilterBlobs = true;
            bbc.MinHeight   = 5;
            bbc.MinWidth    = 5;
            bbc.ProcessImage(bitmap);

            Blob[]             blobs        = bbc.GetObjectsInformation();
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

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

                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners from a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
                    {
                        flagPoints = cornerPoints;
                        continue;
                    }
                    else
                    {
                        flagPoints = null;
                        continue;
                    }
                }
            }

            return(flagPoints);
        }
        private void ProcessImage(Bitmap bitmap)
        {
            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red              = new IntRange(0, 64);
            colorFilter.Green            = new IntRange(0, 64);
            colorFilter.Blue             = new IntRange(0, 64);
            colorFilter.FillOutsideRange = false;

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);


            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            Graphics g = Graphics.FromImage(bitmap);


            Pen brownPen = new Pen(Color.Red, 3);


            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);


                List <IntPoint> corners;


                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);


                    if (subType == PolygonSubType.Unknown)
                    {
                        if (corners.Count == 4)
                        {
                            g.DrawPolygon(brownPen, ToPointsArray(corners));
                        }
                    }
                    else
                    {
                        if (corners.Count == 4)
                        {
                            g.DrawPolygon(brownPen, ToPointsArray(corners));
                        }
                    }
                }
            }

            brownPen.Dispose();
            g.Dispose();


            Clipboard.SetDataObject(bitmap);

            pictureBox5.Image = bitmap;
        }
Beispiel #15
0
        // Process image
        private void ProcessImage(Bitmap bitmap)
        {
            // lock image
            var bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            var colorFilter = new ColorFiltering
            {
                Red              = new IntRange(0, 64),
                Green            = new IntRange(0, 64),
                Blue             = new IntRange(0, 64),
                FillOutsideRange = false
            };

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            var blobCounter = new BlobCounter
            {
                FilterBlobs = true,
                MinHeight   = 5,
                MinWidth    = 5
            };

            blobCounter.ProcessImage(bitmapData);
            var blobs = blobCounter.GetObjectsInformation();

            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            var shapeChecker = new SimpleShapeChecker();

            var g         = Graphics.FromImage(bitmap);
            var yellowPen = new Pen(Color.Yellow, 2);            // circles
            var redPen    = new Pen(Color.Red, 2);               // quadrilateral
            var brownPen  = new Pen(Color.Brown, 2);             // quadrilateral with known sub-type
            var greenPen  = new Pen(Color.Green, 2);             // known triangle
            var bluePen   = new Pen(Color.Blue, 2);              // triangle

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                var edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                AForge.Point center;
                float        radius;

                // is circle ?
                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    g.DrawEllipse(yellowPen,
                                  (float)(center.X - radius), (float)(center.Y - radius),
                                  (float)(radius * 2), (float)(radius * 2));
                }
                else
                {
                    List <IntPoint> corners;

                    // is triangle or quadrilateral
                    if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                    {
                        // get sub-type
                        var subType = shapeChecker.CheckPolygonSubType(corners);

                        Pen pen;

                        if (subType == PolygonSubType.Unknown)
                        {
                            pen = (corners.Count == 4) ? redPen : bluePen;
                        }
                        else
                        {
                            pen = (corners.Count == 4) ? brownPen : greenPen;
                        }

                        g.DrawPolygon(pen, ToPointsArray(corners));
                    }
                }
            }

            yellowPen.Dispose();
            redPen.Dispose();
            greenPen.Dispose();
            bluePen.Dispose();
            brownPen.Dispose();
            g.Dispose();

            // put new image to clipboard
            Clipboard.SetDataObject(bitmap);
            // and to picture box
            pictureBox.Image = bitmap;

            UpdatePictureBoxPosition();
        }
Beispiel #16
0
        public void nesnebul(Bitmap image)
        {
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.MinWidth     = 5;
            blobCounter.MinHeight    = 5;
            blobCounter.FilterBlobs  = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;
            //Grayscale griFiltre = new Grayscale(0.2125, 0.7154, 0.0721);
            //Grayscale griFiltre = new Grayscale(0.2, 0.2, 0.2);
            //Bitmap griImage = griFiltre.Apply(image);

            BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
            // grayscaling
            Grayscale      grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            UnmanagedImage grayImage       = grayscaleFilter.Apply(new UnmanagedImage(objectsData));

            // unlock image
            image.UnlockBits(objectsData);


            blobCounter.ProcessImage(image);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            Blob[]      blobs = blobCounter.GetObjectsInformation();
            pictureBox2.Image = image;



            if (radio_single_object_track.Checked)
            {
                //Single Object Tracking Single Tracking--------

                foreach (Rectangle recs in rects)
                {
                    if (rects.Length > 0)
                    {
                        Rectangle objectRect = rects[0];
                        //Graphics g = Graphics.FromImage(image);
                        Graphics g  = pictureBox1.CreateGraphics();
                        Graphics g2 = pictureBox2.CreateGraphics();
                        using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
                        {
                            g.DrawRectangle(pen, objectRect);
                        }
                        //Coordinates of the rectified rectangle are taken.
                        int objectX = objectRect.X + (objectRect.Width / 2);
                        int objectY = objectRect.Y + (objectRect.Height / 2);
                        g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1));
                        Rectangle rec = new Rectangle(objectX, objectY, 5, 5);
                        g2.DrawEllipse(Pens.White, rec);
                        g.Dispose();

                        if (chek_show_coordi.Checked)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                richTextBox1.Text = objectRect.Location.ToString() + "\n" + richTextBox1.Text + "\n";;
                            });
                        }
                    }
                }
            }



            if (radio_multi_obj_track.Checked)
            {
                //Multi tracking Çoklu cisim Takibi-------

                for (int i = 0; rects.Length > i; i++)
                {
                    Rectangle objectRect = rects[i];
                    //Graphics g = Graphics.FromImage(image);
                    Graphics g = pictureBox1.CreateGraphics();
                    using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
                    {
                        g.DrawRectangle(pen, objectRect);
                        g.DrawString((i + 1).ToString(), new Font("Arial", 12), Brushes.Red, objectRect);
                    }
                    //Cizdirilen Dikdörtgenin Koordinatlari aliniyor.
                    int objectX = objectRect.X + (objectRect.Width / 2);
                    int objectY = objectRect.Y + (objectRect.Height / 2);
                    g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1));


                    if (chck_dis_meter.Checked)
                    {
                        if (rects.Length > 1)
                        {
                            for (int j = 0; j < rects.Length - 1; j++)
                            {
                                int ilkx = (rects[j].Right + rects[j].Left) / 2;
                                int ilky = (rects[j].Top + rects[j].Bottom) / 2;

                                int ikix = (rects[j + 1].Right + rects[j + 1].Left) / 2;
                                int ikiy = (rects[j + 1].Top + rects[j + 1].Bottom) / 2;

                                g = pictureBox1.CreateGraphics();
                                g.DrawLine(Pens.Red, rects[j].Location, rects[j + 1].Location);
                                g.DrawLine(Pens.Blue, rects[0].Location, rects[rects.Length - 1].Location);
                                g.DrawLine(Pens.Red, ilkx, ilky, ikix, ikiy);
                            }
                        }

                        if (rects.Length == 2)
                        {
                            Rectangle ilk = rects[0];
                            Rectangle iki = rects[1];

                            int ilkX = ilk.X + (ilk.Width / 2);
                            int ilkY = ilk.Y + (ilk.Height / 2);

                            int ikiX = iki.X + (iki.Width / 2);
                            int ikiY = iki.Y + (iki.Height / 2);

                            //1 pixel (X) = 0.0264583333333334 centimeter [cm]

                            double formul = Math.Floor((Math.Sqrt((Math.Pow((ilkX - ikiX), 2)) + Math.Pow((ilkY - ikiY), 2))) * 0.0264);

                            string uzaklikY = "Y-" + Convert.ToString(ilkX - ikiX);
                            string uzaklikX = "X-" + Convert.ToString(ilkY - ikiY);

                            string distance = uzaklikX + " " + uzaklikY;

                            AForge.Imaging.Drawing.Line(objectsData, new IntPoint((int)ilkX, (int)ilkY), new IntPoint((int)ikiX, (int)ikiY), Color.Blue);


                            this.Invoke((MethodInvoker) delegate
                            {
                                richTextBox2.Text = formul.ToString() + " cm\n" + richTextBox2.Text + " cm\n";;
                            });


                            if (check_dist_coord.Checked)
                            {
                                this.Invoke((MethodInvoker) delegate
                                {
                                    richTextBox3.Text = distance.ToString() + "\n" + richTextBox3.Text + "\n";;
                                });
                            }
                        }
                    }


                    g.Dispose();

                    //     this.Invoke((MethodInvoker)delegate
                    //{
                    //    richTextBox1.Text = objectRect.Location.ToString() + "\n" + richTextBox1.Text + "\n"; ;
                    //});
                }
            }



            if (radio_geometric.Checked)
            {
                SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

                Graphics g         = pictureBox1.CreateGraphics();
                Pen      yellowPen = new Pen(Color.Yellow, 2); // circles
                Pen      redPen    = new Pen(Color.Red, 2);    // quadrilateral
                Pen      brownPen  = new Pen(Color.Brown, 2);  // quadrilateral with known sub-type
                Pen      greenPen  = new Pen(Color.Green, 2);  // known triangle
                Pen      bluePen   = new Pen(Color.Blue, 2);   // triangle

                for (int i = 0, n = blobs.Length; i < n; i++)
                {
                    List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                    AForge.Point center;
                    float        radius;

                    // is circle ?
                    if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                    {
                        g.DrawEllipse(yellowPen,
                                      (float)(center.X - radius), (float)(center.Y - radius),
                                      (float)(radius * 2), (float)(radius * 2));
                    }
                    else
                    {
                        List <IntPoint> corners;

                        // is triangle or quadrilateral
                        if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                        {
                            // get sub-type
                            PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                            Pen pen;

                            if (subType == PolygonSubType.Unknown)
                            {
                                pen = (corners.Count == 4) ? redPen : bluePen;
                            }
                            else
                            {
                                pen = (corners.Count == 4) ? brownPen : greenPen;
                            }

                            g.DrawPolygon(pen, ToPointsArray(corners));
                        }
                    }
                }

                yellowPen.Dispose();
                redPen.Dispose();
                greenPen.Dispose();
                bluePen.Dispose();
                brownPen.Dispose();
                g.Dispose();
            }
        }
        private void ProcessImage(Bitmap bitmap)
        {
            if (CBApplyCanny.Checked)
            {
                Bitmap            video2 = (Bitmap)bitmap.Clone();
                Grayscale         gray   = new Grayscale(0.2125, 0.7154, 0.0721);
                Bitmap            video3 = gray.Apply(video2);
                CannyEdgeDetector canny  = new CannyEdgeDetector(0, 70);
                canny.ApplyInPlace(video3);
                //PictureViewerEditor.Image = (Bitmap)Image.FromFile(imageFileName);
                PBCanny.Image = (Bitmap)video3.Clone();//assign the pointer back to the clone
                //if (CBFindCircles.Checked)
                //{
                //    //System.Drawing.Image returnImage = null;
                //    //returnImage = (System.Drawing.Image)CaptureBox.Image.Clone();
                //    // Clipboard.SetImage(replacementImage);
                //    Bitmap cannyImage = (Bitmap)PBCapture.Image.Clone();
                //    ProcessImage(cannyImage);
                //    return;
                //}
                GC.Collect();
                //return;
            }
            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red              = new IntRange(0, 64);
            colorFilter.Green            = new IntRange(0, 64);
            colorFilter.Blue             = new IntRange(0, 64);
            colorFilter.FillOutsideRange = false;

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);
            PBCanny.Image = (Bitmap)bitmap.Clone();
            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            Graphics g         = Graphics.FromImage(bitmap);
            Pen      yellowPen = new Pen(Color.Yellow, 2); // circles
            Pen      redPen    = new Pen(Color.Red, 2);    // quadrilateral
            Pen      brownPen  = new Pen(Color.Brown, 2);  // quadrilateral with known sub-type
            Pen      greenPen  = new Pen(Color.Green, 2);  // known triangle
            Pen      bluePen   = new Pen(Color.Blue, 2);   // triangle

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                AForge.Point center;
                float        radius;

                // is circle ?
                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    g.DrawEllipse(yellowPen,
                                  (float)(center.X - radius), (float)(center.Y - radius),
                                  (float)(radius * 2), (float)(radius * 2));
                }
                else
                {
                    List <IntPoint> corners;

                    // is triangle or quadrilateral
                    if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                    {
                        // get sub-type
                        PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                        Pen pen;

                        if (subType == PolygonSubType.Unknown)
                        {
                            pen = (corners.Count == 4) ? redPen : bluePen;
                        }
                        else
                        {
                            pen = (corners.Count == 4) ? brownPen : greenPen;
                        }

                        g.DrawPolygon(pen, ToPointsArray(corners));
                    }
                }
            }

            yellowPen.Dispose();
            redPen.Dispose();
            greenPen.Dispose();
            bluePen.Dispose();
            brownPen.Dispose();
            g.Dispose();

            // put new image to clipboard
            //Clipboard.SetDataObject(bitmap);
            // and to picture box
            PBAnalysis.Image = bitmap;

            //UpdatePictureBoxPosition();
        }
        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);
        }
Beispiel #19
0
        // Process image
        private void ProcessImage(Bitmap bitmap)
        {
            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering
            {
                Blue             = new IntRange(0, 64),
                FillOutsideRange = false
            };

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter
            {
                FilterBlobs = true,
                MinHeight   = 5,
                MinWidth    = 5
            };

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            //Pen colorPen = new Pen(Color.Yellow, 2);   // quadrilateral with known sub-type
            using (Graphics g = Graphics.FromImage(bitmap)) // SourceImage is a Bitmap object
            {
                for (int i = 0, n = blobs.Length; i < n; i++)
                {
                    List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                    // is triangle or quadrilateral
                    if (shapeChecker.IsConvexPolygon(edgePoints, out List <IntPoint> corners))
                    {
                        // get sub-type
                        PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                        if (subType != PolygonSubType.Unknown)
                        {
                            if (corners.Count == 4)
                            {
                                // ignore the application window itself
                                if (corners[0].X <= 15)
                                {
                                    continue;
                                }
                                else
                                {
                                    //g.DrawPolygon(colorPen, ToPointsArray(corners));
                                    int right = corners[0].X, left = corners[0].X, top = corners[0].Y, bottom = corners[0].Y;
                                    for (int j = 0; j < corners.Count; j++)
                                    {
                                        if (corners[j].X > right)
                                        {
                                            right = corners[j].X;
                                        }
                                        if (corners[j].X < left)
                                        {
                                            left = corners[j].X;
                                        }
                                        if (corners[j].Y > bottom)
                                        {
                                            bottom = corners[j].Y;
                                        }
                                        if (corners[j].Y < top)
                                        {
                                            top = corners[j].Y;
                                        }
                                    }
                                    Rectangle section = new Rectangle(new System.Drawing.Point(left, top), new Size(right - left, bottom - top));
                                    IntPoint  center  = new IntPoint(((right - left) / 2) + left, ((top - bottom) / 2) + top);
                                    grid.Add(DetectStatus(CropImage(bitmap, section), center, section));
                                }
                            }
                        }
                    }
                }
            }
            //colorPen.Dispose();
            //sweeperGrid[10].Save(@".\image2.png");

            // put new image to clipboard
            //bitmap.Save(@".\image.png");
        }
        // Process image
        void ProcessImage(Bitmap bitmap)
        {
#if !SILVERLIGHT && !UNITY_WEBPLAYER
            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new RectI(0, 0, bitmap.PixelWidth, bitmap.PixelHeight),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red              = new IntRange(0, 64);
            colorFilter.Green            = new IntRange(0, 64);
            colorFilter.Blue             = new IntRange(0, 64);
            colorFilter.FillOutsideRange = false;

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            Graphics g         = Graphics.FromImage(bitmap);
            Pen      yellowPen = new Pen(Color.Yellow, 2); // circles
            Pen      redPen    = new Pen(Color.Red, 2);    // quadrilateral
            Pen      brownPen  = new Pen(Color.Brown, 2);  // quadrilateral with known sub-type
            Pen      greenPen  = new Pen(Color.Green, 2);  // known triangle
            Pen      bluePen   = new Pen(Color.Blue, 2);   // triangle

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                global::AForge.Point center;
                float radius;

                // is circle ?
                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    g.DrawEllipse(yellowPen,
                                  (float)(center.X - radius), (float)(center.Y - radius),
                                  (float)(radius * 2), (float)(radius * 2));
                }
                else
                {
                    List <IntPoint> corners;

                    // is triangle or quadrilateral
                    if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                    {
                        // get sub-type
                        PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                        Pen pen;

                        if (subType == PolygonSubType.Unknown)
                        {
                            pen = (corners.Count == 4) ? redPen : bluePen;
                        }
                        else
                        {
                            pen = (corners.Count == 4) ? brownPen : greenPen;
                        }

                        g.DrawPolygon(pen, ToPointsArray(corners));
                    }
                }
            }

            yellowPen.Dispose();
            redPen.Dispose();
            greenPen.Dispose();
            bluePen.Dispose();
            brownPen.Dispose();
            g.Dispose();
#endif

            // and to picture box
            pictureBox.Image = bitmap;
        }
Beispiel #21
0
        public void processShapes()
        {
            //Working Variables
            Pen fuschiaPen = new Pen(Color.Fuchsia, 3.0f);
            Pen aquaPen    = new Pen(Color.Aqua, 5.0f);
            Pen redPen     = new Pen(Color.Red, 3.0f);
            Pen orangePen  = new Pen(Color.Orange, 3.0f);


            //Locking Image Bits
            Bitmap workingFrame = edgeDetection();

            System.Drawing.Imaging.BitmapData bmpData = workingFrame.LockBits(new Rectangle(0, 0, workingFrame.Width, workingFrame.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, workingFrame.PixelFormat);


            //Turning the Background Black and getting rid of the green of the PCB

            /*ColorFiltering colorFilter = new ColorFiltering();
             * Threshold filter = new Threshold((int)hScrollBar1.Value);
             *
             * colorFilter.Red = new IntRange(0, hScrollBar1.Value);
             * colorFilter.Blue = new IntRange(0, hScrollBar1.Value);
             * colorFilter.Green = new IntRange(0, hScrollBar1.Value);
             */
            // colorFilter.ApplyInPlace(bmpData);
            workingFrame.UnlockBits(bmpData);



            //Identifing the Blobs in target picture
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            //blobCounter.MinHeight = 5;
            //blobCounter.MinWidth = 5;
            blobCounter.MinHeight = hScrollBar2.Value;
            blobCounter.MinWidth  = hScrollBar2.Value;

            blobCounter.ProcessImage(bmpData);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            //scrollableImagePanel1.Image = (Bitmap)workingFrame.Clone();
            //Classifying the objects{}
            AForge.Math.Geometry.SimpleShapeChecker shapeCheck = new SimpleShapeChecker();
            Bitmap   tempBitmap = new Bitmap(workingFrame.Width, workingFrame.Height);
            Graphics g          = Graphics.FromImage(tempBitmap);

            g.DrawImage(workingFrame, 0, 0);

            if (caseValue == 1)
            {
                scrollableImagePanel1.Image = tempBitmap;
            }
            if (caseValue == 2)
            {
                scrollableImagePanel3.Image = tempBitmap;
            }
            if (caseValue == 3)
            {
                scrollableImagePanel2.Image = tempBitmap;
            }
            if (caseValue == 4)
            {
                scrollableImagePanel5.Image = tempBitmap;
            }
            if (caseValue == 5)
            {
                scrollableImagePanel4.Image = tempBitmap;
            }
            int blobLength = blobs.Length;

            for (int i = 0; i < blobLength; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                AForge.Point    center;
                float           radius;

                if (shapeCheck.IsCircle(edgePoints, out center, out radius))
                {
                    float tempLocationX = (float)(center.X - radius);
                    float tempLocationY = (float)(center.Y - radius);
                    float diameter      = radius * 2.0f;
                    g.DrawEllipse(fuschiaPen, tempLocationX, tempLocationY, diameter, diameter);
                }
                else
                {
                    List <IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
                    if (shapeCheck.IsQuadrilateral(edgePoints, out corners))
                    {
                        Rectangle testRectangle = new Rectangle(corners[0].X, corners[0].Y, (corners[1].X - corners[0].X), corners[3].Y - corners[0].Y);
                        g.DrawRectangle(redPen, testRectangle);

                        if (shapeCheck.CheckPolygonSubType(corners) == PolygonSubType.Rectangle)
                        {
                            g.DrawPolygon(aquaPen, ToPointsArray(corners));
                        }
                        else
                        {
                            g.DrawPolygon(orangePen, ToPointsArray(corners));
                        }
                    }
                }

                //redPen.Dispose();
                //orangePen.Dispose();

                //g.Dispose();
            }
        }
Beispiel #22
0
        public void findObjects(Bitmap image1, Bitmap image2) //find object(s)
        {
            BlobCounter blobCounter1 = new BlobCounter();

            blobCounter1.MinWidth     = 5;
            blobCounter1.MinHeight    = 5;
            blobCounter1.FilterBlobs  = true;
            blobCounter1.ObjectsOrder = ObjectsOrder.Size;

            //----------------------------------

            BlobCounter blobCounter2 = new BlobCounter();

            blobCounter2.MinWidth     = 5;
            blobCounter2.MinHeight    = 5;
            blobCounter2.FilterBlobs  = true;
            blobCounter2.ObjectsOrder = ObjectsOrder.Size;

            BitmapData objectsData1 = image1.LockBits(new Rectangle(0, 0, image1.Width, image1.Height), ImageLockMode.ReadOnly, image1.PixelFormat);
            // grayscaling
            Grayscale      grayscaleFilter1 = new Grayscale(0.2125, 0.7154, 0.0721);
            UnmanagedImage grayImage1       = grayscaleFilter1.Apply(new UnmanagedImage(objectsData1));

            // unlock image
            image1.UnlockBits(objectsData1);

            BitmapData objectsData2 = image1.LockBits(new Rectangle(0, 0, image1.Width, image1.Height), ImageLockMode.ReadOnly, image1.PixelFormat);
            // grayscaling
            Grayscale      grayscaleFilter2 = new Grayscale(0.2125, 0.7154, 0.0721);
            UnmanagedImage grayImage2       = grayscaleFilter2.Apply(new UnmanagedImage(objectsData2));

            // unlock image
            image1.UnlockBits(objectsData2);


            blobCounter1.ProcessImage(image1);
            Rectangle[] rects1 = blobCounter1.GetObjectsRectangles();
            Blob[]      blobs1 = blobCounter1.GetObjectsInformation();

            blobCounter2.ProcessImage(image2);
            Rectangle[] rects2 = blobCounter2.GetObjectsRectangles();
            Blob[]      blobs2 = blobCounter2.GetObjectsInformation();

            if (rdiobtnsingleobject.Checked)
            {
                //Single object Tracking--------
                Graphics g = pictureBox1.CreateGraphics();
                if (rects1.Length > 0)
                {
                    Rectangle objectRect1 = rects1[0];
                    using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
                    {
                        g.DrawRectangle(pen, objectRect1);
                    }
                }
                if (rects2.Length > 0)
                {
                    Rectangle objectRect2 = rects2[0];
                    using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
                    {
                        g.DrawRectangle(pen, objectRect2);
                    }
                }
                g.Dispose();
            }



            if (rdiobtnMultipleObjects.Checked)
            {
                //Multi tracking-------
                for (int i = 0; rects1.Length > i; i++)
                {
                    Rectangle objectRect = rects1[i];
                    Graphics  g          = pictureBox1.CreateGraphics();
                    using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
                    {
                        g.DrawRectangle(pen, objectRect);
                        g.DrawString((i + 1).ToString(), new Font("Arial", 12), Brushes.Red, objectRect);
                    }
                    int objectX = objectRect.X + (objectRect.Width / 2);
                    int objectY = objectRect.Y + (objectRect.Height / 2);
                    g.Dispose();
                }
            }

            if (rdiobtnGeoShape.Checked)
            {
                SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

                int circles   = 0;
                int squares   = 0;
                int triangles = 0;

                Graphics g         = pictureBox1.CreateGraphics();
                Pen      yellowPen = new Pen(Color.Yellow, 2); // circles
                Pen      redPen    = new Pen(Color.Red, 2);    // quadrilateral
                Pen      brownPen  = new Pen(Color.Brown, 2);  // quadrilateral with known sub-type
                Pen      greenPen  = new Pen(Color.Green, 2);  // known triangle
                Pen      bluePen   = new Pen(Color.Blue, 2);   // triangle
                for (int i = 0, n = blobs1.Length; i < n; i++)
                {
                    List <IntPoint> edgePoints = blobCounter1.GetBlobsEdgePoints(blobs1[i]);
                    AForge.Point    center;
                    float           radius;

                    // is circle ?
                    if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                    {
                        g.DrawEllipse(yellowPen,
                                      (float)(center.X - radius), (float)(center.Y - radius),
                                      (float)(radius * 2), (float)(radius * 2));
                        circles++;
                    }
                    else
                    {
                        List <IntPoint> corners;

                        // is triangle or quadrilateral
                        if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                        {
                            // get sub-type
                            PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                            Pen pen;

                            if (subType == PolygonSubType.Unknown)
                            {
                                if (corners.Count == 4)
                                {
                                    pen = redPen;
                                }
                                else
                                {
                                    pen = bluePen;
                                    triangles++;
                                }
                            }
                            else
                            {
                                if (corners.Count == 4)
                                {
                                    pen = brownPen;
                                    squares++;
                                }
                                else
                                {
                                    pen = greenPen;
                                    triangles++;
                                }
                            }

                            g.DrawPolygon(pen, ToPointsArray(corners));
                        }
                    }
                }
                g.DrawString("circles: " + circles.ToString() + " squares: " + squares.ToString() + " triangles: " + triangles.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(0, 0));
                triangles = 0;
                circles   = 0;
                squares   = 0;
                yellowPen.Dispose();
                redPen.Dispose();
                greenPen.Dispose();
                bluePen.Dispose();
                brownPen.Dispose();
                g.Dispose();
            }
        }
        private void ProcessImage(Bitmap bitmap)
        {
            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red              = new IntRange(Properties.Settings.Default.camFilterRed1, Properties.Settings.Default.camFilterRed2);
            colorFilter.Green            = new IntRange(Properties.Settings.Default.camFilterGreen1, Properties.Settings.Default.camFilterGreen2);
            colorFilter.Blue             = new IntRange(Properties.Settings.Default.camFilterBlue1, Properties.Settings.Default.camFilterBlue2);
            colorFilter.FillOutsideRange = Properties.Settings.Default.camFilterOutside;

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = (int)Properties.Settings.Default.camShapeSizeMin * (int)cameraZoom;
            blobCounter.MinWidth    = (int)Properties.Settings.Default.camShapeSizeMin * (int)cameraZoom;
            blobCounter.MaxHeight   = (int)Properties.Settings.Default.camShapeSizeMax * (int)cameraZoom;
            blobCounter.MaxWidth    = (int)Properties.Settings.Default.camShapeSizeMax * (int)cameraZoom;

            blobCounter.ProcessImage(bitmapData);

            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            shapeChecker.MinAcceptableDistortion = (float)Properties.Settings.Default.camShapeDist;
            shapeChecker.RelativeDistortionLimit = (float)Properties.Settings.Default.camShapeDistMax;

            Graphics g         = Graphics.FromImage(bitmap);
            Pen      yellowPen = new Pen(Color.Yellow, 5); // circles
            Pen      redPen    = new Pen(Color.Red, 10);   // circles
            Pen      greenPen  = new Pen(Color.Green, 5);  // known triangle

            double lowestDistance = xmid;
            double distance;

            shapeFound = false;
            AForge.Point center;
            double       shapeRadius = 1;

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                System.Single   radius;
                // is circle ?
                //          g.DrawPolygon(greenPen, ToPointsArray(edgePoints));

                if (Properties.Settings.Default.camShapeCircle && shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    shapeFound = true;
                    distance   = center.DistanceTo((AForge.Point)picCenter);
                    g.DrawEllipse(yellowPen,
                                  (float)(shapeCenter.X - shapeRadius), (float)(shapeCenter.Y - shapeRadius),
                                  (float)(shapeRadius * 2), (float)(shapeRadius * 2));

                    if (lowestDistance > Math.Abs(distance))
                    {
                        lowestDistance = Math.Abs(distance);
                        shapeCenter    = center;
                        shapeRadius    = radius;
                    }
                }
                List <IntPoint> corners;
                if (Properties.Settings.Default.camShapeRect && shapeChecker.IsQuadrilateral(edgePoints, out corners))  //.IsConvexPolygon
                {
                    IntPoint minxy, maxxy, centxy;
                    shapeFound = true;
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
                    g.DrawPolygon(yellowPen, ToPointsArray(corners));
                    PointsCloud.GetBoundingRectangle(corners, out minxy, out maxxy);
                    centxy   = (minxy + maxxy) / 2;
                    distance = picCenter.DistanceTo(centxy);// PointsCloud.GetCenterOfGravity(corners));
                    if (lowestDistance > Math.Abs(distance))
                    {
                        lowestDistance = Math.Abs(distance);
                        shapeCenter    = centxy;                      // PointsCloud.GetCenterOfGravity(corners);
                        shapeRadius    = maxxy.DistanceTo(minxy) / 2; // 50;
                    }
                }
            }
            if (shapeFound)
            {
                g.DrawEllipse(redPen,
                              (float)(shapeCenter.X - shapeRadius * 1.2), (float)(shapeCenter.Y - shapeRadius * 1.2),
                              (float)(shapeRadius * 2.4), (float)(shapeRadius * 2.4));
            }

            yellowPen.Dispose();
            redPen.Dispose();
            greenPen.Dispose();
            g.Dispose();
            pictureBoxVideo.BackgroundImage = bitmap;
        }
Beispiel #24
0
        private void detectQuads(Bitmap bitmap)
        {
            // Greyscale
            filteredBitmap = Grayscale.CommonAlgorithms.BT709.Apply(bitmap);

            // Contrast - try to sharpen edges
            //ContrastStretch filter = new ContrastStretch();
            //filter.ApplyInPlace(filteredBitmap);

            // edge filter
            // This filters accepts 8 bpp grayscale images for processing

            //Alternatives:
            //DifferenceEdgeDetector edgeFilter = new DifferenceEdgeDetector();
            //HomogenityEdgeDetector filter = new HomogenityEdgeDetector();
            //CannyEdgeDetector filter = new CannyEdgeDetector( );

            var edgeFilter = new SobelEdgeDetector();

            edgeFilter.ApplyInPlace(filteredBitmap);

            // Threshhold filter
            var threshholdFilter = new Threshold(240); //180

            threshholdFilter.ApplyInPlace(filteredBitmap);

            var bitmapData = filteredBitmap.LockBits(
                new Rectangle(0, 0, filteredBitmap.Width, filteredBitmap.Height),
                ImageLockMode.ReadWrite, filteredBitmap.PixelFormat);

            var blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;

            //possible finetuning

            blobCounter.MinHeight = Convert.ToInt32(int.Parse(blobHigh.Text) * fScaleFactor);  //fScaleFactor
            blobCounter.MinWidth  = Convert.ToInt32(int.Parse(blobWidth.Text) * fScaleFactor); //fScaleFactor

#if DEBUG
            Console.WriteLine("Calculate min blobsize " + blobCounter.MinWidth + "/" + blobCounter.MinHeight);
#endif

            blobCounter.ProcessImage(bitmapData);
            var blobs = blobCounter.GetObjectsInformation();
            filteredBitmap.UnlockBits(bitmapData);

            var shapeChecker = new SimpleShapeChecker();

            var bm = new Bitmap(filteredBitmap.Width, filteredBitmap.Height, PixelFormat.Format24bppRgb);

            var g = Graphics.FromImage(bm);
            g.DrawImage(filteredBitmap, 0, 0);

            var pen           = new Pen(Color.Red, 5);
            var cardPositions = new List <IntPoint>();


            // Loop through detected shapes
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                var             edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List <IntPoint> corners;
                var             sameCard = false;

                // is triangle or quadrilateral
                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    // get sub-type
                    var subType = shapeChecker.CheckPolygonSubType(corners);

                    // Only return 4 corner rectanges
                    if ((subType == PolygonSubType.Parallelogram || subType == PolygonSubType.Rectangle) &&
                        corners.Count == 4)
                    {
                        // Check if its sideways, if so rearrange the corners so it's veritcal
                        rearrangeCorners(corners);

                        // Prevent it from detecting the same card twice
                        foreach (var point in cardPositions)
                        {
                            if (corners[0].DistanceTo(point) < Convert.ToInt32(40 * fScaleFactor)) //fScaleFactor
                            {
                                sameCard = true;
                            }
                        }

                        if (sameCard)
                        {
                            continue;
                        }

                        /*
                         *  This code seems to have an issue if scaled up with the factor:
                         */

                        // Hack to prevent it from detecting smaller sections of the card instead of the whole card
                        if (GetArea(corners) < Convert.ToInt32(double.Parse(treasholdBox.Text) * fScaleFactor)
                            ) //fScaleFactor
                        {
                            continue;
                        }

                        cardPositions.Add(corners[0]);

                        g.DrawPolygon(pen, ToPointsArray(corners));

                        // Extract the card bitmap

                        // Debug
                        //var transformFilter = new QuadrilateralTransformation(corners, 600, 800);

                        var transformFilter = new QuadrilateralTransformation(corners,
                                                                              Convert.ToInt32(211 * fScaleFactor), Convert.ToInt32(298 * fScaleFactor));

                        cardBitmap = transformFilter.Apply(cameraBitmap);

                        var card = new MagicCard();
                        card.corners       = corners;
                        card.cardBitmap    = cardBitmap;
                        card.cardArtBitmap = cardArtBitmap;

                        magicCards.Add(card);

                        pen.Dispose();
                        g.Dispose();

                        filteredBitmap = bm;

                        return;
                    }
                }
            }

            pen.Dispose();
            g.Dispose();

            filteredBitmap = bm;
        }
Beispiel #25
0
        private void ProcessImage(Bitmap bitmap)
        {
            // reset counters
            numberOfCircleShits   = 0;
            numberOfLineShits     = 0;
            numberOfSquareShits   = 0;
            numberOfTriangleShits = 0;

            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red              = new IntRange(0, 64);
            colorFilter.Green            = new IntRange(0, 64);
            colorFilter.Blue             = new IntRange(0, 64);
            colorFilter.FillOutsideRange = false;

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            Graphics g = Graphics.FromImage(bitmap);

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                AForge.Point center;
                float        radius;

                // is circle ?
                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    numberOfCircleShits++;
                }
                else
                {
                    List <IntPoint> corners;

                    // is triangle or quadrilateral
                    if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                    {
                        // get sub-type
                        PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
                        Pen            pen;
                        if (subType == PolygonSubType.Square)
                        {
                            numberOfSquareShits++;
                        }
                        else if (subType == PolygonSubType.EquilateralTriangle)
                        {
                            numberOfTriangleShits++;
                        }
                        else
                        {
                            numberOfLineShits++;
                        }
                    }
                }
            }
            g.Dispose();
            // put new image to clipboard
            Clipboard.SetDataObject(bitmap);
            // and to picture box
            picture.Image       = bitmap;
            triangleCount.Text  = "" + numberOfTriangleShits;
            CircleCount.Text    = "" + numberOfCircleShits;
            SquareCount.Text    = "" + numberOfSquareShits;
            RectangleCount.Text = "" + +numberOfLineShits;
        }
Beispiel #26
0
        private void altProcess(Bitmap bm, int level)
        {
            var img = new Image <Bgr, byte>(bm);

            if (level == 1)
            {
                var resImage = new Image <Bgr, byte>(img.Bitmap);
                CvInvoke.BilateralFilter(resImage, img, 30, 80, 80);
                CvInvoke.MedianBlur(img, img, 5);
                resImage = img;
            }
            else if (level == 2)
            {
                CvInvoke.MedianBlur(img, img, 5);
                var resImage = new Image <Bgr, byte>(img.Bitmap);
                CvInvoke.BilateralFilter(resImage, img, 25, 75, 75);
                CvInvoke.Blur(img, img, new Size(5, 5), new Point(0, 0));
            }

            var grayimage = new Image <Gray, byte>(bm);

            CvInvoke.CvtColor(img, grayimage, ColorConversion.Bgr2Gray);

            BlackBG(grayimage);

            Console.WriteLine("Filtering done");

            var cannyThreshold = GetKMeansThreshold(grayimage);

            label2.Text = cannyThreshold.ToString();

            Thresholding(grayimage, cannyThreshold);

            Console.WriteLine("Canny threshold using KMEANS found " + cannyThreshold);

            //Convert the image to grayscale and filter out the noise

            var cannyEdges = new UMat();

            Console.WriteLine("Canny threshold using KMEANS found " + cannyThreshold);

            var uimage = new UMat();

            CvInvoke.CvtColor(img, uimage, ColorConversion.Bgr2Gray);

            CvInvoke.Canny(uimage, cannyEdges, cannyThreshold, cannyThreshold);

            BlobCounter blobCounter = new BlobCounter( );

            if (level == 1)
            {
                blobCounter.FilterBlobs = true;
                blobCounter.MinHeight   = 25;
                blobCounter.MinWidth    = 25;
                blobCounter.ProcessImage(cannyEdges.Bitmap);
            }
            else
            {
                blobCounter.ProcessImage(grayimage.ToBitmap());
            }
            //blobCounter.ProcessImage(grayimage.ToBitmap());

            Blob[] blobs = blobCounter.GetObjectsInformation( );

            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();


            var      triangleList = new List <Triangle2DF>();
            var      boxList      = new List <RotatedRect>();
            var      circleList   = new List <CircleF>();
            Bitmap   newBM        = new Bitmap(img.Bitmap);
            Graphics g            = Graphics.FromImage(newBM);
            Pen      redPen       = new Pen(Color.Red, 2);


            Pen yellowPen = new Pen(Color.Yellow, 2);
            Pen greenPen  = new Pen(Color.Green, 2);
            Pen bluePen   = new Pen(Color.Blue, 2);

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints =
                    blobCounter.GetBlobsEdgePoints(blobs[i]);

                AForge.Point center;
                float        radius;

                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    //g.DrawEllipse(bluePen,
                    //    (float)(center.X - radius), (float)(center.Y - radius),
                    //    (float)(radius * 2), (float)(radius * 2));
                    circleList.Add(new CircleF(new PointF(center.X, center.Y), radius));
                }
                else
                {
                    List <IntPoint> corners;
                    if (edgePoints.Count > 1)
                    {
                        if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
                        {
                            System.Console.WriteLine(corners.Count);
                            if (shapeChecker.CheckPolygonSubType(corners) ==
                                PolygonSubType.Square || shapeChecker.CheckPolygonSubType(corners) ==
                                PolygonSubType.Rectangle)
                            {
                                IntPoint minXY, maxXY;

                                PointsCloud.GetBoundingRectangle(corners, out minXY, out maxXY);
                                AForge.Point c = PointsCloud.GetCenterOfGravity(corners);
                                //g.DrawPolygon(greenPen, ToPointsArray(corners));
                                boxList.Add(new RotatedRect(new PointF(c.X, c.Y), new SizeF(maxXY.X - minXY.X, maxXY.Y - minXY.Y), 0));
                            }
                        }
                        else
                        {
                            corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
                            if (corners.Count == 3)
                            {
                                Triangle2DF tri = new Triangle2DF(new PointF(corners[0].X, corners[0].Y), new PointF(corners[1].X, corners[1].Y), new PointF(corners[2].X, corners[2].Y));
                                triangleList.Add(tri);
                                //g.DrawPolygon(yellowPen, ToPointsArray(corners));
                            }
                            //g.DrawPolygon(redPen, ToPointsArray(corners));
                        }
                    }
                }
            }
            Console.WriteLine("boxes " + boxList.Count);
            Console.WriteLine("triangles " + triangleList.Count);
            Console.WriteLine("circles " + circleList.Count);

            redPen.Dispose();
            greenPen.Dispose();
            bluePen.Dispose();
            yellowPen.Dispose();
            //g.Dispose();
            resPicBox.Image = newBM;
            CircleF[] circles = circleList.ToArray();
            var       cList   = circles.ToList();

            FilterSame(boxList, triangleList, cList, img.Width * img.Height);
            circles = cList.ToArray();
            var points = new List <PointF>();

            var Image = img.CopyBlank();

            foreach (var triangle in triangleList)
            {
                Image.Draw(triangle, new Bgr(Color.Red), 3);
                points.Add(triangle.Centeroid);
            }

            foreach (var box in boxList)
            {
                Image.Draw(box, new Bgr(Color.Blue), 3);
                points.Add(box.Center);
            }

            foreach (var circle in circles)
            {
                Image.Draw(circle, new Bgr(Color.DarkCyan), 3);
                points.Add(circle.Center);
            }

            var listPoints = SortPoints(points, img);

            for (var i = 0; i < listPoints.Length; i++)
            {
                Console.WriteLine(listPoints[i].X.ToString() + " " + listPoints[i].Y.ToString());
            }

            System.Console.WriteLine("Points sorted, num of objects " + listPoints.Length.ToString());
            resPicBox.Image = (Image + img).ToBitmap();
            if (listPoints.Length > 3)
            {
                var bezSegList = InterpolatePointWithBeizerCurves(listPoints.ToList <PointF>());
                var gr         = Graphics.FromImage(resPicBox.Image);
                var p          = new Pen(Color.Red);

                foreach (BeizerCurveSegment seg in bezSegList)
                {
                    var bezierList = GetBez(new PointF[]
                                            { seg.StartPoint, seg.FirstControlPoint, seg.SecondControlPoint, seg.EndPoint });
                    for (var i = 0; i < bezierList.Length - 1; i++)
                    {
                        gr.DrawLine(p, bezierList[i], bezierList[i + 1]);
                    }
                }
            }
            else
            {
                var gr = Graphics.FromImage(resPicBox.Image);
                var p  = new Pen(Color.Red);

                for (var i = 0; i < listPoints.Length - 1; i++)
                {
                    gr.DrawLine(p, listPoints[i], listPoints[i + 1]);
                }
            }

            //var bezierList = GetBezierCurve1(listPoints);
        }
Beispiel #27
0
        public void ProcessImage(Bitmap bitmap)
        {
            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering();

            // coin.jpg had these boundaries: red <0,64>, green <0,64>, blue <0,64>
            colorFilter.Red              = new IntRange(redBounds[0], redBounds[1]);
            colorFilter.Green            = new IntRange(greenBounds[0], greenBounds[1]);
            colorFilter.Blue             = new IntRange(blueBounds[0], blueBounds[1]);
            colorFilter.FillOutsideRange = false;
            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 10;
            blobCounter.MinWidth    = 10;
            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            Graphics           g            = Graphics.FromImage(bitmap);
            Pen circlePen       = new Pen(Color.Yellow, 2); // circles
            Pen strangeShapePen = new Pen(Color.Red, 2);    // quadrilateral with unknown sub-type
            Pen rectanglePen    = new Pen(Color.Brown, 2);  // quadrilateral with known sub-type
            Pen trianglePen     = new Pen(Color.Blue, 2);   // triangle

            // step 3.5 - prepare file to save obstacles
            string       path   = @"C:\Users\Robert\Documents\Visual Studio 2015\Projects\SMTMotionPlanning\SMTMotionPlanning\obstacleFiles\";
            FileStream   stream = new FileStream(path + "obstacles.txt", FileMode.Create, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream);

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                edgePoints = eliminateDuplicates(edgePoints);
                AForge.Point    center;
                List <IntPoint> corners;
                float           radius;
                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    g.DrawEllipse(circlePen, center.X - radius, center.Y - radius, 2 * radius, 2 * radius);
                    writer.WriteLine("e " + (int)(center.X - radius) + " " + (int)(center.Y - radius)
                                     + " " + (int)(radius) + " " + (int)(radius));
                }
                else if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    PolygonSubType polygonType = shapeChecker.CheckPolygonSubType(corners);
                    Pen            pen;
                    if (polygonType == PolygonSubType.Rectangle || polygonType == PolygonSubType.Square)
                    {
                        pen = rectanglePen;
                        writer.WriteLine("r " + corners[0].X + " " + corners[0].Y + " " +
                                         Math.Abs(corners[0].X - corners[1].X) + " " + Math.Abs(corners[0].Y - corners[2].Y));
                        g.DrawPolygon(pen, ToPointsArray(corners));
                    }
                    else
                    {
                        pen = corners.Count == 3 ? trianglePen : strangeShapePen;
                        g.DrawPolygon(pen, ToPointsArray(corners));
                        writer.Write("p");
                        foreach (IntPoint corner in corners)
                        {
                            writer.Write(" " + corner.X + " " + corner.Y);
                        }
                        writer.WriteLine();
                    }
                }
                else
                {
                    corners = orderPolygonCorners(findPolygonCorners(edgePoints), edgePoints);
                    g.DrawPolygon(strangeShapePen, ToPointsArray(corners));
                    writer.Write("p");
                    foreach (IntPoint corner in corners)
                    {
                        writer.Write(" " + corner.X + " " + corner.Y);
                    }
                    writer.WriteLine();
                }
            }

            circlePen.Dispose();
            strangeShapePen.Dispose();
            trianglePen.Dispose();
            rectanglePen.Dispose();
            writer.Close();
            stream.Close();
            g.Dispose();
            rescaleCoordinates();
            transformCoordinates();
        }
Beispiel #28
0
        public List <DetectedCard> Detect(Bitmap bitmap, out Bitmap detectionImage)
        {
            var imageTools    = new ImageTools();
            var detectedCards = new List <DetectedCard>();
            // Greyscale
            var greyscaleImage = imageTools.GreyscaleEdgeDetectionImage(bitmap);

            var bitmapData = greyscaleImage.LockBits(
                new Rectangle(0, 0, greyscaleImage.Width, greyscaleImage.Height),
                ImageLockMode.ReadWrite, greyscaleImage.PixelFormat);

            var blobCounter = new BlobCounter
            {
                FilterBlobs = true,
                MinHeight   = Convert.ToInt32(BlobHeight * scaleFactor),
                MinWidth    = Convert.ToInt32(BlobWidth * scaleFactor)
            };

            blobCounter.ProcessImage(bitmapData);
            var blobs = blobCounter.GetObjectsInformation();

            greyscaleImage.UnlockBits(bitmapData);

            var shapeChecker = new SimpleShapeChecker();

            var cardPositions = new List <IntPoint>();


            // Loop through detected shapes
            for (var i = 0; i < blobs.Length; i++)
            {
                var edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                // is triangle or quadrilateral
                if (!shapeChecker.IsConvexPolygon(edgePoints, out var corners))
                {
                    continue;
                }

                var subType = shapeChecker.CheckPolygonSubType(corners);

                // Only return 4 corner rectanges
                if (subType != PolygonSubType.Parallelogram && subType != PolygonSubType.Rectangle ||
                    corners.Count != 4)
                {
                    continue;
                }

                corners = RotateCard(corners);

                // Prevent it from detecting the same card twice
                if (cardPositions.Any(point => corners[0].DistanceTo(point) < Convert.ToInt32(40 * scaleFactor)))
                {
                    continue;
                }

                // Hack to prevent it from detecting smaller sections of the card instead of the whole card
                if (GetArea(corners) < Convert.ToInt32(DetectionThreshold * scaleFactor))
                {
                    continue;
                }

                cardPositions.Add(corners[0]);

                var cardBitmap = imageTools.GetDetectedCardImage(corners, bitmap, scaleFactor);

                var card = new DetectedCard
                {
                    Corners    = corners,
                    CardBitmap = cardBitmap
                };

                detectedCards.Add(card);
            }

            detectionImage = greyscaleImage;
            return(detectedCards);
        }
Beispiel #29
0
        /// <summary>
        /// 图片处理过程
        /// </summary>
        /// <param name="bitmap"></param>
        private /*Bitmap*/ void ProcessImage(Bitmap bitmap)
        {
            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            // step 1 - turn background to black
            //ColorFiltering colorFilter = new ColorFiltering();

            //colorFilter.Red = new IntRange(0, 64);
            //colorFilter.Green = new IntRange(0, 64);
            //colorFilter.Blue = new IntRange(0, 64);
            //colorFilter.FillOutsideRange = false;

            //colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            //Bitmap test = bitmap.Save("12345.jpg", ImageFormat.Gif);



            Graphics g = Graphics.FromImage(CheckPixPicture(bitmap));
            //Pen yellowPen = new Pen(Color.Yellow, 2); // circles
            Pen redPen = new Pen(Color.Red, 2);       // quadrilateral

            //Pen brownPen = new Pen(Color.Brown, 2);   // quadrilateral with known sub-type
            //Pen greenPen = new Pen(Color.Green, 2);   // known triangle
            //Pen bluePen = new Pen(Color.Blue, 2);     // triangle

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                AForge.Point center;
                float        radius;

                // is circle ?
                //if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                //{
                //    //g.DrawEllipse(yellowPen,
                //    //    (float)(center.X - radius), (float)(center.Y - radius),
                //    //    (float)(radius * 2), (float)(radius * 2));
                //}
                //else
                //{
                List <IntPoint> corners;

                // is triangle or quadrilateral
                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    // get sub-type
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                    Pen pen;

                    //if (subtype == polygonsubtype.unknown)
                    //{
                    //    pen = (corners.count == 4) ? redpen : bluepen;
                    //}
                    //else
                    //{
                    //    pen = (corners.count == 4) ? brownpen : greenpen;
                    //}

                    g.DrawPolygon(redPen, ToPointsArray(corners));
                }
                //}
            }

            //yellowPen.Dispose();
            redPen.Dispose();
            //greenPen.Dispose();
            //bluePen.Dispose();
            //brownPen.Dispose();
            g.Dispose();

            // put new image to clipboard
            Clipboard.SetDataObject(bitmap);

            //return bitmap;
        }
Beispiel #30
0
        //private List<Triangle> LoadImage()
        //{
        //    // load arrow bitmap
        //    string embeddedFileName = CheckForUploadedFile();
        //    Assembly assembly = this.GetType().Assembly;
        //    Bitmap image = new Bitmap(assembly.GetManifestResourceStream(embeddedFileName));
        //    List<Triangle>  triangleList;
        //   // triangleList=  ProcessImage(image);
        //    return triangleList;
        //}

        private List <Triangle> ProcessImage()
        {
            List <Triangle> TriangleList = new List <Triangle>();

            List <ULBounds> triBounds = new List <ULBounds>();

            int currentMaxX = 0;
            int currentMaxY = 0;

            int currentUBoundsX = 0;
            int currentUBoundsY = 0;

            int    currentCol = 0;
            String currentRow = String.Empty;

            string embeddedFileName = CheckForUploadedFile();

            if (embeddedFileName == String.Empty)
            {
                TriangleList = NoTriangleFound();
                return(TriangleList);
            }

            Bitmap bitmap = (Bitmap)Bitmap.FromFile(embeddedFileName);
            // Triangle Class List



            // values needed to find area of a triangle
            // double[] X;
            //  double[] Y;
            int    z;
            double area;

            // lock image

            String filePath = CheckForUploadedFile();

            Int16 Tol      = 38;
            Color replace1 = new Color();

            replace1 = Color.FromArgb(129, 187, 237);

            Color newColor = new Color();

            newColor = Color.FromArgb(0, 0, 0);

            ImageConverter ic = new ImageConverter();


            Color replace2 = new Color();

            replace2 = Color.FromArgb(199, 221, 234);
            Tol      = 38;

            System.Drawing.Image tmpImage = System.Drawing.Image.FromFile(filePath);
            tmpImage = ColorReplace(tmpImage, Tol, replace1, newColor);

            ColorReplace(tmpImage, Tol, replace2, newColor);

            // UpdatePictureBoxPositionOutPut();


            bitmap = new Bitmap(tmpImage);

            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);



            // step 1 - turn background to black
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red              = new IntRange(0, 64);
            colorFilter.Green            = new IntRange(0, 64);
            colorFilter.Blue             = new IntRange(0, 64);
            colorFilter.FillOutsideRange = false;

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 5;
            blobCounter.MinWidth    = 5;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            Graphics g         = Graphics.FromImage(bitmap);
            Pen      yellowPen = new Pen(Color.Yellow, 2); // circles
            Pen      redPen    = new Pen(Color.Red, 2);    // quadrilateral
            Pen      brownPen  = new Pen(Color.Brown, 2);  // quadrilateral with known sub-type
            Pen      greenPen  = new Pen(Color.Green, 2);  // known triangle
            Pen      bluePen   = new Pen(Color.Blue, 2);   // triangle

            int triFound = 0;

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                DoublePoint center;
                double      radius;
                int         tempX = 0;
                int         tempY = 0;

                int    tempCol = 0;
                String tempRow = string.Empty;


                // is circle ?
                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {
                    g.DrawEllipse(yellowPen,
                                  (float)(center.X - radius), (float)(center.Y - radius),
                                  (float)(radius * 2), (float)(radius * 2));
                }
                else
                {
                    List <IntPoint> corners;

                    // is triangle or quadrilateral
                    if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                    {
                        // get sub-type
                        PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                        Pen pen;

                        if (subType == PolygonSubType.Unknown)
                        {
                            pen = (corners.Count == 4) ? redPen : bluePen;
                        }
                        else
                        {
                            pen = (corners.Count == 4) ? brownPen : yellowPen;


                            double[] X = { corners[0].X, corners[1].X, corners[2].X };
                            double[] Y = { corners[0].Y, corners[1].Y, corners[2].Y };
                            z = X.Length;

                            area = polygonArea(X, Y, z);

                            if (area < 40000)
                            {
                                triFound = triFound + 1;


                                Triangle tmpTri = new Triangle();
                                ULBounds tmpULB = new ULBounds();
                                tmpTri.Name = "Triangle " + triFound.ToString();
                                tmpTri.PtAx = corners[0].X;
                                tmpTri.PtAy = corners[0].Y;

                                if (tmpTri.PtAx > currentMaxX)
                                {
                                    currentMaxX = tmpTri.PtAx;
                                }

                                if (tmpTri.PtAy > currentMaxY)
                                {
                                    currentMaxY = tmpTri.PtAy;
                                }


                                tmpTri.PtBx = corners[1].X;
                                tmpTri.PtBy = corners[1].Y;

                                if (tmpTri.PtBx > currentMaxX)
                                {
                                    currentMaxX = tmpTri.PtBx;
                                }

                                if (tmpTri.PtBy > currentMaxY)
                                {
                                    currentMaxY = tmpTri.PtBy;
                                }



                                tmpTri.PtCx = corners[2].X;
                                tmpTri.PtCy = corners[2].Y;

                                if (tmpTri.PtCx > currentMaxX)
                                {
                                    currentMaxX = tmpTri.PtCx;
                                }

                                if (tmpTri.PtBy > currentMaxY)
                                {
                                    currentMaxY = tmpTri.PtBy;
                                }

                                if (currentCol == 0)
                                {
                                    currentCol++; tempCol = currentCol; currentUBoundsY = currentMaxY;
                                }
                                else
                                {
                                    currentCol = GetMaxColumn(triBounds);
                                }

                                if (currentRow == string.Empty)
                                {
                                    currentRow = Increment(currentRow); tempRow = currentRow; currentUBoundsX = currentMaxX;
                                }
                                else
                                {
                                    currentRow = GetMaxRow(triBounds);
                                };


                                if (triBounds.Count == 0)
                                {
                                    //adding Row
                                    tmpULB.Col   = 0;
                                    tmpULB.Row   = tempRow;
                                    tmpULB.Lower = 0;
                                    tmpULB.Upper = currentMaxY;

                                    triBounds.Add(tmpULB);

                                    tmpULB = new ULBounds();

                                    //adding Col
                                    tmpULB.Col   = tempCol;
                                    tmpULB.Row   = String.Empty;
                                    tmpULB.Lower = 0;
                                    tmpULB.Upper = currentMaxX;

                                    triBounds.Add(tmpULB);
                                }

                                if (currentMaxX > (currentUBoundsX * 1.15))
                                {
                                    currentUBoundsX = GetMaxColumnUBounds(triBounds);
                                    if (currentMaxX > (currentUBoundsX * 1.15))
                                    {
                                        tempCol = GetMaxColumn(triBounds);

                                        tmpULB  = new ULBounds();
                                        tempCol = tempCol++;

                                        tmpULB.Col   = tempCol;
                                        tmpULB.Row   = String.Empty;
                                        tmpULB.Lower = currentUBoundsX + 1;
                                        tmpULB.Upper = currentMaxX;
                                        triBounds.Add(tmpULB);
                                    }
                                }



                                if (currentMaxY > (currentUBoundsY * 1.15))
                                {
                                    currentUBoundsY = GetMaxColumnUBounds(triBounds);
                                    if (currentMaxY > (currentUBoundsY * 1.15))
                                    {
                                        tempRow = GetMaxRow(triBounds);
                                        tmpULB  = new ULBounds();
                                        tempRow = Increment(tempRow);
                                        //adding Col
                                        tmpULB.Col   = tempCol;
                                        tmpULB.Row   = String.Empty;
                                        tmpULB.Lower = 0;
                                        tmpULB.Upper = currentMaxX;

                                        triBounds.Add(tmpULB);
                                    }
                                }


                                tmpTri.Row = currentRow.ToUpper();
                                tmpTri.Col = currentCol;


                                tmpTri.Area = polygonArea(X, Y, z);


                                TriangleList.Add(tmpTri);
                            }
                        }

                        g.DrawPolygon(pen, ToPointsArray(corners));
                    }
                }
            }

            yellowPen.Dispose();
            redPen.Dispose();
            greenPen.Dispose();
            bluePen.Dispose();
            brownPen.Dispose();
            g.Dispose();



            return(TriangleList);
        }