Python+OpenCV:Hough圆检测(Hough Circle Transform)
####################################################################################################
# Hough圆检测(Hough Circle Transform)
def lmc_cv_image_hough_circle_transform():
"""
函数功能: Hough圆检测(Hough Circle Transform)。
"""
# 读取图像
image = lmc_cv.imread('D:/99-Research/Python/Image/opencv-logo-white.png', flags=lmc_cv.IMREAD_UNCHANGED)
rgb_image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)
gray_image = lmc_cv.cvtColor(rgb_image, lmc_cv.COLOR_RGB2GRAY)
pyplot.figure('Image Display')
pyplot.subplot(1, 2, 1)
pyplot.imshow(rgb_image, cmap='gray')
pyplot.title('Original Image')
pyplot.xticks([])
pyplot.yticks([])
# Hough圆检测(Hough Circle Transform)
gray_image = lmc_cv.medianBlur(gray_image, 3)
cimg = lmc_cv.cvtColor(gray_image, lmc_cv.COLOR_GRAY2BGR)
circles = lmc_cv.HoughCircles(gray_image, method=lmc_cv.HOUGH_GRADIENT, dp=1, minDist=100,
param1=80, param2=30, minRadius=50, maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
# draw the outer circle
lmc_cv.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
# draw the center of the circle
lmc_cv.circle(cimg, (i[0], i[1]), 2, (255, 0, 0), 3)
pyplot.subplot(1, 2, 2)
pyplot.imshow(cimg, cmap='gray')
pyplot.title('Hough Circle Transform Image')
pyplot.xticks([])
pyplot.yticks([])
# 根据用户输入保存图像
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# 销毁窗口
pyplot.close('all')
return

