Commit cb977849 authored by Aylen Ricca's avatar Aylen Ricca
Browse files

finished configuracion.py

parent 1ffb16f8
......@@ -4,12 +4,13 @@
# Calibrador de colores basado en HSV
#
# Obtener HSV maximo y minimo de pixeles clickeados
# Ademรกs hace un threshold para mostrar lo que se selecciona con el rango
# ATENCION: Tener en cuenta que el color rojo es un hue aproximado de 0. Si es menor, los valores retornados pasan por la discontinuidad del hue, retornando un valor aproximado a 180 (maximo hue de opencv).
# Para este caso se puede correr la discontinuidad de los 180 a los 90 grados, cambiando la variable hueNearZero a true.
#
# Se puede usar con la cรกmara o una imagen.
# Ademas hace un threshold para mostrar lo que se selecciona con el rango
# ATENCION: Tener en cuenta que el color rojo es un hue aproximado de 0.
# Si es menor, los valores retornados pasan por la discontinuidad del hue,
# retornando un valor aproximado a 180 (maximo hue de opencv).
# Para este caso se puede correr la discontinuidad de los 180 a los 90 grados,
# cambiando la variable hueNearZero a true.
# Se puede usar con la camara o una imagen.
import cv
#import cv2
......@@ -19,137 +20,139 @@ import numpy as np
global src
#configuracion, captura de camara o archivo.
# CONFIGURACION
# captura de camara (True + cameraId=Id_Cam_Connected) o archivo (False + fileName="Path_To_File").
captureFromCam = True
fileName = "capture2.jpg" # Usado en caso que captureFromCam sea False
cameraId = 0 # Usado en caso que captureFromCam sea True
#Cuando se estรกn calibrando colores con Hue cercano a 0, por ejemplo rojo. Por defecto, dejar en False
#Si estรก en true, la discontinuidad del hue se mueve a los 90 grados.
# Por defecto, dejar en False
# Cuando se estan calibrando colores con Hue cercano a 0, por ejemplo rojo, pasar a True
# (la discontinuidad del hue se mueve a los 90 grados.)
hueNearZero = False
#Variables
# zoom: opcional
show_zoom = True
# VARIABLES
x_co = 0
y_co = 0
dragging = False
minHSV = list((0, 4, 23))
maxHSV = list((174, 205, 83))
#minHSV = None #list((0, 0, 100))
#maxHSV = None #list((100, 2, 54))
#min_hsv_arena = np.array((0, 100, 100), np.int32)
#max_hsv_arena = np.array((100, 224, 256), np.int32)
# si se desea comenzar la calibracion desde un HSV min y max conocido modificar aqui
minHSV = None #list((0, 0, 100))
maxHSV = None #list((100, 2, 54))
#----------------#----------------#----------------#----------------#----------------
# funcion encargada del zoom local (window extra)
def print_zoom_window(x,y):
s=list(cv.Get2D(src,0,0))
if (y-1<0):
zoom_image[0:math.ceil(0.333* height_zoom),0:math.ceil(0.333*width_zoom)] = (0,0,0)
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] = (0,0,0)
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (0,0,0)
else:
if (x-1<0):
zoom_image[0:math.ceil(0.333* height_zoom),0:math.ceil(0.333*width_zoom)] = (0,0,0)
else:
s=list(cv.Get2D(src,y-1,x-1))
zoom_image[0:math.ceil(0.333* height_zoom),0:math.ceil(0.333*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
s=list(cv.Get2D(src,y-1,x))
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
if (x+1>= FRAME_WIDTH):
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (0,0,0)
s=list(cv.Get2D(src,0,0))
if (y-1<0):
zoom_image[0:math.ceil(0.333* height_zoom),0:math.ceil(0.333*width_zoom)] = (0,0,0)
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] = (0,0,0)
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (0,0,0)
else:
#print x+1
s=list(cv.Get2D(src,y-1,x+1))
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (s[0],s[1],s[2]) # (B, G, R)
if (x-1<0):
zoom_image[0:math.ceil(0.333* height_zoom),0:math.ceil(0.333*width_zoom)] = (0,0,0)
else:
s=list(cv.Get2D(src,y-1,x-1))
zoom_image[0:math.ceil(0.333* height_zoom),0:math.ceil(0.333*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
s=list(cv.Get2D(src,y-1,x))
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
if (x+1>= FRAME_WIDTH):
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (0,0,0)
else:
s=list(cv.Get2D(src,y-1,x+1))
zoom_image[0:math.ceil(0.333* height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (s[0],s[1],s[2]) # (B, G, R)
if (x-1<0):
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),0:math.ceil(0.333*width_zoom)] = (0,0,0)
else:
s=list(cv.Get2D(src,y,x-1))
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),0:math.ceil(0.333*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
s=list(cv.Get2D(src,y,x))
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
if (x-1<0):
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),0:math.ceil(0.333*width_zoom)] = (0,0,0)
else:
s=list(cv.Get2D(src,y,x-1))
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),0:math.ceil(0.333*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
s=list(cv.Get2D(src,y,x))
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
if (x+1>= FRAME_WIDTH):
if (x+1>= FRAME_WIDTH):
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (0,0,0) # (B, G, R)
else:
s=list(cv.Get2D(src,y,x+1))
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (s[0],s[1],s[2]) # (B, G, R)
if (y+1>=FRAME_HEIGHT):
zoom_image[math.ceil(0.666*height_zoom):height_zoom,0:math.ceil(0.333*width_zoom)] = (0,0,0) # (B, G, R)
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] =(0,0,0) # (B, G, R)
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.666*width_zoom):width_zoom] = (0,0,0)
else:
s=list(cv.Get2D(src,y,x+1))
zoom_image[math.ceil(0.333*height_zoom):math.ceil(0.666*height_zoom),math.ceil(0.666*width_zoom):width_zoom] = (s[0],s[1],s[2]) # (B, G, R)
else:
if (y+1>=FRAME_HEIGHT):
zoom_image[math.ceil(0.666*height_zoom):height_zoom,0:math.ceil(0.333*width_zoom)] = (0,0,0) # (B, G, R)
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] =(0,0,0) # (B, G, R)
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.666*width_zoom):width_zoom] = (0,0,0)
if (x-1<0):
zoom_image[math.ceil(0.666*height_zoom):height_zoom,0:math.ceil(0.333*width_zoom)] = (0,0,0)
else:
s=list(cv.Get2D(src,y+1,x-1))
zoom_image[math.ceil(0.666*height_zoom):height_zoom,0:math.ceil(0.333*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
s=list(cv.Get2D(src,y+1,x))
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
if (x-1<0):
zoom_image[math.ceil(0.666*height_zoom):height_zoom,0:math.ceil(0.333*width_zoom)] = (0,0,0)
else:
s=list(cv.Get2D(src,y+1,x-1))
zoom_image[math.ceil(0.666*height_zoom):height_zoom,0:math.ceil(0.333*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
if (x+1>= FRAME_WIDTH):
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.666*width_zoom):width_zoom] = (0,0,0) # (B, G, R)
else:
s=list(cv.Get2D(src,y+1,x+1))
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.666*width_zoom):width_zoom] = (s[0],s[1],s[2]) # (B, G, R)
s=list(cv.Get2D(src,y+1,x))
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.333*width_zoom):math.ceil(0.666*width_zoom)] = (s[0],s[1],s[2]) # (B, G, R)
cv.ShowImage("ZOOM",cv.fromarray(zoom_image))
if (x+1>= FRAME_WIDTH):
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.666*width_zoom):width_zoom] = (0,0,0) # (B, G, R)
else:
s=list(cv.Get2D(src,y+1,x+1))
zoom_image[math.ceil(0.666*height_zoom):height_zoom,math.ceil(0.666*width_zoom):width_zoom] = (s[0],s[1],s[2]) # (B, G, R)
cv.ShowImage("ZOOM",cv.fromarray(zoom_image))
#----------------#----------------#----------------#----------------#----------------
#----------------#----------------#----------------#----------------#----------------
# detectando movimientos del mouse
def on_mouse(event,x,y,flag,param):
global x_co
global y_co
global dragging
global minHSV
global maxHSV
x_co=x
y_co=y
print_zoom_window(x,y)
if event == cv.CV_EVENT_LBUTTONDOWN:
dragging = True
if event == cv.CV_EVENT_LBUTTONUP:
dragging = False
print("minHSV",minHSV)
print("maxHSV",maxHSV)
if event == cv.CV_EVENT_RBUTTONDOWN:
minHSV = None
maxHSV = None
global x_co
global y_co
global dragging
global minHSV
global maxHSV
x_co=x
y_co=y
if show_zoom:
print_zoom_window(x,y)
if event == cv.CV_EVENT_LBUTTONDOWN:
dragging = True
if event == cv.CV_EVENT_LBUTTONUP:
dragging = False
print "hsiMin={",int(minHSV[0]),",",int(minHSV[1]),",",int(minHSV[2]),"}"
print "hsiMin={",int(maxHSV[0]),",",int(maxHSV[1]),",",int(maxHSV[2]),"}"
if event == cv.CV_EVENT_RBUTTONDOWN:
minHSV = None
maxHSV = None
#----------------#----------------#----------------#----------------#----------------
#Retorna una imagen binaria con los pixeles que cumplen la condicion de los parametros
#----------------#----------------#----------------#----------------#----------------
# Retorna una imagen binaria con los pixeles que cumplen la condicion de los parametros
def thresholdImage(img_hsv, minHue = 0 , maxHue = 180, minSaturation = 0 , maxSaturation = 255, minValue = 0 , maxValue = 255):
# filtrar por rango:
thresholded_img = cv.CreateImage(cv.GetSize(img_hsv), 8, 1)
# filtrar por rango:
thresholded_img = cv.CreateImage(cv.GetSize(img_hsv), 8, 1)
# HSV stands for hue, saturation, and value
cv.InRangeS(img_hsv, (minHue, minSaturation, minValue), (maxHue, maxSaturation, maxValue), thresholded_img)
# HSV stands for hue, saturation, and value
cv.InRangeS(img_hsv, (minHue, minSaturation, minValue), (maxHue, maxSaturation, maxValue), thresholded_img)
if(minHue < 0 ):
# Si minHue es menor a cero, se calcula 2 veces, uno para valores de hue entre [minHue,maxHue] y otro entre [180+minHue,180]
thresholded_img2 = cv.CreateImage(cv.GetSize(img_hsv), 8, 1)
if(minHue < 0 ):
# Si minHue es menor a cero, se calcula 2 veces, uno para valores de hue entre [minHue,maxHue] y otro entre [180+minHue,180]
thresholded_img2 = cv.CreateImage(cv.GetSize(img_hsv), 8, 1)
cv.InRangeS(img_hsv, (180 + minHue, minSaturation, minValue), (180, maxSaturation, maxValue), thresholded_img2)
cv.Add(thresholded_img,thresholded_img2,thresholded_img) # para imprimir
cv.InRangeS(img_hsv, (180 + minHue, minSaturation, minValue), (180, maxSaturation, maxValue), thresholded_img2)
cv.Add(thresholded_img,thresholded_img2,thresholded_img) # para imprimir
return thresholded_img
return thresholded_img
#----------------#----------------#----------------#----------------#----------------
#----------------#----------------#----------------#----------------#----------------
cv.NamedWindow("camera", 0)
cv.NamedWindow("ZOOM",0)
#img = cv.CreateImage((100,100), 8, 1)
global height_zoom
global height_zoom
global width_zoom
global zoom_image
global FRAME_WIDTH
......@@ -157,49 +160,54 @@ global FRAME_HEIGHT
height_zoom = 100
width_zoom = 100
zoom_image = np.zeros((height_zoom,width_zoom,3), np.uint8)
if show_zoom:
cv.NamedWindow("ZOOM",0)
zoom_image = np.zeros((height_zoom,width_zoom,3), np.uint8)
if captureFromCam:
capture = cv.CreateCameraCapture(cameraId)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
print cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT)
FRAME_HEIGHT = cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT)
print cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH)
FRAME_WIDTH = cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH)
#capture = cv.CaptureFromCAM(cameraId)
capture = cv.CreateCameraCapture(cameraId)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
print cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT)
FRAME_HEIGHT = cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT)
print cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH)
FRAME_WIDTH = cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH)
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 1, 0, 2, 8)
cv.SetMouseCallback("camera",on_mouse, 0);
while True:
if captureFromCam:
src = cv.QueryFrame(capture)
src = cv.QueryFrame(capture)
else:
src = cv.LoadImageM(fileName, True)
src = cv.LoadImageM(fileName, True)
cv.Smooth(src, src, cv.CV_BLUR, 3)
hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
if dragging:
s=list(cv.Get2D(hsv,y_co,x_co))
if hueNearZero and s[0] > 90:
s[0] = s[0] - 180
print "H:",s[0]," S:",s[1]," V:",s[2]
if(minHSV == None):
minHSV = list((s[0],s[1],s[2]))
maxHSV = list((s[0],s[1],s[2]))
for i in range(3):
if s[i] < minHSV[i]:
minHSV[i] = s[i]
if s[i] > maxHSV[i]:
maxHSV[i] = s[i]
s=list(cv.Get2D(hsv,y_co,x_co))
if hueNearZero and s[0] > 90:
s[0] = s[0] - 180
print "H:",s[0]," S:",s[1]," V:",s[2]
if(minHSV == None):
minHSV = list((s[0],s[1],s[2]))
maxHSV = list((s[0],s[1],s[2]))
for i in range(3):
if s[i] < minHSV[i]:
minHSV[i] = s[i]
if s[i] > maxHSV[i]:
maxHSV[i] = s[i]
if minHSV != None:
hsv = thresholdImage(hsv,minHSV[0],maxHSV[0],minHSV[1],maxHSV[1],minHSV[2],maxHSV[2])
cv.ShowImage("threshold", hsv)
hsv = thresholdImage(hsv,minHSV[0],maxHSV[0],minHSV[1],maxHSV[1],minHSV[2],maxHSV[2])
cv.ShowImage("threshold", hsv)
cv.ShowImage("camera", src)
key = cv.WaitKey(33)
#print key
if key == 1048603:
break
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Calibrador de colores basado en HSV
#
# Obtener HSV maximo y minimo de pixeles clickeados
# Ademรกs hace un threshold para mostrar lo que se selecciona con el rango
# ATENCION: Tener en cuenta que el color rojo es un hue aproximado de 0. Si es menor, los valores retornados pasan por la discontinuidad del hue, retornando un valor aproximado a 180 (maximo hue de opencv).
# Para este caso se puede correr la discontinuidad de los 180 a los 90 grados, cambiando la variable hueNearZero a true.
#
# Se puede usar con la cรกmara o una imagen.
import cv
import time
#configuracion, captura de camara o archivo.
captureFromCam = True
fileName = "capture2.jpg" # Usado en caso que captureFromCam sea False
cameraId = 0 # Usado en caso que captureFromCam sea True
#Cuando se estรกn calibrando colores con Hue cercano a 0, por ejemplo rojo. Por defecto, dejar en False
#Si estรก en true, la discontinuidad del hue se mueve a los 90 grados.
hueNearZero = False
#Variables
x_co = 0
y_co = 0
dragging = False
#minHSV = list((0, 4, 23))
#maxHSV = list((174, 205, 83))
minHSV = None #list((0, 0, 100))
maxHSV = None #list((100, 2, 54))
#min_hsv_arena = np.array((0, 100, 100), np.int32)
#max_hsv_arena = np.array((100, 224, 256), np.int32)
def on_mouse(event,x,y,flag,param):
global x_co
global y_co
global dragging
global minHSV
global maxHSV
x_co=x
y_co=y
if event == cv.CV_EVENT_LBUTTONDOWN:
dragging = True
if event == cv.CV_EVENT_LBUTTONUP:
dragging = False
print("minHSV",minHSV)
print("maxHSV",maxHSV)
if event == cv.CV_EVENT_RBUTTONDOWN:
minHSV = None
maxHSV = None
#----------------#----------------#----------------#----------------#----------------
#Retorna una imagen binaria con los pixeles que cumplen la condicion de los parametros
def thresholdImage(img_hsv, minHue = 0 , maxHue = 180, minSaturation = 0 , maxSaturation = 255, minValue = 0 , maxValue = 255):
# filtrar por rango:
thresholded_img = cv.CreateImage(cv.GetSize(img_hsv), 8, 1)
# HSV stands for hue, saturation, and value
cv.InRangeS(img_hsv, (minHue, minSaturation, minValue), (maxHue, maxSaturation, maxValue), thresholded_img)
if(minHue < 0 ):
# Si minHue es menor a cero, se calcula 2 veces, uno para valores de hue entre [minHue,maxHue] y otro entre [180+minHue,180]
thresholded_img2 = cv.CreateImage(cv.GetSize(img_hsv), 8, 1)
cv.InRangeS(img_hsv, (180 + minHue, minSaturation, minValue), (180, maxSaturation, maxValue), thresholded_img2)
cv.Add(thresholded_img,thresholded_img2,thresholded_img) # para imprimir
return thresholded_img
#----------------#----------------#----------------#----------------#----------------
cv.NamedWindow("camera", 0)
if captureFromCam:
capture = cv.CreateCameraCapture(cameraId)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
print cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT)
print cv.GetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH)
#capture = cv.CaptureFromCAM(cameraId)
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 1, 0, 2, 8)
cv.SetMouseCallback("camera",on_mouse, 0);
while True:
if captureFromCam:
src = cv.QueryFrame(capture)
else:
src = cv.LoadImageM(fileName, True)
cv.Smooth(src, src, cv.CV_BLUR, 3)
hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
if dragging:
s=list(cv.Get2D(hsv,y_co,x_co))
if hueNearZero and s[0] > 90:
s[0] = s[0] - 180
print "H:",s[0]," S:",s[1]," V:",s[2]
if(minHSV == None):
minHSV = list((s[0],s[1],s[2]))
maxHSV = list((s[0],s[1],s[2]))
for i in range(3):
if s[i] < minHSV[i]:
minHSV[i] = s[i]
if s[i] > maxHSV[i]:
maxHSV[i] = s[i]
if minHSV != None:
hsv = thresholdImage(hsv,minHSV[0],maxHSV[0],minHSV[1],maxHSV[1],minHSV[2],maxHSV[2])
cv.ShowImage("threshold", hsv)
cv.ShowImage("camera", src)
key = cv.WaitKey(33)
#print key
if key == 1048603:
break
# Clean up everything before leaving
cv.DestroyAllWindows()
del(capture)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment