Skip to content

Commit 98fe0b4

Browse files
authored
Add files via upload
1 parent 9a72a4f commit 98fe0b4

File tree

1 file changed

+255
-0
lines changed

1 file changed

+255
-0
lines changed
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
# -*- coding: utf-8 -*-
2+
# Se importan las librerías a usar
3+
from freenect import*
4+
from numpy import*
5+
from cv2 import*
6+
from time import*
7+
8+
#Funcion de Adquisicion RGB kinect
9+
def frame_RGB():
10+
array,_ = sync_get_video()
11+
array = cvtColor(array,COLOR_RGB2BGR)
12+
return array
13+
14+
#Funcion para adquisicion de profundidad (depth) Kinect
15+
def frame_depth():
16+
array,_ = sync_get_depth()
17+
#array = array.astype(uint8)
18+
return array
19+
20+
#Función que retorna imagen binaria donde lo verde es blanco
21+
#y el resto es negro
22+
def filtLAB_Verde(img):
23+
lab = cvtColor(img, COLOR_BGR2Lab)
24+
# pongo los valores verdes para hacer la mascara
25+
#verde_bajo = array([35, 110, 138]) -> im1 #verde_bajo = array([34, 97, 143]) -> im1
26+
#verde_alto = array([102, 136, 174]) -> im1 #verde_alto = array([126, 118, 174]) -> im1
27+
#verde_bajo = array([44, 111, 144]) -> im2 #verde_bajo = array([32, 108, 131]) -> im2
28+
#verde_alto = array([101, 128, 172]) -> im2 #verde_alto = array([77, 128, 153]) -> im2
29+
#verde_bajo = array([20, 126, 132]) -> im3 #verde_bajo = array([60, 86, 125]) -> im3
30+
#verde_alto = array([80, 136, 154]) -> im3 #verde_alto = array([250, 124, 179]) -> im3
31+
verde_bajo = array([20, 76, 132])
32+
verde_alto = array([240, 121, 215])
33+
34+
35+
mascara = inRange(lab, verde_bajo, verde_alto)
36+
37+
er = ones((7,7),uint8) #matriz para erosion
38+
39+
dil = array([[0,0,0,1,0,0,0],
40+
[0,1,1,1,1,1,0],
41+
[0,1,1,1,1,1,0],
42+
[1,1,1,1,1,1,1],
43+
[0,1,1,1,1,1,0],
44+
[0,1,1,1,1,1,0],
45+
[0,0,0,1,0,0,0]],uint8) #matriz para dilatacion
46+
47+
mascara = erode(mascara,er,iterations = 1) #aplico erosion
48+
mascara = dilate(mascara,dil,iterations = 2) #aplico dilatacion
49+
return mascara
50+
51+
52+
def filtLAB_Naranja(img):
53+
lab = cvtColor(img, COLOR_BGR2Lab)
54+
# pongo los valores de rango naranja para hacer la máscara
55+
#naranja_bajo = array([20, 146, 139]) -> im1
56+
#naranja_alto = array([76, 168, 166]) -> im1
57+
#naranja_bajo = array([35,147,144]) -> im2
58+
#naranja_alto = array([152,172,187]) -> im2
59+
#naranja_bajo = array([47, 136, 138]) -> im3
60+
#naranja_alto = array([228, 183, 194]) -> im3
61+
naranja_bajo = array([20, 136, 152])
62+
naranja_alto = array([235, 192, 198])
63+
64+
mascara = inRange(lab, naranja_bajo, naranja_alto)
65+
66+
er = ones((7,7),uint8) #matriz para erosion
67+
68+
dil = array([[0,0,0,1,0,0,0],
69+
[0,1,1,1,1,1,0],
70+
[0,1,1,1,1,1,0],
71+
[1,1,1,1,1,1,1],
72+
[0,1,1,1,1,1,0],
73+
[0,1,1,1,1,1,0],
74+
[0,0,0,1,0,0,0]],uint8) #matriz para dilatacion
75+
76+
# matriz para erosión y dilación
77+
mascara = erode(mascara,er,iterations = 1) #aplico erosión
78+
mascara = dilate(mascara,dil,iterations = 2)#aplico dilatacion
79+
return mascara
80+
81+
def dibuja_circulos(circuloX,img):
82+
if circuloX is not None:
83+
#Se convierten los valores (x,y,r) de circulo a enteros
84+
circuloX = circuloX.astype("int")
85+
print(circuloX)
86+
x=circuloX[0,0,0]
87+
y=circuloX[0,0,1]
88+
r=circuloX[0,0,2]
89+
# solo tomo el primer circulo
90+
# Dibujo el circulo y luego otro circulo en el
91+
# centro de la esfera de radio 5
92+
circle(img, (x, y), r, (0, 255, 0), 5)
93+
circle(img, (x, y), 5, (0, 0, 255), -1) # -1 relleno
94+
95+
return img
96+
97+
98+
# loop principal
99+
while True:
100+
init=time()
101+
frame = frame_RGB() #leo frame
102+
depth = frame_depth() #leo profundidad depth
103+
depth = resize(depth,(0,0),fx=0.5, fy=0.5)
104+
showdepth = depth.astype('uint8')
105+
showdepth = cvtColor(showdepth, COLOR_GRAY2BGR)
106+
107+
108+
mascaraV = resize(frame, (0,0), fx=0.5, fy=0.5)
109+
mascaraN = mascaraV
110+
frame = mascaraV
111+
frame = medianBlur(frame,5)
112+
#imwrite('frame.jpg',frame)
113+
114+
color=time()
115+
mascaraV = filtLAB_Verde(frame)
116+
mascaraN = filtLAB_Naranja(frame)
117+
tc=time()-color
118+
119+
edge=time()
120+
121+
#Toma el valor absoluto -> convertScaleAbs( grad_y, abs_grad_y )
122+
#explicación Sobel_x + Sobel_y (addWeighted, suma grad_x y grad_y)
123+
#https://github.com/opencv/opencv/blob/master/samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp
124+
125+
mascaraV = Laplacian(mascaraV,CV_8U) #Deteccion de bordes de la mascara
126+
mascaraN = Laplacian(mascaraN,CV_8U)
127+
128+
diler = array([[0,1,0],
129+
[1,1,1],
130+
[0,1,0]],uint8) #Matriz para dilatacion y erosion
131+
132+
mascaraV = dilate(mascaraV,diler,iterations = 1) #aplico dilatacion
133+
mascaraN = dilate(mascaraN,diler,iterations = 1) #aplico dilatacion
134+
135+
te=time()-edge
136+
print('EDGES',te)
137+
print('COLOR',tc)
138+
139+
#Hallo los círculos que estén en detección de bordes
140+
141+
circuloV = HoughCircles(mascaraV,HOUGH_GRADIENT, 1, 40, param1=60,
142+
param2=24,minRadius=0,maxRadius=0)
143+
circuloN = HoughCircles(mascaraN,HOUGH_GRADIENT, 1, 40, param1=60,
144+
param2=24,minRadius=0,maxRadius=0)
145+
146+
#Dibujo los circulos hallados
147+
dibuja_circulos(circuloV,frame)
148+
dibuja_circulos(circuloN,frame)
149+
150+
151+
152+
#Para obtener la distancia depV y depN se utilizó la información de esta
153+
#página: https://openkinect.org/wiki/Imaging_Information (Agosto 18)
154+
#Esa regresión se le hicieron modificaciones para disminuir el error
155+
#hallando una aproximación de la forma 1/(Bx+C), donde x es el valor
156+
#en bytes obtenido por el sensor
157+
158+
#Para la alineación
159+
cteX=9
160+
cteY=9 #Valores alineación RGB y Depth 320 x 240 (18/2)
161+
#circle(rgb, (80-cteX,50+cteY),40,(0,0,255),5)
162+
163+
centimg = round(frame.shape[1]/2) #centro de la imagen donde son 0° horizontal
164+
centVert= round(frame.shape[0]/2) #centro vertical 0° vertical
165+
166+
#Si encontro al menos un ciculo
167+
if circuloV is not None:
168+
circuloV = circuloV.astype("int")
169+
xV = circuloV[0,0,0]
170+
xVd=xV + cteX
171+
yV = circuloV[0,0,1]
172+
yVd=yV - cteY
173+
verde=True
174+
if xVd >= frame.shape[1]:
175+
xVd = 319
176+
if yVd >= frame.shape[0]:
177+
yVd = 239
178+
179+
#dibujo punto donde se medirá depth
180+
circle(showdepth,(xVd,yVd),5,(0,255,0),-1)
181+
182+
#Paso el pixel de coordenadas RGB a depth
183+
depV = 1/(depth[yVd,xVd]*(-0.0028642) + 3.15221) #para obtener dato es en coordenada (y,x)->(480x640)
184+
depV = round(depV,4) #cuatro cifras decimales
185+
if depV < 0:
186+
depV=0
187+
#depV = ((4-0.8)/2048)*(depth[xVd,yVd]+1)+0.8 aprox propia
188+
putText(frame,str(depV)+'m', (xV,yV), FONT_HERSHEY_PLAIN, 1.5, (0,255,0),2)
189+
else:
190+
verde = False
191+
192+
if circuloN is not None:
193+
circuloN = circuloN.astype("int")
194+
xN = circuloN[0,0,0]
195+
xNd=xN + cteX
196+
yN = circuloN[0,0,1]
197+
yNd=yN - cteY
198+
naranja=True
199+
if xNd >= frame.shape[1]:
200+
xNd = 319
201+
if yNd >= frame.shape[0]:
202+
yNd = 239
203+
#dibujo punto donde se medirá depth
204+
circle(showdepth,(xNd,yNd),5,(255,0,0),-1)
205+
206+
#para obtener dato es en coordenada (y,x)->(480x640)
207+
depN = 1/(depth[yNd,xNd]*(-0.0028642) + 3.15221)
208+
depN = round(depN,4) #cuatro cifras decimales
209+
if depN < 0:
210+
depN=0
211+
#dep = ((4-0.8)/2048)*(depth[xNd,yNd]+1)+0.8 aprox propia
212+
putText(frame,str(depN)+'m', (xN,yN), FONT_HERSHEY_PLAIN, 1.5, (0,0,255),2)
213+
else:
214+
naranja = False
215+
216+
if naranja or (verde and naranja):
217+
c1,c2=1,0
218+
bethaN = abs(centVert - yNd)*0.17916 #0.17916 son °/Px en vertical (43°/240)
219+
bethaN = (bethaN*pi)/180
220+
depN = depN*cos(bethaN) # centro valor vertical para ubicar la distancia en 0° Vertical
221+
alphaN = (xNd - centimg)*0.1781 #0.1781 son los grados por pixel (°/px) 320 x 240
222+
alphaN = (alphaN*pi)/180 # en radianes
223+
xm = depN*sin(alphaN)
224+
ym = depN*cos(alphaN)
225+
putText(frame,str((alphaN*180)/pi)+'grados', (50,50), FONT_HERSHEY_PLAIN, 1.5, (255,0,255),2)
226+
elif verde and (not naranja):
227+
c1,c2=0,1
228+
bethaV = abs(centVert - yVd)*0.17916 #0.17916 son °/Px en vertical (43°/240)
229+
bethaV = (bethaV*pi)/180
230+
depV = depV*cos(bethaV) # centro valor vertical para ubicar la distancia en 0° Vertical
231+
alphaV = (xVd - centimg)*0.1781 #0.1781 son los grados por pixel (°/px)
232+
alphaV = (alphaV*pi)/180 # en radianes
233+
xm = depV*sin(alphaV)
234+
ym = depV*cos(alphaV)
235+
putText(frame,str((alphaV*180)/pi)+'grados', (50,50), FONT_HERSHEY_PLAIN, 1.5, (255,80,255),2)
236+
else:
237+
c1,c2=0,0
238+
xm,ym=0,0
239+
240+
print('c1',c1,'c2',c2,'xm',xm,'ym',ym)
241+
imshow("Frame", frame)
242+
imshow("MaskVerde", mascaraV)
243+
imshow("MaskNaranja", mascaraN)
244+
imshow("Depth medido", showdepth)
245+
key = waitKey(1)
246+
# si se oprime la tecla 'q' se sale del loop
247+
if key == ord("q"):
248+
break
249+
250+
t=time()-init
251+
print('FIN',t)
252+
253+
# detener y cerrar ventanas
254+
sys.exit()
255+
destroyAllWindows()

0 commit comments

Comments
 (0)