-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle_utils.py
186 lines (113 loc) · 4.06 KB
/
puzzle_utils.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
#!/usr/bin/env python
# coding: utf-8
# In[8]:
import numpy as np
import cv2
from skimage.segmentation import clear_border
import matplotlib.pyplot as plt
# In[2]:
def order_points(pts):
rect = np.zeros((4, 2), dtype = "float32")
s = pts.sum(axis = 1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
diff = np.diff(pts, axis = 1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
return rect
# In[3]:
def four_point_transform(image, pts):
rect = order_points(pts)
(tl, tr, br, bl) = rect
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
return warped
# In[31]:
def find_puzzle(image,debug=False):
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blurred=cv2.GaussianBlur(gray,(7,7),3)
thresh=cv2.adaptiveThreshold(blurred,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
thresh=cv2.bitwise_not(thresh)
if debug:
plt.figure(figsize=(18,18))
plt.subplot(221)
plt.axis('off')
plt.title("Thresholded image")
plt.imshow(thresh,cmap='gray')
cnts,_=cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts=sorted(cnts,key=cv2.contourArea,reverse=True)
puzzleCnt=None
for c in cnts:
peri=cv2.arcLength(c,True)
approx=cv2.approxPolyDP(c,0.02*peri,True)
if len(approx)==4:
puzzleCnt=approx
break
if puzzleCnt is None:
raise Exception("Could not find the puzzle outline. Try debugging the threshold and contour steps")
if debug:
output=image.copy()
cv2.drawContours(output,[puzzleCnt],-1,(0,255,0),2)
plt.subplot(222)
plt.axis('off')
plt.title("Puzzle Outline")
plt.imshow(output,cmap='gray')
puzzle = four_point_transform(image, puzzleCnt.reshape(4, 2))
warped = four_point_transform(gray, puzzleCnt.reshape(4, 2))
if debug:
# show the output warped image (again, for debugging purposes)
plt.subplot(223)
plt.axis('off')
plt.title("orignal warped")
plt.imshow(puzzle,cmap='gray')
plt.subplot(224)
plt.axis('off')
plt.title("gray warped")
plt.imshow(warped,cmap='gray')
return (puzzle, warped)
# In[32]:
image=cv2.imread('sudoku_puzzle.jpg')
a=find_puzzle(image,True)
# In[36]:
def extract_digit(cell,debug=False):
thresh=cv2.threshold(cell,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
thresh=clear_border(thresh)
if debug:
plt.figure(figsize=(12,12))
plt.subplot(121)
plt.title("Thresholded Image")
plt.axis('off')
plt.imshow(thresh,cmap='gray')
cnts,_ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
if len(cnts)==0:
return None
c=max(cnts,key=cv2.contourArea)
mask=np.zeros(thresh.shape,dtype='uint8')
cv2.drawContours(mask,[c],-1,255,-1)
(h,w)=thresh.shape
percent_filled=cv2.countNonZero(mask)/float(w*h)
if percent_filled<0.03:
return None
digit=cv2.bitwise_and(thresh,thresh,mask=mask)
if debug:
plt.subplot(122)
plt.title("Digit")
plt.axis('off')
plt.imshow(digit,cmap='gray')
return digit
# In[37]:
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blurred=cv2.GaussianBlur(gray,(7,7),3)
b=extract_digit(blurred,True)
# In[ ]: