El procedimiento para detectarlo es
- Cargar la imagen
- Convertirla a escala de grises
- Aplicar convolución discreta
- Aplicar BFS
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
def convolucion(im, g): | |
w, h = im.size | |
pix = im.load() | |
out_im = Image.new("RGB", (w, h)) | |
out = out_im.load() | |
for i in xrange(1, w-2): | |
for j in xrange(1, h-1): | |
suma1, suma2, suma3 = 0, 0, 0 | |
for n in xrange(i-1, i+2): | |
for m in xrange(j-1, j+2): | |
if n >= 0 and m >= 0 and n < w and m < h: | |
suma1 += g[n - (i - 1)][ m - (j - 1)] * pix[n, m][0] | |
suma2 += g[n - (i - 1)][ m - (j - 1)] * pix[n, m][1] | |
suma3 += g[n - (i - 1)][ m - (j - 1)] * pix[n, m][2] | |
out[i, j] = int(suma1), int(suma2), int(suma3) | |
out_im.save('output.png', "png") | |
return out_im | |
def normalize(im): | |
w, h = im.size | |
pix1 = im.load() | |
im2 = Image.new("RGB", (w, h)) | |
pix2 = im2.load() | |
max_ = 0 | |
min_ = 256 | |
for i in range(w): | |
for j in range(h): | |
if pix1[i, j][0] > max_: | |
max_ = pix1[i, j][0] | |
if pix1[i, j][0] < min_: | |
min_ = pix1[i, j][0] | |
#print max_, min_ | |
prop = 256.0/(max_ - min_); | |
for i in range(w): | |
for j in range(h): | |
curr = int(math.floor((pix1[i, j][0] - min_)*prop)) | |
pix2[i, j] = curr, curr, curr | |
im2.save('output.png', "png") | |
return im2 | |
def bfs(im, origen, color, rewrite_file="output_bfs.png"): | |
pix = im.load() | |
w, h = im.size | |
q = [] | |
coords = [] | |
q.append(origen) | |
original = pix[origen] | |
while len(q) > 0: | |
(x, y) = q.pop(0) | |
actual = pix[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 < w and j >= 0 and j < h: | |
contenido = pix[i, j] | |
if contenido == original: | |
pix[i, j] = color | |
coords.append((i, j)) | |
q.append((i, j)) | |
im.save(rewrite_file, 'png') | |
return im, len(coords), coords |
En la imagen resultante solo logre detectar la línea.
1 comentario:
Ay, Sergio, ya reprobaste el laboratorio. Tus cuatro pasos mencionados de hecho no llegan ni a la mitad de los pasos que había que realizar. Hubiera sido mucho mejor pedir ayuda hace días... 2 pts.
Publicar un comentario