def correct_circle img circles for in reversed range len circles img s

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def correct_circle(img, circles):
for i in reversed(range(len(circles))):
h, w = img.shape[:2]
if int(circles[i][0]) > h - 1 or int(circles[i][1]) > w - 1 or img[circles[i][1], circles[i][0]] != 0:
circles[i][2] = 0
return circles
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 30,
param1=30,
param2=30,
minRadius=0,
maxRadius=100)
if circles is not None:
circles = circles[0]
print circles
circles = correct_circle(gray, circles)
circles = np.uint16(np.around(circles))
for i in circles:
if i[2] != 0:
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('circles', img)