Para el proceso de la imagen se empleo nuevamente la transformada de Hough, la diferencia es que le agregaremos unas fórmulas para obtener el centro del círculo.
Fórmula del gradiente:
[https://files.nyu.edu/jb4457/public/files/research/bristol/hough-report.pdf]
Theta:
[https://files.nyu.edu/jb4457/public/files/research/bristol/hough-report.pdf]
Obtener centro de x & y:
donde x & y son los puntos de circunsferencia del círculo y a & b el centro del círculo, como nosotros buscamos encontrar a & b, despejamos en la ecuación teniendo la siguiente fórmula:
Código:
A continuación les 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
from PIL import Image, ImageDraw | |
import sys, math | |
def centros(imgX, imgY, img, radio): | |
w, h = im.size | |
frecuencia = dict() | |
circulos = dict() | |
for i in range(w): | |
for j in range(h): | |
gx = imgX[i,j][0] | |
gy = imgY[i,j][0] | |
gradiente = math.sqrt(pow(gx, 2) + pow(gy, 2)) | |
if abs(gradiente) > 0: | |
theta = math.atan2(gy,gx) | |
cx = int(round(i - radio * math.cos(theta))) | |
cy = int(round(j - radio * math.cos(theta))) | |
centro = (cx, cy) | |
if not centro in frecuencia: | |
frecuencia[centro] = 1 | |
else: | |
frecuencia[centro] += 1 | |
circulos[i,j] = centro | |
else: | |
circulos[i,j] = None | |
return frecuencia, circulos | |
def circulos(img, frecuencia, circulo): | |
pix = img.load() | |
imagen = ImageDraw.Draw(img) | |
for i in frecuencia.keys(): | |
imagen.ellipse((i[0]-1, i[1]-1, i[0]+1, i[1]+1), fill=(0,255,0)) | |
return img | |
def detect_circule(): | |
img = Image.open(sys.argv[1]) | |
imgX = convolucion(img, 'x') | |
imgY = convolucion(img, 'y') | |
radio = float(sys.argv[2]) | |
frecuencia, circule = centros(imgX, imgY, img, radio) | |
circulos(img, frecuencia, radio, circule) | |
img.save('circulosSalida.png') | |
def main(): | |
detect_circule() | |
main() |
[radio = 60]
Notas:
El código ésta incompleto, por problemas nocturnos (dormido) y solo podemos distinguir los puntos que se obtienen, pronto lo actualizaré.
Referencias:
https://files.nyu.edu/jb4457/public/files/research/bristol/hough-report.pdf
En realidad no hace falta calcular theta, si te fijas en las diapositivas. 3 pts por el avance intermedio.
ResponderEliminar