Intente varias pruebas para detectar los círculos de diferentes, pensé en hacer que desde el centro de mas hacia la frontera del circulo y sacar la distancia , pero me tope con muchos problemas y no pude sacarlo a tiempo.
Se detectan los círculos aquí dejo el 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 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 | |
def detectarcentros(imgx, imgy, image, s,um): | |
ancho,altura = image.size | |
frecuencia = dict() | |
circulos = dict() | |
pixelesx = imgx.load() | |
pixelesy = imgy.load() | |
for x in range(ancho): | |
for y in range(altura): | |
(rx,gx,bx) = pixelesx[x,y] | |
(ry,gy,by) = pixelesy[x,y] | |
valor=10 | |
gx = float(rx+gx+bx)/3 | |
gy = float(ry+gy+by)/3 | |
gxalcuadrado = pow(gx, 2) | |
gyalcuadrado = pow(gy, 2) | |
gResultant = sqrt((gxalcuadrado+gyalcuadrado)) | |
if fabs(gResultant) > 0: | |
cosTheta = gx/gResultant | |
sinTheta = gy/gResultant | |
theta = atan2(gy,gx) | |
radio=50 | |
centro = (int( x - radio * cos(theta)), int( y - radio * sin(theta))) | |
centro = ((centro[0]/valor)*valor, (centro[1]/valor)*valor) | |
circulos[x,y]=centro | |
if not centro in frecuencia: | |
frecuencia[centro] = 1 | |
else: | |
frecuencia[centro] += 1 | |
else: | |
circulos[x,y]=None | |
return circulos,frecuencia | |
def amarillo(): | |
return (255, random.randint(100,255), random.randint(0,50)) | |
def nombraracirc(image, frecuencia, circulos): | |
pixeles = image.load() | |
ancho, altura = image.size | |
fuente = ImageFont.truetype('/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-LI.ttf',18) | |
imagen = ImageDraw.Draw(image) | |
cont = 1 | |
color = dict() | |
aux = 2 | |
radio = 50 | |
for i in frecuencia.keys(): | |
color[i] = amarillo() | |
imagen.ellipse((i[0]-aux, i[1]-aux, i[0]+aux, i[1]+aux), fill=(0,255,0)) | |
imagen.text((i[0]+aux+3, i[1]), ('Circulo '+str(cont)), fill=(0,255,0), font=fuente) | |
cont +=1 | |
for c in color.keys(): | |
i ,j = c | |
for angulo in range(360): | |
x = i + radio*cos(angulo) | |
y = j + radio*sin(angulo) | |
try: | |
pixeles[x,y] = color[c] | |
except: | |
pass | |
return image | |
if __name__ =="__main__": | |
pygame.init() | |
pantalla = pygame.display.set_mode((275,250)) | |
imagens = pygame.image.load("sal.png") | |
image = Image.open("sal.png") | |
pixeles = image.load() | |
ancho, altura =image.size | |
mascarax = [[-1, -1, -1], [2, 2, 2], [-1, -1, -1]] | |
mascaray = [[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]] | |
#mx = ([-1, 0, 1], [-2, 0, 2], [-1, 0, 1]) #Para gradiente de x. | |
# my = ([1, 2, 1], [0, 0, 0], [-1, -2, -1]) #Para gradiente de y. | |
imgx = convolucion(image, mascarax) | |
imgy = convolucion(image, mascaray) | |
s=10 | |
um=10.0 | |
circulos,frecuencia = detectarcentros(imgx, imgy, image, s,um) | |
maximo = 0 | |
suma = 0.0 | |
for i in frecuencia.keys(): | |
if frecuencia[i] < s*8: | |
frecuencia.pop(i) | |
else: | |
print frecuencia[i], i | |
nombraracirc(image,frecuencia,circulos) | |
image.save('circulos.png') | |
while True: | |
for eventos in pygame.event.get(): | |
if eventos.type == pygame.QUIT: | |
exit() | |
pantalla.blit(imagens,(0,0)) | |
pygame.display.update() |