A continuación les dejo el código el cuál esta incompleto, pero proximamente lo actualizaré.
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 detect_circule(im): | |
imX = convolucion(im, 'imX') | |
imY = convolucion(im, 'imY') | |
radio = float(sys.argv[2]) | |
#for radio in range(sys.argv[2], sys.argv[3]): | |
frecuencia, circule = centros(imX, imY, im, radio) | |
maximo = 0 | |
suma = 0.0 | |
for xy in frecuencia.keys(): | |
suma += frecuencia[xy] | |
if frecuencia[xy] > maximo: | |
maximo = frecuencia[xy] | |
promedio = round(suma / len(frecuencia)) | |
print promedio | |
umbral = maximo - promedio | |
for xy in frecuencia.keys(): | |
if frecuencia[xy] < umbral: | |
frecuencia.pop(xy) | |
print '...' | |
print max(frecuencia.values()) | |
print '...' | |
dibujar_circulos_centro(im,frecuencia,radio, circule) | |
im.save('salidaCirculo3.png') | |
def centros(imX, imY, img, r): | |
w, h = img.size | |
frecuencia = dict() | |
circulos = dict() | |
cX = imX.load() | |
cY = imY.load() | |
for x in range(w): | |
for y in range(h): | |
gX = cX[x,y][0] | |
gY = cY[x,y][0] | |
gradiente = math.sqrt(pow(gX, 2) + pow(gY, 2)) | |
if abs(gradiente) > 0: | |
#Forma sin la necesidad de obtener theta | |
cos_theta = gX / gradiente | |
sin_theta = gY / gradiente | |
xc = int(round(x - r * cos_theta)) | |
xy = int(round(y - r * sin_theta)) | |
centro = (xc, xy) | |
#obteniendolo con theta | |
#theta = math.atan2(gY,gX) | |
#centro = (int( round(x - r * math.cos(theta))), int( round(y - r * math.sin(theta)))) | |
if not centro in frecuencia: | |
frecuencia[centro] = 1 | |
else: | |
frecuencia[centro] += 1 | |
circulos[x,y] = centro | |
else: | |
circulos[x,y] = None | |
return frecuencia, circulos | |
def dibujar_circulos_centro(im, frecuencia, r, circle_matrix): | |
pix = im.load() | |
w, h = im.size | |
imagen = ImageDraw.Draw(im) | |
circulo = dict() | |
for i in frecuencia.keys(): | |
circulo[i] = (250,250,0) | |
imagen.ellipse((i[0]-1, i[1]-1, i[0]+1, i[1]+1), fill=(0,255,0)) | |
print i | |
imagen.text(i, str(i), fill=('red')) | |
for i in range(w): | |
for j in range(h): | |
if circle_matrix[i, j] in frecuencia: | |
try: | |
pix[circle_matrix[i,j]] = (255, 0, 0) | |
except: | |
pass | |
for posicion in circulo.keys(): | |
i , j = posicion | |
for angulo in range(360): | |
x = i + r * math.cos(angulo) | |
y = j + r * math.sin(angulo) | |
try: | |
pix[x,y] = circulo[posicion] | |
except: | |
pass | |
return im |