#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Application de démonstration de détection Vidéo. Cette application permet de détecter des cercles, et d'afficher le résultat. Utilise la librairie de fonctions OpenCV. ''' # Compatibilité Python 2/3 from __future__ import print_function ############################################################################### # Informations de versions ############################################################################### __project__ = u'Particles Detection' __author__ = u'Simon CHOLLET' __modifiers__ = u'' __history__ = u''' - Révision 1.0 (2016/10/04) : Première version. ''' __date__ = u'2016/10/04' __version__ = u'1.0.0' ############################################################################### # Importations ############################################################################### # Librairies systeme import time # Librairie OpenCV import cv2 # Librairie Numpy import numpy as np ############################################################################### # Définitions specifiques ############################################################################### # Numéro de Webcam à utiliser : -1 pour la première WEBCAM_NUMBER = 0 # Epaisseur du cercle à dessiner en pixel CIRCLE_THIN = 1 # Epaisseur du rectangle de centre à dessiner en pixel CENTER_THIN = 1 ############################################################################### # Déclaration des classes / fonctions ############################################################################### ############################################################################### # Exécution ... ############################################################################### # Paramètres de fonction : # image: 8-bit, single channel image. If working with a color image, convert to # grayscale first. # method: Defines the method to detect circles in images. Currently, the only # implemented method is cv2.HOUGH_GRADIENT, which corresponds to the # Yuen et al. paper. # dp: This parameter is the inverse ratio of the accumulator resolution to the # image resolution (see Yuen et al. for more details). Essentially, the # larger the dp gets, the smaller the accumulator array gets. # minDist: Minimum distance between the center (x, y) coordinates of detected # circles. If the minDist is too small, multiple circles in the same # neighborhood as the original may be (falsely) detected. If the # minDist is too large, then some circles may not be detected at all. # param1: Gradient value used to handle edge detection in the Yuen et al. # method. # param2: Accumulator threshold value for the cv2.HOUGH_GRADIENT method. The # smaller the threshold is, the more circles will be detected # (including false circles). The larger the threshold is, the more # circles will potentially be returned. # minRadius: Minimum size of the radius (in pixels). # maxRadius: Maximum size of the radius (in pixels). # # # Fonction appelée lors de la demande # d'exécution de la classe/module if __name__ == "__main__": # Ouverture de la Webcam cap = cv2.VideoCapture(WEBCAM_NUMBER) # Réglage de la caméra # cap.set(14, 10) # On acquière tout le temps while True: # Capture de l'image ret, frame = cap.read() # Copie de l'image acquise frameCopy = frame.copy() # Transformation en Gris gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Reduction du bruit de l'image #gray = cv2.GaussianBlur(gray, (9, 9), 2) gray = cv2.adaptiveThreshold( gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 3) # Recherche de cercles circles = cv2.HoughCircles( image=gray, method=cv2.HOUGH_GRADIENT, dp=1.2, minDist=50, param1=10, param2=10, minRadius=1, maxRadius=7) # Verifie si l'on a trouvé un cercle if circles is not None: # Conversion des coordonnées et du rayon en entier circles = np.round(circles[0, :]).astype("int") # Pour tous les cercles trouvés cptIdx = 0 for (x, y, r) in circles: # Dessine le cercle sur la copie d'image cv2.circle(frameCopy, (x, y), r, (0, 255, 0), CIRCLE_THIN) # Dessine le centre du cercle cv2.rectangle(frameCopy, (x - CENTER_THIN, y - CENTER_THIN), (x + CENTER_THIN, y + CENTER_THIN), (0, 128, 255), -1) print (u'Particule trouvée : (%d, %d, %d)' % (x, y, r)) # Affiche les 2 images #cv2.imshow(__project__, gray) cv2.imshow(__project__, np.hstack([frame, frameCopy])) # Touche Esc pour quitter ! key = cv2.waitKey(1) & 0xFF if key == 27: break # Attente pour la prochaine image #time.sleep(0.5) # Libère les ressources cap.release() cv2.destroyAllWindows()