Para esta entrada lo primero que se hizo fue corregir el código de detección de líneas para usar "arctang", una vez teniendo esto también se corrigieron algunas condiciones para poder encontrar los ángulos en todas las direcciones.
Algunos ejemplos del resultado, donde el lado izquierdo es la imagen original y el derecho es la imagen que dio como resultado el programa.
Las líneas rojas son diagonales, las verdes son verticales y las azules son horizontales:
El código es:
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 main(): | |
"""funcion principal | |
""" | |
umb = .75 | |
try: | |
imagen_path = sys.argv[1] | |
print imagen_path | |
imagen = filtros.abrir_imagen(imagen_path) | |
except: | |
print "No seleccionaste una imagen" | |
return | |
imagen = filtros.hacer_gris(imagen) | |
sobelx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] | |
sobely = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]] | |
CERO = 0.0001 | |
imagenx = filtros.convolucion(imagen, sobelx) | |
imageny = filtros.convolucion(imagen, sobely) | |
#imagenx = filtros.normalizar(imagenx) | |
#imageny = filtros.normalizar(imageny) | |
imagenx = filtros.umbral(imagenx) | |
imageny = filtros.umbral(imageny) | |
imagenx.save("imagenx.png") | |
imageny.save("imageny.png") | |
lin_x = imagenx.load() | |
lin_y = imageny.load() | |
w, h = imagen.size | |
res = [] | |
for j in xrange(h): | |
datos = list() | |
for i in xrange(w): | |
x = lin_x[i,j][0] | |
y = lin_y[i,j][0] | |
if fabs(x) + fabs(y) <= CERO: | |
angulo = None | |
else: | |
angulo = atan2(y,x) | |
print angulo | |
if angulo is not None: | |
p = int(i-w/2)*cos(angulo)+(h/2-j)*sin(angulo) | |
angulo = int(degrees(angulo)) | |
print angulo | |
datos.append(('%d' %angulo, '%.0f' %p)) | |
#print '%.2f' %angulo | |
else: | |
datos.append((None, None)) | |
res.append(datos) | |
comb = dict() | |
for y in xrange(h): | |
for x in xrange(w): | |
if x > 0 and y > 0 and x < w - 1 and y < h - 1: | |
(angulo, p) = res[y][x] | |
if angulo is not None: | |
combinacion = (angulo, p) | |
if combinacion in comb: | |
comb[combinacion] += 1 | |
else: | |
comb[combinacion] = 1 | |
frec = frecuentes(comb, int(ceil(len(comb) * 1))) | |
#print frec | |
for y in range(h): | |
renglon = list() | |
for x in range(w): | |
(angulo, p) = res[y][x] | |
if (angulo, p) in frec: | |
# if float(angulo) != 90 and float(angulo) != 0: | |
#print angulo | |
if float(angulo) == 90 or float(angulo) == 270: | |
#print "90" | |
imagen.putpixel((x,y), (0,255,0)) | |
elif float(angulo) == 0 or float(angulo) == 180: | |
#print "0" | |
imagen.putpixel((x,y), (0,0,255)) | |
else: | |
imagen.putpixel((x,y), (255,0,0)) | |
imagen.save("res.png") | |
return imagen |
Otro ejemplo:
Liga al repositorio
Referencias
Diapositivas de la clase
Transformada de Hough
Lo obligatorio está decente; 7 pts.
ResponderEliminar