Created
February 12, 2013 14:22
This file contains hidden or 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 * | |
import Image | |
import math | |
pygame.init() | |
pantalla = pygame.display.set_mode((190,250)) | |
# FOTO NORMAL | |
imagen = pygame.image.load("linea.jpg") | |
#CONVOLUCION A ESCALA DE GRISES Y FILTRADA | |
image = Image.open("convol.jpg") | |
pixeles = image.load() | |
ancho, altura =image.size | |
msobelX = ([-1, 0, 1], [-2, 0, 2], [-1, 0, 1]) #Para gradiente de x. | |
msobelY = ([1, 2, 1], [0, 0, 0], [-1, -2, -1]) #Para gradiente de y. | |
prewittX=([-1, 0, 1], [-1, 0, 1], [-1, 0, 1])#EJE X PREWITT | |
prewittY=([1, 1, 1], [0, 0, 0], [-1,-1,-1])#EJE Y PREWITT | |
tamanomatriz=3 | |
sumatoriaX = 0 | |
sumariaY = 0 | |
seleccion=raw_input("INGRESA 1 PARA SOBEL y DOS PARA PREWITT ") | |
matrizagarrada=int(seleccion) | |
if matrizagarrada==1: | |
for x in range(altura): | |
for y in range(ancho): | |
sumatoriaX = 0 | |
sumatoriaY = 0 | |
for i in range(tamanomatriz): | |
for j in range(tamanomatriz): | |
try: | |
gx = msobelX[i][j]*pixeles[y+j, x+i][1] | |
gy = msobelY[i][j]*pixeles[y+j, x+i][1] | |
except: | |
productosGX = 0 | |
productosGY = 0 | |
sumatoriaX = gx+sumatoriaX | |
sumatoriaY = gy+sumatoriaY | |
gxalcuadrado = pow(sumatoriaX, 2) | |
gyalcuadrado = pow(sumatoriaY, 2) | |
gradienteResultante = int(math.sqrt(gxalcuadrado+gyalcuadrado)) | |
pixelNuevo=gradienteResultante | |
if pixelNuevo> 255: | |
pixelNuevo = 255 | |
if pixelNuevo < 0: | |
pixelNuevo = 0 | |
pixeles[y,x] = ( pixelNuevo, pixelNuevo, pixelNuevo) | |
image.show() | |
if matrizagarrada==2: | |
for x in range(altura): | |
for y in range(ancho): | |
sumatoriaX = 0 | |
sumatoriaY = 0 | |
for i in range(tamanomatriz): | |
for j in range(tamanomatriz): | |
try: | |
gx = prewittX[i][j]*pixeles[y+j, x+i][1] | |
gy = prewittY[i][j]*pixeles[y+j, x+i][1] | |
except: | |
productosGX = 0 | |
productosGY = 0 | |
sumatoriaX = gx+sumatoriaX | |
sumatoriaY = gy+sumatoriaY | |
gxalcuadrado = pow(sumatoriaX, 2) | |
gyalcuadrado = pow(sumatoriaY, 2) | |
gradienteResultante = int(math.sqrt(gxalcuadrado+gyalcuadrado)) | |
pixelNuevo=gradienteResultante | |
if pixelNuevo> 255: | |
pixelNuevo = 255 | |
if pixelNuevo < 0: | |
pixelNuevo = 0 | |
pixeles[y,x] = ( pixelNuevo, pixelNuevo, pixelNuevo) | |
image.show() | |
while True: | |
for eventos in pygame.event.get(): | |
if eventos.type == pygame.QUIT: | |
exit() | |
pantalla.blit(imagen,(0,0)) | |
pygame.display.update() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment