通过python做圆形检测,基本都是一个套路,利用霍夫检测的思路,借用cv包的HoughCircles进行检测,这里要注意该函数的参数,不同的参数应对不同的业务场景,参数说明如下表
我经常修改的参数为minDist,param2,minRadius,maxRadius。一定不要犯懒,都挨个去理解,去试。
贴上我的代码和业务数据:代码也是我在别的地方抄的,随便百度就能找到
import cv2
img = cv2.imread('O:/work/circleimg/1009/yuan4.png')
# 灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th2 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
cv2.THRESH_BINARY, 11, 2)
ret, thresh1 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
canny = cv2.Canny(thresh1, 40, 80)
canny = cv2.blur(canny, (3, 3))
# 霍夫变换圆检测
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 1000, param1=10, param2=35, minRadius=40, maxRadius=150)
# 输出检测到圆的个数
print(len(circles[0]))
for circle in circles[0]:
if (circle[2] >= 100):
continue
x = int(circle[0])
y = int(circle[1])
r = int(circle[2])
img = cv2.circle(img, (x, y), r, (0, 0, 255), -1)
cv2.imshow('res', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
因为业务场景要求不一定是圆形可以当作圆,闭合的曲线也可以作为圆,所以就探索了新的思路。这里我贴一个链接,应该能给你一些启发思路
我的思路来源于对cv.findContours函数的参数和返回值的发现。
import cv2 as cv
import os
import numpy as np
# 定义圆形轮廓层级结构
x = [[[-1, -1, 1,-1], [-1, -1, 2,0], [-1, -1, -1,1]]]
x = np.array(x)
def circle(j):
src = cv.imread('O:/work/circleimg/1009/'+str(j))
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
_, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
contours, hierarchy = cv.findContours(gray, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
# 轮廓数量为3 且符合x标准
print(j)
if len(contours)==3 and (hierarchy == x).all():
if abs(len(contours[1])-len(contours[2]))<40:
print("有缘")
else:
print("无缘")
else:
print("无缘")
cv.drawContours(src,contours,-1,(0,0,255),2)
cv.imshow("edged", src)
cv.waitKey(0)
cv.destroyAllWindows()
for i in os.listdir('O:/work/circleimg/1009/'):
circle(i)
简要说明: