opencv检测到多个小圆圈,但不是大圆圈

时间:2016-12-11 22:49:47

标签: 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

2 个答案:

答案 0 :(得分:2)

你的半径和直径混淆了。如果图像仅为600x480,则最小半径不能为400。将min_radius设置为200。

答案 1 :(得分:1)

HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
             max(gray.cols,gray.rows), // to find only the biggest perfect circle
             100, 100, 0,0 // leave other params as default

);

enter image description here