/
SymbolIdentity.cs
536 lines (469 loc) · 17.2 KB
/
SymbolIdentity.cs
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using HistogramOCRTrainer.FEctra;
using NPCIlin.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
namespace HistogramOCRTrainer
{
public class SymbolIdentity
{
private const char v = '\v';
public static Bitmap WhiteBitmap = new Bitmap(1, 1);
public int HistogramHeight;
public int HistogramWidth;
public List<HistogramLetter> Letters;
/// <summary>
/// initialise with a file, or blank
/// </summary>
/// <param name="HWidth">Width of the h.</param>
/// <param name="HHeight">Height of the h.</param>
public SymbolIdentity(int HWidth = 20, int HHeight = 20)
{
HistogramHeight = HHeight;
HistogramWidth = HWidth;
Letters = new List<HistogramLetter>();
WhiteBitmap.SetPixel(0, 0, Color.White);
}
/// <summary>
/// Serialises the specified filename.
/// </summary>
/// <param name="filename">The filename.</param>
public void Serialise(string filename)
{
var ret = HistogramWidth + v.ToString(CultureInfo.InvariantCulture) + HistogramHeight +
v.ToString(CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
var text = Letters.Aggregate("", (a, b) => a + (b.Serialise() + v.ToString(CultureInfo.InvariantCulture)));
ret += text;
FileExtras.SaveToFile(filename, ret);
}
/// <summary>
/// Des the serialise.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns></returns>
public static SymbolIdentity DeSerialise(string filename)
{
var ret = new SymbolIdentity();
try
{
var text = FileExtras.LoadFile(filename);
var t1 = text.Split(new[] { v }, StringSplitOptions.RemoveEmptyEntries);
var w = int.Parse(t1[0]);
var h = int.Parse(t1[1]);
var letters = new List<HistogramLetter>();
for (var a = 2; a < t1.Count(); a++)
{
var ltext = t1[a];
var l = HistogramLetter.DeSerialise(ltext);
if (letters.Any(s => s.Letter.Equals(l.Letter)) == false)
letters.Add(l);
}
ret.HistogramHeight = h;
ret.HistogramWidth = w;
ret.Letters = letters;
}
catch (ArgumentException)
{
}
return ret;
}
/// <summary>
/// Splits up.
/// </summary>
/// <param name="b">The b.</param>
/// <param name="whiteToSpace">default = will calculate</param>
/// <returns></returns>
public Bitmap[][] SplitUp(Bitmap b, int whiteToSpace = -1)
{
var retlists = new List<List<Bitmap>>();
//get rows
var rows = Split(b, true, ref whiteToSpace);
//for each of the rows, get the columns
var row = 0;
foreach (var r in rows)
{
var cols = Split(r, false, ref whiteToSpace);
if (retlists.Count <= row)
retlists.Add(new List<Bitmap>());
retlists[row].AddRange(cols);
row++;
}
//return results as array
//normalise
redo:
for (var y = 0; y < retlists.Count(); y++)
{
for (var x = 0; x < retlists[y].Count(); x++)
{
var norm = Normalise(retlists[y][x]);
if (norm == null)
{
retlists[y].RemoveAt(x);
goto redo;
}
retlists[y][x] = norm;
}
}
return retlists.Select(s => s.ToArray()).ToArray();
}
/// <summary>
/// Splits the specified b.
/// </summary>
/// <param name="b">The b.</param>
/// <param name="byRow">if set to <c>true</c> [by row].</param>
/// <param name="whiteToSpace">The white to space.</param>
/// <returns></returns>
/// <exception cref="Exception">error splitting</exception>
private static IEnumerable<Bitmap> Split(Bitmap b, bool byRow, ref int whiteToSpace)
{
//split rows then cols
var w = b.Width;
var h = b.Height;
var to = byRow ? h : w;
var items = new List<Bitmap>();
var whiteStart = 0;
var lastValue = -1;
for (var v = 0; v < to; v++)
{
//keep going until we find a row with black
var isWhite = BitmapExtras.IsRowOrColSameColour(b, v, byRow, Color.White);
if (isWhite)
{
if (whiteStart == v)
{
whiteStart++;
continue;
}
//see if there is a space at the start
if (whiteToSpace != -1 && items.Count == 0 && v >= whiteToSpace)
{
items.Add(WhiteBitmap);
}
//if there is more than a certain amount of space check/add a white item
if (whiteToSpace != -1 && lastValue != -1 && ((v - lastValue) >= whiteToSpace))
{
items.Add(WhiteBitmap);
}
//if this occurs, there is a character before
Bitmap i = null;
if (byRow)
{
//if by row, and the second time, we can estimate the white space for a space
if (whiteToSpace == -1 && items.Count == 1)
whiteToSpace = whiteStart;
i = BitmapExtras.Crop(b, -1, v - whiteStart, 0, whiteStart);
}
else
{
i = BitmapExtras.Crop(b, v - whiteStart, -1, whiteStart, 0);
}
if (i == null)
{
throw new Exception("error splitting");
}
items.Add(i);
lastValue = v;
whiteStart = v + 1;
}
}
//see if there is a space at the end
if (whiteToSpace != -1 && lastValue != -1 && ((to - lastValue) >= whiteToSpace))
{
items.Add(WhiteBitmap);
}
return items;
}
/// <summary>
/// Performs the ocr.
/// </summary>
/// <param name="b">The b.</param>
/// <param name="spaceForWhiteSpace">The space for white space.</param>
/// <returns></returns>
public string[] PerformOCR(Bitmap b, int spaceForWhiteSpace = -1)
{
//split bitmap here
var bits = SplitUp(b, spaceForWhiteSpace);
//for each bitmap, find the corresponding character
var ret = new string[bits.Count()];
var row = 0;
foreach (var brow in bits)
{
foreach (var bcol in brow)
{
var c = PerformOCRCharacter(bcol);
ret[row] += c;
}
row++;
}
return ret;
}
/// <summary>
/// Performs the ocr.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="spaceForWhiteSpace">The space for white space.</param>
/// <returns></returns>
public string[] PerformOCR(string filename, int spaceForWhiteSpace = -1)
{
try
{
var b = new Bitmap(filename);
return PerformOCR(b);
}
catch (Exception e )
{
MessageBox.Show(e.ToString());
}
return null;
}
/// <summary>
/// Performs the ocr character perfect.
/// </summary>
/// <param name="b">The b.</param>
/// <returns></returns>
public char? PerformOCRCharacterPerfect(Bitmap b)
{
//go through all the saved characters, and print the most likely
var res = Letters.FirstOrDefault(s => GetOffScore(b, s) == 0);
if (res == null)
return null;
return res.Letter;
}
/// <summary>
/// Performs the ocr character.
/// </summary>
/// <param name="b">The b.</param>
/// <param name="perfectOnly">if set to <c>true</c> [perfect only].</param>
/// <returns></returns>
private char PerformOCRCharacter(Bitmap b, bool perfectOnly = false)
{
//go through all the saved characters, and print the most likely
var best = Letters.OrderBy(s => GetOffScore(b, s));
var best1 = best.First().Letter;
return best1;
}
/// <summary>
/// take an image of a letter and a character of what it is, and train the ocr
/// </summary>
/// <param name="b">The b.</param>
/// <param name="letterChar">The letter character.</param>
/// <returns></returns>
public bool Train(Bitmap b, char letterChar)
{
try
{
var l = new HistogramLetter(b, letterChar);
Letters.Add(l);
return true;
}
catch (ArgumentNullException)
{
}
return false;
}
/// <summary>
/// get score. score of 0 is perfect
/// </summary>
/// <param name="b">The b.</param>
/// <param name="l">The l.</param>
/// <returns></returns>
private int GetOffScore(Bitmap b, HistogramLetter l)
{
var bba = BitmapBoolArray.GetBitmapBoolArray(b);
var h = HistogramLetter.GetHistogram(bba, b);
var score = l.GetHistogramOffsetScore(h.Item1, h.Item2);
return score;
}
/// <summary>
/// trim white and resize to created dimensions
/// </summary>
/// <param name="b1">The b1.</param>
/// <returns></returns>
public Bitmap Normalise(Bitmap b1)
{
//white 1x1=empty
if (b1.Width == 1 && b1.Height == 1)
{
return b1;
}
//trim white
var b = BitmapExtras.RemoveExcessWhitespace(b1);
if (b == null)
return null;
//resize to internal size
b = BitmapExtras.StretchBitmap(b, HistogramWidth, HistogramHeight);
//convert to black and white
b = BitmapExtras.ChangeAllNonWhitePixelsToColour(b, Color.Black);
return b;
}
/// <summary>
///
/// </summary>
public class BitmapBoolArray
{
/// <summary>
/// true if value, false if white
/// </summary>
public bool[][] bitmapBool;
private BitmapBoolArray(int w, int h)
{
bitmapBool = ArrayExtras.InstantiateArray<bool>(w, h);
}
/// <summary>
/// Gets the bitmap bool array.
/// </summary>
/// <param name="b">The b.</param>
/// <returns></returns>
public static BitmapBoolArray GetBitmapBoolArray(Bitmap b)
{
var w = b.Width;
var h = b.Height;
var bba = new BitmapBoolArray(w, h);
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
{
var p = b.GetPixel(x, y);
var white = p.R == 255 && p.G == 255 && p.B == 255;
bba.bitmapBool[y][x] = !white;
}
}
return bba;
}
}
/// <summary>
///
/// </summary>
public class HistogramLetter
{
private const char b = '\b';
private const char f = '\f';
internal char Letter;
internal int[] XValues;
internal int[] YValues;
private HistogramLetter()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HistogramLetter"/> class.
/// </summary>
/// <param name="b">The b.</param>
/// <param name="letter">The letter.</param>
public HistogramLetter(Bitmap b, char letter)
{
var bba = BitmapBoolArray.GetBitmapBoolArray(b);
Letter = letter;
var val = GetHistogram(bba, b);
XValues = val.Item1;
YValues = val.Item2;
}
/// <summary>
/// Serialises this instance.
/// </summary>
/// <returns></returns>
public string Serialise()
{
var ret = Letter + b.ToString(CultureInfo.InvariantCulture);
for (var a = 0; a < XValues.Count(); a++)
{
ret += XValues[a] + f.ToString(CultureInfo.InvariantCulture);
}
ret += b;
for (var a = 0; a < YValues.Count(); a++)
{
ret += YValues[a] + f.ToString(CultureInfo.InvariantCulture);
}
return ret;
}
/// <summary>
/// Des the serialise.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
public static HistogramLetter DeSerialise(string text)
{
HistogramLetter ret = null;
try
{
var t1 = text.Split(new[] { b }, StringSplitOptions.RemoveEmptyEntries);
var let = t1[0][0];
//get values
var xarr = t1[1];
var xval = xarr.Split(new[] { f }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
var yarr = t1[2];
var yval = yarr.Split(new[] { f }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
//set values
ret = new HistogramLetter { XValues = xval, YValues = yval, Letter = let };
}
catch (ArgumentException)
{
}
return ret;
}
/// <summary>
/// Gets the histogram.
/// </summary>
/// <param name="bba">The bba.</param>
/// <param name="b">The b.</param>
/// <returns></returns>
public static Tuple<int[], int[]> GetHistogram(BitmapBoolArray bba, Bitmap b)
{
var w = b.Width;
var h = b.Height;
var XValues = new int[w];
var YValues = new int[h];
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
{
var v = bba.bitmapBool[y][x] ? 1 : 0;
YValues[y] += v;
XValues[x] += v;
}
}
return new Tuple<int[], int[]>(XValues, YValues);
}
/// <summary>
/// Gets the histogram offset score.
/// </summary>
/// <param name="xv">The xv.</param>
/// <param name="yv">The yv.</param>
/// <returns></returns>
public int GetHistogramOffsetScore(int[] xv, int[] yv)
{
//go by the largest
var letterwidth = XValues.Count();
var letterheight = YValues.Count();
var inwidth = xv.Count();
var inheight = yv.Count();
var w = Math.Max(letterwidth, inwidth);
var h = Math.Max(letterheight, inheight);
var score = 0;
for (var y = 0; y < h; y++)
{
var letterval = 0;
var inval = 0;
if (y < letterheight)
letterval += YValues[y];
if (y < inheight)
inval += yv[y];
score += Math.Abs(letterval - inval);
}
for (var x = 0; x < w; x++)
{
var letterval = 0;
var inval = 0;
if (x < letterwidth)
letterval += XValues[x];
if (x < inwidth)
inval += xv[x];
score += Math.Abs(letterval - inval);
}
return score;
}
}
}
}