霍夫变换方法后得到一圈

Get one circle after Hough transform method

我使用霍夫变换方法得到了两个圆圈,如何从for循环中只得到大圆圈的区域?

vector<Vec3f> circles;

/// Apply the Hough Transform to find the circles;
HoughCircles(openImg, circles, CV_HOUGH_GRADIENT, 1,1,67, 17,35, 80);

/// Draw the circles detected
for (size_t i = 0; i < circles.size(); i++)
{
    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    // circle center
    circle(openImg, center, 1, Scalar(255, 255, 255), -1, 8, 0);
    // circle outline
    circle(openImg, center, radius, Scalar(255, 255, 255), 1, 4, 0);

}

/// Show your results
namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
imshow("Hough Circle Transform Demo", openImg);

opencv 的文档指出:

circles – Output vector of found circles. Each vector is encoded as a 3-element floating-point vector (x, y, radius)

https://docs.opencv.org/3.4.1/d3/de5/tutorial_js_houghcircles.html

您正在读取半径 (int radius = cvRound(circles[i][2]);)。那实际上就是圆的大小。

因此,您需要遍历数组 circles 并选择半径最大的圆:

// remember biggest radius
float radiusBiggest = 0;
// remember the index of the biggest radius / circle
int indexBiggest = -1;
// loop through all circles
for (size_t i = 0; i < circles.size(); i++)
{
    // get the radius
    float radius = circles[i][2];
    // check if this radius is bigger than any previous
    if (radius > radiusBiggest)
    {
        // this radius/circle is the biggest so far. remember it
        radiusBiggest = radius;
        indexBiggest = i;
    }
}
// if we found a circle then draw it
if (indexBiggest != -1)
{
    Point center(cvRound(circles[indexBiggest][0]), cvRound(circles[indexBiggest][1]));
    int radius = cvRound(circles[indexBiggest][2]);
    // circle center
    circle(openImg, center, 1, Scalar(255, 255, 255), -1, 8, 0);
    // circle outline
    circle(openImg, center, radius, Scalar(255, 255, 255), 1, 4, 0);
}