Last active
December 16, 2015 13:19
-
-
Save robertovalenzuela91/5441141 to your computer and use it in GitHub Desktop.
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 obtenerpicos(vechisto, valormax): | |
umbmin = min(vechisto) | |
umbmax = max(vechisto) | |
corde = [] | |
sumaminmax = umbmax + umbmin | |
umbral = (sumaminmax)/2 | |
for i in range(1, len(vechisto) - 1): | |
if valormax = 1: | |
if (vechisto[i] > vechisto[i-1]) and (vechisto[i] > vechisto[i+1]): | |
corde.append(i) | |
if vechisto[i] > umbral: corde.append(i) | |
elif valormin = 0: | |
if (vechisto[i] < vechisto[i-1]) and (vechisto[i] < vechisto[i+1]): | |
corde.append(i) | |
if vechisto[i] > umbral: corde.append(i) | |
return corde | |
def dibujar(image, vertical, horizontal): | |
ancho, altura = image.size | |
image = Image.open(argv[1]) | |
draw = ImageDraw.Draw(image) | |
pixeles = image.load() | |
conta = 1 | |
total = ancho*altura | |
areas = [] | |
for j in horizontal: | |
for i in vertical: | |
(pos1, pos3), (pos2, pos4) = (i-(tamanoagujero/2), j-(tamanoagujero/2)), (i+(tamanoagujero/2), j+(tamanoagujero/2)) | |
aceptado, start = areafucion(image, (pos1, pos3), (pos2, pos4)) | |
if aceptado: | |
if start is not (None, None): | |
popin, coordenads = bfs(image, start, (255, 0, 0)) | |
suma = [sum(x) for x in zip(*coordenads)] | |
centro = (suma[0] / len(coordenads), suma[1] / len(coordenads)) | |
pixeles[centro] = (255, 255, 0) | |
areas.append(popin) | |
draw.ellipse(((centro[0]-tamanoagujero/2, centro[1]-tamanoagujero/2), (centro[0]+tamanoagujero/2, centro[1]+tamanoagujero/2)), outline = morado('relleno'), | |
fill = morado('bordclar')) | |
draw.text(centro, "%s"%conta, fill = (0, 0, 0)) | |
conta += 1 | |
for i in range(len(areas)): | |
print "ID # %s area: %0.2f%%"%(i, 100.0*(areas[i]/float(total))) | |
image.save('salida.png', 'PNG') | |
def morado(borde_o_relleno): | |
if borde_o_relleno == 'bordclar': | |
return (random.randint(85, 100), random.randint(33, 50), random.randint(100, 115)) | |
elif borde_o_relleno == 'relleno': | |
return (random.randint(74, 78), random.randint(33, 35), random.randint(100, 104)) | |
def areafucion(image, (pos1, pos3), (pos2, pos4)): | |
ancho, altura = image.size | |
pixeles = image.load() | |
area = abs(pos2 - pos1) * abs(pos4 - pos3) | |
contador = 0 | |
i, j = (None, None) | |
for x in range(pos1, pos2): | |
for y in range(pos3, pos4): | |
if x > 0 and x < ancho and y > 0 and y < altura: | |
if pixeles[x, y] == (0, 0, 0): | |
i, j = x, y | |
contador += 1 | |
if contador > area * 0.2: | |
return True, (i, j) | |
else: | |
return False, (i, j) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment