AForge.Math.Geometry.SimpleShapeChecker.IsCircle C# (CSharp) Method

IsCircle() public method

Check if the specified set of points form a circle shape.

Circle shape must contain at least 8 points to be recognized. The method returns always, of number of points in the specified shape is less than 8.

public IsCircle ( List edgePoints, Point &center, float &radius ) : bool
edgePoints List Shape's points to check.
center Point Receives circle's center on successful return.
radius float Receives circle's radius on successful return.
return bool
        public bool IsCircle( List<IntPoint> edgePoints, out Point center, out float radius )
        {
            // make sure we have at least 8 points for curcle shape
            if ( edgePoints.Count < 8 )
            {
                center = new Point( 0, 0 );
                radius = 0;
                return false;
            }

            // get bounding rectangle of the points list
            IntPoint minXY, maxXY;
            PointsCloud.GetBoundingRectangle( edgePoints, out minXY, out maxXY );
            // get cloud's size
            IntPoint cloudSize = maxXY - minXY;
            // calculate center point
            center = minXY + (Point) cloudSize / 2;

            radius = ( (float) cloudSize.X + cloudSize.Y ) / 4;

            // calculate mean distance between provided edge points and estimated circle’s edge
            float meanDistance = 0;

            for ( int i = 0, n = edgePoints.Count; i < n; i++ )
            {
                meanDistance += (float) Math.Abs( center.DistanceTo( edgePoints[i] ) - radius );
            }
            meanDistance /= edgePoints.Count;

            float maxDitance = Math.Max( minAcceptableDistortion,
                ( (float) cloudSize.X + cloudSize.Y ) / 2 * relativeDistortionLimit );

            return ( meanDistance <= maxDitance );
        }

Same methods

SimpleShapeChecker::IsCircle ( List edgePoints ) : bool

Usage Example

Beispiel #1
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( );

            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] );

                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
            pictureBox.Image = bitmap;

            UpdatePictureBoxPosition( );
        }
All Usage Examples Of AForge.Math.Geometry.SimpleShapeChecker::IsCircle