Click here to Skip to main content
15,890,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using HoughCircleTransformation class of AForge to detect circles. I want to draw circles on the image where the circles are detected (to highlight the circles on the original image). How can i modify the 'foreach loop' to do that?
C#
public Bitmap hough(Bitmap bmp)
   {
       HoughCircleTransformation circleTransform = new HoughCircleTransformation(35);
       // apply Hough circle transform
       circleTransform.ProcessImage(bmp);
       Bitmap houghCirlceImage = circleTransform.ToBitmap();
       // get circles using relative intensity
       HoughCircle[] circles = circleTransform.GetCirclesByRelativeIntensity(0.5);
       int numCircles = circleTransform.CirclesCount;
       MessageBox.Show("Number of circles found : " + numCircles.ToString());
       foreach (HoughCircle circle in circles)
       {

         //code to draw circle
       }
       return bmp;
   }
Posted

You need a Graphics[^] object to draw someting. When you have it, use it to draw:
C#
MessageBox.Show("Number of circles found : " + numCircles.ToString());
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
foreach (HoughCircle circle in circles)
{
    g.DrawEllipse(Pens.Green, circle.GetTheThingsCoordinates());
    //code to draw circle
}
 
Share this answer
 
Comments
lukeer 3-Apr-14 8:02am    
Come on, I've never touched the AForge library and still came to find the HoughCircle documentation within minutes. The class has members named "X", "Y", and "Radius". That's very close to what you need.

[Edit]Where is that comment I replied to?[/Edit]
AmnaRahman 3-Apr-14 13:03pm    
i did it thanks
foreach (HoughCircle circle in circles)
{
Pen redPen = new Pen(Color.Red, 1);
using (var graphics = Graphics.FromImage(bmp2))
{
graphics.DrawEllipse(redPen, circle.X, circle.Y, circle.Radius, circle.Radius);

}

}
 
Share this answer
 
Comments
lukeer 4-Apr-14 2:27am    
You got it to work. That's great!
Let me add a few hints:
Depending on the number of detected circles, this approach might degrade performance more than necessary because it's creating new graphics and pen objects for each circle.
If drawing seems too slow, create the graphics object outside the loop (as suggested in my solution). Same for the pen.
AmnaRahman 4-Apr-14 9:59am    
ok i will try it this way. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900