Skip to content

Instantly share code, notes, and snippets.

@KenoLeon
Created August 6, 2020 19:33
Show Gist options
  • Select an option

  • Save KenoLeon/c4554c6fe6ce851857b3bb94e3cc6b84 to your computer and use it in GitHub Desktop.

Select an option

Save KenoLeon/c4554c6fe6ce851857b3bb94e3cc6b84 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
# Extract regions/pixels of interest
rows = gray.shape[0]
# Find circles:
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8,
param1=100, param2=30,
minRadius=1, maxRadius=30)
# Found circles ? Draw on top of them
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1])
# circle center
cv2.circle(frame, center, 1, (0, 100, 100), 3)
# circle outline
radius = i[2]
cv2.circle(frame, center, radius, (255, 0, 255), 3)
# Display orignal frame:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment