java - 用于 Java 的 OpenCV : HoughCircles finding all the wrong circles

标签 java opencv computer-vision

我是 OpenCV 的新手,我想从事对象检测工作以帮助我的 FRC 机器人团队。我正在尝试使用 HSV 过滤器和 HoughCircles 在网络摄像头图像中找到一个网球并在其周围画一个圆圈。这是我的代码:

Mat currentFrame = new Mat();
Mat hsv = new Mat();
Mat threshImage = new Mat();
Mat circles = new Mat();

while (true) {
    camera.read(currentFrame);

    Imgproc.resize(currentFrame, currentFrame, new Size(WIDTH, HEIGHT));
    Imgproc.cvtColor(currentFrame, hsv, Imgproc.COLOR_RGB2HSV);

    hsvWindow.showImage(hsv);

    Core.inRange(hsv, new Scalar(50, 100, 0), new Scalar(95, 255, 255), threshImage);

    threshWindow.showImage(threshImage);

    Imgproc.HoughCircles(threshImage, circles, Imgproc.CV_HOUGH_GRADIENT, 2, 100, 100, 100, 0, 500);
    for (int i = 0; i < circles.cols(); i++) {
        double[] vCircle = circles.get(0, i);

        Point pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
        int radius = (int)Math.round(vCircle[2]);

        Core.circle(currentFrame, pt, radius, new Scalar(255, 0, 0), 2);
    }

    drawWindow.showImage(currentFrame);
}

原图、hsv图、滤镜图在本相册:http://imgur.com/a/hO8vs

当我用这里的参数运行 HoughCircles 时,它在钢琴凳和玩具兔子上找到了圆圈,但没有找到网球,它显示为一个白色的大圆圈。

最佳答案

我修好了!在对 HoughCircles 的参数进行一些调整并对二值图像进行模糊和阈值处理后,它可靠地找到了它,但圆圈不稳定且不一致。因此,我用 findContours 替换了 HoughCircles,循环遍历轮廓以寻找最大的轮廓,并使用了 minEnclosingCircle。现在是代码:

Mat currentFrame = new Mat(), hsv = new Mat(), threshImage = new Mat();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

while (true) {
    camera.read(currentFrame);

    Imgproc.resize(currentFrame, currentFrame, new Size(WIDTH, HEIGHT));
    Imgproc.cvtColor(currentFrame, hsv, Imgproc.COLOR_RGB2HSV);

    hsvWindow.showImage(hsv);

    Core.inRange(hsv, new Scalar(50, 100, 50), new Scalar(95, 255, 255), threshImage);
    Imgproc.blur(threshImage, threshImage, new Size(10, 10));
    Imgproc.threshold(threshImage, threshImage, 150, 255, Imgproc.THRESH_BINARY);

    threshWindow.showImage(threshImage);

    Imgproc.findContours(threshImage, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    double maxArea = 0;
    float[] radius = new float[1];
    Point center = new Point();
    for (int i = 0; i < contours.size(); i++) {
        MatOfPoint c = contours.get(i);
        if (Imgproc.contourArea(c) > maxArea) {
            MatOfPoint2f c2f = new MatOfPoint2f(c.toArray());
            Imgproc.minEnclosingCircle(c2f, center, radius);
        }
    }
    Core.circle(currentFrame, center, (int)radius[0], new Scalar(255, 0, 0), 2);

    drawWindow.showImage(currentFrame);
}

我知道这对于希望专门使用 HoughCircles 的人来说可能不是特别有用,但它证明了模糊二值图像的能力。如果您要在许多事物中寻找一个圆,您会寻找轮廓并将轮廓面积与其外接圆的面积进行比较。

关于java - 用于 Java 的 OpenCV : HoughCircles finding all the wrong circles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31504366/

相关文章:

java - 我的飞行路线脚本未滚动

安卓 OpenCV : How to slow down camera capture frame rate (purposefully)

Python 笔画宽度变换

python - OpenCV:用轮廓上的大多数点拟合椭圆(而不是最小二乘法)

java - 动态添加textview到CardView

java - Skiplist - 尝试实现 get() 和 set() 方法

c++ - 如何在openCV 2.3.1中加载xml级联文件

image - 从一组图片中检测(并去除)划痕

python - 如何使用pillow的python lib实现图像 channel 下降

java - 为什么 "boolean inclusive"版本的 NavigableSet 返回 NavigableSet,而那些没有 "boolean inclusive"版本的 NavigableSet 返回 SortedSet?