如何在图像上找到避开内部圆圈的圆圈?

时间:2014-05-10 17:42:39

标签: c++ opencv image-processing optimization

如何使用minEnclosingCircle方法在图像上找到圆圈,但避开内部圆圈?

我正在使用带c ++的opencv来检测图像上的圆圈。我正在使用HoughCircles方法,但有时会丢失一些圆圈或检测到假圆圈。

因此我将它替换为minEnclosingCircle。现在算法找到所有圆圈,但在某些情况下,它在其他圆圈内找到圆圈,我想避免这种情况。

图片1是输入的示例,图片2是输出的。

enter image description here enter image description here

用于处理这些图像的代码是:

代码开始:


vector < Circle > houghCircles(Mat thresholdImage, float minRadius, float maxRadiuss) {

  vector < vector < Point > > contours;
  vector < Vec4i > hierarchy;

  findContours(thresholdImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

  vector < vector < Point > > contours_poly(contours.size());
  vector < Circle > circlesTMP(contours.size());
  for (int i = 0; i < contours.size(); i++) {

    Point2f detectedCenter;
    float detectedRadius;

    approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
    minEnclosingCircle((Mat) contours_poly[i], detectedCenter, detectedRadius);

    if (minRadius != 0 && detectedRadius < minRadius) {
      continue;
    }
    if (maxRadiuss != 0 && detectedRadius > maxRadiuss) {
      continue;
    }

    sf::Circle _circle(detectedCenter, detectedRadius);
    circlesTMP.push_back(_circle);
  }

  vector < Circle > circles;
  for (int i = 0; i < circlesTMP.size(); i++) {
    sf::Circle _circle = circlesTMP[i];
    if (_circle.getRadius() > 0) {
      circles.push_back(circlesTMP[i]);
    }
  }

  cout << "Circles found: " << circles.size() << endl;
  Mat drawing = Mat::zeros(thresholdImage.size(), CV_8UC3);
  for (int i = 0; i < circles.size(); i++) {
    sf::Circle _circle = circles[i];
    Scalar color = Scalar(200, 187, 255);
    circle(drawing, _circle.getCenter(), (int) _circle.getRadius(), color, 2, 8, 0);
  }

  imshow(drawing, "OpenCVUtil");
  waitkey();

  return circles;
}

1 个答案:

答案 0 :(得分:0)

您可以检查此链接并更改模式参数并测试您的代码: http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours

查看您获得的CV_RETR_EXTERNAL而不是CV_RETR_TREE作为模式的结果。