Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Open CV example for finding the number of circles in an image
# Tuned to find the number of balls/circles in this image
import cv2
import as cv
import numpy as np
img = cv2.imread('circles.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,, 1, 3,
param1=170, #this controls the edge detection thresholds - 170-200 produces reasonable results all in that 20k-30k range
print len(circles[0,:]) #this is the answer we care about
circles = np.uint16(np.around(circles))
for i in circles[0,:]:,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',img) #show the image with all found circles highlighted as a sanity check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment