Hough Circle Detection

void HoughCircles(InputArray image, OutputArray circles, int method, double dp, double minDist, double param1=100, double param2=100, int minRadius=0, int maxRadius=0 )
 Parameters:
  • image – 8-bit, single-channel, grayscale input image.
  • circles – Output vector of found circles. Each vector is encoded as a 3-element floating-point vector (x,y,radius) .
  • circle_storage – Memory storage that will contain the output sequence of found circles.
  • method – Currently, the only implemented method is CV_HOUGH_GRADIENT.
  • dp – Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has half as big width and height.
  • minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
  • param1 – First method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny()edge detector (the lower one is twice smaller).
  • param2 – Second method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.
  • minRadius – Minimum circle radius.
  • maxRadius – Maximum circle radius.
 A good example for Hough Circle Transform is provided in OpenCV Documentation.

Steps:

  1. Load image and convert to gray-scale.
  2. Blur (low pass filter) the image to reduce noise.
  3. Apply the Hough Transform to find the circles.(HoughCircles)
  4. Draw the circles detected.(circle)
  5. Show the result

Functions:


Example:

-------------
#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( "building.jpg", 1 );resize(src,src,Size(640,480));
  cvtColor( src, gray, CV_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, CV_HOUGH_GRADIENT, 1, 30, 200, 50, 0, 0 );

  // 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", CV_WINDOW_AUTOSIZE );
  imshow( "Hough Circle Transform Demo", src );

  waitKey(0);
  return 0;
}

-------------

 Result:

 

Note:
  • If this code is not able to detect the circles that you want to detect, then play with the parameters (dp, minDist, param1 & param2).
  • Keeping rest parameters constant, if you increase dp, then increase param2 too to avoid false detection.

16 comments:

  1. thanks
    how to access (x,y) for center of circle?

    ReplyDelete
    Replies
    1. X: circles[n][0]
      Y: circles[n][1]
      Radius: circles[n][2]

      Delete
  2. thanks
    how to access (x,y) for center of circle

    ReplyDelete
  3. X: circles[n][0]
    Y: circles[n][1]
    Radius: circles[n][2]

    ReplyDelete
  4. Can you send me the full code for rectangle detection, please
    yousfi_samira@hotmail.fr

    ReplyDelete
  5. Can you send me the full code for rectangle détection, please
    yousfi_samira@hotmail.fr

    ReplyDelete
  6. Can you send me the full code for rectangle détection, please
    yousfi_samira@hotmail.fr

    ReplyDelete
  7. Can you send me the full code for rectangle détection, please
    yousfi_samira@hotmail.fr

    ReplyDelete
  8. can you send me this code in java ??? Sohail8338@gmail.com

    ReplyDelete
  9. hi how can I get the number of votes at the edge point ? how can we get the parameter space or accumulator vote image or in ab-space? please can you share the code ?

    ReplyDelete
  10. I am writing this comment with tears of joy a months ago i found out i was diagnosed with Herpes. I was heart broken, However that same month i was recommended to a Dr. who helped me to cured the virus with natural herbal treatment now i fully believed on natural treatment iam so happy thanks to Dr. Odion . contact information: drodionherbalhome12@gmail.com or massage him on WhatsApp +234 9019421176 and He also have herbs medicine to cured the following diseases; eczema,urethra wart,chronic problems.Herpes, Cancer, Als,Hepatitis, Diabetes, HPV,Infections,ulcer, asthma, COPD. ETC

    ReplyDelete
  11. Everybody does not purchase an expensive DSLR blur camera to capture/snap the auto blur image. But don’t worry we solve this problem freely. Our team makes a Blur Background - Blur image app for blurry photo. After using this Blur background Photo Editor App you will make your photos similar to like capture with a DSLR camera. blur

    ReplyDelete
  12. This comment is so relevant to the topic.

    ReplyDelete
  13. I'm 61 years old. I contracted hpv in 2011' I has be taking lot treatment for it and embarrassed some months ago the wart stated coming out seriously, I used lot recommendation because there was lot warts around my anus and was so . but today I'm totally happy I got the virus eliminated by using natural treatment from Dr voodoo herbal center after his treatment I got cured. all the warts went away' seriously believed Dr. voodoo he have the cure for human papillomavirus because he has eliminated hpv been in my body since 2011, Dr. voodoo make it possible for me. Here is Dr. voodoo email: voodoospelltemple66@gmail.com or WhatsApp him via +2348140120719 he is welled capable of curing terrible diseases.

    ReplyDelete