Parte para sacar los histogramas
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 histo_agujero(): | |
image = argv[1] | |
im = Image.open(image) | |
imagen=im.load() | |
ancho, alto = im.size | |
#ima, nueva = pixeleg() | |
histoh = [] | |
histov = [] | |
#pixel_horizontal | |
for a in range(alto): | |
sumart = 0 | |
for b in range(ancho): | |
sumart += imagen[b,a][1] | |
histoh.append(sumart) | |
#pixel_vertical | |
for a in range(ancho): | |
sumart = 0 | |
for b in range(alto): | |
sumart += imagen[a,b][1] | |
histov.append(sumart) | |
#promediar pixeles horizontales y verticales | |
sumart = 0 | |
ph = 0 | |
pv = 0 | |
for a in range(len (histoh)): | |
sumart += histoh[a] | |
ph= sumart/(len(histoh)) | |
#print ph | |
sumart = 0 | |
for a in range(len(histov)): | |
sumart += histov[a] | |
pv = sumart/(len(histov)) | |
#print pv | |
def lineas_histo(ima, histoh, histov, ph, pv): | |
imagen = im.load(ima) | |
ancho, alto = image.size | |
draw = ImageDraw.Draw(image) | |
todos = histoh[0] | |
for a in range(len(histoh)): | |
forma = histoh[a+1] | |
if todos > histoh[a] < forma and histoh[a] < ph: | |
for b in range(ancho): | |
imagen[b,a] = (0, 255, 0) | |
todos = histoh[a] | |
draw.line((0, a), (alto, a), (0, 255,0)) | |
#draw = ImageDraw.Draw(image) | |
todos = histov[0] | |
for a in range(len(histov)-1): | |
forma = histov[a+1] | |
if todos > histov[a] < forma and histov[a] < pv: | |
for b in range(alto): | |
imagen[a,b] = (0, 0, 255) | |
todos = histov[a] | |
draw.line((b, 0), (b, ancho), (255, 0 , 0)) | |
pixt = [] | |
todos = histoh[0] | |
todosn = histov[0] | |
for a in range(len(histoh)-1): | |
forma = histoh[a+1] | |
if todos > histoh[a] < forma and histoh[a] < ph-20: | |
for b in range(len(histov)-1): | |
forma2 = histov[b+1] | |
if todosn > histov[b] < forma2 and histov[b] <pv-20: | |
imagen[b,a] = (255, 255,0) | |
pixt.append((b,a)) | |
todosn = histov[b] | |
todos = histoh[a] | |
ima1 = "histop.png" | |
im.save(ima1) | |
#imagent = "agujero.png" | |
#im.save (imagent) | |
return imagent, pixt, ima1 |
Lo que se pidio es:
- Detectar todos los agujeros de una imagen.
- Los agujeros se marcan con bordes morado oscuro y relleno de morado claro.
- Tonos ligeramente diferentes para cada agujero.
- Se debe marcar el centro de cada agujero con un punto amarillo.
- En el centro de cada agujero se agrega una etiqueta con el ID del agujero.
Esto se agrego para colorear el contorno y rellenar el agujero de un color morado y agregar la etiqueta con su ID a lo detectado mediante bfs y por el area econtrada anteriormente por el histograma donde se marcaron las intersecciones.
Dentro de esta area se aplica el bfs, pintando los bordes del circulo con color morado oscuro y rellenarlos de un morado claro, además de marcar lo centros e identificarlos con su ID.
Imagen original
Intersección de lineas en la imagen
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 bfs(image,color,a,b): #para detectar por formas | |
imagen=image.load() | |
ancho,alto=image.size | |
original = imagen[a,b] | |
#punto=[] | |
c=[] | |
xs=[] | |
ys=[] | |
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 + dx, y + dy) | |
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)) | |
#punto.append((i, j)) | |
forma1 = image.save('formas.png') | |
return n, xs, ys, c | |
#Dibujar contornos morado y aplicar bfs | |
def detecta_agujero(image, histov, histoh, xs, ys): | |
ancho, alto = image.size | |
image = Image.open(argv[1]) | |
draw = ImageDraw.Draw(image) | |
imagenes = image.load() | |
m = 1 | |
total = ancho*alto | |
mide = 65 | |
a = [] | |
for j in histoh: | |
for i in histov: | |
(px1, py1), (px2, py2) = (i-(mide/2), j-(mide/2)), (i+(mide/2), j+(mide/2)) | |
nuevo, iniciar = funciona(image, (px1, py1), (px2, py2)) | |
if nuevo: | |
if iniciar is not (None, None): | |
n, xs, ys = bfs(image, iniciar, (255, 0, 0)) | |
suma = [sum(x) for x in (xs,ys)] | |
centro = (suma[0] / len(xs), suma[1] / len(ys)) | |
imagenes[centro] = (255, 255, 0) | |
a.append(n) | |
draw.ellipse(((centro[0]-mide/2, centro[1]-mide/2), (centro[0]+mide/2, centro[1]+mide/2)), outline = color('relleno'), | |
fill = morado('contorno')) | |
draw.text(centro, "%s"%m, fill = (0, 0, 0)) | |
m += 1 | |
for i in range(len(a)): | |
print "ID # %s area de agujero: %0.2f%%"%(i, 100.0*(a[i]/float(total))) | |
image.save('detectaa.png', 'PNG') | |
def color(contorno_y_relleno): | |
if contorno_y_relleno == 'contorno': | |
return (random.randint(85, 100), random.randint(85, 100), random.randint(100, 115)) | |
elif contorno_y_relleno == 'relleno': | |
return (random.randint(80, 78), random.randint(30, 35), random.randint(100, 104)) | |
def funciona(image, (px1, py1), (px2, py2)): | |
ancho, alto = image.size | |
imagenes = image.load() | |
area_a = abs(py1 - px1) * abs(py2 - px2) | |
cuenta = 0 | |
i, j = (None, None) | |
for a in range(px1, px2): | |
for b in range(py1, py2): | |
if a > 0 and a < ancho and b > 0 and b < alto: | |
if imagenes[a, b] == (0, 0, 0): | |
i, j = a, b | |
cuenta += 1 | |
if cuenta > area_a * 0.2: | |
return True, (i, j) | |
else: | |
return False, (i, j) | |
def main(): | |
#imagen = pixeleg() | |
imagen2 = detecta_agujero() | |
imagen3 = funciona(image, (px1, py1), (px2, py2)) | |
if __name__ == "__main__": | |
main() |
Lo de ruido es manejable eligiendo bien los umbrales. 4 pts.
ResponderEliminar