A class for checking simple geometrical shapes.

Namespace:  AForge.Math.Geometry
Assembly:  AForge.Math (in AForge.Math.dll) Version: 2.2.5.0 (2.2.5.0)

Syntax

C#
public class SimpleShapeChecker

Remarks

The class performs checking/detection of some simple geometrical shapes for provided set of points (shape's edge points). During the check the class goes through the list of all provided points and checks how accurately they fit into assumed shape.

All the shape checks allow some deviation of points from the shape with assumed parameters. In other words it is allowed that specified set of points may form a little bit distorted shape, which may be still recognized. The allowed amount of distortion is controlled by two properties (MinAcceptableDistortion and RelativeDistortionLimit), which allow higher distortion level for bigger shapes and smaller amount of distortion for smaller shapes. Checking specified set of points, the class calculates mean distance between specified set of points and edge of the assumed shape. If the mean distance is equal to or less than maximum allowed distance, then a shape is recognized. The maximum allowed distance is calculated as:

Copy 
maxDistance = max( minAcceptableDistortion, relativeDistortionLimit * ( width + height ) / 2 )
, where width and height is the size of bounding rectangle for the specified points.

See also AngleError and LengthError properties, which set acceptable errors for polygon sub type checking done by CheckPolygonSubType(List<(Of <(IntPoint>)>)) method.

Note:See the next article for details about the implemented algorithms: Detecting some simple shapes in images.

Sample usage:

CopyC#
private List<IntPoint> idealCicle = new List<IntPoint>( );
private List<IntPoint> distorredCircle = new List<IntPoint>( );
System.Random rand = new System.Random( );

// generate sample circles
float radius = 100;

for ( int i = 0; i < 360; i += 10 )
{
    float angle = (float) ( (float) i / 180 * System.Math.PI );

    // add point to ideal circle
    idealCicle.Add( new IntPoint(
        (int) ( radius * System.Math.Cos( angle ) ),
        (int) ( radius * System.Math.Sin( angle ) ) ) );

    // add a bit distortion for distorred cirlce
    float distorredRadius = radius + rand.Next( 7 ) - 3;

    distorredCircle.Add( new IntPoint(
        (int) ( distorredRadius * System.Math.Cos( angle ) ),
        (int) ( distorredRadius * System.Math.Sin( angle ) ) ) );
}

// check shape
SimpleShapeChecker shapeChecker = new SimpleShapeChecker( );

if ( shapeChecker.IsCircle( idealCicle ) )
{
    // ...
}

if ( shapeChecker.CheckShapeType( distorredCircle ) == ShapeType.Circle )
{
    // ...
}

Inheritance Hierarchy

System..::.Object
  AForge.Math.Geometry..::.SimpleShapeChecker

See Also