【问题标题】:OpenCV HoughCircles not detecting circlesOpenCV HoughCircles 未检测到圆圈
【发布时间】:2016-11-09 19:08:23
【问题描述】:

我正在实现一个函数来检测图像中的圆圈。我正在使用OpenCV for Java 来识别圆圈。灰度图像确实显示了一个圆圈。

这是我的代码:

Mat gray = new Mat();
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.blur(gray, gray, new Size(3, 3));

Mat edges = new Mat();
int lowThreshold = 100;
int ratio = 3;
Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio);

Mat circles = new Mat();
Vector<Mat> circlesList = new Vector<Mat>();

Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 60, 200, 20, 30, 0);

Imshow grayIM = new Imshow("grayscale");
grayIM.showImage(edges);

知道为什么会这样吗?

【问题讨论】:

  • 你能发布源图像和传递给 HoughCircle 的图像吗?如果可能的话,还有中间图像。
  • Houghcircles 应该用于 tge 灰度图像,而不是边缘图像

标签: java opencv geometry detection hough-transform


【解决方案1】:

首先,正如 Miki 指出的,HughCircles 应该直接应用于灰度,它有自己的内部 Canny Edge 检测器。

HughCircles 的第二个参数应该根据您的特定图像类型进行调整。没有一种设置适合所有公式。

根据您的代码,这在一些生成的圈子上对我有用:

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat img = Highgui.imread("circle-in.jpg", Highgui.CV_LOAD_IMAGE_ANYCOLOR);

    Mat gray = new Mat();
    Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.blur(gray, gray, new Size(3, 3));

    Mat circles = new Mat();
    double minDist = 60;
    // higher threshold of Canny Edge detector, lower threshold is twice smaller
    double p1UpperThreshold = 200;
    // the smaller it is, the more false circles may be detected
    double p2AccumulatorThreshold = 20;
    int minRadius = 30;
    int maxRadius = 0;
    // use gray image, not edge detected
    Imgproc.HoughCircles(gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, minDist, p1UpperThreshold, p2AccumulatorThreshold, minRadius, maxRadius);

    // draw the detected circles
    Mat detected = img.clone();
    for (int x = 0; x < circles.cols(); x++) {
        double[] c1xyr = circles.get(0, x);
        Point xy = new Point(Math.round(c1xyr[0]), Math.round(c1xyr[1]));
        int radius = (int) Math.round(c1xyr[2]);
        Core.circle(detected, xy, radius, new Scalar(0, 0, 255), 3);
    }

    Highgui.imwrite("circle-out.jpg", detected);
}

输入带圆圈的图像:

检测到的圆圈为红色:

请注意,在输出图像中,左侧的白色圆圈未检测到非常接近白色。如果您设置p1UpperThreshold=20,它将是。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多