martes, 26 de febrero de 2013

Detección de lineas

En esta semana para la materia de visión computacional se encargo detección de lineas. 
Se utilizaron las tareas pasadas para realizar esto. 


Se tiene que sacar los gradientes verticales y horizontales de la imagen en este caso yo utilice esta imagen .



Ahora utilizando las mascaras de gradientes para detectar lineas verticales y horizontales de una imagen se calculan utilizando el método de convolución de la tarea pasad en este caso utilice el Prewitt.

Aquí el código

import pygame
from pygame.locals import *
import Image
import math
pygame.init()
pantalla = pygame.display.set_mode((190,250))
# FOTO NORMAL
imagen = pygame.image.load("salidaconvolubinariza.png")
#CONVOLUCION A ESCALA DE GRISES Y FILTRADA
image = Image.open("salidaconvolubinariza.png")
pixeles = image.load()
ancho, altura =image.size
rango=100
msobelX = ([-1, 0, 1], [-2, 0, 2], [-1, 0, 1]) #Para gradiente de x.
msobelY = ([1, 2, 1], [0, 0, 0], [-1, -2, -1]) #Para gradiente de y.
prewittX=([-1, 0, 1], [-1, 0, 1], [-1, 0, 1])#EJE X PREWITT
prewittY=([1, 1, 1], [0, 0, 0], [-1,-1,-1])#EJE Y PREWITT
tamanomatriz=3
sumatoriaX = 0
sumariaY = 0
seleccion=raw_input("INGRESA 1 PARA SOBEL y DOS PARA PREWITT ")
matrizagarrada=int(seleccion)
if matrizagarrada==1:
for x in range(altura):
for y in range(ancho):
sumatoriaX = 0
sumatoriaY = 0
if x != 0 and y != 0 and y != ancho and x != altura:
for i in range(tamanomatriz):
for j in range(tamanomatriz):
try:
gx = prewittX[i][j]*pixeles[y+j, x+i][1]
gy = prewittY[i][j]*pixeles[y+j, x+i][1]
except:
productosGX = 0
productosGY = 0
sumatoriaX = gx+sumatoriaX
sumatoriaY = gy+sumatoriaY
gxalcuadrado = pow(sumatoriaX, 2)
gyalcuadrado = pow(sumatoriaY, 2)
gradienteResultante = int(math.sqrt(gxalcuadrado+gyalcuadrado))
pixelNuevo=gradienteResultante
if pixelNuevo> 255:
pixelNuevo = 255
if pixelNuevo < 0:
pixelNuevo = 0
pixeles[y,x] = ( gyalcuadrado, gyalcuadrado,gyalcuadrado)#para verticales u horizontales
image.show()
while True:
for eventos in pygame.event.get():
if eventos.type == pygame.QUIT:
exit()
pantalla.blit(imagen,(0,0))
pygame.display.update()
view raw gistfile1.py hosted with ❤ by GitHub


Y aquí el resultado.





Ahora como se menciono en la clase se debe sacar el angulo de  el pixel y dependiendo el angulo que tenga serán verticales u horizontales.



Aquí dejo el código

