1#!/usr/bin/python 2 3''' 4This example illustrates how to use cv2.HoughCircles() function. 5Usage: ./houghcircles.py [<image_name>] 6image argument defaults to ../data/board.jpg 7''' 8 9import cv2 10import numpy as np 11import sys 12 13 14print __doc__ 15try: 16 fn = sys.argv[1] 17except: 18 fn = "../data/board.jpg" 19 20src = cv2.imread(fn, 1) 21img = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) 22img = cv2.medianBlur(img, 5) 23cimg = src.copy() # numpy function 24 25circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30) 26a, b, c = circles.shape 27for i in range(b): 28 cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA) 29 cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle 30 31cv2.imshow("source", src) 32cv2.imshow("detected circles", cimg) 33cv2.waitKey(0) 34