nealsyrkel / aforge

Automatically exported from code.google.com/p/aforge
Other
0 stars 0 forks source link

recognize other geometric shapes #330

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Assuming that all shapes in the image have the same color, it is possible to 
recognize an annulus of a circle or of an ellipse, when the color of the center 
of the Blob is different from the color of the shape.
Recovering the edges through the functions:
blobCounter.GetBlobsLeftAndRightEdges and blobCounter.GetBlobsTopAndBottomEdges 
then you can do this:
 ____________
|   /    \   |   Assuming that the shape is Blue
|  /  /\  \  |   if it was a circle the center should be also Blue
| |  |  |  | |   but in this case is "White"
|  \  \/  /  |
|___\____/___|

sample code:
BlobCounter blobCounter = new BlobCounter();
blobCounter.ProcessImage(bmpTemp);
Blob[] blobs = blobCounter.GetObjectsInformation();

GrahamConvexHull grahamScan = new GrahamConvexHull();
SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

foreach (Blob blob in blobs)
{
  List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
  List<IntPoint> hull = grahamScan.FindHull(edgePoints);

  shapeChecker.MinAcceptableDistortion = 0; //For circles
  List<IntPoint> corners;
  AForge.Point center;
  float radius;
  if (shapeChecker.IsCircle(edgePoints, out center, out radius))
  {
    //check if is an annulus
  }
  else if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
  {
    ...
  }
  else
  {
    // For ellipses
    shapeChecker.MinAcceptableDistortion = blob.Rectangle.Width / 2;
    if (shapeChecker.IsCircle(edgePoints, out center, out radius))
    {
      //check if is an annulus
      List<IntPoint> leftEdge, rightEdge, topEdge, bottomEdge;
      blobCounter.GetBlobsLeftAndRightEdges(blob, out leftEdge, out rightEdge);
      blobCounter.GetBlobsTopAndBottomEdges(blob, out topEdge, out bottomEdge);

      Bitmap bmpRect = new Bitmap(originalImage.Width, originalImage.Height, PixelFormat.Format24bppRgb);
      Graphics gRect = Graphics.FromImage(bmpRect);
      gRect.DrawImage(originalImage, blob.Rectangle, blob.Rectangle, GraphicsUnit.Pixel);

      GraphicsPath path = new GraphicsPath();
      leftEdge.Insert(0, new IntPoint(0, 0));
      leftEdge.Add(new IntPoint(0, originalImage.Height));
      path.AddPolygon(PointsListToArray(leftEdge));
      rightEdge.Insert(0, new IntPoint(originalImage.Width, 0));
      rightEdge.Add(new IntPoint(bmpTemp.Width, originalImage.Height));
      path.AddPolygon(PointsListToArray(rightEdge));
      gRect.FillPath(brush, path);

      path = new GraphicsPath();
      topEdge.Insert(0, new IntPoint(0, 0));
      topEdge.Add(new IntPoint(originalImage.Width, 0));
      path.AddPolygon(PointsListToArray(topEdge));
      bottomEdge.Insert(0, new IntPoint(0, originalImage.Height));
      bottomEdge.Add(new IntPoint(originalImage.Width, originalImage.Height));
      path.AddPolygon(PointsListToArray(bottomEdge));
      gRect.FillPath(brush, path);                            

      gRect.Dispose();

      //recall blobCounter to find the inner circle or ellipse on bmpRect
    }
  }
}

Original issue reported on code.google.com by marco.di...@gmail.com on 24 Jan 2013 at 12:50

GoogleCodeExporter commented 8 years ago
Could you please clarify what is this ticket about? Do you ask for detection of 
other shapes or do you suggest a way to detect ellipses? If you suggest, then I 
would say it does not look right to detect ellipses using circle detection 
routine with large distortion limit. Ellipse has well defined equation, which 
needs to be used to find how well is the points’ fit to the shape.

Original comment by andrew.k...@gmail.com on 24 Jan 2013 at 11:11

GoogleCodeExporter commented 8 years ago
I know that ellipse has well defined equation, but the shapechecker does not 
recognize ellipses but only circle. I suggest a way to detect all type of 
shapes inserting also the ellipse equation, I use the method described in the 
issue because the shapechecker has only IsCircle method. Once it will detect 
also ellipses, you can use my suggestion to detect annulus, or annulus sectors 
with start angle, end angle and arcwidth which for circles is: (outer circle 
width - inner circle width) / 2, for ellipses i don't know

Original comment by marco.di...@gmail.com on 24 Jan 2013 at 1:33