The principles of hough circle detection and hough line detection are similar. For circles, they are expressed as C:(x,y,r) in the parameter coordinate system.
function:
Imgproc.HoughCircles(Mat image, Mat circuits, int method, double dp, double minDist, double param1, double param2, int minRadius, int maxRadius)
Parameter description:
image: source image
circles: The output vector of the detected circle (x,y,r)
method: The detection method used, currently there is only one Imgproc.HOUGH_GRADIENT
dp: The inverse of the ratio between the accumulator image at the center of the circle and the source image
minDist: Minimum distance between the centers of the detected circle
param1: The corresponding parameters of the detection method set by method. For HOUGH_GRADIENT, it indicates the high threshold of the edge detection operator (the low threshold is half of the high threshold), and the default value is 100
param2: The corresponding parameters of the detection method set by method are for HOUGH_GRADIENT, indicating the threshold value of the accumulator. The smaller the value, the unrelated circle detected
minRadius: The minimum radius of the circle radius, default is 0
maxRadius: The maximum radius of the circle radius, default to 0 (if both minRadius and maxRadius are default to 0, the HoughCircles function will automatically calculate the radius)
Sample code:
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); }Source image:
Output image:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.