import pygame
from pygame.locals import *
import Image
from math import floor, atan, fabs, pi, cos, sin, ceil
import math
pygame.init()
pantalla = pygame.display.set_mode((190,250))
# FOTO NORMAL
imagen = pygame.image.load("salidaconvolubinariza.png")
#CONVOLUCION A ESCALA DE GRISES Y FILTRADA
image = Image.open("rayitas.png")
pixeles = image.load()
pixelesY= image.load()
ancho, altura =image.size
rango=100
msobelX = ([-1, 0, 1], [-2, 0, 2], [-1, 0, 1]) #Para gradiente de x.
msobelY = ([1, 2, 1], [0, 0, 0], [-1, -2, -1]) #Para gradiente de y.
prewittX=([-1, 0, 1], [-1, 0, 1], [-1, 0, 1])#EJE X PREWITT
prewittY=([1, 1, 1], [0, 0, 0], [-1,-1,-1])#EJE Y PREWITT
tamanomatriz=3
sumatoriaX = 0
sumariaY = 0
seleccion=raw_input("INGRESA 1 PARA SOBEL y DOS PARA PREWITT ")
matrizagarrada=int(seleccion)
if matrizagarrada==1:
for x in range(altura):
for y in range(ancho):
sumatoriaX = 0
sumatoriaY = 0
if x != 0 and y != 0 and y != ancho and x != altura:
for i in range(tamanomatriz):
for j in range(tamanomatriz):
try:
gx = prewittX[i][j]*pixeles[y+j, x+i][1]
gy = prewittY[i][j]*pixelesY[y+j, x+i][1]
except:
productosGX = 0
productosGY = 0
sumatoriaX = gx+sumatoriaX
sumatoriaY = gy+sumatoriaY
gxalcuadrado = pow(sumatoriaX, 2)
gyalcuadrado = pow(sumatoriaY, 2)
#gradienteResultante = int(math.sqrt(gxalcuadrado+gyalcuadrado))
#pixelNuevo=gradienteResultante
#if pixelNuevo> 255:
# pixelNuevo = 255
#if pixelNuevo < 0:
#pixelNuevo = 0
pixeles[y,x] = ( gxalcuadrado, gxalcuadrado,gxalcuadrado)
image.save("imagenhorizo.png",'png')
pixelesY[y,x] = ( gyalcuadrado, gyalcuadrado,gyalcuadrado)
image.save("imagenverti.png",'png')
#image.show()
listcompl = list()
valor=raw_input("INGRESA el valor para trabajar ")
#
valoragarr=float(valor)
#print (" valor agarrado",valoragarr)
comparaAng = 0.00001 #comparacion de ang
for y in range(altura):
listtempor = list()
for x in range(ancho):
enX = float(sum(pixeles[x, y]))/3.0
enY = float(sum(pixelesY[x, y]))/3.0
#se checa si no es cero para poder dividirlo
if fabs(enX) > comparaAng:
angulo = atan(enY/enX)
else: #si llega a ser igual a cero
if fabs(enX) + fabs(enY) <= comparaAng:
angulo = None #No hay nada en ninguna direccion
elif enX == 0 and enY == 255:
angulo = 90.0
else:
angulo = 0.0
if angulo is not None:
rho = (x - ancho/2) * math.cos(angulo) + (altura/2 - y) * math.sin(angulo)
listtempor.append(('%.2f' % angulo, '%.0f' % rho))
else:
listtempor.append((None, None))
listcompl.append(listtempor)
diccionario = dict()
for y in xrange(altura):
for x in xrange(ancho):
if x > 0 and y > 0 and x < ancho - 1 and y < altura - 1:
(angulo, rho) = listcompl[y][x]
if angulo is not None:
matr = (angulo, rho)
if matr in diccionario:
diccionario[matr] += 1#checa si esta sino
else:
diccionario[matr] = 1# lo agrega aqui y pone un 1
valfrec = repeticion(comb, int(math.ceil(len(comb) * valoragarr)))
pixeles3 = image.load()
for y in range(altura):
for x in range(ancho):
(angulo, rho) = listcompl[y][x]
if (angulo, rho) in valfrec:
if angulo == 0:
pixeles3[x,y] (255,0,0)
if angulo == 90:
pixeles3[x,y], (0,0,255)
imagen.save("imagensalida.png")
while True:
for eventos in pygame.event.get():
if eventos.type == pygame.QUIT:
exit()
pantalla.blit(imagen,(0,0))
pygame.display.update()
view raw gistfile1.py hosted with ❤ by GitHub
X



1 comentario:

  1. ¿X? La función atan2 ayudaría a disminuir el desmadre de if-elif-else. La estructura de la entrada se podría mejorar... 3 pts.

    ResponderEliminar