c++ - opencv检测到多个小圆圈,但没有检测到更大的圆圈

标签 c++ opencv

我是 OpenCV 的新手,我试图只检测一分钱图像,但我得到了一堆小圆圈。谁能告诉我我做错了什么?

代码来自这里:https://github.com/opencv/opencv/blob/master/samples/cpp/houghcircles.cpp

我唯一改变的是使最小圆半径为 400,最大圆为 0。因为我知道图像将是 600x480,所以便士圆必须至少为 400。

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

static void help()
{
    cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
            "Usage:\n"
            "./houghcircles <image_name>, Default is ../data/board.jpg\n" << endl;
}

int main(int argc, char** argv)
{
    cv::CommandLineParser parser(argc, argv,
        "{help h ||}{@image|../data/board.jpg|}"
    );
    if (parser.has("help"))
    {
        help();
        return 0;
    }
    //![load]
    string filename = parser.get<string>("@image");
    Mat img = imread(filename, IMREAD_COLOR);
    if(img.empty())
    {
        help();
        cout << "can not open " << filename << endl;
        return -1;
    }
    //![load]

    //![convert_to_gray]
    Mat gray;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    //![convert_to_gray]

    //![reduce_noise]
    medianBlur(gray, gray, 5);
    //![reduce_noise]

    //![houghcircles]
    vector<Vec3f> circles;
    HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
                 gray.rows/16, // change this value to detect circles with different distances to each other
                 100, 30, 400,0 // change the last two parameters
                                // (min_radius & max_radius) to detect larger circles
                 );
    //![houghcircles]

    //![draw]
    for( size_t i = 0; i < circles.size(); i++ )
    {
        Vec3i c = circles[i];
        circle( img, Point(c[0], c[1]), c[2], Scalar(0,0,255), 3, LINE_AA);
        circle( img, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, LINE_AA);
    }
    //![draw]

    //![display]
    imshow("detected circles", img);
    waitKey();
    //![display]

    return 0;
}

enter image description here

enter image description here

最佳答案

您混淆了半径和直径。如果图片只有 600x480,则最小半径不能为 400。将您的 min_radius 设置为 200。

关于c++ - opencv检测到多个小圆圈,但没有检测到更大的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41092023/

相关文章:

c++ - 多线程 OpenCV 程序

c++ - 使用哪个,utf8 还是 Ascii

python - 使用 python-opencv 的水平和垂直边缘轮廓

python - 卡在 OpenCV 中的颜色变化上

c++ - 在 C++ Introsort 中手动展开循环运行不正确

python - 根据累积量从轮廓填充区域

python-3.x - 带有光流的箭袋图?

c++ - 打印对象的映射,其中另一个对象作为键

c++ - Qt 5.7 QNetworkProxy 没有考虑cookies

c++ - 为什么建议在删除后将指针设置为null?