0
votes

I am trying to detect multiple circles in one image using java and the HoughCircles method from opencv . I can detect all circles but only one at a time (see pictures).

I haven't found any other people with similar problems except when they put minDist too high, but I can't lower it anymore since it's already 1. I have also tried experimenting with param2 and param1 but that didn't help much.

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat source = Imgcodecs.imread("C:\\Users\\Anton\\Desktop\\Cirkel.png");
    Mat grey = new Mat();
    Imgproc.cvtColor(source, grey, Imgproc.COLOR_BGR2GRAY);
    Mat circles = new Mat();
    double dp = 1;
    int minDist = 1;
    int param2 = 15;
    int param1 = param2*2;
    int minRadius = 0;
    int maxRadius = 5000;
    Imgproc.HoughCircles(grey, circles, Imgproc.HOUGH_GRADIENT,dp, minDist, param1, param2, minRadius, maxRadius);

    if (!circles.empty()) {
        double x;
        double y;
        int r;
        for (int i = 0; i < circles.rows(); i++) {
            double[] data = circles.get(i, 0);
            for (int j = 0; j < data.length; j++) {
                x = data[0];
                y = data[1];
                r = (int) data[2];
                Imgproc.circle(source, new Point(x,y), r, new Scalar(0,0,255),3);
            }
        }
    } else {
        System.out.println("geen circles");
    }
    BufferedImage bi = Mat2BufferedImage(source); 
    displayImage(bi);

}
1
Can you upload the image? You should iterate with size of the circles mat. - Rick M.
i couldn't add images because my account is new. Here they are: imgur.com/a/NFEN2 imgur.com/8ItXBzT imgur.com/a/bUXFC - couragous cucumber
you were right, the iteration was wrong. I don't know how i would iterate in size but iterating the circles.cols() instead of the circles.rows() helped. Thanks! - couragous cucumber
Great! I've added that as an answer! - Rick M.

1 Answers

0
votes

You should iterate like this

for (int i = 0; i < circles.cols(); i++)

instead of

for (int i = 0; i < circles.rows(); i++)