本文作者Color Space,文章未经作者允许禁止转载!
测试图像与题目来源:https://www.cnpython.com/qa/116720
测试图:
结果图:
实现代码:
import cv2
import numpy as np
img = cv2.imread('./test.png',0)
cv2.imshow('src',img)
img = cv2.medianBlur(img,3)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,50,\
param1=100,param2=30,minRadius=25,maxRadius=35)
if circles is None:
print("找圆失败!")
else:
circles = np.uint16(np.around(circles))
a, b, c = circles.shape
print (circles.shape)
for i in range(b):
cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 255, 0), 2, cv2.LINE_AA)
cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 0, 255), 3, cv2.LINE_AA) # draw center of circle
cv2.imshow('detected circles',cimg)
cv2.imwrite('circle.png',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
更多视觉图像处理相关内容,请扫码关注:OpenCV与AI深度学习。
|