Para realizar esto partimos de la detección de borde que se había visto anteriormente en la Tarea 1.
Partiendo de ese mismo operador continuamos trabajando utilizando gx y gy,
pero ahora en lugar de utilizar la formula
para obtener la intensidad del gradiente utilizamos la formula

que nos servirá para encontrar el angulo de orientación de las lineas en la imagen, posteriormente aplicaremos la formula para calcular rho:
ρ = x cos θ + y sin θ
Este es el código que utilice, aunque no me salio nada de lo que esperaba :(
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 Image #esto para trabajar con imagenes | |
import sys | |
import pygame | |
from math import pi, atan, sqrt, sin, cos | |
from time import * | |
import random | |
import ImageDraw | |
import math | |
#definimos | |
minimo = 127 | |
maximo = 200 | |
#cargamos y abrimos imagen | |
def imagen(): | |
img = Image.open("binarizada.jpg") | |
img2= Image.open("binarizada.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(img,ancho,alto) | |
conv(img,ancho,alto) | |
lineas(img,ancho,alto) | |
#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(img,ancho,alto): | |
z= random.randint(0,100) | |
pixel=img.load() | |
for i in range (ancho): | |
for j in range(alto): | |
(r,g,b)=img.getpixel((i,j)) | |
prom = (r+g+b)/3 | |
if (prom > z): | |
pixel[i,j]= (255,255,255) | |
else: | |
pixel[i,j] = (0,0,0) | |
img.save('binarizada.jpg') | |
def conv(img,ancho,alto): | |
tiemp = time() | |
pixels =img.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(ancho): | |
for j in range(alto): | |
sumx = 0 | |
sumy = 0 | |
a=3 | |
for x in range(a): | |
for y in range(a): | |
try: | |
sumx +=(pixels[x+i,y+j][0]*matrizX[x][y]) | |
sumy += (pixels[x+i,y+j][0]*matrizY[x][y]) | |
except: | |
pass | |
gx.append(sumx) | |
gy.append(sumy) | |
grad = math.sqrt(pow(sumx,2)+pow(sumy,2)) | |
grad = int(grad) | |
pixels[i,j] = (grad,grad,grad) | |
im= img.save('conv.png') | |
timei=time() | |
timef= timei - tiemp | |
print "Tiempo de ejecucion deteccion de bordes: "+str(timef)+"segundos" | |
return gx, gy | |
def lineas(img,ancho,alto,gx,gy): | |
pixels=img.load() | |
cero = 0.0001 | |
angulos = [] | |
rho = [] | |
for x in range(alto): | |
for y in range(ancho): | |
hor = gx[y][x] | |
ver = gy[y][x] | |
if hor > 0: | |
angulo = atan(ver/hor) #calculamos angulo | |
else: | |
if (ver+hor)< cero: | |
angulo = None | |
else: | |
angulo = pi | |
if angulo is not None: | |
rho =((x-ancho/2)*cos(angulo))+((ancho/2 -y)*sin(angulo)) | |
angulos[x].append(int(angulo)) | |
rho[x].append(rho) | |
else: | |
angulos[x].append(None) | |
rho[x].append(None) | |
hola=dic() | |
for i in range(alto): | |
for j in range(ancho): | |
try: | |
if pixels[i,j][0]==255: | |
dato = (int(rho[x][y]),(angulo[x][y])) | |
if dato in hola: | |
hola[dato]+=1 | |
else: | |
hola[dato] =1 | |
except: | |
pass | |
for i in range(alto): | |
for j in range(ancho): | |
if i>0 and j>0 and i<altura-1 and j<ancho-1: | |
try: | |
if pixels[j,i][1] ==255: | |
if rho[i,j] != None: | |
pixels[i,j] = (255,0,0) | |
print 'ola ke ase' | |
else: | |
print 'no ase' | |
except: | |
pass | |
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() |
***
Aun estoy trabajando sobre este código y subiré resultados cuando lo haya corregido.
Problema Resuelto
Este es el código corregido:
***
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 | |
from time import * | |
import random | |
import ImageDraw | |
import math | |
#definimos | |
minimo = 127 | |
maximo = 200 | |
#cargamos y abrimos imagen | |
def imagen(): | |
img = Image.open("lab.jpg") | |
img2= Image.open("lab.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 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 | |
else: | |
angulo = pi | |
if angulo is not None: | |
rho =( ( (y-ancho/2)*cos(angulo) )+((ancho/2 -x)*sin(angulo)) ) | |
#rho | |
#rho = (y - ancho/2)*cos(angulo)+(x -alto/2)*sin(angulo) | |
angulos[x].append(int(angulo)) | |
rhos[x].append(rho) | |
else: | |
angulos[x].append(None) | |
rhos[x].append(None) | |
hola=dict() | |
for i in range(alto): | |
for j in range(ancho): | |
try: | |
if pixels[i,j][0]==255: | |
dato = (int(rho[x][y]),(angulo[x][y])) | |
if dato in hola: | |
hola[dato]+=1 | |
else: | |
hola[dato] =1 | |
except: | |
pass | |
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: | |
# if rhos[i][j] == 0.75: | |
pixels[j,i] = (255,0,0) | |
print 'ola ke ase' | |
else: | |
print 'no ase' | |
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() |
***
Y este es el resultado :D
Los votos serían para PARES (rho, theta) para que pueda funcionar esto. Van 2 pts por el avance.
ResponderEliminar