-
Notifications
You must be signed in to change notification settings - Fork 0
/
hist.py
111 lines (93 loc) · 2.46 KB
/
hist.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
import sys
from sys import argv
import math
import Image
from PIL import ImageDraw
from PIL import ImageOps
from math import fabs
from subprocess import call
#histograma del lado horizontal
def horizontalh(image):
h = list()
im = image.load()
fl = open('hzt.dat','w')
#prom = 0
for x in range(image.size[0]):
suma= 0
for y in range(image.size[1]):
suma += im[x,y]
fl.write(str(x)+' '+str(suma)+'\n')
h.append(suma)
fl.close()
#for i in h:
# prom+=1
#prom= float(prom)/len(h)
return h #, prom
#histograma vertical
def verticalh(image):
h = list()
im = image.load()
fl = open('vert.dat','w')
# prom=0
for y in range(image.size[1]):
suma=0
for x in range(image.size[0]):
suma+=im[x,y]
fl.write(str(y)+' '+str(suma)+'\n')
h.append(suma)
#for i in h:
# prom +=i
#prom = float(prom)/len(h)
fl.close()
return h#, prom
########################
def filtro(image):
im= image.load()
im_copy= (image.copy()).load()
for i in range(image.size[0]):
for j in range(image.size[1]):
temp=[]
for x in range(i-1,i+2):
for y in range(j-1,j+2):
if x>= 0 and y>= 0 and x<image.size[0] and y<image.size[1]:
temp.append(im_copy[x,y])
temp.sort()
im[i,j] = int(temp[int(len(temp)/2)])
return im
########################
def mins(h):
algo = list()
for i in range(len(h)):
try:
if(h[i-1] > h[i] and h[i+1] > h[i]):
algo.append(i)
except:
pass
algo.sort()
return algo
#######################
def det_agujeros(image_name, size = (128,128)):
image = Image.open(image_name)
original_image = image.copy()
image = ImageOps.grayscale(image)
filtro(image)
hist_hor = horizontalh(image)
hist_vert = verticalh(image)
horizontal = mins(hist_hor)
vertical = mins(hist_vert)
call(['gnuplot','plot.gnu'])
r=image.size
image = original_image
draw = ImageDraw.Draw(image)
for x in horizontal:
draw.line((x,0,x, image.size[0]), fill=(255,0,0))
print 'pasa'
for y in vertical:
draw.line((0,y,image.size[1],y), fill = (0,0,255))
print 'pasa'
image.save('lin.png')
#image.show()
def main():
det_agujeros(argv[1])
print 'OLA K ASE'
main()