Para elaborar esto fue necesario apoyarnos con lo que se había trabajado en clase anteriormente (tarea 3) y utilizamos las mismas formulas para calcular theta y rho.
ρ = x cos θ + y sin θ
En este caso identificamos los ángulos con pi para 180°, pi/2 para 90° y pi/4 para 45° , esto para identificar las diagonales.
Estas son las imágenes que use de prueba:
Y este es el resultado que obtuve:
Como podemos observar las lineas diagonales están marcadas en color verde, incluso las partes diagonales de los círculos están marcadas así.
Como le seguí moviendo al código encontré un pequeño detalle y lo corregí y al cambiarlo obtuve el siguiente resultado:
Ahora me marca las diagonales hacia la izq de un color y las de la derecha de otro.
Este es el código que utilice :)
**
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 Image #esto para trabajar con imagenes | |
import sys | |
import pygame | |
from math import pi, atan, sqrt, sin, cos, fabs, ceil | |
from time import * | |
import random | |
import ImageDraw | |
import math | |
#definimos | |
minimo = 127 | |
maximo = 200 | |
#cargamos y abrimos imagen | |
def imagen(): | |
img = Image.open("Lineas.JPG") | |
img2= Image.open("Lineas.JPG") | |
ancho,alto = img.size | |
img = eg(img,ancho,alto,img2) | |
return img, ancho, alto | |
def eg(img,ancho,alto,img2): | |
pixeles = img.load() | |
imageng = 'escg.jpg' | |
for i in range (ancho): | |
for j in range(alto): | |
(r,g,b)= img.getpixel((i,j)) | |
prom = int((r+g+b)/3) | |
#Aqui agregamos umbrales | |
pixeles[i,j] = (prom,prom,prom) | |
img.save(imageng) | |
filtro(img,ancho,alto) | |
binarizacion(img2,ancho,alto) | |
gx,gy=conv(img2,ancho,alto) | |
#binarizacion(img2,ancho,alto) | |
lineas(img2,ancho,alto,gx,gy) | |
#ruido(img2,ancho,alto) | |
#byeruido(img2,ancho,alto) | |
#formas(img,ancho,alto) | |
#convex(img,ancho,alto) | |
return imageng | |
def filtro(img,ancho,alto): | |
tiemp= time() | |
pixel =img.load() | |
for i in range (ancho): | |
for j in range(alto): | |
c = 0 | |
prom = 0.0 | |
try: | |
if(pixel[i+1,j]): | |
prom += pixel[i+1,j][0] | |
c +=1 | |
except: | |
prom += 0 | |
try: | |
if(pixel[i-1,j]): | |
prom += pixel[i-1,j][0] | |
c +=1 | |
except: | |
prom += 0 | |
try: | |
if(pixel[i,j+1]): | |
prom += pixel[i,j+1][0] | |
c+=1 | |
except: | |
prom += 0 | |
try: | |
if(pixel[i,j-1]): | |
prom += pixel[i,j-1][0] | |
c+=1 | |
except: | |
prom += 0 | |
promt = int(prom/c) | |
pixel[i,j] = (promt, promt, promt) | |
im=img.save ('filtro.jpg') | |
timei=time() | |
timef= timei - tiemp | |
print "Tiempo de ejecucion del filtro: "+str(timef)+"segundos" | |
def binarizacion(img2,ancho,alto): | |
z= random.randint(0,100) | |
pixel=img2.load() | |
for i in range (ancho): | |
for j in range(alto): | |
(r,g,b)=img2.getpixel((i,j)) | |
prom = (r+g+b)/3 | |
if (prom > z): | |
pixel[i,j]= (255,255,255) | |
if(prom<z): | |
pixel[i,j] = (0,0,0) | |
#if(prom<130): | |
#pixel[i,j]=(0,0,0) | |
#else: | |
#pixel[i,j]=(255,255,255) | |
img2.save('binarizada.jpg') | |
def conv(img2,ancho,alto): | |
tiemp = time() | |
pixels =img2.load() | |
angulos = [] | |
gy = [] | |
gx = [] | |
matrizX =([-1,0,1],[-2,0,2],[-1,0,1]) | |
matrizY =([1,2,1],[0,0,0],[-1,-2,-1]) | |
for i in range(alto): | |
gx.append([]) | |
gy.append([]) | |
for j in range(ancho): | |
sumx = 0 | |
sumy = 0 | |
a=3 | |
for x in range(a): | |
for y in range(a): | |
try: | |
sumx +=(pixels[j+y-1,i+x-1][0]*matrizX[x][y]) | |
sumy +=(pixels[j+y-1,i+x-1][0]*matrizY[x][y]) | |
except: | |
pass | |
gx[i].append(sumx) | |
gy[i].append(sumy) | |
grad = math.sqrt(pow(sumx,2)+pow(sumy,2)) | |
grad = int(grad) | |
pixels[j,i] = (grad,grad,grad) | |
im= img2.save('conv.jpg') | |
timei=time() | |
timef= timei - tiemp | |
print "Tiempo de ejecucion deteccion de bordes: "+str(timef)+"segundos" | |
return gx, gy | |
def frecuentes(histo, cantidad): | |
frec = list() | |
for valor in histo: | |
if valor is None: | |
continue | |
frecuencia = histo[valor] | |
acepta = False | |
if len(frec) <= cantidad: | |
acepta = True | |
if not acepta: | |
for (v, f) in frec: | |
if frecuencia > f: | |
acepta = True | |
break | |
if acepta: | |
frec.append((valor, frecuencia)) | |
frec = sorted(frec, key = lambda tupla: tupla[1]) | |
if len(frec) > cantidad: | |
frec.pop(0) | |
incluidos = list() | |
for (valor, frecuencia) in frec: | |
incluidos.append(valor) | |
return incluidos | |
def lineas(img2,ancho,alto,gx,gy): | |
pixels=img2.load() | |
cero = 0.0001 | |
angulos = [] | |
rho = 0.0 | |
rhos = [] | |
print len(gx) | |
print alto | |
for x in range(alto): | |
rhos.append([]) | |
angulos.append([]) | |
for y in range(ancho): | |
hor = gx[x][y] | |
ver = gy[x][y] | |
if fabs(hor) > 0: | |
angulo = atan(ver/hor) #calculamos angulo | |
else: | |
if fabs(ver)+fabs(hor)< cero: | |
angulo = None | |
elif fabs(ver - hor) < cero: | |
angulo = 0.79 | |
elif fabs(hor)*fabs(ver) > 0.0: | |
angulo = 3 | |
else: | |
angulo = 0.0 | |
if angulo is not None: | |
while angulo< cero: | |
angulo+=pi | |
while angulo >pi: | |
angulo -= pi | |
angulo= float('%0.2f'%angulo) | |
rho =( ( (y-ancho/2)*cos(angulo) )+((ancho/2 -x)*sin(angulo)) ) | |
print "valor angulos= ",angulo | |
#rho | |
#rho = (y - ancho/2)*cos(angulo)+(x -alto/2)*sin(angulo) | |
angulos[x].append(angulo) | |
rhos[x].append(int(rho)) | |
else: | |
angulos[x].append(None) | |
rhos[x].append(None) | |
hola=dict() | |
for i in range(alto): | |
for j in range(ancho): | |
try: | |
#print "si se puede o no se puede " | |
if rhos[i][j] != None: | |
if pixels[i,j][0]==255: | |
dato = ((rhos[x][y]),(angulos[x][y])) | |
if dato in hola: | |
hola[dato]+=1 | |
else: | |
hola[dato] =1 | |
except: | |
pass | |
#frecuencias :D | |
frec = frecuentes(hola, int(ceil(len(hola)*50))) | |
print 'pase aki' | |
for i in range(alto): | |
for j in range(ancho): | |
if i>0 and j>0 and i<alto-1 and j<ancho-1: | |
try: | |
#print "si paso aki la llama" | |
if pixels[j,i][1] ==255: | |
if rhos[i][j] != None: | |
rho,ang = rhos[i][j],angulos[i][j] | |
print "segunda llama: ",ang | |
#if (rho,ang) in frec: | |
print'aki paso' | |
if ang ==3.14: #or ang == 0: | |
print 'rojo' | |
pixels[j,i] = (255,0,0) | |
if ang == 2.36: | |
print 'verde' | |
pixels[j,i]=(0,255,0) | |
if ang == 0.79: | |
print'azul' | |
pixels[j,i]=(0,0,255) | |
else: | |
pixels[j,i]=(154,255,80) | |
except: | |
pass | |
img2.save('lineas.jpg') | |
def main (): | |
pygame.init() | |
#pygame.display.set_caption("Ventana") | |
r,ancho,alto = imagen() | |
screen = pygame.display.set_mode((ancho,alto)) | |
pygame.display.set_caption("Ventana") | |
im = pygame.image.load(r) | |
while True: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit(0) | |
screen.blit(im,(0,0)) | |
pygame.display.update() | |
return 0 | |
main() |
**
Para realizar esto , me apoye en el código de la Dra. Elisa Schaeffer.
Lo obligatorio está decente y hasta el color depende de la orientación dentro de los diagonales. 8 pts.
ResponderEliminar