En esta semana se nos pidio que realizaramos la detección de polígonos.
Los pasos para la detección son:
Primero que nada sacamos lo que son las pendientes de cada pixel, lo hice de la siguiente manere, además hice un filtrado para poder trabajar con las pendientes más facilmente:
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 get_pendientes(self,imagen): | |
Gx=self.gx | |
Gy=self.gy | |
# print Gx | |
# print Gy | |
#pendientes=numpy.empty((ancho, alto)) | |
pixels=imagen.load() | |
ancho,alto=imagen.size | |
pendientes=numpy.empty((ancho, alto)) | |
for i in range(ancho): | |
for j in range(alto): | |
if Gx[i,j]!=0 and Gy[i,j]!=0: | |
m=0 | |
elif Gx[i,j]==0 and Gy[i,j]==0: | |
m=1 | |
elif Gy[i,j]<=0 and Gx[i,j]==0: | |
m=2 | |
elif Gy[i,j]>=0 and Gx[i,j]==0: | |
m=3 | |
elif Gx[i,j]<=0 and Gy[i,j]==0: | |
m=4 | |
elif Gx[i,j]>=0 and Gy[i,j]==0: | |
m=5 | |
#elif Gx[i,j]!=0 and Gy[i,j]<0: | |
# m=1 | |
pendientes[i,j]=m | |
#print m | |
# pixels[i,j]=(255,0,0) | |
# imagen.save('hhh.png') | |
print 'termino' | |
print 'pendientes',pendientes | |
return pendientes |
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 c_colorear(self): | |
img=self.boton_convolucion() | |
pixels=img.load() | |
porcentajes=[] | |
fondos=[] | |
centro_masa=[] | |
masa=[] | |
ancho,alto=img.size | |
t_pixels=ancho*alto | |
c=0 | |
pintar=[] | |
f=0 | |
m=[] | |
for i in range(ancho): | |
for j in range(alto): | |
pix = pixels[i,j] | |
r,g,b= random.randint(0,255),random.randint(0,255), random.randint(0,255) | |
fondo=(r,g,b) | |
if (pix==(0,0,0)): | |
c +=1 | |
origen=(i,j) | |
num_pixels,abscisa,ordenada,puntos=self.bfs(pix,origen,img,fondo) | |
p=(num_pixels/float(t_pixels))*100 | |
if p>.10: | |
centro=(sum(abscisa)/float(num_pixels),sum(ordenada)/float(num_pixels)) | |
centro_masa.append(centro) | |
masa.append(num_pixels) | |
porcentajes.append(p) | |
fondos.append(fondo) | |
m.append(puntos) | |
centro_masa.append(centro) | |
# self.centro_masa(img,centro_masa) | |
# self.imprimir_porcentajes(porcentajes) | |
img.save('final.jpg') | |
return img,m |
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 detectar_rectas(self,img,masa,pendientes): | |
masa.pop(0) | |
#print 'pendientes 2',pendientes | |
pixels=img.load | |
for forma in masa: | |
#print 'forma',forma | |
segmentos={} | |
for m in forma: | |
print m | |
#for n in m: | |
i=m[0] | |
j=m[1] | |
if pendientes[i,j] not in segmentos: | |
segmentos[pendientes[i,j]]=([]) | |
#segmentos.apend(pendientes[i,j]:pixeles[i,j]) | |
#segmentos.setdefault(pendientes[i,j],[]) | |
#segmentos[pendientes[i,j]].append(pixels[i,j]) | |
segmentos.setdefault(pendientes[i,j],[]) | |
segmentos[pendientes[i,j]].append((i,j)) | |
#segmentos[pendientes[i,j]]= segmentos[pendientes[i,j]].append((i,j)) | |
#print pend | |
#print segmentos | |
for k in segmentos.keys(): | |
r,g,b= random.randint(0,255),random.randint(0,255), random.randint(0,255) | |
fondo=(r,g,b) | |
print 'k',k | |
for line in segmentos[k]: | |
print line | |
pixels[line]=fondo | |
img.save('jjjj.png') |
6 pts por el avance alcanzado.
ResponderEliminar