balltracking with python 2.7 and opencv
im working in ball tracking with hsv and houghcircles, my code is
import cv2
import numpy as np
#Iniciamos la camara
captura = cv2.VideoCapture(0)
if captura.isOpened(): # try to get the first frame rval, frame = captura.read() else: rval = False
while rval:
#Capturamos una imagen y la convertimos de RGB -> HSV
_,frame = captura.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#Establecemos el rango de colores que vamos a detectar
#En este caso de verde oscuro a verde-azulado claro
verde_bajos = np.array([49,50,50], dtype=np.uint8)
verde_altos = np.array([80, 255, 255], dtype=np.uint8)
#Crear una mascara con solo los pixeles dentro del rango de verdes
mask = cv2.inRange(hsv, verde_bajos, verde_altos)
#Encontrar el area de los objetos que detecta la camara
#moments = cv2.moments(mask)
#area = moments['m00']
#Descomentar para ver el area por pantalla
#print area
#if(area > 20000000):
#Buscamos el centro x, y del objeto
#x = int(moments['m10']/moments['m00'])
#y = int(moments['m01']/moments['m00'])
#Mostramos sus coordenadas por pantalla
#print "x = ", x
#print "y = ", y
#Dibujamos una marca en el centro del objeto
#cv2.rectangle(imagen, (x, y), (x+2, y+2),(0,0,255), 2)
#imgg = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
#cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
res = cv2.bitwise_or(frame,frame, mask= mask)
image = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
##-----DETECCIÓN de Círculos-----
circles = cv2.HoughCircles(image,cv2.HOUGH_GRADIENT,1,20,param1=100,param2=30,minRadius=50,maxRadius=100)
if circles is None:
#cv2.imshow("frame", frame)
continue
#circles = np.uint16(np.around(circles))
for i in circles[0,:]:
print i
cv2.circle(imagen,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
cv2.circle(imagen,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
#Mostramos la imagen original con la marca del centro y
#la mascara
cv2.imshow('mask', mask)
cv2.imshow('Camara', frame)
tecla = cv2.waitKey(5) & 0xFF
if tecla == 27:
break
cv2.destroyAllWindows()
But it doesnot work, it doesnot run and i think that is a problem with the function houghcircles.
Anybody can help me?
thanks
"it doesnot run" is NO description of an error. How does your input image look like? Which error message do you get? What happens?
thanks my friend, you are right,
i didn't get an error, but my code doesn't work, when i run the code, the window "preview" appear but do not shoy nothing