C# Class AForge.Math.Geometry.SimpleShapeChecker

A class for checking simple geometrical shapes.

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: maxDitance = 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 method.

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

Sample usage:

private List<IntPoint> idealCicle = new List<IntPoint>( ); private List<IntPoint> distorredCircle = new List<IntPoint>( ); System.Random rand = new System.Random( ); // generate sample circles double radius = 100; for ( int i = 0; i < 360; i += 10 ) { double angle = (double) 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 double 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 ) { // ... }
Show file Open project: holisticware-admin/MonoVersal.AForgeNET Class Usage Examples

Public Methods

Method Description
CheckIfPointsFitShape ( List edgePoints, List corners ) : bool

Check if a shape specified by the set of points fits a convex polygon specified by the set of corners.

The method checks if the set of specified points form the same shape as the set of provided corners.

CheckPolygonSubType ( List corners ) : PolygonSubType

Check sub type of a convex polygon.

The method check corners of a convex polygon detecting its subtype. Polygon's corners are usually retrieved using IsConvexPolygon method, but can be any list of 3-4 points (only sub types of triangles and quadrilateral are checked).

See AngleError and LengthError properties, which set acceptable errors for polygon sub type checking.

CheckShapeType ( List edgePoints ) : ShapeType

Check type of the shape formed by specified points.

IsCircle ( List edgePoints ) : bool

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.

IsCircle ( List edgePoints, Point &center, float &radius ) : bool

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.

IsConvexPolygon ( List edgePoints, List &corners ) : bool

Check if the specified set of points form a convex polygon shape.

The method is able to detect only triangles and quadrilaterals for now. Check number of detected corners to resolve type of the detected polygon.

IsQuadrilateral ( List edgePoints ) : bool

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

IsQuadrilateral ( List edgePoints, List &corners ) : bool

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

IsTriangle ( List edgePoints ) : bool

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

IsTriangle ( List edgePoints, List &corners ) : bool

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

Private Methods

Method Description
GetShapeCorners ( List edgePoints ) : List

Method Details

CheckIfPointsFitShape() public method

Check if a shape specified by the set of points fits a convex polygon specified by the set of corners.

The method checks if the set of specified points form the same shape as the set of provided corners.

public CheckIfPointsFitShape ( List edgePoints, List corners ) : bool
edgePoints List Shape's points to check.
corners List Corners of convex polygon to check fitting into.
return bool

CheckPolygonSubType() public method

Check sub type of a convex polygon.

The method check corners of a convex polygon detecting its subtype. Polygon's corners are usually retrieved using IsConvexPolygon method, but can be any list of 3-4 points (only sub types of triangles and quadrilateral are checked).

See AngleError and LengthError properties, which set acceptable errors for polygon sub type checking.

public CheckPolygonSubType ( List corners ) : PolygonSubType
corners List Corners of the convex polygon to check.
return PolygonSubType

CheckShapeType() public method

Check type of the shape formed by specified points.
public CheckShapeType ( List edgePoints ) : ShapeType
edgePoints List Shape's points to check.
return ShapeType

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 ) : bool
edgePoints List Shape's points to check.
return bool

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

IsConvexPolygon() public method

Check if the specified set of points form a convex polygon shape.

The method is able to detect only triangles and quadrilaterals for now. Check number of detected corners to resolve type of the detected polygon.

public IsConvexPolygon ( List edgePoints, List &corners ) : bool
edgePoints List Shape's points to check.
corners List List of polygon corners on successful return.
return bool

IsQuadrilateral() public method

Check if the specified set of points form a quadrilateral shape.
public IsQuadrilateral ( List edgePoints ) : bool
edgePoints List Shape's points to check.
return bool

IsQuadrilateral() public method

Check if the specified set of points form a quadrilateral shape.
public IsQuadrilateral ( List edgePoints, List &corners ) : bool
edgePoints List Shape's points to check.
corners List List of quadrilateral corners on successful return.
return bool

IsTriangle() public method

Check if the specified set of points form a triangle shape.
public IsTriangle ( List edgePoints ) : bool
edgePoints List Shape's points to check.
return bool

IsTriangle() public method

Check if the specified set of points form a triangle shape.
public IsTriangle ( List edgePoints, List &corners ) : bool
edgePoints List Shape's points to check.
corners List List of triangle corners on successful return.
return bool