欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java+opencv3.2.0实现hough圆检测功能

程序员文章站 2022-06-19 23:41:27
hough圆检测和hough线检测的原理近似,对于圆来说,在参数坐标系中表示为c:(x,y,r)。 函数: imgproc.houghcircles(mat image...

hough圆检测和hough线检测的原理近似,对于圆来说,在参数坐标系中表示为c:(x,y,r)。

函数:

imgproc.houghcircles(mat image, mat circles, int method, double dp, double mindist, double param1, double param2, int minradius, int maxradius)

参数说明:

image:源图像
circles:检测到的圆的输出矢量(x,y,r)
method:使用的检测方法,目前只有一种imgproc.hough_gradient
dp:检测圆心的累加器图像与源图像之间的比值倒数
mindist:检测到的圆的圆心之间的最小距离
param1:method设置的检测方法对应参数,针对hough_gradient,表示边缘检测算子的高阈值(低阈值是高阈值的一半),默认值100
param2:method设置的检测方法对应参数,针对hough_gradient,表示累加器的阈值。值越小,检测到的无关的圆
minradius:圆半径的最小半径,默认为0
maxradius:圆半径的最大半径,默认为0(若minradius和maxradius都默认为0,则houghcircles函数会自动计算半径)

示例代码:

public static void main(string[] args)
  {
    system.loadlibrary(core.native_library_name);
    mat src = imgcodecs.imread("f:\\websbook_com_1589226.jpg");

    mat dst = src.clone();
    imgproc.cvtcolor(src, dst, imgproc.color_bgr2gray);

    mat circles = new mat();
    imgproc.houghcircles(dst, circles, imgproc.hough_gradient, 1, 100, 440, 50, 0, 345);
    // imgproc.houghcircles(dst, circles, imgproc.hough_gradient, 1, 100,
    // 440, 50, 0, 0);
    for (int i = 0; i < circles.cols(); i++)
    {
      double[] vcircle = circles.get(0, i);

      point center = new point(vcircle[0], vcircle[1]);
      int radius = (int) math.round(vcircle[2]);

      // circle center
      imgproc.circle(src, center, 3, new scalar(0, 255, 0), -1, 8, 0);
      // circle outline
      imgproc.circle(src, center, radius, new scalar(0, 0, 255), 3, 8, 0);
    }

    imgcodecs.imwrite("f:\\dst2.jpg", src);
  }

源图像:

Java+opencv3.2.0实现hough圆检测功能

输出图像:

Java+opencv3.2.0实现hough圆检测功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。