Ask Your Question
0

Detecting Circles in opencv

asked 2015-02-12 10:00:15 -0600

Hi guys,

I am using the following code to detect circles

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main()
{
  Mat src, gray;
  src = imread( "/homepath/pic1.png", 1 );resize(src,src,Size(640,480));
  cvtColor( src, gray, COLOR_BGR2GRAY );

  // Reduce the noise so we avoid false circle detection
  GaussianBlur( gray, gray, Size(9, 9), 2, 2 );

  vector<Vec3f> circles;

  // Apply the Hough Transform to find the circles
  HoughCircles( gray, circles, HOUGH_GRADIENT, 1, 30 );

  // Draw the circles detected
  for( size_t i = 0; i < circles.size(); i++ )
  {
      Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
      int radius = cvRound(circles[i][2]);
      circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );// circle center
      circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );// circle outline
      cout << "center : " << center << "\nradius : " << radius << endl;
   }

  // Show your results
  namedWindow( "Hough Circle Transform Demo", WINDOW_AUTOSIZE );
  imshow( "Hough Circle Transform Demo", src );

  waitKey(0);
  return 0;
}

However the output look like this:

image description

What am I doing wrong?

edit retag flag offensive close merge delete

Comments

2

If you know the minimum and maximum radius for the circles you want to detect, then maybe you shall use the last 2 parameters of the function. Anyway, you shall play with the parameters (param1 and param2), too, for adapting the application to your purpose

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-12 10:31:14 -0600 )edit
2

Why don't you like the result? I think it's really good as your circles approximate your ellipses. HoughCircles looks for circles, so which result did you expect if your input only contains ellipses?

FooBar gravatar imageFooBar ( 2015-02-12 12:38:03 -0600 )edit

@FooBar Don't be so sure, it contains small green circles...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-13 02:16:40 -0600 )edit

@thdrksdfthmn The green circles are the center of the circles. The provided code calls HoughCircles and then draws the circle centers in green and the circle outlines in red.

knumat gravatar imageknumat ( 2019-04-26 14:36:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-04-26 14:43:02 -0600

knumat gravatar image

HoughCircles only detects circles, but the image provided here only has ellipses in it. If you want to detect ellipses, you should resize the image first so that the ellipses turn into round circles. In this case, you can simply call resize() to reduce the width of the image. (It is much more complex if the ellipses are rotated.)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-02-12 10:00:15 -0600

Seen: 595 times

Last updated: Feb 12 '15