python cv2.HoughCircles Hough circle detection

HoughCircles Use and Instructions


cv2 provides a method of circle detection: HoughCircles. The return result of this function has a lot to do with the parameter settings.
The detected images are 9 coins, using thresholding (Otsu method and triangulation method), mean shift filtering and unprocessed images respectively. The result of the experiment is that as long as the two parameters of param1 and param2 are adjusted, the above method can accurately identify the circle in the image. The closest thing to a circle is the Otsu method threshold. Using this method also requires the use of cv2.THRESHOLD_TRUNC.

1. HoughCircles description

The function definition is as follows:

HoughCircles(image, method, dp, minDist, circles=None, param1=None, param2=None, minRadius=None, maxRadius=None)
parameter meaning
image The original image
method Currently only cv2.HOUGH_GRADIENT is supported
dp Inverse scaling of image parsing. 1 is the original size, 2 is half the original size
minDist Minimum distance between circle centers. If it is too small, it will increase the misjudgment of the circle, if it is too large, the existing circle will be lost
parameter1 High threshold for Canny detector
param2 Accumulator threshold for the center of the detection phase. If it is smaller, it will increase the circle that does not exist; if it is larger, the detected circle will be closer to a perfect circle
minRadius The radius of the smallest circle detected
maxRadius The radius of the largest circle detected

2. Code

# coding:utf8

import cv2
import numpy as np


def row_method(src):
    image = np.array(src)
    cimage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # 灰度图
    circles = cv2.HoughCircles(cimage, cv2.HOUGH_GRADIENT, 1, 40, param1=250, param2=58, minRadius=0)
    circles = np.uint16(np.around(circles))  # 取整
    for i in circles[0, :]:
        cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 在原图上画圆,圆心,半径,颜色,线框
        cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2)  # 画圆心
    cv2.putText(image, "param1=250, param2=58", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    cv2.imshow("row_circles", image)


def threshold_OTSU_method(src):
    image = np.array(src)
    cimage = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)  # 灰度图
    th, dst = cv2.threshold(cimage, 200, 255, cv2.THRESH_BINARY + cv2.THRESH_TRUNC + cv2.THRESH_OTSU)
    circles = cv2.HoughCircles(dst, cv2.HOUGH_GRADIENT, 1, 40, param1=50, param2=47, minRadius=0)
    circles = np.uint16(np.around(circles))  # 取整
    for i in circles[0, :]:
        cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 在原图上画圆,圆心,半径,颜色,线框
        cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2)  # 画圆心
    cv2.putText(image, "param1=50, param2=47", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    cv2.imshow("otsu_circles", image)


def threshold_triangle_method(src):
    image = np.array(src)
    cimage = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)  # 灰度图
    th, dst = cv2.threshold(cimage, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_TRIANGLE)
    circles = cv2.HoughCircles(dst, cv2.HOUGH_GRADIENT, 1, 40, param1=50, param2=17, minRadius=0)
    circles = np.uint16(np.around(circles))  # 取整
    for i in circles[0, :]:
        cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 在原图上画圆,圆心,半径,颜色,线框
        cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2)  # 画圆心
    cv2.putText(image, "param1=50, param2=17", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    cv2.imshow("triangle_circles", image)


def mean_circles(src):
    image = np.array(src)
    dst = cv2.pyrMeanShiftFiltering(image, 10, 100)  # 均值偏移滤波
    cimage = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)  # 灰度图
    circles = cv2.HoughCircles(cimage, cv2.HOUGH_GRADIENT, 1, 40, param1=50, param2=20, minRadius=0)
    circles = np.uint16(np.around(circles))  # 取整
    for i in circles[0, :]:
        cv2.circle(image, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 在原图上画圆,圆心,半径,颜色,线框
        cv2.circle(image, (i[0], i[1]), 2, (255, 0, 0), 2)  # 画圆心

    cv2.putText(image, "param1=50, param2=20", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    cv2.imshow("mean_circles", image)


src = cv2.imread("circle.png")  # 读取图片位置
cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image", src)
threshold_OTSU_method(src)
threshold_triangle_method(src)
mean_circles(src)
row_method(src)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. Results

insert image description here

Guess you like

Origin blog.csdn.net/weixin_42272768/article/details/125218619