Search code examples
opencvgeometrydetectionobjective-c++hough-transform

Seed Growing and Hough Circles in Objective-C++ using OpenCV


I'm working on detecting balls on images with Objective-C++ and OpenCV. I tried to use HoughCircle transformation, but I am not detecting the balls properly. For me it seems like the parameters do not fit to the description of the function. Is it different in Objective-C++ than in C++?

This is my code and the description of the OpenCV function:

cv::medianBlur(grayMat, grayMat, 17);
std::vector<cv::Vec3f> circles;
cv::HoughCircles(grayMat, circles, CV_HOUGH_GRADIENT,
                 1,     //dp resolution
                 1,     //minDist
                 140.0, //higher threshold
                 120.0, //lower threshold
                 15,    //min radius
                 50);   //max radius

If the parameters are correct as I implemented them, the idea was to use seed growing to get better results. I have no idea how to implement this in Objective-C++ and OpenCV. I already saw some examples in C++ but I do not know how to "translate" that into Objective-C++.

Could you please have a look at my HoughCircle parameters or give me an Objective-C++ example for seed growing?

Thanks a lot!

Edit: Here are two example images - I need universal parameters to detect the balls on both pictures:

Example image 1

Example image 2


Solution

  • You can use the C++ API of OpenCV on Objective C++ projects, with no change.

    I tried your code and edited the parameters a little bit. The following parameters will detect all the circles:

    cvtColor(img, gray, COLOR_BGR2GRAY);
    medianBlur(gray, gray, 13);
    vector<Vec3f> circles;
    HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
                     gray.rows/16, 
                     100, 30, 15, 250
                     );
    

    You will note three things after trying this code:

    1. Increasing the median filter size removed false circles in the second image.
    2. Changing the size range of circles to be detected resulted in detecting all circles in image 1.
    3. Circles in image two are now detected, but the size is highly inaccurate. For both images, I recommend post-processing (seed growing, as you mention?) to detect all the correct pixels.

    When doing a project like this, it is good to resize the images to a fixed size before starting to analyze them. The parameter values will be different for different image resolutions, unless you find a way to normalize them.