En esta tarea se nos pidió realizar un código que
detectara círculos.
Para poder realizar esto hay que calcular para cada pixel
el gradiente en “x” y en “y”
Después se calcula Cos y Sin.
Teniendo estos valores se puede calcular los posibles
centros del círculo:
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 conv(img,ancho,altura): | |
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(altura): | |
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= img.save("conv.jpg") | |
return gx, gy | |
def circ(gx,gy): | |
magnitud = [] | |
angulos = [] | |
rhos = [] | |
ancho,altura,pixels,im = cargar(imgesc) | |
angulo = 0.0 | |
ancho2,altura2,pixels2,im2 = cargar(conv) | |
coseno = 0 | |
seno = 0 | |
votos = list() | |
for x in range(altura): | |
votos.append([]) | |
for y in range(ancho): | |
votos[x].append(int(0)) | |
for y in range(altura): | |
rhos.append([]) | |
angulos.append([]) | |
magnitud.append([]) | |
votos.append([]) | |
for x in range(ancho): | |
if x > 0 and y > 0 and y < altura -1 and x < ancho -1: | |
hor = gx[y][x] | |
ver = gy[y][x] | |
g = math.sqrt(hor ** 2 + ver ** 2) | |
if fabs(g) > 0.0: | |
coseno = hor / g | |
seno = ver / g | |
xc = int(round(x -radio*coseno)) | |
yc = int(round(y -radio*seno)) | |
xcm = xc | |
ycm = yc | |
if xcm > 0 and xcm < ancho -1 and ycm > 0 and ycm < altura: | |
votos[ycm][xcm] += 1 | |
pixels[xcm,ycm] = (255,0,255) |
En cuanto al resultado, no logre terminarlo.
1 comentario:
No necesitas calcular ni rhos ni thetas en esta tarea. 1 punto por el intento / avance parcial.
Publicar un comentario