org.opencv.imgproc.Imgproc.HoughCircles()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(4.5k)|赞(0)|评价(0)|浏览(268)

本文整理了Java中org.opencv.imgproc.Imgproc.HoughCircles()方法的一些代码示例,展示了Imgproc.HoughCircles()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Imgproc.HoughCircles()方法的具体详情如下:
包路径:org.opencv.imgproc.Imgproc
类名称:Imgproc
方法名:HoughCircles

Imgproc.HoughCircles介绍

[英]Finds circles in a grayscale image using the Hough transform.

The function finds circles in a grayscale image using a modification of the Hough transform. Example: ``

// C++ code:

#include

#include

#include

using namespace cv;

int main(int argc, char argv)

Mat img, gray;

if(argc != 2 && !(img=imread(argv[1], 1)).data)

return -1;

cvtColor(img, gray, CV_BGR2GRAY);

// smooth it, otherwise a lot of false circles may be detected

GaussianBlur(gray, gray, Size(9, 9), 2, 2);

vector circles;

HoughCircles(gray, circles, CV_HOUGH_GRADIENT,

2, gray->rows/4, 200, 100);

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

// draw the circle center

circle(img, center, 3, Scalar(0,255,0), -1, 8, 0);

// draw the circle outline

circle(img, center, radius, Scalar(0,0,255), 3, 8, 0);

namedWindow("circles", 1);

imshow("circles", img);

return 0;

Note: Usually the function detects the centers of circles well. However, it may fail to find correct radii. You can assist to the function by specifying the radius range (minRadius and maxRadius) if you know it. Or, you may ignore the returned radius, use only the center, and find the correct radius using an additional procedure.

Note:

  • An example using the Hough circle detector can be found at opencv_source_code/samples/cpp/houghcircles.cpp
    [中]使用Hough变换在灰度图像中查找圆。
    该函数使用对Hough变换的修改在灰度图像中查找圆。示例:``
    //C代码:
    #包括
    #包括
    #包括
    使用名称空间cv;
    int main(int argc、char argv)
    Mat img,灰色;
    如果(argc!=2&&!(img=imread(argv[1],1))。(数据)
    返回-1;
    CVT颜色(img、灰色、CV_bgr2灰色);
    //将其平滑,否则可能会检测到大量假圆
    高斯模糊(灰色,灰色,大小(9,9),2,2);
    矢量圆;
    HOUGH圆(灰色、圆形、CV_HOUGH_渐变、,
    2,灰色->行/42000);
    对于(size_t i=0;i<circles.size();(i

    点中心(cvRound(圆[i][0]),cvRound(圆[i][1]);
    int radius=cvRound(圆[i][2]);
    //画圆心
    圆(img,中心,3,标量(0255,0),-1,8,0);
    //画圆的轮廓
    圆(img,中心,半径,标量(0,0255),3,8,0);
    名称(“圆圈”,1);
    imshow(“圆圈”,img);
    返回0;
    注:通常该功能可以很好地检测圆的中心。但是,它可能无法找到正确的半径。如果您知道半径范围(minRadiusmaxRadius),则可以通过指定半径范围来辅助该函数。或者,您可以忽略返回的半径,仅使用中心,然后使用其他步骤找到正确的半径。
    注:
    *使用Hough圆检测器的示例可在opencv_源代码/samples/cpp/Hough圆中找到。cpp

代码示例

代码示例来源:origin: openpnp/openpnp

public FluentCv findCirclesHough(int minDiameter, int maxDiameter, int minDistance,
    String... tag) {
  Mat circles = new Mat();
  Imgproc.HoughCircles(mat, circles, Imgproc.CV_HOUGH_GRADIENT, 1, minDistance, 80, 10,
      minDiameter / 2, maxDiameter / 2);
  store(circles, tag);
  return this;
}

代码示例来源:origin: openpnp/openpnp

@Override
  public Result process(CvPipeline pipeline) throws Exception {
    Mat mat = pipeline.getWorkingImage();
    Mat output = new Mat();
    Imgproc.HoughCircles(mat, output, Imgproc.CV_HOUGH_GRADIENT, dp, minDistance, param1,
        param2, minDiameter / 2, maxDiameter / 2);
    List<Result.Circle> circles = new ArrayList<>();
    for (int i = 0; i < output.cols(); i++) {
      double[] circle = output.get(0, i);
      double x = circle[0];
      double y = circle[1];
      double radius = circle[2];
      circles.add(new Result.Circle(x, y, radius * 2.0));
    }
    output.release();

    return new Result(null, circles);
  }
}

代码示例来源:origin: openpnp/openpnp

Imgproc.HoughCircles(mat, output, Imgproc.CV_HOUGH_GRADIENT, dp,
    minDistance,
    param1, param2,

代码示例来源:origin: us.ihmc/IHMCPerception

Imgproc.HoughCircles(thresholdMat, houghCirclesOutputMat, Imgproc.CV_HOUGH_GRADIENT, 1, thresholdMat.rows() / 8, 100, 15, 0, 0);

代码示例来源:origin: us.ihmc/ihmc-perception

Imgproc.HoughCircles(thresholdMat, houghCirclesOutputMat, Imgproc.CV_HOUGH_GRADIENT, 1, thresholdMat.rows() / 8, 100, 15, 0, 0);

代码示例来源:origin: openpnp/openpnp

public static Mat houghCircles(Mat mat, double minDiameter, double maxDiameter,
    double minDistance) {
  Logger.debug("houghCircles(Mat, {}, {}, {})",
      new Object[] {minDiameter, maxDiameter, minDistance});
  saveDebugImage(OpenCvUtils.class, "houghCircles", "input", mat);
  // save a copy of the image for debugging
  Mat debug = mat.clone();
  // hough requires grayscale images
  mat = toGray(mat);
  // and prefers a blurred image
  mat = gaussianBlur(mat, 9);
  // run the houghcircles algorithm
  Mat circles = new Mat();
  Imgproc.HoughCircles(mat, circles, Imgproc.CV_HOUGH_GRADIENT, 1, minDistance, 80, 10,
      (int) (minDiameter / 2), (int) (maxDiameter / 2));
  if (LogUtils.isDebugEnabled()) {
    drawCircles(debug, circles);
    saveDebugImage(OpenCvUtils.class, "houghCircles", "debug", debug);
  }
  saveDebugImage(OpenCvUtils.class, "houghCircles", "output", mat);
  return circles;
}

代码示例来源:origin: JavaOpenCVBook/code

Imgproc.HoughCircles(canny, circles,Imgproc.CV_HOUGH_GRADIENT, 1, canny.rows()/8, 200, lowThreshold, 0, 0 );
System.out.println(circles.size());
for( int i=0;i<circles.cols();i++){

相关文章

微信公众号

最新文章

更多

Imgproc类方法