由于最近的事情一直有涉及霍夫圆变换,免得老翻书,这里记录下:
不用多说,OpenCV利用的是梯度法Python的API函数如下:
cv2.HoughCircles(image,method,dp,minDist[, circles[,param1, param2[,minRadius[,maxRadius]]]]])
其返回N个圆的信息储存在1×N×的ndarray。
在上我以前写的代码
from PIL import Image
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('05.jpg')
GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
GrayImage= cv2.medianBlur(GrayImage,5)
ret,th1 = cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(GrayImage,255,cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,3,5)
th3 = cv2.adaptiveThreshold(GrayImage,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,3,5)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(th2,kernel,iterations=1)
dilation = cv2.dilate(erosion,kernel,iterations=1)
imgray=cv2.Canny(erosion,30,100)
circles = cv2.HoughCircles(imgray,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=20,maxRadius=40)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
print(len(circles[0,:]))
cv2.imshow('detected circles',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
下面是效果图: