简体   繁体   English

houghcircles opencv检测到多个圆圈

[英]houghcircles opencv detect multiple circles

I am trying to detect multiple circles in one image using java and the HoughCircles method from opencv . 我正在尝试使用java和来自opencv的HoughCircles方法在一个图像中检测多个圆圈。 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. 除了将minDist设置得过高之外,我没有发现其他有类似问题的人,但是由于它已经是1,所以我无法再降低它了。我还尝试了param2和param1的尝试,但这没有太大帮助。

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

}

You should iterate like this 你应该这样迭代

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

instead of 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM