Bien la imagen con la que se esta trabajando es la siguiente:
Ahora aplicaremos sal y pimienta a la imagen:
Los resultados fueron un éxito :D
Este es uno de los resultados que conseguí cuando estaba intentando quitar la sal y pimienta. (De las pocas veces que me compilo)
Para quitar la sal y pimienta lamentablemente no se pudo hacer por que siempre me arrojaba errores de que se salia la imagen pero estoy trabajando para poder realizarlo.
Normalización
Este es el resultado de restar la imagen escala de grises por la imagen difusa.
Ahora binarizamos la imagen y este es el resultado.
Practicamente no hubo mucho cambio.
Aquí 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
#!/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() | |
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("norma.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 main(): | |
pygame.init() | |
window() | |
escalagrises() | |
filtro() | |
umbrales() | |
sal_pimienta() | |
quitar_salpim() | |
normaliza() | |
main() |
Algo me dice que el paso de quitar ruido no funciona :P 8 pts lab 2.
ResponderEliminar