Files
Python-2019/Detection/detect_circles.py
T
2019-09-02 17:56:04 +02:00

29 lines
887 B
Python

# import the necessary packages
import numpy as np
import argparse
import cv2
import cv2.cv as cv
circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT)
img = cv2.imread("numeriser.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# show the output image
cv2.imshow("output", np.hstack([image, output]))
cv2.waitKey(0)