En esta semana tenemos que detectar las esquinas de polígonos.
Pasos
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 boton_esquinas(self): | |
imagen_grises=self.escala_grises() | |
imagen_filtro,medianas=self.filtro(imagen_grises) | |
imagen_dif=self.diferencia(imagen_filtro,imagen_grises,medianas) | |
imagen_nor=self.normalizar(imagen_dif) | |
esquinas=self.binarizar(imagen_nor) | |
bordes_c=self.bordes() | |
self.detectar(esquinas,bordes_c,bordes_c) | |
print 'termino' | |
return |
- Para primero porder detectar las esquinas:
- Tomamos una imagen que contenga polígonos.
- La pasamos a escala de grises
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 escala_grises(self): | |
inicio = time() | |
image = Image.open(self.o_imagen) | |
pixels = image.load() | |
ancho,alto = image.size | |
self.matriz = numpy.empty((ancho, alto)) | |
for i in range(ancho): | |
for j in range(alto): | |
(r,g,b) = image.getpixel((i,j)) | |
escala = (r+g+b)/3 | |
pixels[i,j] = (escala,escala,escala) | |
self.matriz[i,j] = int(escala) | |
fin = time() | |
tiempo_t = fin - inicio | |
df = image.save('escala.png') | |
print self.matriz | |
raw_input() | |
return image |
Comenzamos a sacar su filtro medio:
Lo que hacemos es checar cada vecino de pixel por pixel y ponerlos en una lista.
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 vecindad(self,i,j,lista,matriz,ancho,alto): | |
pix=[] | |
for x in lista: | |
for y in lista: | |
a = i+x | |
b = j+y | |
try: | |
if a >= 0 and a < ancho and b >= 0 and b < alto: | |
pix.append(self.matriz[a,b]) | |
except IndexError: | |
pass | |
return pix |
Ya que estan guardados en una lista, los ordenamos de menor a mayor.
Seleccionamos la media de la lista ordenada.
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 p_medio(self,pix): | |
o=sorted(pix) | |
mediana=np.median(pix) | |
return mediana |
Y nos queda la siguiente imagen:
Después de lo anterior lo que sigue es sacar la diferencia entre la imagen de escala de grises y la imagen obtenida con el filtro medio:
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 diferencia(self,filtro,grises,medianas): | |
pixels = grises.load() | |
ancho,alto = grises.size | |
self.min = 255 | |
self.max = 0 | |
self.fil = numpy.empty((ancho, alto)) | |
for i in range(ancho): | |
for j in range(alto): | |
dif=0 | |
dif=medianas[i,j]-self.matriz[i,j] | |
self.fil[i,j]=dif | |
if dif < self.min: | |
self.min = dif | |
if dif > self.max: | |
self.max = dif | |
dif=int(dif) | |
pixels[i,j]=(dif,dif,dif) | |
grises.save('diferencia.png') | |
return grises |
Ahora filtramos las esquinas, haciendo una normalización y después binarizamos:
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 normalizar(self,image): | |
pixels = image.load() | |
r = self.max-self.min | |
prop = 255.0/r | |
ancho,alto = image.size | |
for i in range(ancho): | |
for j in range(alto): | |
if pixels[i,j][0]!=0: | |
p =int(floor((self.fil[i,j]-self.min)*prop)) | |
pixels[i,j]=(p,p,p) | |
image.save('nrmaliza.png') | |
return image | |
def binarizar(self,img): | |
pixels = img.load() | |
ancho,alto = img.size | |
minimo = int(argv[2]) | |
for i in range(ancho): | |
for j in range(alto): | |
if pixels[i,j][1] < minimo: | |
p=0 | |
else: | |
p= 255 | |
pixels[i,j]=(p,p,p) | |
img.save('binarizar.png') | |
return img |
Normalizar:
Binarizar
Ya obteniendo las esquinas sacamos los bordes solo si los pixeles son blancos, osea que tiene que coincidir las esquinas con bordes blancos(Código de bfs implementado anteriormente).
Otro ejemplo:
La parte obligatoria está decentemente cubierta; 7 pts.
ResponderEliminar