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:

24 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
  6. Hey, I have problem whis "cvSmooth" - ..."Not supported yet"

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. can you please tell me how to extract the circle form the image as a separate image.? so that i could fit a rectangle cropping on it.

    ReplyDelete
  9. My machine is not giving any error in your code but also not detecting any circles...can you tell me why???

    ReplyDelete
  10. excuse me sir...coud you show me Line detection in javacv thank you in advance

    ReplyDelete
  11. Its not working..
    please any one tell me why?

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Hiv disease for the last 3 years and had pain hard to eat and cough are nightmares,especially the first year At this stage, the immune system is severely weakened, and the risk of contracting opportunistic infections is much greater. However, not everyone with HIV will go on to develop AIDS. The earlier you receive treatment, the better your outcome will be.I started taking ARV to avoid early death but I had faith in God that i would be healed someday.As a Hiv patent we are advise to be taking antiretroviral treatments to reduce our chance of transmitting the virus to others , few weeks ago i came on search on the internet if i could get any information on Hiv treatment with herbal medicine, on my search i saw a testimony of someone who has been healed from Hiv her name was Achima Abelard and other Herpes Virus patent Tasha Moore also giving testimony about this same man,Called Dr Itua Herbal Center.I was moved by the testimony and i contacted him by his Email.drituaherbalcenter@gmail.com We chatted and he send me a bottle of herbal medicine I drank it as he instructed me to.After drinking it he ask me to go for a test that how i ended my suffering life of Hiv patent,I'm cured and free of Arv Pills.I'm forever grateful to him Drituaherbalcenter.Here his contact Number +2348149277967...He assure me he can cure the following disease..Hiv,Cancer,Herpes Virus,Epilepsy, fibromyalgia ,ALS,Hepatitis,Copd,Parkinson disease.Genetic disease,Fibrodysplasia Ossificans Progressiva,Factor V Leiden Mutation ,Fatal Familial Insomnia ,Dupuytren's disease,Fibrodysplasia Ossificans Progressiva,Fluoroquinolone Toxicity Syndrome,Inflammatory bowel disease ,Huntington's disease ,Diabetes,Fibroid...

    ReplyDelete
  14. my husband and I have been married for about 7 yrs now. We were happily married with two kids, a boy and a girl. 3 months ago, I started to notice some strange behavior from him and a few weeks later I found out that my husband is seeing someone. He started coming home late from work, he hardly care about me or the kids anymore, Sometimes he goes out and doesn't even come back home for about 2-3 days. I did all I could to rectify this problem but all to no avail. I became very worried and needed help. As I was browsing through the Internet one day, I came across a website that suggested that Dr jeffrey can help solve marital problems, restore broken relationships and so on. So, I felt I should give him a try. I contacted him and he did a spell for me. Two days later, my husband came to me and apologized for the wrongs he did and promise never to do it again. Ever since then, everything has returned back to normal. I and my family are living together happily again.. All thanks to Dr jeffrey . If you need a spell caster that can cast a spell that truly works, I suggest you contact him. He will not disappoint you. This is his E-mail:

    dctrjeffrey@gmail.com

    WhatsApp +2348162061202

    ReplyDelete
  15. I want to thank Dr Emu a very powerful spell caster who help me to bring my husband back to me, few month ago i have a serious problem with my husband, to the extend that he left the house, and he started dating another woman and he stayed with the woman, i tried all i can to bring him back, but all my effort was useless until the day my friend came to my house and i told her every thing that had happened between me and my husband, then she told me of a powerful spell caster who help her when she was in the same problem I then contact Dr Emu and told him every thing and he told me not to worry my self again that my husband will come back to me after he has cast a spell on him, i thought it was a joke, after he had finish casting the spell, he told me that he had just finish casting the spell, to my greatest surprise within 48 hours, my husband really came back begging me to forgive him, if you need his help you can contact him with via email: Emutemple@gmail.com or add him up on his whatsapp +2347012841542 is willing to help any body that need his help.

    ReplyDelete
  16. Great Article
    Image Processing Projects


    Deep Learning Projects for Final Year


    JavaScript Training in Chennai

    JavaScript

    Training in Chennai


    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals,

    Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer.

    Angular Training Project Centers in Chennai

    ReplyDelete
  17. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..
    artificial intelligence internship | best final year projects for cse | internship certificate online | internship for mba finance students | internship meaning in tamil

    ReplyDelete