-
Notifications
You must be signed in to change notification settings - Fork 0
/
avance_2.py
181 lines (165 loc) · 4.91 KB
/
avance_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import math
import sys
import os
import Image, ImageDraw, ImageFont
import random
import numpy as np
from math import *
from sys import argv
import numpy
def boton(img):
image = filtrar(img)
image.save('filtro.png')
image,gx,gy,minimo,maximo,conv = contorno(image)
image.save('bordes.png')
img=normalizar(image,minimo,maximo,conv)
img.save('normalizada.png')
im_bin,analis = binarizar(img)
img.save('binarizada.png')
return im_bin,gx,gy,minimo,maximo,conv,analis
def contorno(image):
sobelx = ([-1,0,1],[-2,0,2],[-1,0,1])
sobely = ([1,2,1],[0,0,0],[-1,-2,-1])
img,gx,gy,minimo,maximo,conv=convolucion(sobelx,sobely,image)
return img,gx,gy,minimo,maximo,conv
def convolucion(sobelx,sobely,image):
foto = image.load()
ancho, alto = image.size
m=len(sobelx[0])
conv = np.empty((ancho, alto))
gx=numpy.empty((ancho, alto))
gy=numpy.empty((ancho, alto))
minimo = 255
maximo = 0
for x in range(ancho):
for y in range(alto):
sumax = 0.0
sumay = 0.0
for i in range(m):
for j in range(m):
try:
sumax +=(foto[x+i,y+j][0]*sobelx[i][j])
sumay +=(foto[x+i,y+j][0]*sobely[i][j])
except:
pass
gradiente = math.sqrt(pow(sumax,2)+pow(sumay,2))
conv[x,y]=gradiente
gx[x,y]=sumax
gy[x,y]=sumay
gradiente = int(gradiente)
foto[x,y] = (gradiente,gradiente,gradiente)
p = gradiente
if p <minimo:
minimo = p
if p > maximo:
maximo = p
image.save('convolucion.png')
return image,gx,gy,minimo,maximo,conv
def normalizar(image,minimo,maximo,conv):
foto = image.load()
dif = maximo-minimo
prom_pixel = 255.0/dif
ancho,alto = image.size
for i in range(ancho):
for j in range(alto):
pixel =int(floor((conv[i,j]-minimo)*prom_pixel))
foto[i,j]=(pixel,pixel,pixel);
return image
def binarizar(img):
foto = img.load()
ancho,alto = img.size
analis = numpy.empty((ancho, alto))
minimo = int(argv[2])
for i in range(ancho):
for j in range(alto):
if foto[i,j][1] < minimo:
p=0
else:
p= 255
foto[i,j]=(p,p,p)
analis[i,j]= p
return img,analis
def filtrar(image):
image,analis = escala(image)
foto = image.load()
ancho, alto =image.size
listas = [-1,0,1]
for i in range(ancho):
for j in range(alto):
prom = vecino(i,j,listas,analis)
foto[i,j] = (prom,prom,prom)
image.save('filtro.png')
return image
def escala(image):
image = Image.open(image)
foto = image.load()
ancho,alto = image.size
analis = numpy.empty((ancho, alto))
for i in range(ancho):
for j in range(alto):
(r,g,b) = image.getpixel((i,j))
tam = (r+g+b)/3
foto[i,j] = (tam,tam,tam)
analis[i,j] = int(tam)
a = image.save('escala_gris.png')
return image,analis
def vecino(i,j,listas,analis):
promedio = 0
n = 0
for x in listas:
for y in listas:
a = i+x
b = j+y
try:
if analis[a,b] and (x!=a and y!=b):
promedio += analis[a,b]
n +=1
except IndexError:
pass
try:
promedio=int(promedio/n)
return promedio
except ZeroDivisionError:
return 0
def obtener_pixel(i,j,listas,analis,ancho,alto):
lista_pixel=[]
for x in listas:
for y in listas:
a = i+x
b = j+y
try:
if a >= 0 and a < ancho and b >= 0 and b < alto:
lista_pixel.append(analis[a,b])
except IndexError:
pass
return lista_pixel
def erosion(image,analis):
foto = image.load()
ancho, alto =image.size
analis_2 = numpy.empty((ancho, alto))
listas = [-1,0,1]
for i in range(ancho):
for j in range(alto):
valor_min = obtener_pixel(i,j,listas,analis,ancho,alto)
valor_min = int(min(valor_min))
#print 'valor minimo',valor_min
analis_2[i,j] = valor_min
foto[i,j] = (valor_min,valor_min,valor_min)
image.save('erosion.png')
return image,analis
def dilatacion(image,analis):
foto = image.load()
ancho, alto =image.size
listas = [-1,0,1]
for i in range(ancho):
for j in range(alto):
valor_max = obtener_pixel(i,j,listas,analis,ancho,alto)
valor_max = int(max(valor_max))
#print 'valor maximo',valor_max
foto[i,j] = (valor_max,valor_max,valor_max)
image.save('dilatacion.png')
return image
if __name__ == "__main__":
image,gx,gy,minimo,maximo,conv,analis=boton('estudio.png')
erosion(image,analis)
dilatacion(image,analis)