Hough Circle detection in Javacv

Opencv provides Hough circle Detection algorithm which can be used to detect circles. Some information about how algorithm works and its example using Opencv in cpp can be found in below link

We will see how to use cvHoughCircle using Javacv. first we will have to process image to get grayscale or binary image. Using cvSmooth() helps most of the time for good detection however it depending upon kind of object  and background more image processing may be required.

First we will load image and then convert it to grayscale. Then use cvSmooth() to smooth the edges. cvHoughCircle() is used to detect circles and are stored in CvSeq. cvGetSeqElem() is used to extract each circle. We have to use each element in CvPoint3D32f. Center of circle is obtained in CvPoint type using cvPointFrom32f(). Obtained center and radius is used to draw circle on input image using cvCircle.

Following code is a demonstration of all the above processes.


Input Image:

Output Image:

12 comments:

  1. could you please checkout the following link to a qs I asked on opencv forum.Please help out.
    http://answers.opencv.org/question/15274/iris-detection/

    ReplyDelete
    Replies
    1. You should try debug your code. See if your threshold is working properly to obtain grayscale image. Also try to tweak with parameters in cvHoughCircles function.

      Delete
    2. I have tried to debug the code but i get stuck at the following piece of code:

      if (grayImage1 == null || grayImage1.width() != width/f || grayImage1.height() != height/f)
      -breakpoint-----grayImage1 = IplImage.create(width/f, height/f, IPL_DEPTH_8U, 1);

      if I try to step over it it gives:

      public NoClassDefFoundError(String detailMessage) {
      super(detailMessage);
      }

      and gets stuck in a loop

      Delete
  2. You have converted the input image into grayscale and processed and in which line are u converting the grayscale image back to RGB or BGR ?

    ReplyDelete
    Replies
    1. I am not converting gray scale image back to RGB or BGR. I am just displaying result on the original 'src' image on code line 31 and discarding gray scale image.

      Delete
    2. Oh I see, thanks. But can you pls tell me how to import this code to android ? After importing java cv jars, how can I use this code in android ?

      Delete
    3. I am going to post article on implementation for android soon. Meanwhile refer to https://code.google.com/p/javacv/

      Delete
  3. I want to detect doctor's tool from an image. The tools are grey but I don't know how i take the position. Can you help me?

    ReplyDelete
    Replies
    1. I am not getting your problem Please elaborate more on what are you trying to do and where you are facing problem.

      Delete
  4. When i executed your code , I got this :

    Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError

    ReplyDelete
    Replies
    1. Are you the same code in above post? At which line are you getting this error?

      Delete
  5. thats bogus you have to change the parameters each time
    thats good plastic toy for all the kids to play around

    ReplyDelete