Para esta entrada se trabajó con un preprocesamiento para detectar agujeros. Se trabajó con las intensidades de color de filas y columnas de una imagen.
Los histogramas nos permiten reducir el espacio de búsqueda y encontrar donde existen los cambis de intensidad.
Para obtener el histograma horizontal basta con sumar los pixeles de las filas y para el histograma vertical se suman los pixeles de las columnas.
Por ejemplo de la siguiente imagen:
Se obtiene este histograma:
Después de obtener el histograma se tenía que dibujar en las imágenes líneas rectas para cada uno de los picos de cada histograma lateral.
Lo primera idea fue obtener un promedio para establecerlo como rango y todos los puntos que estuvieran debajo de ese rango serían los posibles agujeros pero lo que obtuve fue lo siguiente:
Como se puede apreciar son demasiadas líneas. Por lo que me puse a ver los histogramas y observé que para obtener un pico sólo es comparar el dato actual con el anterior y posterior, si el actual es menor que el anterior y menor que posterior quiere decir que es uno de los mínimos y ya con esto me trajo mejores resultados:
Código
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
import sys | |
from PIL import Image, ImageDraw | |
def grayScale(im): | |
w,h = im.size | |
gray = im.copy() | |
pix = gray.load() | |
for x in range(w): | |
for y in range(h): | |
curr = pix[x,y] | |
prom = max(curr) | |
pix[x,y] = prom, prom, prom | |
return gray | |
def blur(im): | |
w,h = im.size | |
blurred = im.copy() | |
pix = blurred.load() | |
for x in range(w): | |
for y in range(h): | |
ngbs = [] | |
ngbs.append(list(pix[x,y])) | |
for i in range(x-1, x+2): | |
for j in range(y-1, y+2): | |
try: | |
ngbs.append(list(pix[i,j])) | |
except IndexError: | |
pass | |
total = [ sum(z) for z in zip(*ngbs) ] | |
pix[x,y] = total[0]/len(ngbs), total[1]/len(ngbs), total[2]/len(ngbs) | |
return blurred | |
def horizontalHistogram(im): | |
w,h = im.size | |
pix = im.load() | |
file_ = open('horizontal.txt', 'w') | |
hist = [] | |
prom = 0 | |
for x in range(w): | |
temp = 0 | |
for y in range(h): | |
temp += pix[x,y][0] | |
file_.write(str(x)+ ' ' + str(temp) + '\n') | |
hist.append(temp) | |
file_.close() | |
for i in hist: | |
prom += i | |
prom = float(prom)/ len(hist) | |
return hist, prom | |
def verticalHistogram(im): | |
w,h = im.size | |
pix = im.load() | |
file_ = open('vertical.txt', 'w') | |
hist = [] | |
prom = 0 | |
for y in range(h): | |
temp = 0 | |
for x in range(w): | |
temp += pix[x,y][0] | |
file_.write(str(y)+ ' ' + str(temp) + '\n') | |
hist.append(temp) | |
for i in hist: | |
prom +=i | |
prom = float(prom)/len(hist) | |
file_.close() | |
return hist, prom | |
def possibleHoles(hist): | |
coord = [] | |
for i in range(1, len(hist) - 1): | |
if hist[i-1] > hist[i] and hist[i+1] > hist[i]: | |
coord.append(i) | |
return coord | |
def holeDetection(im): | |
size = 128,128 | |
original = im.copy() | |
im.thumbnail(size, Image.ANTIALIAS) | |
im = grayScale(im) | |
im = blur(im) | |
im.save('gray.png') | |
histx, promx = horizontalHistogram(im) | |
histy, promy = verticalHistogram(im) | |
holesx = possibleHoles(histx) | |
holesy = possibleHoles(histy) | |
draw = ImageDraw.Draw(original) | |
prop = im.size | |
prop = float(original.size[0])/prop[0] , float(original.size[1]/prop[1]) | |
w, h = original.size | |
''' | |
for i in range (len(histx)): | |
if histx[i] < promx: | |
x = int(i*prop[0]) | |
draw.line((x,0,x,h), fill='yellow') | |
''' | |
for i in holesx: | |
x = int(i*prop[0]) | |
draw.line((x,0,x,h), fill='yellow') | |
''' | |
for i in range (len(histy)): | |
if histy[i] < promy: | |
y = int(i*prop[1]) | |
draw.line((0,y,w,y), fill='red') | |
''' | |
for i in holesy: | |
y = int(i*prop[1]) | |
draw.line((0,y,w,y), fill='skyblue') | |
original.save('lineas.png') | |
original.show() | |
if __name__ == '__main__': | |
path = sys.argv[1] | |
image = Image.open(path).convert('RGB') | |
holeDetection(image) |
No comments:
Post a Comment