def detect_circles(image_path):
    gray = cv2.imread(image_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
gray_blur = cv2.medianBlur(gray, 13)
gray_lap = cv2.Laplacian(gray_blur, cv2.CV_8UC1, ksize=5)
dilate_lap = cv2.dilate(gray_lap, (3, 3))
lap_blur = cv2.bilateralFilter(dilate_lap, 5, 9, 9)
circles = cv2.HoughCircles(lap_blur, cv2.cv.CV_HOUGH_GRADIENT, 16, 55,
    param2=450, minRadius=20, maxRadius=40)
cimg = draw_circles(gray, circles)
print('{} circles detected.'.format(circles[0].shape[0]))
return cimg
Find all circles in image