Ask Your Question
0

balltracking with python 2.7 and opencv

asked 2015-08-21 15:33:54 -0600

CesarOCV gravatar image

updated 2015-08-21 15:45:20 -0600

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

edit retag flag offensive close merge delete

Comments

1

"it doesnot run" is NO description of an error. How does your input image look like? Which error message do you get? What happens?

FooBar gravatar imageFooBar ( 2015-08-22 05:46:18 -0600 )edit

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

CesarOCV gravatar imageCesarOCV ( 2015-08-23 15:31:25 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2015-08-23 17:14:26 -0600

CesarOCV gravatar image

updated 2015-08-23 17:25:00 -0600

this is my new code

import cv2

import numpy as np

Iniciamos la camara

cv2.namedWindow("preview") 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()
blur = cv2.GaussianBlur(frame, (5,5), 0)
hsv = cv2.cvtColor(blur, 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)

 #Encontrar el area de los objetos que detecta la camara
#moments = cv2.moments(mask)
#area = moments['m00']

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("preview", frame)
    continue
    circles = np.uint16(np.around(circles))

for i in circles[0,:]:

    print i
    cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
    cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle

#print area

#Mostramos la imagen original con la marca del centro y
#la mascara

cv2.imshow('mask',mask)

cv2.imshow('circles', frame)
tecla = cv2.waitKey(5) & 0xFF
if tecla == 27:
    break

cv2.destroyAllWindows()

now my code run, but only detect circles if the background is green, only detect circles inside a background green, its not util, can anybody help me?

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-08-21 15:33:54 -0600

Seen: 1,195 times

Last updated: Aug 23 '15