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