/
grid.go
305 lines (248 loc) · 7.04 KB
/
grid.go
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
package sudoku
import (
"fmt"
"image"
"math"
"sort"
"github.com/mrfuxi/sudoku/digits"
)
type lineGrid struct {
Horizontal []polarLine
Vertical []polarLine
Score float64
}
type lineGridByScore []lineGrid
func (a lineGridByScore) Len() int { return len(a) }
func (a lineGridByScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a lineGridByScore) Less(i, j int) bool { return a[i].Score > a[j].Score } // Reversed order most to least
type scoredLines struct {
Lines []polarLine
Score float64
}
type scoredLinesByScore []scoredLines
func (a scoredLinesByScore) Len() int { return len(a) }
func (a scoredLinesByScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a scoredLinesByScore) Less(i, j int) bool { return a[i].Score > a[j].Score } // Reversed order most to least
func (s *scoredLines) HashKey() string {
return polarLineHash(s.Lines).HashKey()
}
type meanAcc struct {
values []float64
}
func (m *meanAcc) Add(value float64) {
m.values = append(m.values, value)
}
func (m *meanAcc) Mean() float64 {
res := 0.0
count := float64(len(m.values))
for _, v := range m.values {
res += v
}
return res / count
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
// Builds possible line grouppings by using muiltple "cutting" lines
func buildScoredLines(primary, secondary []polarLine, top uint) []scoredLines {
lines := make(map[string]scoredLines, 0)
scores := make(map[string]*meanAcc, 0)
for _, s := range secondary {
matches := linearDistances(primary, s)
for _, match := range matches {
hash := match.HashKey()
if scores[hash] == nil {
scores[hash] = new(meanAcc)
lines[hash] = match
}
scores[hash].Add(match.Score)
}
}
scoredLn := make([]scoredLines, len(lines), len(lines))
s := 0
for hash, scoredLine := range lines {
scoredLine.Score = scores[hash].Mean()
scoredLn[s] = scoredLine
s++
}
sort.Sort(scoredLinesByScore(scoredLn))
return scoredLn[:minInt(int(top), len(scoredLn))]
}
func possibleGrids(horizontal, vertical []polarLine) []lineGrid {
// Make sure lines are ordered correctly
sort.Sort(polarLinesByDistance(vertical))
sort.Sort(polarLinesByDistance(horizontal))
linesH := buildScoredLines(horizontal, vertical, 3)
linesV := buildScoredLines(vertical, horizontal, 3)
var grids []lineGrid
for _, h := range linesH {
for _, v := range linesV {
grid := lineGrid{
Horizontal: h.Lines,
Vertical: v.Lines,
Score: h.Score * v.Score,
}
grids = append(grids, grid)
}
}
sort.Sort(lineGridByScore(grids))
return grids
}
func evaluateGrids(src image.Gray, grids []lineGrid) []lineGrid {
for _, grid := range grids {
hCount := len(grid.Horizontal)
vCount := len(grid.Vertical)
fragments := make([]lineFragment, hCount+vCount)
firstVertLine := grid.Vertical[0]
lastVertLine := grid.Vertical[vCount-1]
for j, h := range grid.Horizontal {
_, start := intersection(h, firstVertLine)
_, end := intersection(h, lastVertLine)
fragments[j] = lineFragment{start, end}
}
firstHorizLine := grid.Horizontal[0]
lastHorizLine := grid.Horizontal[hCount-1]
for j, h := range grid.Vertical {
_, start := intersection(h, firstHorizLine)
_, end := intersection(h, lastHorizLine)
fragments[hCount+j] = lineFragment{start, end}
}
score := 0.0
for _, fragment := range fragments {
points := pointsOnLineFragment(fragment)
value := 1.0 / fragment.Length()
for _, point := range points {
if src.Pix[src.PixOffset(point.X, point.Y)] != 0 {
score += value
}
}
}
grid.Score = grid.Score * score / float64(len(fragments))
}
sort.Sort(lineGridByScore(grids))
return grids
}
// Splits lines into groups of 10 with score of how much linearly distributed they are
func linearDistances(lines []polarLine, dividerLine polarLine) []scoredLines {
// Lines have to be sorted correctly!
var matches []scoredLines
linesCount := len(lines)
if linesCount < 10 {
return matches
}
intersections := make([]image.Point, linesCount, linesCount)
for i, line := range lines {
_, point := intersection(line, dividerLine)
intersections[i] = point
}
points := make([]float64, len(lines), len(lines))
for i, point := range intersections {
points[i] = distanceBetweenPoints(intersections[0], point)
}
distances := preparePointDistances(points)
expectedPoints := make([]float64, 10, 10)
for i := range points[:linesCount-10+1] {
dI := i + 10 - 1
for j := range points[dI:] {
start, end := points[i], points[j+dI]
step := (end - start) / 9.0
for k := range expectedPoints {
expectedPoints[k] = start + step*float64(k)
}
score, selectedPoints := pointSimilarities(expectedPoints, distances)
if len(selectedPoints) != 10 {
continue
}
match := scoredLines{
Score: score,
Lines: make([]polarLine, 10, 10),
}
searchablePoints := sort.Float64Slice(points)
for l := range match.Lines {
match.Lines[l] = lines[searchablePoints.Search(selectedPoints[l])]
}
matches = append(matches, match)
}
}
return matches
}
func preparePointDistances(positions []float64) []float64 {
maxPos := positions[len(positions)-1]
ld := int(maxPos) + 1
closest := make([]float64, ld, ld)
posI := 0
for c := range closest {
d1 := math.Abs(positions[posI] - float64(c))
d2 := float64(ld)
if posI+1 < len(positions) {
d2 = math.Abs(positions[posI+1] - float64(c))
}
if d1 <= d2 {
closest[c] = positions[posI]
} else {
closest[c] = positions[posI+1]
posI++
}
}
return closest
}
func pointSimilarities(expectedPoints, distances []float64) (float64, []float64) {
fit := 0.0
var matches []float64
step := expectedPoints[1] - expectedPoints[0]
for _, expected := range expectedPoints {
point := distances[int(expected)]
if len(matches) > 0 {
f := math.Abs(math.Abs(point-matches[len(matches)-1])-step) / step
if f >= 0.2 {
break
}
fit += f / 9.0
}
matches = append(matches, point)
}
return (1 - fit), matches
}
func extractCells(grid lineGrid, img image.Image) (cells [9][9]image.Gray) {
grayImg := grayImage(img)
margin := 0
size := 28.0 // Size of learning data set: MNIST
dst := [4]pointF{
pointF{0, 0},
pointF{size, 0},
pointF{size, size},
pointF{0, size},
}
for row := 0; row < 9; row++ {
for col := 0; col < 9; col++ {
_, p1 := intersection(grid.Horizontal[row], grid.Vertical[col])
_, p2 := intersection(grid.Horizontal[row], grid.Vertical[col+1])
_, p3 := intersection(grid.Horizontal[row+1], grid.Vertical[col+1])
_, p4 := intersection(grid.Horizontal[row+1], grid.Vertical[col])
p1.Y -= margin
p1.X -= margin
p2.Y -= margin
p2.X += margin
p3.X += margin
p3.Y += margin
p4.X -= margin
p4.Y += margin
src := [4]pointF{
newPointF(p1),
newPointF(p2),
newPointF(p3),
newPointF(p4),
}
proj := newPerspective(src, dst)
cells[row][col] = proj.warpPerspective(grayImg)
cell := cells[row][col]
digit, conf := digits.RecogniseDigit(cell, otsuValue(cell))
fn := fmt.Sprintf("%v_%v-%v-%.2f.png", row, col, digit, conf)
saveImage(&cells[row][col], fn)
}
}
return
}