-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberPlateReader.py
More file actions
365 lines (294 loc) · 11.9 KB
/
NumberPlateReader.py
File metadata and controls
365 lines (294 loc) · 11.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from PIL import Image
import numpy as np
from matplotlib import pyplot
from matplotlib.patches import Rectangle
import Utils
def readImage(path: str) -> np.ndarray:
image = Image.open(path)
image = image.convert("RGB")
return np.array(image)
def showImage(image: np.ndarray, boxes=None) -> None:
fig, axs = pyplot.subplots(1, 1)
axs.imshow(image, aspect="equal")
if boxes is not None:
for bounding_box in boxes:
bbox_min_x = bounding_box[0]
bbox_min_y = bounding_box[1]
bbox_max_x = bounding_box[2]
bbox_max_y = bounding_box[3]
bbox_xy = (bbox_min_x, bbox_min_y)
bbox_width = bbox_max_x - bbox_min_x
bbox_height = bbox_max_y - bbox_min_y
rect = Rectangle(
bbox_xy,
bbox_width,
bbox_height,
linewidth=2,
edgecolor="r",
facecolor="none",
)
axs.add_patch(rect)
pyplot.tight_layout()
pyplot.imshow(image, cmap="gray", aspect="equal")
pyplot.show()
def createCanvas(height: int, width: int) -> np.ndarray:
return np.zeros((height, width), dtype=np.uint8)
def rgbToGreyscale(image: np.ndarray) -> np.ndarray:
height, width = image.shape[:2]
greyscale = createCanvas(height, width)
for h in range(height):
for w in range(width):
greyscale[h, w] = image[h, w, 0] * 0.299 + \
image[h, w, 1] * 0.587 + image[h, w, 2] * 0.114
return greyscale
# return np.dot(image[..., :3], [0.299, 0.587, 0.114])
def stretchContrast(image: np.ndarray) -> np.ndarray:
height, width = image.shape[:2]
stretched = createCanvas(height, width)
min_value = np.min(image)
max_value = np.max(image)
for h in range(height):
for w in range(width):
stretched[h, w] = (image[h, w] - min_value) * \
255 / (max_value - min_value)
return stretched
def meanFilter(image: np.ndarray) -> np.ndarray:
height, width = image.shape[:2]
filtered = createCanvas(height, width)
WINDOW_SIZE = 3
window_half = WINDOW_SIZE // 2
for row in range(window_half, height - window_half):
for col in range(window_half, width - window_half):
result = 0
for i in range(-window_half, window_half+1):
for j in range(-window_half, window_half+1):
result += image[row + i, col + j]
filtered[row, col] = abs(float(result / WINDOW_SIZE**2))
return filtered
def simpleThreshold(image: np.ndarray, theta: int) -> np.ndarray:
height, width = image.shape[:2]
thresh = createCanvas(height, width)
for row in range(height):
for col in range(width):
if image[row, col] < theta:
thresh[row, col] = 255
else:
thresh[row, col] = 0
return thresh
def adaptiveThreshold(image: np.ndarray) -> np.ndarray:
# Get the image dimensions
height, width = image.shape
# Create the histogram (using numpy)
hist, bins = np.histogram(image.flatten(), bins=256, range=(0, 256))
# Calculate N and theta0
N = hist.cumsum()[-1] # Total number of pixels
theta = np.sum(np.arange(256) * hist) / N # Initial theta
# Calculate Nob and Nbg
Nob = np.sum(hist[:round(theta)])
Nbg = np.sum(hist[round(theta):])
# Calculate uob and ubg
uob = np.sum(np.arange(round(theta)) *
hist[:round(theta)]) / Nob if Nob > 0 else 0
ubg = np.sum(np.arange(round(theta), 256) *
hist[round(theta):]) / Nbg if Nbg > 0 else 0
# Find theta j+1
thetaNext = (uob + ubg) / 2
# Iteration loop until convergence
while abs(theta - thetaNext) > 1e-5: # Set a small tolerance for convergence
theta = thetaNext
Nob = np.sum(hist[:round(theta)])
Nbg = np.sum(hist[round(theta):])
uob = np.sum(np.arange(round(theta)) *
hist[:round(theta)]) / Nob if Nob > 0 else 0
ubg = np.sum(np.arange(round(theta), 256) *
hist[round(theta):]) / Nbg if Nbg > 0 else 0
thetaNext = (uob + ubg) / 2
# Apply the threshold using the calculated theta
thresh = simpleThreshold(image, theta)
return thresh
def dilation(image: np.ndarray) -> np.ndarray:
global kernel
height, width = image.shape
k = np.array(kernel)
kernel_half = len(k) // 2
# Create a padded version of the image to handle borders
padded_image = np.pad(image, kernel_half,
mode='constant', constant_values=0)
result = np.zeros_like(image)
# Apply the kernel on each pixel
for i in range(-kernel_half, kernel_half + 1):
for j in range(-kernel_half, kernel_half + 1):
if k[i + kernel_half, j + kernel_half] == 1:
# Shift the padded image and apply maximum where kernel is 1
result = np.maximum(
result, padded_image[kernel_half + i:kernel_half + i + height, kernel_half + j:kernel_half + j + width])
return result
def erosion(image: np.ndarray) -> np.ndarray:
global kernel
height, width = image.shape
k = np.array(kernel)
kernel_half = len(k) // 2
# Create a padded version of the image to handle borders
padded_image = np.pad(image, kernel_half,
mode='constant', constant_values=255)
result = np.full_like(image, 255)
# Apply the kernel on each pixel
for i in range(-kernel_half, kernel_half + 1):
for j in range(-kernel_half, kernel_half + 1):
if k[i + kernel_half, j + kernel_half] == 1:
# Shift the padded image and apply minimum where kernel is 1
result = np.minimum(
result, padded_image[kernel_half + i:kernel_half + i + height, kernel_half + j:kernel_half + j + width])
return result
def opening(image: np.ndarray) -> np.ndarray:
return dilation(erosion(image))
def closing(image: np.ndarray) -> np.ndarray:
return erosion(dilation(image))
def connectedComponents(image: np.ndarray) -> list:
components = []
height, width = image.shape[:2]
visited = createCanvas(height, width)
queue = []
for row in range(height):
for col in range(width):
if not visited[row, col] and image[row, col] != 0:
queue.append((row, col))
visited[row, col] = 1
min_x, min_y, max_x, max_y = col, row, 0, 0
# BFS, enqueue if pixel not 0 and not visited
while queue:
row, col = queue.pop(0)
min_x = min(min_x, col)
min_y = min(min_y, row)
max_x = max(max_x, col)
max_y = max(max_y, row)
# Left
try:
if visited[row, col-1] == 0 and image[row, col-1] != 0:
queue.append((row, col-1))
visited[row, col-1] = 1
except IndexError:
pass
# Right
try:
if visited[row, col+1] == 0 and image[row, col+1] != 0:
queue.append((row, col+1))
visited[row, col+1] = 1
except IndexError:
pass
# Up
try:
if visited[row-1, col] == 0 and image[row-1, col] != 0:
queue.append((row-1, col))
visited[row-1, col] = 1
except IndexError:
pass
# Down
try:
if visited[row+1, col] == 0 and image[row+1, col] != 0:
queue.append((row+1, col))
visited[row+1, col] = 1
except IndexError:
pass
diameter_x = max_x-min_x
diameter_y = max_y-min_y
# Minimum size threshold to filter out little noise
threshold_size = height//10
if diameter_x < threshold_size and diameter_y < threshold_size:
pass
else:
components.append((min_x, min_y, max_x, max_y))
components.sort(key=lambda x: x[0]) # Order boxes by min_x
return components
def getComponents(image: np.ndarray, boxes: list) -> list:
components = []
for box in boxes:
min_x, min_y, max_x, max_y = box
component = image[min_y:max_y, min_x:max_x]
if len(component) != 0:
components.append(component)
return components
def initDB():
image = readImage("letters.png")
image = rgbToGreyscale(image)
image = stretchContrast(image)
image = meanFilter(image)
image = adaptiveThreshold(image)
image = dilation(image)
image = erosion(image)
boxes = connectedComponents(image)
# boxes.sort(key=lambda x: x[0]) # Order boxes by min_x
components = getComponents(image, boxes)
for component in components:
bitmap = "["
for i in range(len(component)):
for j in range(len(component[i])):
if j == 0:
bitmap += "["
bitmap += "1" if component[i][j] == 255 else "0"
if j < len(component[i]) - 1:
bitmap += ", "
else:
bitmap += "]"
if i < len(component) - 1:
bitmap += ","
bitmap += "\n"
with open("bitmap.txt", "a") as f:
f.write(bitmap + "]")
f.write("\n")
def resizeImage(bitmap: np.ndarray, new_height: int, new_width: int) -> np.ndarray:
old_height, old_width = bitmap.shape
resized_bitmap = np.zeros((new_height, new_width), dtype=bitmap.dtype)
for i in range(new_height):
for j in range(new_width):
old_i = int(i * old_height / new_height)
old_j = int(j * old_width / new_width)
resized_bitmap[i, j] = bitmap[old_i, old_j]
return resized_bitmap
def computeSSIM(bitmap1: np.ndarray, bitmap2: np.ndarray) -> float:
mean1, mean2 = np.mean(bitmap1), np.mean(bitmap2)
var1, var2 = np.var(bitmap1), np.var(bitmap2)
cov = np.mean((bitmap1 - mean1) * (bitmap2 - mean2))
c1, c2 = 0.01**2, 0.03**2
return ((2 * mean1 * mean2 + c1) * (2 * cov + c2)) / ((mean1**2 + mean2**2 + c1) * (var1 + var2 + c2))
def compareImages(bitmap1: np.ndarray, bitmap2: list) -> float:
bitmap2 = np.asarray(bitmap2, dtype=np.uint8)
resized_bitmap2 = resizeImage(
bitmap2, bitmap1.shape[0], bitmap1.shape[1])
return computeSSIM(bitmap1, resized_bitmap2)
def matchLetter(component: np.ndarray) -> list:
global templates
results = []
for letter, bitmap in templates.items():
matchRate = compareImages(component, bitmap)
results.append((letter, matchRate))
results.sort(key=lambda x: x[1], reverse=True)
most_confident = results[0][1]
new_results = []
for i in range(len(results)):
new_results.append((results[i][0], round(
(results[i][1] - most_confident)/most_confident, 2)))
print(new_results[:3])
return new_results[:3]
if __name__ == "__main__":
# initDB()
global kernel
kernel = [
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0]
]
templates = Utils.TEMPLATES
image = readImage("images/3.jpg")
image = rgbToGreyscale(image)
# image = stretchContrast(image)
image = meanFilter(image)
image = adaptiveThreshold(image)
# image = dilation(image)
# image = erosion(image)
boxes = connectedComponents(image)
components = getComponents(image, boxes)
for component in components:
matchLetter(component)