Detecting Circle Using Hough Transform
Asked Answered
N

1

8

I'm trying to detect circles with using hough transform.

enter image description here

With my current code I can detect the one below

enter image description here

But I want to find black hole inside the circle I've detected. however changing parameters of houghcircle method is not helped me. Actually it found circles that are not exist.

enter image description here

Also I've tried crop the circle I've found and do another hough transform on this new part it also didn't help me.

here is my code

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"  // needs imgproc, imgcodecs & highgui
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat src, circleroi;

    /// Read the image
    src = imread( "/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/delikli/gainfull.jpg", 2 );


    /// Convert it to gray
//    cvtColor( src, src_gray, CV_BGR2GRAY );
       /// Reduce the noise so we avoid false circle detection
   GaussianBlur( src, src, Size(3, 3), 2, 2 );
   // adaptiveThreshold(src,src,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,9,14);
    vector<Vec3f> circles,circlessmall;
 //   Canny( src, src, 50  , 70, 3 );
       /// Apply the Hough Transform to find the circles
    HoughCircles( src, circles, CV_HOUGH_GRADIENT, 1, src.rows/8, 200, 100, 0, 0 );

    /// Draw the circles detected
    for( size_t i = 0; i < circles.size(); i++ )
    {
        Point center(cvRound(circles[i][0]), cvRound(circles[i][4]));
        int radius = cvRound(circles[i][5]);
        // circle center
     circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
       //  circle outline
      circle( src, center, radius, Scalar(0,255,0), 3, 8, 0 );

         circleroi = src(Rect(center.x - radius, // ROI x-offset, left coordinate
                                        center.y - radius, // ROI y-offset, top coordinate
                                        2*radius,          // ROI width
                                        2*radius));



  //      imshow( "Hough Circle Transform Demo", circleroi );


}

  resize(src, src, Size(src.cols/2, src.rows/2));
//   threshold( circleroi, circleroi, 50, 255,CV_THRESH_BINARY );

  //  cout<<circleroi<<endl;
    imshow("asd",src);

   //    imwrite("/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/cikti/deliksiz.jpg",circleroi);


    waitKey(0);
    return 0;
}

Update: since hough uses canny inside I'm manually used canny to see wether it finds the circle or not.

here canny results with Canny(src,src, 100, 200,3); enter image description here

thank you

Noella answered 3/3, 2015 at 8:3 Comment(10)
Did you try it without thresholding? HoughCircles uses canny internally...Strophanthus
there is no threshold on image. only gaussian blur for illumination but I've disabled it too.Noella
Could you post the image with the holes but without the black circles, please. I can't see any black holes in the first two images.Kith
Hi check the first image this is real imageNoella
Do you want to detect the large black hole in the centre of the first image, or do you want to detect the small black holes in the third image? The only black hole I can see in the first image the large one in the centre.Kith
Yes this the one i'd like to detect black hole in the center of the imageNoella
What are the small holes in the third image? What purpose does the third image fulfil?Kith
This is the problem when I increase sensivity it finds circles that are not actually a circle.Noella
Sorry on my mobile it looked like threshold wasnt in comment... did you try grayscale without blur/canny/threshold?Strophanthus
yeah I've tried nothing happened.Noella
C
1

You're setting one of the HoughCircles parameters minDist = src.rows/8, which is fairly large. The docs explain:

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.

The method can't return both the circle that it does find and the circle that you want, since they have nearly the same center (to within src.rows/8), just different sizes. If you set maxRadius to a value around 30 in order to exclude the larger circle, do you get the desired smaller circle?

Constantia answered 3/3, 2015 at 9:28 Comment(3)
hi, I've tried HoughCircles( src, circles, CV_HOUGH_GRADIENT, 1, src.rows/16(32,64) , 200, 100, 0, 30 ); and none of circles are actually foundNoella
Hmm, maybe param2 is too large. It's worth trying smaller values there. If that doesn't work, you can display the output edges from Canny(src, edges, 100, 200) and make sure it's finding the edge of the inner circle in the first place.Constantia
hi, let me update my question with the canny results.Noella

© 2022 - 2024 — McMap. All rights reserved.