Social Icons

twitterfacebookgoogle plusemail

jueves, 25 de abril de 2013

Detección de Hoyos

Se creó un programa para hacer la detección de agujeros en Python lo que se realizó fue hacer sumatorias del arreglo de la imagen en las filas y las columnas, y se crea un vector para la sumatoría de los mismos. Una vez hecho eso se busca el mínimo peso en el vector donde ese será tomado como el agujero,a partir del valor mínimo podemos estar iterando los vectores para buscar los indices en el cual se encuentra el mismo valor anteriormente mencionado. A continuación presento el código el cual se hace todo el proceso mencionado dentro de la función llamada "deteccionHoyos".


from PIL import ImageDraw
from sys import argv
import numpy as np
from Tkinter import *
import Image
import ImageTk
DEBUG = True
def grayScale(image):
global DEBUG
width, height = image.size
if DEBUG:
print "Prueba de impresion de proporciones w: %s h: %s"%(width, height)
img = image.load()
for y in xrange(height):
for x in xrange(width):
r, g, b = img[ x, y ]
escala = int(r+g+b/3)
img[ x, y] = escala, escala, escala
return Image.fromarray(np.array(image))
def detectHole(image):
xIndex, yIndex = indicesDondeHayObjetos(image)
width, height = image.size
image = image.convert('RGB')
img = image.load()
#print xIndex
#print yIndex
for y in xrange(height):
for x in xrange(width):
r, g, b = img[ x, y ]
if x in xIndex:
#print "Encontre uno : ",x
img[x, y] = 255, 0, 0
if y in yIndex:
#print "Encontre uno : ",y
img[x, y] = 0, 0, 255
return Image.fromarray(np.array(image))
def indicesDondeHayObjetos(image):
global DEBUG
width, height = image.size
if DEBUG:
print "Prueba de impresion de proporciones w: %s h: %s"%(width, height)
image = image.convert('L')
img = image.load()
img_array = np.array(image)
xSum = histogramAxisX(img_array)
ySum = histogramAxisY(img_array)
#print xSum, ySum
return indices(xSum, np.amin(xSum)), indices(ySum, np.amin(ySum))
def indices(array, min):
#print "Valor min: ",min
return np.where(array == min)[0]
def histogramAxisX(array):
return np.sum( array, axis=0 )
def histogramAxisY(array):
return np.sum( array, axis=1 )
def printValues(x, y):
print "Tamanio de x: %s y: %s "%(len(x), len(y))
for j in xrange(len(y)):
for i in xrange(len(x)):
print "%s %s"%(x[i], y[j])
return
def tk(imagen):
root = Tk()
width, height = imagen.size
canvas = Canvas(root, width=width, height=height)
canvas.pack(expand=YES, fill=BOTH)
imagen_canvas = ImageTk.PhotoImage(imagen)
imagen_canvas_setting = canvas.create_image((2, 2), image=imagen_canvas, anchor=NW)
root.mainloop()
return
def main():
global DEBUG
imagen = Image.open(argv[1])
try:
DEBUG = argv[2]
except:
DEBUG = False
grayScale(imagen)
imagen = detectHole(imagen)
return tk(imagen)
main()


Ahora probamos el código y podemos ver las siguientes pruebas.






Como vemos solamente hace la detección de solamente un agujero en algunos caso, esto se hace porque se tiene un filtro a escalas de grises y no filtro binario, además de que nomina como posible agujero el valor mínimo existente en el vector, aunque de todos modos hice una pequeña modificación que toma el promedio de los valores del vector y lo toma como umbral para hacer la selección de posibles agujeros.


def detectHole(image):
xIndex, yIndex = indicesDondeHayObjetos(image)
width, height = image.size
image = image.convert('RGB')
img = image.load()
#print xIndex
#print yIndex
for y in xrange(height):
for x in xrange(width):
r, g, b = img[ x, y ]
if x in xIndex:
#print "Encontre uno : ",x
img[x, y] = 255, 0, 0
if y in yIndex:
#print "Encontre uno : ",y
img[x, y] = 0, 0, 255
return Image.fromarray(np.array(image))
def indicesDondeHayObjetos(image):
global DEBUG
width, height = image.size
if DEBUG:
print "Prueba de impresion de proporciones w: %s h: %s"%(width, height)
image = image.convert('L')
img = image.load()
img_array = np.array(image)
xSum = histogramAxisX(img_array)
ySum = histogramAxisY(img_array)
#print xSum, ySum
return indices(xSum), indices(ySum)
def indices(array):
prom = sum(array)/len(array)
return np.where(array > prom)[0]




1 comentarios:

  1. No entendí bien lo de la modificación. Debería ser posible detectar todos. 9 pts.

    ResponderEliminar