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);
}