Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/python/houghcircles.py
16337 views
1
#!/usr/bin/python
2
3
'''
4
This example illustrates how to use cv.HoughCircles() function.
5
6
Usage:
7
houghcircles.py [<image_name>]
8
image argument defaults to ../data/board.jpg
9
'''
10
11
# Python 2/3 compatibility
12
from __future__ import print_function
13
14
import cv2 as cv
15
import numpy as np
16
import sys
17
18
if __name__ == '__main__':
19
print(__doc__)
20
21
try:
22
fn = sys.argv[1]
23
except IndexError:
24
fn = "../data/board.jpg"
25
26
src = cv.imread(fn, 1)
27
img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
28
img = cv.medianBlur(img, 5)
29
cimg = src.copy() # numpy function
30
31
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30)
32
33
if circles is not None: # Check if circles have been found and only then iterate over these and add them to the image
34
a, b, c = circles.shape
35
for i in range(b):
36
cv.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv.LINE_AA)
37
cv.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv.LINE_AA) # draw center of circle
38
39
cv.imshow("detected circles", cimg)
40
41
cv.imshow("source", src)
42
cv.waitKey(0)
43
44