Skip to content

Instantly share code, notes, and snippets.

@bechu
Created October 18, 2013 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bechu/7043296 to your computer and use it in GitHub Desktop.
Save bechu/7043296 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
if __name__ == '__main__':
stylo = cv2.imread("stylo.png")
hsv = cv2.cvtColor(stylo, cv2.COLOR_BGR2HSV)
maxs = np.max(np.max(hsv, axis=0), axis=0)
mins = np.min(np.min(hsv, axis=0), axis=0)
# on accede au device
capture = cv2.VideoCapture(0)
while True:
# on capture une image
_, frame = capture.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
thresh = cv2.inRange(hsv, mins, maxs)
# on recherche les contours
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# quel est l'aire la plus grande ?
max_area = 0
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area and area > 20:
max_area = area
best_cnt = cnt
# on dessine le point le plus proche de nos attentes
if max_area > 0:
M = cv2.moments(best_cnt)
cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
cv2.circle(frame,(cx,cy),15,(0, 0, 255),-1)
# et on affiche
cv2.imshow("Track blue blob", frame)
# si interuption clavier, on quitte le prgm
if (cv2.waitKey(2) >= 0):
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment