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
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.
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 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] |
No entendí bien lo de la modificación. Debería ser posible detectar todos. 9 pts.
ResponderEliminar