Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find circle inside Rectangle in AForge

Tags:

c#

aforge

I am trying to detect Circle inside Rectangle in AForge. I have successfully determined Rectangles but unable to find circles inside Rectangle. How to find shape inside another shape in AForge.

string strPath = Server.MapPath("~/Recipt001.png");
Bitmap myBitmap = new Bitmap(strPath);

//Some filters Grayscale, invert, threshold

//Blod Filtering                      


BlobCounter blobCounter = new BlobCounter();
blobCounter.ProcessImage(temp);
blobCounter.ObjectsOrder = ObjectsOrder.YX;
blobCounter.FilterBlobs = true;

Blob[] blobs = blobCounter.GetObjectsInformation();
Graphics g = Graphics.FromImage(myBitmap);
Pen redPen = new Pen(Color.Red, 2);
SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

// dictionary of color to highlight different shapes
Dictionary<PolygonSubType, Color> colors = new Dictionary<PolygonSubType, Color>();

colors.Add(PolygonSubType.Unknown, Color.White);
colors.Add(PolygonSubType.Trapezoid, Color.Orange);
colors.Add(PolygonSubType.Parallelogram, Color.Red);
colors.Add(PolygonSubType.Rectangle, Color.Green);
colors.Add(PolygonSubType.Square, Color.Blue);
colors.Add(PolygonSubType.Rhombus, Color.Gray);

colors.Add(PolygonSubType.EquilateralTriangle, Color.Pink);
colors.Add(PolygonSubType.IsoscelesTriangle, Color.Purple);
colors.Add(PolygonSubType.RectangledTriangle, Color.SkyBlue);
colors.Add(PolygonSubType.RectangledIsoscelesTriangle, Color.SeaGreen);

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

    if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
    {
        if (shapeChecker.CheckPolygonSubType(corners) == PolygonSubType.Rectangle)
        {
            g.DrawPolygon(redPen, ToPointsArray(corners));
        }
    }
}

redPen.Dispose();
g.Dispose();
like image 382
Afnan Ahmad Avatar asked Feb 08 '17 14:02

Afnan Ahmad


1 Answers

None of image processing libraries and even image processing in MATLAB lets you search ROI inside ROI (ROI - region of intrest like rectangles or circles). Concept is CROP REGION -> SEARCH OBJECTS IN REGION

So first locate primary rectangles, thereafter crop image to rectangles and perform circle search inside them. Otherwise search for all circles and all rectangles and then classify circles to be belonging to which rectangle using simple maths.

like image 62
SACn Avatar answered Oct 11 '22 15:10

SACn