Skip to content

Instantly share code, notes, and snippets.

@alice-rdz

alice-rdz/con.py Secret

Created February 12, 2013 07:55
def mascara(self,image):
inicio = time()
#Mascara Sobel
sobelx = ([-1,0,1],[-2,0,2],[-1,0,1]) #gradiente horizontal
sobely = ([1,2,1],[0,0,0],[-1,-2,-1]) # gradiente vertical
img=self.convolucion(sobelx,sobely,image)
fin=time()
tiempo_t = fin - inicio
print "Tiempo que tardo en ejecutarse convolucion = "+str(tiempo_t)+" segundos"
return img
def convolucion(self,h1,h2,image):
pixels = image.load()
ancho,alto = image.size
a=len(h1[0])
self.conv = numpy.empty((ancho, alto))
self.minimo = 255
self.maximo = 0
for x in range(ancho):
for y in range(alto):
sumax = 0.0
sumay = 0.0
for i in range(a):
for j in range(a):
try:
sumax +=(pixels[x+i,y+j][0]*h1[i][j])
sumay +=(pixels[x+i,y+j][0]*h2[i][j])
except:
pass
gradiente = math.sqrt(pow(sumax,2)+pow(sumay,2))
self.conv[x,y]=gradiente
gradiente = int(gradiente)
pixels[x,y] = (gradiente,gradiente,gradiente)
p = gradiente
if p < self.minimo:
self.minimo = p
if p > self.maximo:
self.maximo = p
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment