2b5f7572773782c0e27d076ee01a55df94efb1e6
[matches/honours.git] / research / TCS / pressure / process_digits.py
1 #! /usr/bin/python
2
3 from PIL import Image
4 import Image, ImageDraw
5 import sys
6 import numpy
7
8
9
10
11 def PixelGood(pixel, threshold):
12         #print str(pixel)
13         return GreyScale(pixel) < threshold;
14         #return (pixel[1] > (pixel[0] + pixel[2]) and pixel[1] > threshold)
15
16 def rect_size(e):
17         return (e[3] - e[1]) * (e[2] - e[0])
18
19 def GreyScale(pixel):
20         greyscale = 0
21         for i in range(0, len(pixel)):
22                 greyscale += pixel[i]
23         greyscale = greyscale / len(pixel)
24         return greyscale
25
26 def SubDivide(pix, bounds, fill_fraction=0.1, threshold=200, seperation=4, min_area=250):
27         rect = []
28
29         xStart = bounds[2]
30         xEnd = bounds[0]
31         yStart = bounds[3]
32         yEnd = bounds[1]
33         xLast = bounds[0]
34         good = 0
35         for x in range(bounds[0], bounds[2]):
36                 for y in range(bounds[1], bounds[3]):
37                         if PixelGood(pix[x, y], threshold):
38                                 if (x - xLast) > seperation:
39                                         if ((xEnd - xStart) * (yEnd - yStart) > min_area):
40                                                 rect.append([xStart, yStart, xEnd, yEnd])
41                                                 
42                                         xStart = bounds[2]
43                                         xEnd = bounds[0]
44                                         yStart = bounds[3]
45                                         yEnd = bounds[1]
46                                         good = 0
47
48                                 if (x < xStart):
49                                         xStart = x
50                                 if (y < yStart):
51                                         yStart = y
52                                 if (x > xEnd):
53                                         xEnd = x
54                                 if (y > yEnd):
55                                         yEnd = y
56                                 xLast = x
57                                 good += 1
58
59         rect.append([xStart, yStart, xEnd, yEnd])
60
61         return rect
62
63 def GoodFraction(pix, bounds,threshold=200):
64         good = 0
65         for x in range(int(bounds[0]), int(bounds[2])):
66                 for y in range(int(bounds[1]), int(bounds[3])):
67                         if PixelGood(pix[x, y], threshold):
68                                 good += 1       
69
70         return float(good) / float(int(bounds[2] - bounds[0])*int(bounds[3] - bounds[1]))
71
72 def PickDigit(pix, r):
73
74         # top, mid, bot, ltop, lbot, rtop, rmid
75         d = {
76                 "1111011" : 9,
77                 "1111111" : 8,
78                 "1000011" : 7,
79                 #"1010011" : 7,
80                 "1111101" : 6,
81                 "1111001" : 5,
82                 "0101011" : 4,
83                 "1110011" : 3,
84                 "1110110" : 2,
85                 "0000011" : 1,
86                 "1011111" : 0
87         }
88
89         w = r[2] - r[0]
90         h = r[3] - r[1]
91
92         if (w < 0.4 * h):
93                 return 1
94
95         
96         top = GoodFraction(pix,(r[0]+0.0*w, r[1], r[0]+1.0*w, r[1]+0.15*h)) # Top
97         mid = GoodFraction(pix,(r[0]+0.0*w, r[1]+0.45*h, r[0]+1.0*w, r[1]+0.55*h)) # Middle
98         bot = GoodFraction(pix,(r[0]+0.0*w, r[1]+0.9*h, r[0]+1.0*w, r[1]+h)) # Bottom
99         ltop = GoodFraction(pix,(r[0], r[1]+0.1*h, r[0]+0.4*w, r[1]+0.4*h)) # Left Top
100         lbot = GoodFraction(pix,(r[0], r[1]+0.6*h, r[0]+0.4*w, r[1]+0.9*h)) #Left Bottom
101         rtop = GoodFraction(pix,(r[0]+0.6*w, r[1]+0.1*h,r[0]+w, r[1]+0.4*h)) # Right Top
102         rbot = GoodFraction(pix,(r[0]+0.6*w, r[1]+0.6*h, r[0]+w, r[1]+0.9*h)) # Right Bottom
103
104         filled = ""
105         
106         fraction = 0.4
107         for f in [top,mid,bot,ltop,lbot,rtop,rbot]:
108                 if f > fraction:
109                         filled += "1"
110                 else:
111                         filled += "0"
112         
113         
114
115         
116                 #Check where the boxes are
117         """
118         if (filled in d) == False:
119                 img = Image.new("RGB", (r[2]-r[0], r[3]-r[1]), "white")
120                 draw = ImageDraw.Draw(img)
121                 for x in range(r[0], r[2]):
122                         for y in range(r[1], r[3]):
123                                 draw.rectangle((x-r[0], y-r[1], x-r[0], y-r[1]), fill=pix[x, y], outline=None)
124
125                 boxes = []
126                 boxes.append((0, 0, w, 0.15*h)) # Top
127                 boxes.append((0, 0.45*h, w, 0.55*h)) # Middle
128                 boxes.append((0, 0.9*h, w, h)) # Bottom
129                 boxes.append((0, 0, 0.4*w, 0.5*h)) # Left Top
130                 boxes.append((0, 0.5*h, 0.4*w, h)) # Left Bottom
131                 boxes.append((0.6*w, 0, w, 0.5*h)) # Right Top
132                 boxes.append((0.6*w, 0.5*h, w, h)) # Right Bottom
133                 for box in boxes:
134                         draw.rectangle(box, fill=None, outline="blue")
135                 img.show()
136                 #return 0
137         """
138
139         if filled in d:
140                 return d[filled]
141         else:
142                 return None
143 def Process(fileName):
144         
145         results = []
146         image = Image.open(fileName)
147         pix = image.load()              
148         rect = SubDivide(pix, (0, 0, image.size[0], image.size[1]))                     
149         #print "Found " + str(len(rect)) + " rectangles"
150         #test = Image.new("RGB", image.size, "white")
151         #test.paste(image)
152         #draw1 = ImageDraw.Draw(test)
153         #for i in range(0, len(rect)):
154         #       draw1.rectangle(rect[i], fill=None, outline="red")
155                 
156
157                 #img = Image.new("RGB", (rect[i][2]-rect[i][0], rect[i][3]-rect[i][1]), "white")
158                 #draw2 = ImageDraw.Draw(img)
159                 #for x in range(rect[i][0], rect[i][2]):
160                 #       for y in range(rect[i][1], rect[i][3]):
161                 #               draw2.rectangle((x-rect[i][0], y-rect[i][1], x-rect[i][0], y-rect[i][1]), fill=pix[x, y], outline=None)
162                 #img.show()
163
164         #test.show()
165         
166         for r in rect:
167                 digit = PickDigit(pix, r)
168                 if digit != None:
169                         results.append(digit)
170                 else:
171                         return None
172         return results
173
174 def PixToList(pix, bounds):
175         a = []
176         for x in range(bounds[0], bounds[2]):
177                 a.append([])
178
179         for x in range(bounds[0], bounds[2]):
180                 for y in range(bounds[1], bounds[3]):
181                         
182                         a[x - bounds[0]].append(GreyScale(pix[x, y]))
183                         #print(str(x) + "," + str(y), len(fft[x - bounds[0]]))
184                         #print(str(fft[x - bounds[0]][y - bounds[1]]))
185         return a
186
187         
188 if __name__ == "__main__":
189         
190         if (len(sys.argv) != 2):
191                 print("Usage " + str(sys.argv[0]) + " input_image")
192
193         
194
195                 
196         #sys.exit(0)
197         r = Process(sys.argv[1])
198         if r == None:
199                 print("?")
200                 sys.exit(1)
201         
202         print(str(r[0]) + "." + str(r[1]) + str(r[2]) + "e-"  + str(r[3]))
203         sys.exit(0)
204
205
206

UCC git Repository :: git.ucc.asn.au