Created
April 25, 2013 05:13
-
-
Save anonymous/5457642 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 histograma_h(im): | |
w,h = im.size | |
pix = im.load() | |
lista = [] | |
prom = 0 | |
archivo = open('horizontal.txt', 'w') | |
for x in range(w): | |
datos = 0 | |
for y in range(h): | |
datos += pix[x,y][0] | |
archivo.write(str(x)+ ' ' + str(temp) + '\n') | |
lista.append(datos) | |
archivo.close() | |
for i in lista: | |
prom += i | |
prom = float(prom)/ len(lista) | |
for i in lista: | |
for x in h: | |
im.putpixel((x,i),(0, 255, 0) | |
return lista, prom | |
def histograma_v(im): | |
w,h = im.size | |
pix = im.load() | |
lista = [] | |
prom = 0 | |
archivo = open('vertical.txt', 'w') | |
for y in range(h): | |
datos = 0 | |
for x in range(w): | |
datos += pix[x,y][0] | |
archivo.write(str(y)+ ' ' + str(temp) + '\n') | |
lista.append(archivo) | |
for i in lista: | |
prom +=i | |
prom = float(prom)/len(hist) | |
archivo.close() | |
for i in lista: | |
for x in w: | |
im.putpixel((x,i),(255, 0, 0) | |
return lista, prom | |
def posibles_agujeros(hist_h, hist_v, im): | |
pix = im.load() | |
cruces = [] | |
for i in range(1, len(hist_h)-1): | |
if hist_h[i-1] > hist_h[i] < hist_h[i+1]: | |
for j in range(1, len(hist_v) -1): | |
if hist_v[j-1] > hist_v[j] < hist_v[j+1]: | |
cruces.append((i,j)) | |
return cruces | |
def centros(coords, im, orig, total): | |
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 12) | |
r = 2 | |
contador = 0 | |
for i in coords: | |
id_ = str(t+1) | |
i.sort() | |
max_ = i[-1] | |
min_ = i[0] | |
promx = (max_[0] + min_[0])/2 | |
promy = (max_[1] + min_[1])/2 | |
draw.ellipse((promx-r,promy-r, promx+r, promy+r), fill='purple') | |
draw.text((promx,promy),id_, fill=(0,0,0),font=font) | |
print '\tID agujero %s %.2f%%'%(id_,total[contador]*100) | |
contador += 1 | |
return im | |
def deteccion_agujeros(im): | |
output = im.copy() | |
hist_h, prom_h = histograma_h(im) | |
hist_v, prom_v = histograma_v(im) | |
cruces = posibles_agujeros(hist_v, hist_v, im) | |
pix = im.load() | |
coords = [] | |
sumas = [] | |
area = im.size[0] * im.size[1] | |
for i in cruces: | |
if pix[i] == (0,0,0): | |
n, coords = bfs(im, (i), (randint(85,255), randint(0, 150), randint(128, 255)), output) | |
coords.append(c) | |
p = (float(t)/area) | |
sumas.append(p) | |
im = centros(coords, n, img,sumas) | |
im.save('agujeros.png') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment