En esta semana en laboratorio corresponde a la detección de circulos con diferentes radios.
Para este proceso se utilizo parte del código de Clase. Detección circulos, como yo aun no detectaba los circulos aquí incluyo esta parte ya terminada, así como los centros de los circulos detectados con una correspondiente etiqueta.
Las ecuaciones para identificar el centro que utilice son las mismas que se mostraron en la entrada anterior:
xc= x-radioCosθ
yc=y-radioSinθ
Probe con algunas imagenes, tuve muchos problemas para llegar a un buen resultado y si creo que aun no es el resultado esperado.
Con un radio = 100 y otro de 25 primero solo me mostraba un solo centro.
Otra imagen con un radio = 35
Y parte del código con los cambios hechos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def circulos(): | |
ima1 = Image.open("vane2.jpg") | |
imagen = ima1.load() | |
radio = int(argv[1]) | |
ancho,alto = ima1.size | |
#frec = list() | |
frec = numpy.empty((ancho, alto)) #frecuecia en centros | |
#frec = [] | |
mat_x = ([-1,0,1],[-2,0,2],[-1,0,1]) | |
mat_y = ([1,2,1],[0,0,0],[-1,-2,-1]) | |
for i in range(ancho): | |
for j in range(alto): | |
sumx=0.0 | |
sumy = 0.0 | |
for m in range(len(mat_x[0])): | |
for h in range(len(mat_y[0])): | |
try: | |
mul_x= mat_x[m][h] * imagen[i+m, j+h][0] | |
mul_y= mat_y[m][h] * imagen[i+m, j+h][0] | |
except: | |
mul_x=0 | |
mul_y=0 | |
sumx=mul_x+sumx | |
sumy=mul_y+sumy | |
gx = pow(sumx,2) | |
gy = pow(sumy,2) | |
grad = int(math.sqrt(gx + gy)) | |
#obtener votos y centros | |
if fabs(grad) > 0: | |
costheta = (float(sumx / grad)) | |
sintheta = (float(sumy / grad)) | |
xc = int(round(i - radio * costheta)) | |
yc = int(round(j - radio * sintheta)) | |
xcm = (xc + alto)/2 | |
ycm = (ancho/2) - yc | |
#centro = (xc) | |
if ycm >= 0 and xcm < alto and xcm >= 0 and ycm < ancho: | |
frec[xcm][ycm] += 1 | |
# agregar los votados | |
for rango in range(1, int(round(alto*0.1))): | |
agregado = True | |
while agregado: | |
agregado = False | |
for y in range(ancho): | |
for x in range(alto): | |
v = frec[y][x] | |
if v > 0: | |
for dx in range(-rango, rango): | |
for dy in range(-rango, rango): | |
if not (dx == 0 and dy == 0): | |
if y + dy < ancho and y + dy >= 0 and x + dx >=0 and x + dx < ancho: | |
w = frec[y+dy][x+dx] | |
if w >0: | |
if v -rango >=w: | |
frec[y][x] = v + w | |
frec[y+dy][x+dx] = 0 | |
agregado = True | |
#return frec | |
#votos codigo Dra | |
maximo = 0 | |
suma = 0.0 | |
print "sumando" | |
for i in range(alto): | |
for j in range(ancho): | |
v = frec[j][i] | |
suma += v | |
if v > maximo: | |
maximo = v | |
promedio = suma / (ancho * alto) | |
umbral = (maximo-promedio)/2.0 | |
centro = [] | |
for i in range(alto): | |
for j in range(ancho): | |
v = frec[j][i] | |
if v > umbral: | |
print 'Posible centro en (%d, %d). ' % (j, i) | |
centro.append((j,i)) | |
#def dibujac(ima1, radio, centro) | |
#Dibuja circulo | |
draw = ImageDraw.Draw(ima1) | |
ancho,alto = ima1.size | |
for c in range(len(centro)): | |
a = centro[c][0] | |
b = centro[c][1] | |
color = (255,random.randint(200,255), random.randint(0,200)) | |
#toma el valor de radio para dibujar los circulos | |
draw.ellipse((a-radio, b-radio,a+radio,b+radio), fill=None, outline=color) | |
radio+=1 | |
draw.ellipse((a-radio, b-radio, a+radio,b+radio), fill=None, outline=color) | |
radio+=2 | |
#draw.ellipse((a-radio)) | |
#Etiquetas y Id | |
draw.ellipse((a-2,b-2,a+2,b+2),fill="green") | |
draw.text(((a+2,b+2)), str(c),fill="red") | |
print "ID %s"%c | |
print frec | |
nueva = 'circulo.png' | |
otra = ima1.save(nueva) | |
return nueva | |
if __name__ == "__main__": | |
main() |