- Una rutina en este caso bfs la cual nos ayuda a darle color a las figuras.
- El componente más grande debe ser el fondo, y colorearse gris.
- A los demás dibujos se les asignan otros colores.
La imagen con la que se trabajo fue la siguiente:
A esta figura le aplicamos el procesamiento que se le a hecho a las demás imágenes en las tareas anteriores.
Aquí imagen con bordes encontrados
Ahora la binarizamos para juntar los bordes que se encuentren despegados
Ahora pintamos la imagen con la rutina bfs para colorear y le sacamos los porcentajes que ocupan en ese lugar.
Aqui el codigo:
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
#!/usr/bin/env python | |
import pygame # interfaz | |
from pygame.locals import * # para funcion de botones y raton | |
from pygame import * | |
from PIL import Image # Para cargar imagen | |
from math import * | |
import numpy #Libreria para arreglos | |
import sys | |
import time | |
import math | |
import random | |
image = str(raw_input('Dame el nombre de la imagen con extencion: ')) | |
img = Image.open(image) # Abrimos imagen con PIL | |
width, height = img.size #Obtencion de medidas de la imagen | |
def window(): | |
screen = pygame.display.set_mode((width, height)) # cargar ventana con medidas | |
pygame.display.set_caption("window vision") # Mostrar ventana | |
background = pygame.image.load(image) # carga de imagen | |
#button = pygame.Surface((100, 25)) | |
screen.blit(background, (0,0)) #posicion de imagen en la ventana | |
pygame.display.flip() #Refrescar pantalla | |
while True: #ciclo para cerrar ventana | |
for event in pygame.event.get(): # Para eventos en de pygame | |
if event.type == QUIT: #Cerrar ventana | |
sys.exit(0) | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_g: | |
escalagrises() | |
background = pygame.image.load("grayim.jpg") # carga de imagen | |
screen.blit(background, (0,0)) #posicion de imagen en ventana | |
pygame.display.flip() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_f: | |
filtro() | |
background = pygame.image.load("imfil.jpg") # carga de imagen | |
screen.blit(background, (0,0)) #posicion de imagen en ventana | |
pygame.display.flip() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_c: | |
convolucion() | |
background = pygame.image.load("sobel.jpg") # carga de imagen | |
screen.blit(background, (0,0)) #posicion de imagen en ventana | |
pygame.display.flip() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_b: | |
umbrales() | |
background = pygame.image.load("bin.jpg") # carga de imagen | |
screen.blit(background, (0,0)) #posicion de imagen en ventana | |
pygame.display.flip() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_s: | |
sal_pimienta() | |
background = pygame.image.load("salpi.jpg") | |
screen.blit(background, (0,0)) | |
pygame.display.flip() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_p: | |
quitar_salpim() | |
background = pygame.image.load("nosalpi.jpg") | |
screen.blit(background, (0,0)) | |
pygame.display.flip() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_n: | |
normaliza() | |
background = pygame.image.load("norma.jpg") | |
screen.blit(background, (0,0)) | |
pygame.display.flip() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_a: | |
Figuras() | |
background = pygame.image.load("fig.jpg") | |
screen.blit(background, (0,0)) | |
pygame.display.flip() | |
return 0 | |
def escalagrises(): # Funcion para hacer a escala de grises | |
tiempoInicial = time.time() | |
img = Image.open(image) # Abrimos imagen con PIL | |
pixel = img.load() #Carga de matriz de pixeles | |
width, height = img.size #Obtencion de medidas de la imagen | |
for i in range(width): #Contar pixeles a lo ancho | |
for j in range(height): # Contar pixeles a lo largo | |
(r, g, b) = pixel[i,j] | |
gs = int((r + g + b) / 3) | |
pixel [i, j] = (gs, gs, gs) | |
img.save("grayim.jpg") | |
tiempoFinal = time.time() | |
transcurrido = tiempoFinal - tiempoInicial | |
print "Tiempo transcurrido durante la escala de grises = ", transcurrido | |
def umbrales(): # Funcion para los umbrales | |
tiempoInicial = time.time() | |
umbraln = 77 # Umbral para color negro en funcion umbral | |
umbralb = 177 # Umbral para color blanco en funcion umbral | |
img = Image.open("sobel.jpg") # Abrimos imagen con PIL | |
pixel = img.load() #Carga de matriz de pixeles | |
width, height = img.size #Obtencion de medidas de la imagen | |
for i in range(width): | |
for j in range(height): | |
(r, g, b) = pixel[i,j] | |
prom = int((r + g + b) / 3) | |
if prom < umbraln: | |
pixel[i,j] = (0, 0, 0) | |
elif prom > umbralb: | |
pixel[i,j] = (255, 255, 255) | |
else: | |
pixel[i,j] = (prom, prom, prom) | |
#img.save("umb.png") | |
img.save("bin.jpg") | |
tiempoFinal = time.time() | |
transcurrido = tiempoFinal - tiempoInicial | |
print "Tiempo transcurrido durante la binarizacion = ", transcurrido | |
def filtro(): #Filtrada por el metodo de los vecinos | |
tiempoInicial = time.time() | |
img = Image.open("grayim.jpg") | |
width, height = img.size | |
pixel = img.load() | |
promedio = 0 | |
width = width-1 | |
height = height-1 | |
for x in range(height): | |
for y in range(width): | |
#esquina superior izquierda | |
if y == 0 and x == 0: | |
promedio = (sum(pixel[y + 1,x])/3 + sum(pixel[y,x + 1])/3 + sum(pixel[y,x])/3)/3 | |
#esquina superior derecha | |
if y == width and x == 0: | |
promedio = (sum(pixel[y,x+1])/3 + sum(pixel[y-1,x])/3 + sum(pixel[y,x])/3)/3 | |
if y == 0 and x == height: | |
promedio = (sum(pixel[y,x-1])/3 + sum(pixel[y+1,x])/3 + sum(pixel[y,x])/3)/3 | |
if y == height and x == width: | |
promedio = (sum(pixel[y - 1,x])/3 + sum(pixel[y,x - 1])/3 + sum(pixel[y,x])/3)/3 | |
if y > 0 and y < width and x == 0: | |
promedio = (sum(pixel[y+1,x])/3 + sum(pixel[y-1,x])/3 +sum(pixel[y,x+1])/3+ sum(pixel[y,x])/3)/4 | |
if y > 0 and y < width and x == height: | |
promedio = (sum(pixel[y -1,x])/3 + sum(pixel[y,x-1])/3 +sum(pixel[y+1,x])/3+ sum(pixel[y,x])/3)/4 | |
if x >0 and x <height and y == 0: | |
promedio = (sum(pixel[y+1,x])/3 + sum(pixel[y,x-1])/3 +sum(pixel[y,x +1])/3+ sum(pixel[y,x])/3)/4 | |
if y == width and x >0 and x < height: | |
promedio = (sum(pixel[y - 1,x])/3 + sum(pixel[y,x-1])/3 + sum(pixel[y,x +1])/3+ sum(pixel[y,x])/3)/4 | |
if y > 0 and y< width and x>0 and x< height: | |
promedio = (sum(pixel[y,x])/3 + sum(pixel[y + 1,x])/3 + sum(pixel[y - 1,x])/3 + sum(pixel[y,x + 1])/3 + sum(pixel[y,x -1])/3)/5 | |
a = promedio | |
b = promedio | |
c = promedio | |
pixel[y, x] = (a,b,c) | |
img.save('imfil.jpg') | |
tiempoFinal = time.time() | |
transcurrido = tiempoFinal - tiempoInicial | |
print "Tiempo transcurrido durante la filtrado = ", transcurrido | |
def convolucion(): | |
tiempoInicial = time.time() | |
img = Image.open("imfil.jpg") | |
width, height = img.size | |
pix = img.load() | |
sobelx = ([-1, 0, 1], [-2, 0, 2], [-1, 0, 1]) #Operador sobel | |
sobely = ([1, 2, 1], [0, 0, 0], [-1, -2, -1]) | |
for x in range(height): | |
for y in range(width): | |
resx = 0 | |
resy = 0 | |
if x != 0 and y != 0 and y != width and x != height: #centrar mascara | |
for a in range(3): #Matriz 3x3 | |
for b in range(3): | |
try: | |
mascx = sobelx[a][b]*pix[y+b, x+a][1] | |
mascy = sobely[a][b]*pix[y+b, x+a][1] | |
except: | |
mascx = 0 | |
mascy = 0 | |
resx = mascx+resx | |
resy = mascy+resy | |
exponx = pow(resx, 2) #cuadrados de x e y | |
expony = pow(resy, 2) | |
resfi = int(sqrt(exponx+expony)) | |
if resfi > 255: #Por si se pasan los valores | |
resfi = 255 | |
if resfi < 0: | |
resfi = 0 | |
pix[y,x] = (resfi, resfi, resfi) | |
img.save('sobel.jpg') | |
tiempoFinal = time.time() | |
transcurrido = tiempoFinal - tiempoInicial | |
print "Tiempo transcurrido durante la convolucion = ", transcurrido | |
def sal_pimienta(): | |
tiempoInicial = time.time() | |
intensidad = .10 | |
img = Image.open(image) | |
pix = img.load() | |
width, height = img.size | |
for i in range(width): | |
for j in range(height): | |
(r, g, b) = pix[i, j] | |
if (random.random() < intensidad): | |
ruido = random.randint(0,255) | |
if ruido < 100: | |
pix[i, j] = (255,255,255) | |
else: | |
pix[i, j] = (0,0,0) | |
pix[i, j] = (ruido, ruido, ruido) | |
img.save("salpi.jpg") | |
tiempoFinal = time.time() | |
transcurrido = tiempoFinal - tiempoInicial | |
print "Tiempo transcurrido durante la aplicacion de sal y pimienta = ", transcurrido | |
def quitar_salpim(): | |
tiempoInicial = time.time() | |
img = Image.open("salpi.jpg") | |
pix = img.load() | |
promedio = 0 | |
width, height = img.size | |
#width = width - 1 | |
#height = height -1 | |
for x in range(width): | |
for y in range(height): | |
pixelesAgregados = list() | |
(r,g,b) = pix[x,y] | |
if y == 0 and x == 0: | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p1 = pixeles[1] | |
pixelesAgregados.append(p1) | |
#promedio = (sum(pixel[y + 1,x])/3 + sum(pixel[y,x + 1])/3 + sum(pixel[y,x])/3)/3 | |
#esquina superior derecha | |
if y == width and x == 0: | |
#promedio = (sum(pixel[y,x+1])/3 + sum(pixel[y-1,x])/3 + sum(pixel[y,x])/3)/3 | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p2 = pixeles[1] | |
pixelesAgregados.append(p2) | |
if y == 0 and x == height: | |
#promedio = (sum(pixel[y,x-1])/3 + sum(pixel[y+1,x])/3 + sum(pixel[y,x])/3)/3 | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p3 = pixeles[1] | |
pixelesAgregados.append(p3) | |
if y == height and x == width: | |
#promedio = (sum(pixel[y - 1,x])/3 + sum(pixel[y,x - 1])/3 + sum(pixel[y,x])/3)/3 | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p4 = pixeles[1] | |
pixelesAgregados.append(p4) | |
if y > 0 and y < width and x == 0: | |
#promedio = (sum(pixel[y+1,x])/3 + sum(pixel[y-1,x])/3 +sum(pixel[y,x+1])/3+ sum(pixel[y,x])/3)/4 | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p5 = pixeles[1] | |
pixelesAgregados.append(p5) | |
if y > 0 and y < width and x == height: | |
#promedio = (sum(pixel[y -1,x])/3 + sum(pixel[y,x-1])/3 +sum(pixel[y+1,x])/3+ sum(pixel[y,x])/3)/4 | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p6 = pixeles[1] | |
pixelesAgregados.append(p6) | |
if x >0 and x <height and y == 0: | |
#promedio = (sum(pixel[y+1,x])/3 + sum(pixel[y,x-1])/3 +sum(pixel[y,x +1])/3+ sum(pixel[y,x])/3)/4 | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p7 = pixeles[1] | |
pixelesAgregados.append(p7) | |
if y == width and x >0 and x < height: | |
#promedio = (sum(pixel[y - 1,x])/3 + sum(pixel[y,x-1])/3 + sum(pixel[y,x +1])/3+ sum(pixel[y,x])/3)/4 | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p8 = pixeles[1] | |
pixelesAgregados.append(p8) | |
if y > 0 and y< width and x>0 and x< height: | |
#promedio = (sum(pixel[y,x])/3 + sum(pixel[y + 1,x])/3 + sum(pixel[y - 1,x])/3 + sum(pixel[y,x + 1])/3 + sum(pixel[y,x -1])/3)/5 | |
pixeles = list(pix[x,y]) | |
pixeles.sort() | |
p9 = pixeles[1] | |
pixelesAgregados.append(p9) | |
#pixeles2 = list(p1, p2, p3, p4, p5, p6, p7, p8) | |
pixelesAgregados.sort() | |
nuevoPixel = int(len(pixelesAgregados) / 2) | |
nuevoPixel2 = pixelesAgregados[nuevoPixel] | |
print nuevoPixel2 | |
raw_input() | |
pix[x, y] = (nuevoPixel2, nuevoPixel2, nuevoPixel2) | |
img.save("nosalpi.jpg") | |
tiempoFinal = time.time() | |
transcurrido = tiempoFinal - tiempoInicial | |
print "Tiempo transcurrido durante el borrado de sal y pimienta = ", transcurrido | |
def normaliza(): | |
tiempoInicial = time.time() | |
img1 = Image.open("grayim.jpg") | |
img2 = Image.open("imfil.jpg") | |
width, height = img1.size | |
pix = img1.load() | |
pix2 = img2.load() | |
for i in range(width): | |
for j in range(height): | |
(r1,g1,b1) = pix[i,j] | |
(r2,g2,b2) = pix2[i,j] | |
prom1 = r1+g1+b1/3 | |
prom2 = r2+g2+b2/3 | |
promfinal = prom1 - prom2 | |
if prom2 > 115: | |
prom2 = 0 | |
else: | |
prom2 = 255 | |
pix[i,j] = (prom2, prom2, prom2) | |
img1.save("norma.jpg") | |
tiempoFinal = time.time() | |
transcurrido = tiempoFinal - tiempoInicial | |
print "Tiempo transcurrido durante la normalizacion = ", transcurrido | |
def bfs(img, origen, color): | |
res = 0 | |
pixel = img.load() | |
width, height = img.size | |
c = [] | |
listx = [] | |
listy = [] | |
c.append(origen) | |
principio = pixel[origen] | |
while len(c) > 0: | |
(x, y) = c.pop(0) | |
actual = pixel[x, y] | |
if actual == principio or actual == color: | |
for dx in [-1, 0, 1]: # coordenadas donde empezara a buscar y pintar | |
for dy in [-1, 0, 1]: | |
i, j = (x + dx, y + dy) | |
if i >= 0 and i < width and j >= 0 and j < height: | |
visitado = pixel[i, j] | |
if visitado == principio: | |
pixel[i, j] = color | |
listx.append(i) | |
listy.append(j) | |
res += 1 | |
c.append((i, j)) | |
return res, listx,listy | |
def Figuras(): | |
img = Image.open("bin.jpg") # llamamos a la imagen binarizada | |
width, height = img.size | |
total = width * height | |
porc = [] | |
pc = [] #punto centrico | |
resfinal = 0 | |
contado = 0 | |
pixe = img.load() | |
algo = [] | |
for i in range(width): # Recorremos la imagen ancho y alto | |
for j in range(height): | |
if pixe[i, j] == (0, 0, 0): | |
r, g, b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) # pintamos los pixeles con un color random | |
n, x, y = bfs(img, (i,j),(r,g,b)) #mandamos resultados a funcion | |
ptemp = float(n)/float(total) * 100.0 # obtenemos los porcentajes | |
if ptemp > 0.2: | |
pc.append((sum(x) / len(x), sum(y) / len(y))) # encontramos sus puntos centricos | |
porc.append([ptemp, (r, g, b)]) | |
contado += 1 | |
else: | |
algo.append((x, y)) | |
fondo = porc.index(max(porc)) | |
color = porc[fondo][1] | |
for i in range(width): | |
for j in range(height): | |
if pixe[i, j] == color: | |
pixe[i, j] = (150, 150, 150) | |
for i in range(len(pc)): | |
if i == fondo: | |
pixe[pc[i]] = (255, 0, 0) #colocamos centros de masa | |
else: | |
pixe[pc[i]] = (0, 0, 0) | |
img.save('fig.jpg') #guardamos imagen creada | |
contado = 0 | |
for ptemp in porc: | |
print "Porcentaje de figura %s: %.2f"%(contado, ptemp[0]) | |
contado = contado + 1 | |
resfinal = ptemp[0] + resfinal | |
def main(): | |
pygame.init() | |
window() | |
escalagrises() | |
filtro() | |
umbrales() | |
sal_pimienta() | |
quitar_salpim() | |
normaliza() | |
bfs() | |
Figuras() | |
main() |
Faltó lo de centros de masa y etiquetas. 4 pts.
ResponderEliminarNP lab 3 de visión.
ResponderEliminar