Skip to content

Instantly share code, notes, and snippets.

@vane90
Created April 25, 2013 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vane90/5460868 to your computer and use it in GitHub Desktop.
Save vane90/5460868 to your computer and use it in GitHub Desktop.
import Image, ImageDraw
from sys import argv
import random
import matplotlib.pyplot as plt
from numpy import *
def cruz_histo(image):
imagen = image.load()
ancho, alto = image.size
histov = []
histoh = []
#pixel_horizontal
for b in range(alto):
sumart = 0
for a in range(ancho):
sumart += sum(imagen[a, b])/3
histoh.append(sumart)
#pixel_vertical
for a in range(ancho):
sumart = 0
for b in range(alto):
sumart += sum(imagen[a, b])/3
histov.append(sumart)
print "valores horizontales =", len(histoh)
print "valores verticales =", len(histov)
minimohor = minimos_pix(histoh)
minimover = minimos_pix(histov)
minimohor =minimohor[-26:]
minimover =minimover[-26:]
print len(minimohor), len(minimover)
#promediar pixeles horizontales y verticales
prox=[]
proy=[]
lineahor = []
lineaver = []
for a in range(len(minimohor)):
lineahor.append(histoh.index(minimohor[a]))
for a in range(len(minimover)):
lineaver.append(histov.index(minimover[a]))
for b in range(ancho):
for a in range(len(lineahor)):
imagen[b, lineahor[a]] = (255, 0, 0)
prox.append((lineahor[a], b))
for b in range(len(lineaver)):
for a in range(alto):
imagen[lineaver[b], a] = (0, 0, 255)
proy.append((lineaver[b], a))
image.save('inter.png')
return prox, proy, histoh, histov
def minimos_pix(vechisto):
minimo = list()
media = sum(vechisto)/len(vechisto)
print media
for a in range(1, len(vechisto)-1):
if vechisto[a-1] > vechisto[a] and vechisto[a+1] > vechisto[a]:
if(vechisto[a] < media):
minimo.append(vechisto[a])
return minimo
def bfs(image2,color,a,b):
imagen=image2.load()
ancho,alto=image2.size
original = imagen[a,b]
c=[] #lista color
xs=[] #coordenada x
ys=[] #coordenada y
c.append((a,b))
n = 0
while len(c) > 0:
(x, y) = c.pop(0)
actual = imagen[x, y]
if actual == original or actual == color:
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
i, j = (x + dy, y + dx)
if i >= 0 and i < ancho and j >= 0 and j < alto:
contiene = imagen[i, j]
if contiene == original:
imagen[i, j] = color
xs.append(i)
ys.append(j)
n += 1
c.append((i, j))
image2.save('bfs.png')
return image2, n, xs, ys
def graficar_histo(histoh,histov,image):
plt.clf()
fig1=plt.subplot(111)
nivel_x=max(histoh)
nivel_y=max(histov)
ancho,alto=image.size
if ancho>alto:
n=ancho
else:
n=alto
if nivel_x > nivel_y:
nivelt=nivel_x
else:
nivelt=nivel_y
print 'horixontal',histoh
print 'vertical',histov
plt.ylim(-0.1 * nivel,nivel * 1.1)
plt.xlim(-0.1*n,1.1*n)
plt.title('Histograma')
x=range(1,ancho+1)
y=range(1,alto+1)
plt.plot(x,histoh,'r-',linewidth=2,label='horizontal')
plt.plot(y,histov,'b-',linewidth=2,label='vertical')
cua = fig.get_position()
fig.set_position([cua.x0, cua.y0 + cua.height * 0.1,cua.width, cua.height * 0.9])
fig.legend(loc = 'upper center', bbox_to_anchor=(0.5, -0.05),
fancybox = True, shadow = True, ncol = 1)
plt.show()
return
def main():
img = argv[1]
image = Image.open(img)
prox, proy = cruz_histo(image)
grafica1 = graficar(image, histoh, histov)
centros = []
image2 = Image.open("nueva.png")
ancho, alto= image2.size
total = alto*ancho
imagen2 = image2.load()
for a in range(ancho):
for b in range(alto):
color=(random.randint(0,256),random.randint(0,256),random.randint(0,256))
if imagen2[a,b]==(0,0,0):
image3, n,xs,xy= bfs(image2,color,a,b)
p = float(n)/float(total) * 100.0 #promedio maximo
if (p < 1.0):
centros.append((sum(xs)/len(xs),sum(xy)/len(xy)))
pasax = []
for a in range(len(prox)):
pasax.append(prox[a][0])
pasay = []
for a in range(len(proy)):
pasay.append(proy[a][1])
hoyo = []
for a in range(len(centros)):
if(centros[a][0] in pasax or centros[a][1] in pasay):
hoyo.append(centros[a])
print "\nAgujeros =", len(hoyo)
imagen3 = image3.load()
color = []
for center in range(len(hoyo)):
color.append(imagen3[hoyo[center]])
forma = 0
forma1 = []
for center in range(len(color)):
for a in range(ancho):
for b in range(alto):
if (imagen3[a, b] == color[center]):
forma += 1
imagen3[a, b] = (102, 15, 130)
forma1.append(forma)
draw = ImageDraw.Draw(image3)
for a in range(len(hoyo)):
print "centro ", a+1, "en x,y =", hoyo[a], "con area de ", (forma[a]/float(total))*100, "%"
x = hoyo[a][0]
y = hoyo[a][1]
draw.ellipse((x-2, y-2,x+2,y+2), fill=(255,255,0))
draw.text((x+10,y), str(a+1), fill=(0,0,0))
image3.save('bfs.png')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment