Se utilizaron distintos códigos de tareas pasadas , se hizo uso de escala de grises , convolución, detección de lineas y bfs.
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 pygame | |
from pygame.locals import * | |
from PIL import Image, ImageDraw,ImageFont | |
import sys, random | |
from math import sqrt,fabs,sin,cos,atan2, pow | |
def convolucion(image,gResultant): | |
ancho, altura = image.size | |
pixeles = image.load() | |
imageN = Image.new('RGB', (ancho,altura)) | |
pixels = imageN.load() | |
for x in range(ancho): | |
for y in range(altura): | |
sumatoria = 0 | |
for i in range(x-1, x+2): | |
for j in range(y-1, y+2): | |
try: | |
sumatoria += gResultant[i - (x-1)][j - (y-1)] * pixeles[i,j][1] | |
except: | |
pass | |
pixels[x,y] = (sumatoria,sumatoria,sumatoria) | |
return 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, origen, color): | |
pixeles = image.load() | |
ancho,altura = image.size | |
cola = [] | |
coordenadas = [] | |
cola.append(origen) | |
original = pixeles[origen] | |
while len(cola) > 0: | |
(x, y) = cola.pop(0) | |
actual = pixeles[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 < altura: | |
contenido = pixeles[i, j] | |
if contenido == original: | |
pixeles[i, j] = color | |
coordenadas.append((i, j)) | |
cola.append((i, j)) | |
image.save('output.png', 'png') | |
return len(coordenadas), coordenadas |
Aquí también el código para detección de lineas .
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 pygame | |
from pygame.locals import * | |
import Image | |
from math import floor, atan, fabs, pi, cos, sin, ceil | |
import math | |
pygame.init() | |
pantalla = pygame.display.set_mode((190,250)) | |
# FOTO NORMAL | |
imagen = pygame.image.load("salidaconvolubinariza.png") | |
#CONVOLUCION A ESCALA DE GRISES Y FILTRADA | |
image = Image.open("rayitas.png") | |
pixeles = image.load() | |
pixelesY= image.load() | |
ancho, altura =image.size | |
rango=100 | |
msobelX = ([-1, 0, 1], [-2, 0, 2], [-1, 0, 1]) #Para gradiente de x. | |
msobelY = ([1, 2, 1], [0, 0, 0], [-1, -2, -1]) #Para gradiente de y. | |
prewittX=([-1, 0, 1], [-1, 0, 1], [-1, 0, 1])#EJE X PREWITT | |
prewittY=([1, 1, 1], [0, 0, 0], [-1,-1,-1])#EJE Y PREWITT | |
tamanomatriz=3 | |
sumatoriaX = 0 | |
sumariaY = 0 | |
seleccion=raw_input("INGRESA 1 PARA SOBEL y DOS PARA PREWITT ") | |
matrizagarrada=int(seleccion) | |
if matrizagarrada==1: | |
for x in range(altura): | |
for y in range(ancho): | |
sumatoriaX = 0 | |
sumatoriaY = 0 | |
if x != 0 and y != 0 and y != ancho and x != altura: | |
for i in range(tamanomatriz): | |
for j in range(tamanomatriz): | |
try: | |
gx = prewittX[i][j]*pixeles[y+j, x+i][1] | |
gy = prewittY[i][j]*pixelesY[y+j, x+i][1] | |
except: | |
productosGX = 0 | |
productosGY = 0 | |
sumatoriaX = gx+sumatoriaX | |
sumatoriaY = gy+sumatoriaY | |
gxalcuadrado = pow(sumatoriaX, 2) | |
gyalcuadrado = pow(sumatoriaY, 2) | |
#gradienteResultante = int(math.sqrt(gxalcuadrado+gyalcuadrado)) | |
#pixelNuevo=gradienteResultante | |
#if pixelNuevo> 255: | |
# pixelNuevo = 255 | |
#if pixelNuevo < 0: | |
#pixelNuevo = 0 | |
pixeles[y,x] = ( gxalcuadrado, gxalcuadrado,gxalcuadrado) | |
image.save("imagenhorizo.png",'png') | |
pixelesY[y,x] = ( gyalcuadrado, gyalcuadrado,gyalcuadrado) | |
image.save("imagenverti.png",'png') | |
#image.show() | |
listcompl = list() | |
valor=raw_input("INGRESA el valor para trabajar ") | |
# | |
valoragarr=float(valor) | |
#print (" valor agarrado",valoragarr) | |
comparaAng = 0.00001 #comparacion de ang | |
for y in range(altura): | |
listtempor = list() | |
for x in range(ancho): | |
enX = float(sum(pixeles[x, y]))/3.0 | |
enY = float(sum(pixelesY[x, y]))/3.0 | |
#se checa si no es cero para poder dividirlo | |
if fabs(enX) > comparaAng: | |
angulo = atan(enY/enX) | |
else: #si llega a ser igual a cero | |
if fabs(enX) + fabs(enY) <= comparaAng: | |
angulo = None #No hay nada en ninguna direccion | |
elif enX == 0 and enY == 255: | |
angulo = 90.0 | |
else: | |
angulo = 0.0 | |
if angulo is not None: | |
rho = (x - ancho/2) * math.cos(angulo) + (altura/2 - y) * math.sin(angulo) | |
listtempor.append(('%.2f' % angulo, '%.0f' % rho)) | |
else: | |
listtempor.append((None, None)) | |
listcompl.append(listtempor) | |
diccionario = dict() | |
for y in xrange(altura): | |
for x in xrange(ancho): | |
if x > 0 and y > 0 and x < ancho - 1 and y < altura - 1: | |
(angulo, rho) = listcompl[y][x] | |
if angulo is not None: | |
matr = (angulo, rho) | |
if matr in diccionario: | |
diccionario[matr] += 1#checa si esta sino | |
else: | |
diccionario[matr] = 1# lo agrega aqui y pone un 1 | |
valfrec = repeticion(comb, int(math.ceil(len(comb) * valoragarr))) #Relevante | |
pixeles3 = image.load() | |
for y in range(altura): | |
for x in range(ancho): | |
(angulo, rho) = listcompl[y][x] | |
if (angulo, rho) in valfrec: | |
if angulo == 0.00: | |
pixeles3[x,y] (255,0,0) | |
elif angulo == 90.00: | |
pixeles3[x,y], (0,0,255) | |
else | |
pixeles3[x,y], (0,255,0) | |
imagen.save("imagensalida.png") | |
while True: | |
for eventos in pygame.event.get(): | |
if eventos.type == pygame.QUIT: | |
exit() | |
pantalla.blit(imagen,(0,0)) | |
pygame.display.update() | |
Esta fue la imagen de prueba :

Resultados
No tiene mucho que ver con el método que se solicitó. Estás reciclando código anterior tal cual sin agregar los mecanismos necesarios para la identificación de segmentos individuales. 4 pts.
ResponderEliminar