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