forked from neeru1207/AI_Sudoku
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoardExtractor.py
410 lines (352 loc) · 15.8 KB
/
BoardExtractor.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'''This class takes an image path as input, performs preprocessing, identifies the
grid, crops the grid, corrects perspective, writes all these stages to StagesImages folder and
finally slices the grid into 81 cells and returns the 2D array of 81 cell images'''
import cv2
import os
import numpy as np
class BoardExtractor:
'''Initializes the Class'''
def __init__(self, imagepath):
self.image = cv2.imread(imagepath, 0)
self.originalimage = np.copy(self.image)
self.extractedgrid = None
'''This function blurs the image, applies thresholding, inverts it and dilates the image'''
def preprocess_image(self):
gray = self.image
#Applying Gaussian Blur to smooth out the noise
gray = cv2.GaussianBlur(gray, (11, 11), 0)
try:
os.remove("StagesImages/1.jpg")
except:
pass
cv2.imwrite("StagesImages/1.jpg", gray)
# Applying thresholding using adaptive Gaussian|Mean thresholding
gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C | cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 2)
try:
os.remove("StagesImages/2.jpg")
except:
pass
cv2.imwrite("StagesImages/2.jpg", gray)
#Inverting the image
gray = cv2.bitwise_not(gray)
try:
os.remove("StagesImages/3.jpg")
except:
pass
cv2.imwrite("StagesImages/3.jpg", gray)
#Dilating the image to fill up the "cracks" in lines
kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], np.uint8)
gray = cv2.dilate(gray, kernel)
self.image = gray
try:
os.remove("StagesImages/4.jpg")
except:
pass
cv2.imwrite("StagesImages/4.jpg", gray)
'''This function finds the grid (the biggest blob), uses Hough transform to find lines,
fuses related lines, finds the border lines of the grid, warps the board to correct
perspective and stores the board in self.extractedgrid'''
def detect_and_crop_grid(self):
#Using flood filling to find the biggest blob in the picture
outerbox = self.image
maxi = -1
maxpt = None
value = 10
try:
os.remove("StagesImages/5.jpg")
except:
pass
height, width = np.shape(outerbox)
for y in range(height):
row = self.image[y]
for x in range(width):
if row[x] >= 128:
area = cv2.floodFill(outerbox, None, (x, y), 64)[0]
if value > 0:
cv2.imwrite("StagesImages/5.jpg", outerbox)
value -= 1
if area > maxi:
maxpt = (x, y)
maxi = area
# Floodfill the biggest blob with white (Our sudoku board's outer grid)
cv2.floodFill(outerbox, None, maxpt, (255, 255, 255))
# Floodfill the other blobs with black
for y in range(height):
row = self.image[y]
for x in range(width):
if row[x] == 64 and x != maxpt[0] and y != maxpt[1]:
cv2.floodFill(outerbox, None, (x, y), 0)
try:
os.remove("StagesImages/6.jpg")
except:
pass
cv2.imwrite("StagesImages/6.jpg", outerbox)
# Eroding it a bit to restore the image
kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], np.uint8)
outerbox = cv2.erode(outerbox, kernel)
try:
os.remove("StagesImages/7.jpg")
except:
pass
cv2.imwrite("StagesImages/7.jpg", outerbox)
# Using "Hough Transform" to detect lines
lines = cv2.HoughLines(outerbox, 1, np.pi / 180, 200)
'''This function takes a line in it's normal form and draws it on an image'''
def drawLine(line, img):
height, width = np.shape(img)
if line[0][1] != 0:
m = -1 / np.tan(line[0][1])
c = line[0][0] / np.sin(line[0][1])
cv2.line(img, (0, int(c)), (width, int(m * width + c)), 255)
else:
cv2.line(img, (line[0][0], 0), (line[0][0], height), 255)
return img
# Draw and display all the lines
tmpimg = np.copy(outerbox)
for i in range(len(lines)):
tmpimp = drawLine(lines[i], tmpimg)
try:
os.remove("StagesImages/8.jpg")
except:
pass
cv2.imwrite("StagesImages/8.jpg", tmpimg)
'''This function takes a list of lines and an image, fuses related a.k.a close
lines and returns the modified list of lines'''
def mergeLines(lines, img):
height, width = np.shape(img)
for current in lines:
if current[0][0] is None and current[0][1] is None:
continue
p1 = current[0][0]
theta1 = current[0][1]
pt1current = [None, None]
pt2current = [None, None]
#If the line is almost horizontal
if theta1 > np.pi * 45 / 180 and theta1 < np.pi * 135 / 180:
pt1current[0] = 0
pt1current[1] = p1 / np.sin(theta1)
pt2current[0] = width
pt2current[1] = -pt2current[0] / np.tan(theta1) + p1 / np.sin(theta1)
#If the line is almost vertical
else:
pt1current[1] = 0
pt1current[0] = p1 / np.cos(theta1)
pt2current[1] = height
pt2current[0] = -pt2current[1] * np.tan(theta1) + p1 / np.cos(theta1)
#Now to fuse lines
for pos in lines:
if pos[0].all() == current[0].all():
continue
if abs(pos[0][0] - current[0][0]) < 20 and abs(pos[0][1] - current[0][1]) < np.pi * 10 / 180:
p = pos[0][0]
theta = pos[0][1]
pt1 = [None, None]
pt2 = [None, None]
# If the line is almost horizontal
if theta > np.pi * 45 / 180 and theta < np.pi * 135 / 180:
pt1[0] = 0
pt1[1] = p / np.sin(theta)
pt2[0] = width
pt2[1] = -pt2[0] / np.tan(theta) + p / np.sin(theta)
# If the line is almost vertical
else:
pt1[1] = 0
pt1[0] = p / np.cos(theta)
pt2[1] = height
pt2[0] = -pt2[1] * np.tan(theta) + p / np.cos(theta)
#If the endpoints are close to each other, merge the lines
if (pt1[0] - pt1current[0])**2 + (pt1[1] - pt1current[1])**2 < 64**2 and (pt2[0] - pt2current[0])**2 + (pt2[1] - pt2current[1])**2 < 64**2:
current[0][0] = (current[0][0] + pos[0][0]) / 2
current[0][1] = (current[0][1] + pos[0][1]) / 2
pos[0][0] = None
pos[0][1] = None
#Now to remove the "None" Lines
lines = list(filter(lambda a : a[0][0] is not None and a[0][1] is not None, lines))
return lines
#Call the Merge Lines function and store the fused lines
lines = mergeLines(lines, outerbox)
#Now to find the extreme lines (The approximate borders of our sudoku board
topedge = [[1000, 1000]]
bottomedge = [[-1000, -1000]]
leftedge = [[1000, 1000]]
leftxintercept = 100000
rightedge = [[-1000, -1000]]
rightxintercept = 0
for i in range(len(lines)):
current = lines[i][0]
p = current[0]
theta = current[1]
xIntercept = p / np.cos(theta)
#If the line is nearly vertical
if theta > np.pi * 80 / 180 and theta < np.pi * 100 / 180:
if p < topedge[0][0]:
topedge[0] = current[:]
if p > bottomedge[0][0]:
bottomedge[0] = current[:]
#If the line is nearly horizontal
if theta < np.pi * 10 / 180 or theta > np.pi * 170 / 180:
if xIntercept > rightxintercept:
rightedge[0] = current[:]
rightxintercept = xIntercept
elif xIntercept <= leftxintercept:
leftedge[0] = current[:]
leftxintercept = xIntercept
#Drawing the lines
tmpimg= np.copy(outerbox)
tmppp = np.copy(self.originalimage)
tmppp = drawLine(leftedge, tmppp)
tmppp = drawLine(rightedge, tmppp)
tmppp = drawLine(topedge, tmppp)
tmppp = drawLine(bottomedge, tmppp)
tmpimg = drawLine(leftedge, tmpimg)
tmpimg = drawLine(rightedge, tmpimg)
tmpimg = drawLine(topedge, tmpimg)
tmpimg = drawLine(bottomedge, tmpimg)
try:
os.remove("StagesImages/9.jpg")
except:
pass
cv2.imwrite("StagesImages/9.jpg", tmpimg)
leftedge = leftedge[0]
rightedge = rightedge[0]
bottomedge = bottomedge[0]
topedge = topedge[0]
# Calculating two points that lie on each of the four lines
left1 = [None, None]
left2 = [None, None]
right1 = [None, None]
right2 = [None, None]
top1 = [None, None]
top2 = [None, None]
bottom1 = [None, None]
bottom2 = [None, None]
if leftedge[1] != 0:
left1[0] = 0
left1[1] = leftedge[0] / np.sin(leftedge[1])
left2[0] = width
left2[1] = -left2[0] / np.tan(leftedge[1]) + left1[1]
else:
left1[1] = 0
left1[0] = leftedge[0] / np.cos(leftedge[1])
left2[1] = height
left2[0] = left1[0] - height * np.tan(leftedge[1])
if rightedge[1] != 0:
right1[0] = 0
right1[1] = rightedge[0] / np.sin(rightedge[1])
right2[0] = width
right2[1] = -right2[0] / np.tan(rightedge[1]) + right1[1]
else:
right1[1] = 0
right1[0] = rightedge[0] / np.cos(rightedge[1])
right2[1] = height
right2[0] = right1[0] - height * np.tan(rightedge[1])
bottom1[0] = 0
bottom1[1] = bottomedge[0] / np.sin(bottomedge[1])
bottom2[0] = width
bottom2[1] = -bottom2[0] / np.tan(bottomedge[1]) + bottom1[1]
top1[0] = 0
top1[1] = topedge[0] / np.sin(topedge[1])
top2[0] = width
top2[1] = -top2[0] / np.tan(topedge[1]) + top1[1]
# Next, we find the intersection of these four lines
leftA = left2[1] - left1[1]
leftB = left1[0] - left2[0]
leftC = leftA * left1[0] + leftB * left1[1]
rightA = right2[1] - right1[1]
rightB = right1[0] - right2[0]
rightC = rightA * right1[0] + rightB * right1[1]
topA = top2[1] - top1[1]
topB = top1[0] - top2[0]
topC = topA * top1[0] + topB * top1[1]
bottomA = bottom2[1] - bottom1[1]
bottomB = bottom1[0] - bottom2[0]
bottomC = bottomA * bottom1[0] + bottomB * bottom1[1]
# Intersection of left and top
detTopLeft = leftA * topB - leftB * topA
ptTopLeft = ((topB * leftC - leftB * topC) / detTopLeft, (leftA * topC - topA * leftC) / detTopLeft)
# Intersection of top and right
detTopRight = rightA * topB - rightB * topA
ptTopRight = ((topB * rightC - rightB * topC) / detTopRight, (rightA * topC - topA * rightC) / detTopRight)
# Intersection of right and bottom
detBottomRight = rightA * bottomB - rightB * bottomA
ptBottomRight = ((bottomB * rightC - rightB * bottomC) / detBottomRight, (rightA * bottomC - bottomA * rightC) / detBottomRight)
# Intersection of bottom and left
detBottomLeft = leftA * bottomB - leftB * bottomA
ptBottomLeft = ((bottomB * leftC - leftB * bottomC) / detBottomLeft,
(leftA * bottomC - bottomA * leftC) / detBottomLeft)
# Plotting the found extreme points
cv2.circle(tmppp, (int(ptTopLeft[0]), int(ptTopLeft[1])), 5, 0, -1)
cv2.circle(tmppp, (int(ptTopRight[0]), int(ptTopRight[1])), 5, 0, -1)
cv2.circle(tmppp, (int(ptBottomLeft[0]), int(ptBottomLeft[1])), 5, 0, -1)
cv2.circle(tmppp, (int(ptBottomRight[0]), int(ptBottomRight[1])), 5, 0, -1)
try:
os.remove("StagesImages/10.jpg")
except:
pass
cv2.imwrite("StagesImages/10.jpg", tmppp)
#Finding the maximum length side
leftedgelensq = (ptBottomLeft[0] - ptTopLeft[0])**2 + (ptBottomLeft[1] - ptTopLeft[1])**2
rightedgelensq = (ptBottomRight[0] - ptTopRight[0])**2 + (ptBottomRight[1] - ptTopRight[1])**2
topedgelensq = (ptTopRight[0] - ptTopLeft[0])**2 + (ptTopLeft[1] - ptTopRight[1])**2
bottomedgelensq = (ptBottomRight[0] - ptBottomLeft[0])**2 + (ptBottomLeft[1] - ptBottomRight[1])**2
maxlength = int(max(leftedgelensq, rightedgelensq, bottomedgelensq, topedgelensq)**0.5)
#Correcting the skewed perspective
src = [(0, 0)] * 4
dst = [(0, 0)] * 4
src[0] = ptTopLeft[:]
dst[0] = (0, 0)
src[1] = ptTopRight[:]
dst[1] = (maxlength - 1, 0)
src[2] = ptBottomRight[:]
dst[2] = (maxlength - 1, maxlength - 1)
src[3] = ptBottomLeft[:]
dst[3] = (0, maxlength - 1)
src = np.array(src).astype(np.float32)
dst = np.array(dst).astype(np.float32)
self.extractedgrid = cv2.warpPerspective(self.originalimage, cv2.getPerspectiveTransform(src, dst), (maxlength, maxlength))
try:
os.remove("StagesImages/11.jpg")
except:
pass
cv2.imwrite("StagesImages/11.jpg", self.extractedgrid)
# Resizing the grid to a 252X252 size because MNIST has 28X28 images
self.extractedgrid = cv2.resize(self.extractedgrid, (252, 252))
'''This function thresholds, inverts, slices the grid into 81 pieces and returns the 2D array
of cell images'''
def create_image_grid(self):
if self.extractedgrid is None:
raise Exception("Grid not yet extracted")
grid = np.copy(self.extractedgrid)
edge = np.shape(grid)[0]
celledge = edge // 9
#Adaptive thresholding the cropped grid and inverting it
grid = cv2.bitwise_not(cv2.adaptiveThreshold(grid, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 101, 1))
try:
os.remove("StagesImages/12.jpg")
except:
pass
cv2.imwrite("StagesImages/12.jpg", grid)
#Creating a vector of size 81 of all the cell images
tempgrid = []
for i in range(celledge, edge+1, celledge):
for j in range(celledge, edge+1, celledge):
rows = grid[i-celledge:i]
tempgrid.append([rows[k][j-celledge:j] for k in range(len(rows))])
#Creating the 9X9 grid of images
finalgrid = []
for i in range(0, len(tempgrid)-8, 9):
finalgrid.append(tempgrid[i:i+9])
#Converting all the cell images to np.array
for i in range(9):
for j in range(9):
finalgrid[i][j] = np.array(finalgrid[i][j])
try:
for i in range(9):
for j in range(9):
os.remove("BoardCells/cell"+str(i)+str(j)+".jpg")
except:
pass
for i in range(9):
for j in range(9):
cv2.imwrite(str("BoardCells/cell"+str(i)+str(j)+".jpg"), finalgrid[i][j])
return finalgrid