-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimpleocr.engine.pas
More file actions
716 lines (580 loc) · 20.2 KB
/
simpleocr.engine.pas
File metadata and controls
716 lines (580 loc) · 20.2 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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
unit simpleocr.engine;
{==============================================================================]
Copyright (c) 2021, Jarl `slacky` Holta
Project: SimpleOCR
Project URL: https://github.com/slackydev/SimpleOCR
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
[==============================================================================}
{$i simpleocr.inc}
interface
uses
Classes, SysUtils,
simpleocr.types, simpleocr.filters;
const
FONTSET_SPACE = #32;
FONTSET_START = #32;
FONTSET_END = #126;
const
ALPHA_NUMERIC_SYM = ['a'..'z', 'A'..'Z', '0'..'9','%','&','#','$','[',']','{','}','@','!','?'];
type
PFontCharacter = ^TFontCharacter;
TFontCharacter = packed record
ImageWidth, ImageHeight: Integer;
Width, Height: Integer;
CharacterBounds: TBox;
CharacterPoints: TPointArray;
CharacterPointsLength: Integer;
ShadowPoints: TPointArray;
BackgroundPoints: TPointArray;
BackgroundPointsLength: Integer;
TotalBounds: TBox;
Value: Char;
end;
PFontSet = ^TFontSet;
TFontSet = packed record
private
function GetCharacterPoints(const Character: Char): Integer; inline;
public
Name: String;
Characters: array[FONTSET_START..FONTSET_END] of TFontCharacter;
SpaceWidth: Integer;
MaxWidth: Integer;
MaxHeight: Integer;
property CharacterPoints[Character: Char]: Integer read GetCharacterPoints;
procedure Load(FontPath: String; Space: Integer = 4);
end;
PSimpleOCR = ^TSimpleOCR;
TSimpleOCR = packed record
private
FFontSet: TFontSet;
FClient: TIntegerMatrix;
// "Internal data"
FWidth: Integer;
FHeight: Integer;
FSearchArea: TBox;
FBinaryImage: Boolean;
FMaxShadowValue: Integer;
FTolerance: Integer;
function Init(Matrix: TIntegerMatrix; constref FontSet: TFontSet; Filter: TOCRFilter; Static: Boolean): Boolean;
function _RecognizeX(Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
function _RecognizeXY(Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
public
property Client: TIntegerMatrix read FClient;
function TextToMatrix(Text: String; constref FontSet: TFontSet): TIntegerMatrix;
function TextToTPA(Text: String; constref FontSet: TFontSet): TPointArray;
function LocateText(Matrix: TIntegerMatrix; Text: String; constref FontSet: TFontSet; Filter: TOCRFilter; out Bounds: TBox): Single; overload;
function LocateText(Matrix: TIntegerMatrix; Text: String; constref FontSet: TFontSet; Filter: TOCRFilter; MinMatch: Single = 1): Boolean; overload;
function Recognize(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref FontSet: TFontSet): String;
function RecognizeStatic(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref FontSet: TFontSet; MaxWalk: Integer = 20): String;
function RecognizeLines(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref FontSet: TFontSet; out TextBounds: TBoxArray): TStringArray; overload;
function RecognizeLines(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref FontSet: TFontSet): TStringArray; overload;
end;
implementation
uses
graphtype, intfgraphics, graphics, math;
function ContainsAlphaNumSym(text: string): Boolean; inline;
var i: Int32;
begin
Result := False;
for i:=1 to Length(text) do
if Text[i] in ALPHA_NUMERIC_SYM then
Exit(True);
end;
function TFontSet.GetCharacterPoints(const Character: Char): Integer;
begin
if (Character in [FONTSET_START..FONTSET_END]) then
Result := Characters[Character].CharacterPointsLength
else
Result := 0;
end;
procedure TFontSet.Load(FontPath: String; Space: Integer);
function FindColor(Image: TLazIntfImage; Color: Integer): TPointArray;
var
X, Y: Integer;
Count: Integer = 0;
begin
SetLength(Result, Image.Width * Image.Height);
for X := 0 to Image.Width - 1 do
for Y := 0 to Image.Height - 1 do
begin
if FPColorToTColor(Image.Colors[X, Y]) = Color then
begin
Result[Count].X := X;
Result[Count].Y := Y;
Inc(Count);
end;
end;
SetLength(Result, Count);
end;
var
I: Integer;
Image: TLazIntfImage;
Description: TRawImageDescription;
FontChar: TFontCharacter;
begin
FontPath := IncludeTrailingPathDelimiter(ExpandFileName(FontPath));
if (not DirectoryExists(FontPath)) then
begin
WriteLn('TFontSet.Load: Font does not exist "' + FontPath + '"');
Halt(1);
end;
Self := Default(TFontSet);
Self.Name := ExtractFileName(ExcludeTrailingPathDelimiter(FontPath));
Self.SpaceWidth := Space;
Description.Init_BPP32_B8G8R8_BIO_TTB(0, 0);
Image := TLazIntfImage.Create(0, 0);
Image.DataDescription := Description;
for I := 32 to 126 do
begin
if FileExists(FontPath + IntToStr(I) + '.bmp') then
Image.LoadFromFile(FontPath + IntToStr(I) + '.bmp')
else
if FileExists(FontPath + IntToStr(I) + '.png') then
Image.LoadFromFile(FontPath + IntToStr(I) + '.png')
else
Continue;
FontChar := Default(TFontCharacter);
FontChar.ImageWidth := Image.Width;
FontChar.ImageHeight := Image.Height;
FontChar.Value := Chr(I);
if (FontChar.Value = FONTSET_SPACE) then
begin
FontChar.Width := Image.Width;
FontChar.Height := Image.Height;
end else
begin
FontChar.CharacterPoints := FindColor(Image, $FFFFFF);
FontChar.CharacterBounds := TPABounds(FontChar.CharacterPoints);
FontChar.ShadowPoints := FindColor(Image, $0000FF);
if (FontChar.CharacterBounds.X1 > 0) then
begin
OffsetTPA(FontChar.CharacterPoints, -FontChar.CharacterBounds.X1, 0);
OffsetTPA(FontChar.ShadowPoints, -FontChar.CharacterBounds.X1, 0);
SortTPAByColumn(FontChar.CharacterPoints);
end;
FontChar.BackgroundPoints := InvertTPA(FontChar.CharacterPoints + FontChar.ShadowPoints);
FontChar.BackgroundPointsLength := Length(FontChar.BackgroundPoints);
with TPABounds(FontChar.CharacterPoints + FontChar.ShadowPoints) do
begin
FontChar.Width := X2-X1+1;
FontChar.Height := Y2-Y1+1;
end;
end;
if (FontChar.Width > MaxWidth) then
MaxWidth := FontChar.Width;
if (FontChar.Height > MaxHeight) then
MaxHeight := FontChar.Height;
FontChar.TotalBounds := TPABounds(FontChar.CharacterPoints + FontChar.ShadowPoints + FontChar.BackgroundPoints);
FontChar.CharacterPointsLength := Length(FontChar.CharacterPoints);
Self.Characters[FontChar.Value] := FontChar;
end;
Image.Free();
end;
function TSimpleOCR.Init(Matrix: TIntegerMatrix; constref FontSet: TFontSet; Filter: TOCRFilter; Static: Boolean): Boolean;
begin
Result := MatrixDimensions(Matrix, FWidth, FHeight);
if Result then
begin
FClient := Matrix;
FFontSet := FontSet;
case Filter.FilterType of
EOCRFilterType.COLOR,
EOCRFilterType.INVERT_COLOR:
begin
Result := ApplyColorFilter(Filter, FClient, FSearchArea);
FBinaryImage := True;
FMaxShadowValue := 0;
FTolerance := 0;
end;
EOCRFilterType.ANY_COLOR:
begin
FBinaryImage := False;
FMaxShadowValue := Filter.AnyColorFilter.MaxShadowValue;
FTolerance := Sqr(Filter.AnyColorFilter.Tolerance);
end;
EOCRFilterType.THRESHOLD:
begin
Result := ApplyThresholdFilter(Filter, FClient, FSearchArea);
FBinaryImage := True;
FMaxShadowValue := 0;
FTolerance := 0;
end;
EOCRFilterType.SHADOW:
begin
Result := ApplyShadowFilter(Filter, FClient, FSearchArea);
FBinaryImage := True;
FMaxShadowValue := 0;
FTolerance := 0;
end;
end;
if Static then
FSearchArea := Box(0, 0, FWidth - 1, FHeight - 1)
else
begin
// Filter sets the bounds
FSearchArea.X1 -= FontSet.MaxWidth div 2;
FSearchArea.Y1 -= FFontSet.MaxHeight div 2;
FSearchArea.X2 += FontSet.MaxWidth div 2;
FSearchArea.Y2 += FFontSet.MaxHeight div 2;
end;
end;
end;
function TSimpleOCR._RecognizeX(Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
function CompareChar(const Character: TFontCharacter; const OffsetX, OffsetY: Integer): Integer; inline;
var
Hits, Any: Integer;
First: TRGB32;
P: TPoint;
begin
Result := 0;
// Check if character is loaded
if (Character.CharacterPointsLength = 0) then
Exit;
// Check if entire character is in client
with Character.TotalBounds do
if (X1 + OffsetX < 0) or (Y1 + OffsetY < 0) or (X2 + OffsetX >= FWidth) or (Y2 + OffsetY >= FHeight) then
Exit;
Hits := 0;
Any := 0;
First := TRGB32(FClient[Character.CharacterPoints[0].Y + OffsetY, Character.CharacterPoints[0].X + OffsetX]);
// If binary image and not non-zero it cannot be a character point
if FBinaryImage and (Integer(First) = 0) then
Exit;
// Check if not a shadow
if (FMaxShadowValue > 0) then
begin
if ((First.R + First.G + First.B) div 3 < 85) and
((First.R < FMaxShadowValue * 2) and (First.G < FMaxShadowValue * 2) and (First.B < FMaxShadowValue * 2)) then
Exit;
end;
// count hits for the character
for P in Character.CharacterPoints do
begin
with TRGB32(FClient[P.Y + OffsetY, P.X + OffsetX]) do
if Sqr(R - First.R) + Sqr(B - First.B) + Sqr(G - First.G) > FTolerance then
Exit;
Inc(Hits, 2);
end;
if (Hits < Character.CharacterPointsLength) then
Exit; // < 50% match.
if (FMaxShadowValue = 0) then
begin
// counts hits for the points that should not have equal Color to character
// not needed for shadow-fonts
for P in Character.BackgroundPoints do
begin
with TRGB32(FClient[P.Y + OffsetY, P.X + OffsetX]) do
if Sqr(R - First.R) + Sqr(B - First.B) + Sqr(G - First.G) > FTolerance then
Inc(Any)
else
Dec(Hits);
end;
if (Character.BackgroundPointsLength > 0) and (Any <= (Character.BackgroundPointsLength div 2)) then
Exit;
Inc(Hits, Any);
end else
begin
// count hits for shadow
for P in Character.ShadowPoints do
begin
with TRGB32(FClient[P.Y + OffsetY, P.X + OffsetX]) do
if (R > FMaxShadowValue) or (G > FMaxShadowValue) or (B > FMaxShadowValue) then
Exit;
Inc(Hits);
end;
end;
Result := Hits;
end;
var
Space, Hits, BestHits: Integer;
BestCharacter: PFontCharacter;
Character: Char;
begin
Result := '';
TextHits := 0;
TextBounds.X1 := $FFFFFF;
TextBounds.Y1 := $FFFFFF;
TextBounds.X2 := 0;
TextBounds.Y2 := 0;
Space := 0;
while (Bounds.X1 < Bounds.X2) and (Space < MaxWalk) do
begin
BestHits := 0;
for Character := FONTSET_START to FONTSET_END do
begin
Hits := CompareChar(FFontSet.Characters[Character], Bounds.X1, Bounds.Y1);
if (Hits > BestHits) then
begin
BestHits := Hits;
BestCharacter := @FFontSet.Characters[Character];
end;
end;
if (BestHits > 0) then
begin
if ({%H-}BestCharacter^.CharacterPointsLength >= MinCharacterCount) then
begin
if (Result <> '') and (Space >= FFontSet.SpaceWidth) then
Result += ' ';
Space := 0;
TextHits += BestHits;
TextBounds.X1 := Min(TextBounds.X1, Bounds.X1 + BestCharacter^.CharacterBounds.X1);
TextBounds.Y1 := Min(TextBounds.Y1, Bounds.Y1 + BestCharacter^.CharacterBounds.Y1);
TextBounds.X2 := Max(TextBounds.X2, Bounds.X1 + BestCharacter^.CharacterBounds.X2);
TextBounds.Y2 := Max(TextBounds.Y2, Bounds.Y1 + BestCharacter^.CharacterBounds.Y2);
Result += BestCharacter^.Value;
Bounds.X1 += BestCharacter^.Width;
Continue;
end else
Space := 0;
end else
Space += 1;
Bounds.X1 += 1;
end;
end;
function TSimpleOCR._RecognizeXY(Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
var
Text: String;
Best: record
Hits: Integer;
Bounds: TBox;
Text: String;
end;
begin
Best.Hits := 0;
while (Bounds.Y1 < Bounds.Y2) do
begin
Text := Self._RecognizeX(Bounds, MinCharacterCount, MaxWalk, TextHits, TextBounds);
if (TextHits > Best.Hits) then
begin
Best.Hits := TextHits;
Best.Bounds := TextBounds;
Best.Text := Text;
end;
Bounds.Y1 += 1;
end;
TextHits := Best.Hits;
TextBounds := Best.Bounds;
Result := Best.Text;
end;
function TSimpleOCR.TextToMatrix(Text: String; constref FontSet: TFontSet): TIntegerMatrix;
var
I, J, X, Y: Integer;
Bounds: TBox;
begin
Bounds.X1 := 0;
Bounds.X2 := 0;
Bounds.Y1 := $FFFFFF;
Bounds.Y2 := 0;
for I := 1 to Length(Text) do
if (Text[I] in [FONTSET_START..FONTSET_END]) then
with FontSet.Characters[Text[I]] do
begin
if (Text[I] <> FONTSET_SPACE) then
begin
if (CharacterBounds.Y1 < Bounds.Y1) then
Bounds.Y1 := CharacterBounds.Y1;
if (CharacterBounds.Y2 > Bounds.Y2) then
Bounds.Y2 := CharacterBounds.Y2;
end;
Bounds.X2 += ImageWidth;
end;
SetLength(Result, Max(0, (Bounds.Y2 - Bounds.Y1) + 1), Bounds.X2);
if (Length(Result) = 0) or (Length(Result[0]) = 0) then
Exit;
for I := 1 to Length(Text) do
begin
if (not (Text[I] in [FONTSET_START..FONTSET_END])) then
Continue;
with FontSet.Characters[Text[I]] do
begin
if (Text[I] = FONTSET_SPACE) then
begin
for X := Bounds.X1 to (Bounds.X1 + ImageWidth) - 1 do
for Y := 0 to Bounds.Y2 - Bounds.Y1 do
Result[Y, X] := $00FF00;
end else
begin
for J := 0 to CharacterPointsLength - 1 do
Result[CharacterPoints[J].Y - Bounds.Y1, Bounds.X1 + CharacterPoints[J].X + CharacterBounds.X1] := $0000FF;
end;
Bounds.X1 += ImageWidth;
end;
end;
end;
function TSimpleOCR.TextToTPA(Text: String; constref FontSet: TFontSet): TPointArray;
var
Matrix: TIntegerMatrix;
X, Y, W, H: Integer;
Count: Integer;
begin
Result := nil;
Matrix := Self.TextToMatrix(Text, FontSet);
if MatrixDimensions(Matrix, W, H) then
begin
SetLength(Result, W*H);
Count := 0;
for Y := 0 to H-1 do
for X := 0 to W-1 do
if (Matrix[Y, X] = $0000FF) then
begin
Result[Count] := Point(X, Y);
Inc(Count);
end;
SetLength(Result, Count);
end;
end;
function TSimpleOCR.LocateText(Matrix: TIntegerMatrix; Text: String; constref FontSet: TFontSet; Filter: TOCRFilter; out Bounds: TBox): Single;
function SimilarColors(const Color1, Color2: TRGB32; const Tolerance: Integer): Boolean; inline;
begin
Result := Sqr(Color1.R - Color2.R) + Sqr(Color1.G - Color2.G) + Sqr(Color1.B - Color2.B) <= Tolerance;
end;
var
X, Y: Integer;
Color, Bad, I: Integer;
P: TPoint;
Match: Single;
TextMatrix: TIntegerMatrix;
TextWidth, TextHeight: Integer;
CharacterIndices, OtherIndices: TPointArray;
CharacterCount, OtherCount: Integer;
label
NotFound;
begin
Result := 0;
TextMatrix := Self.TextToMatrix(Text, FontSet);
if not MatrixDimensions(TextMatrix, TextWidth, TextHeight) then
Exit;
if not Self.Init(Matrix, FontSet, Filter, True) then
Exit;
SetLength(CharacterIndices, TextWidth * TextHeight);
SetLength(OtherIndices, TextWidth * TextHeight);
CharacterCount := 0;
OtherCount := 0;
for Y := 0 to TextHeight - 1 do
for X := 0 to TextWidth - 1 do
if (TextMatrix[Y][X] = 255) then
begin
CharacterIndices[CharacterCount].X := X;
CharacterIndices[CharacterCount].Y := Y;
Inc(CharacterCount);
end else
begin
OtherIndices[OtherCount].X := X;
OtherIndices[OtherCount].Y := Y;
Inc(OtherCount);
end;
CharacterCount := CharacterCount - 1;
OtherCount := OtherCount - 1;
if (Length(CharacterIndices) > 0) then
begin
FSearchArea.X2 -= TextWidth - 1;
FSearchArea.Y2 -= TextHeight - 1;
for Y := FSearchArea.Y1 to FSearchArea.Y2 do
for X := FSearchArea.X1 to FSearchArea.X2 do
begin
P.Y := Y + CharacterIndices[0].Y;
P.X := X + CharacterIndices[0].X;
if (P.X < 0) or (P.Y < 0) or (P.X >= FWidth) or (P.Y >= FHeight) then
Continue;
Color := FClient[P.Y, P.X];
// If binary image and not non-zero it cannot be a character point
if FBinaryImage and (Integer(Color) = 0) then
Continue;
for I := 1 to CharacterCount do
begin
P.Y := Y + CharacterIndices[I].Y;
P.X := X + CharacterIndices[I].X;
if (P.X < 0) or (P.Y < 0) or (P.X >= FWidth) or (P.Y >= FHeight) or (not SimilarColors(TRGB32(FClient[P.Y, P.X]), TRGB32(Color), FTolerance)) then
goto NotFound;
end;
Bad := 0;
for I := 0 to OtherCount do
begin
P.Y := Y + OtherIndices[I].Y;
P.X := X + OtherIndices[I].X;
if (P.X < 0) or (P.Y < 0) or (P.X >= FWidth) or (P.Y >= FHeight) or SimilarColors(TRGB32(FClient[P.Y, P.X]), TRGB32(Color), FTolerance) then
Inc(Bad);
end;
Match := 1 - (Bad / OtherCount);
if (Match > Result) then
begin
Result := Match;
Bounds.X1 := X;
Bounds.Y1 := Y;
Bounds.X2 := X + TextWidth;
Bounds.Y2 := Y + TextHeight;
if (Result = 1) then
Exit;
end;
NotFound:
end;
end;
end;
function TSimpleOCR.LocateText(Matrix: TIntegerMatrix; Text: String; constref FontSet: TFontSet; Filter: TOCRFilter; MinMatch: Single): Boolean;
var
_: TBox;
begin
Result := LocateText(Matrix, Text, FontSet, Filter, _) >= MinMatch;
end;
function TSimpleOCR.Recognize(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref FontSet: TFontSet): String;
var
Hits: Integer;
Bounds: TBox;
begin
Result := '';
if Self.Init(Matrix, FontSet, Filter, False) then
Result := _RecognizeXY(FSearchArea, FontSet.CharacterPoints[Filter.MinCharacterMatch], $FFFFFF, Hits, Bounds);
end;
function TSimpleOCR.RecognizeStatic(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref FontSet: TFontSet; MaxWalk: Integer): String;
var
Hits: Integer;
Bounds: TBox;
begin
Result := '';
if Self.Init(Matrix, FontSet, Filter, True) then
Result := Self._RecognizeX(FSearchArea, FontSet.CharacterPoints[Filter.MinCharacterMatch], MaxWalk, Hits, Bounds);
end;
function TSimpleOCR.RecognizeLines(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref FontSet: TFontSet; out TextBounds: TBoxArray): TStringArray;
var
SearchBox, Bounds: TBox;
Text: String;
Hits: Integer;
MinCharacterPoints: Integer;
begin
Result := nil;
TextBounds := nil;
if Self.Init(Matrix, FontSet, Filter, False) then
begin
MinCharacterPoints := FontSet.CharacterPoints[','] + 1;
SearchBox := FSearchArea;
while (SearchBox.Y1 + (FFontSet.MaxHeight div 2) < FSearchArea.Y2) do
begin
// Find something on a row that is larger than `,`
Self._RecognizeX(SearchBox, MinCharacterPoints, $FFFFFF, Hits, Bounds);
if (Hits > 0) then
begin
// OCR the row and some extra columns
Text := Self._RecognizeXY(Box(SearchBox.X1, SearchBox.Y1, SearchBox.X2, SearchBox.Y1 + (FFontSet.MaxHeight div 2)), FontSet.CharacterPoints[Filter.MinCharacterMatch], $FFFFFF, Hits, Bounds);
if (Text = '') then
Exit;
// Ensure that actual text was extracted, not just a symbol mess of short or small character symbols.
if ContainsAlphaNumSym(Text) then
begin
Result := Result + [Text];
TextBounds := TextBounds + [Bounds];
// Now we can confidently skip this search line by a jump, but we dont skip it fully in case of overlapping text
// So we divide the texts max glyph height by 4, and subtract that from the lower end of the found bounds.
SearchBox.Y1 := Max(SearchBox.Y1, Bounds.Y2 - (FFontSet.MaxHeight div 4));
end;
end;
SearchBox.Y1 += 1;
end;
end;
end;
function TSimpleOCR.RecognizeLines(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref FontSet: TFontSet): TStringArray;
var
_: TBoxArray;
begin
Result := Self.RecognizeLines(Matrix, Filter, FontSet, _);
end;
end.