霍夫变换方法后得到一个圆

时间:2018-09-08 09:38:45

标签: c++ opencv hough-transform

我使用Hough变换方法,所以得到2个圆,如何才能从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);

The image

1 个答案:

答案 0 :(得分:0)

opencv状态的文档:

  

circles –找到的圆的输出向量。每个向量都编码为3个元素的浮点向量(x,y,半径)

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);
}