-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimpleocr.filters.pas
More file actions
247 lines (199 loc) · 5.53 KB
/
simpleocr.filters.pas
File metadata and controls
247 lines (199 loc) · 5.53 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
unit simpleocr.filters;
{==============================================================================]
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;
type
EOCRFilterType = (
ANY_COLOR,
COLOR,
THRESHOLD,
SHADOW,
INVERT_COLOR
);
POCRFilter = ^TOCRFilter;
TOCRFilter = packed record
FilterType: EOCRFilterType;
AnyColorFilter: packed record
MaxShadowValue: Integer;
Tolerance: Integer;
end;
ColorRule: packed record
Colors: array of packed record
Color: Integer;
Tolerance: Integer;
end;
Invert: Boolean;
end;
ThresholdRule: packed record
Amount: Integer;
Invert: Boolean;
end;
ShadowRule: packed record
MaxShadowValue: Integer;
Tolerance: Integer;
end;
MinCharacterMatch: Char;
end;
function ApplyColorFilter(Filter: TOCRFilter; var Matrix: TIntegerMatrix; out Bounds: TBox): Boolean;
function ApplyThresholdFilter(Filter: TOCRFilter; var Matrix: TIntegerMatrix; out Bounds: TBox): Boolean;
function ApplyShadowFilter(Filter: TOCRFilter; var Matrix: TIntegerMatrix; out Bounds: TBox): Boolean;
implementation
function ApplyColorFilter(Filter: TOCRFilter; var Matrix: TIntegerMatrix; out Bounds: TBox): Boolean;
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, Width, Height: Integer;
I, H: Integer;
Hit, Miss: Integer;
Tols: TIntegerArray;
label
Next;
begin
H := High(Filter.ColorRule.Colors);
Height := High(Matrix);
Width := High(Matrix[0]);
Bounds.X1 := $FFFFFF;
Bounds.Y1 := $FFFFFF;
Bounds.X2 := 0;
Bounds.Y2 := 0;
case Filter.ColorRule.Invert of
True:
begin
Hit := $000000;
Miss := $FFFFFF;
end;
False:
begin
Hit := $FFFFFF;
Miss := $000000;
end;
end;
SetLength(Tols, H+1);
for I:=0 to H do
Tols[I] := Sqr(Filter.ColorRule.Colors[I].Tolerance);
for Y := 0 to Height do
for X := 0 to Width do
begin
for I := 0 to H do
if SimilarColors(TRGB32(Matrix[Y, X]), TRGB32(Filter.ColorRule.Colors[I].Color), Tols[I]) then
begin
if (X < Bounds.X1) then Bounds.X1 := X;
if (Y < Bounds.Y1) then Bounds.Y1 := Y;
if (X > Bounds.X2) then Bounds.X2 := X;
if (Y > Bounds.Y2) then Bounds.Y2 := Y;
Matrix[Y, X] := Hit;
goto Next;
end;
Matrix[Y, X] := Miss;
Next:
end;
Result := (Bounds.X1 <> $FFFFFF) and (Bounds.Y1 <> $FFFFFF) and (Bounds.X2 <> 0) and (Bounds.Y2 <> 0);
end;
function ApplyThresholdFilter(Filter: TOCRFilter; var Matrix: TIntegerMatrix; out Bounds: TBox): Boolean;
var
X, Y, W, H: Integer;
Threshold: UInt8;
Counter: Int64;
Temp: TIntegerMatrix;
Hit, Miss: Integer;
begin
H := Length(Matrix);
W := Length(Matrix[0]);
SetLength(Temp, H, W);
Dec(W);
Dec(H);
//Finding the threshold - While at it set blue-scale to the RGB mean (needed for later).
Threshold := 0;
Counter := 0;
for Y := 0 to H do
for X := 0 to W do
begin
with TRGB32(Matrix[Y, X]) do
Temp[Y, X] := (B + G + R) div 3;
Counter += Temp[Y, X];
end;
Threshold := (Counter div ((W * H) - 1)) + Filter.ThresholdRule.Amount;
case Filter.ThresholdRule.Invert of
True:
begin
Hit := $000000;
Miss := $FFFFFF;
end;
False:
begin
Hit := $FFFFFF;
Miss := $000000;
end;
end;
Bounds.X1 := $FFFFFF;
Bounds.Y1 := $FFFFFF;
Bounds.X2 := 0;
Bounds.Y2 := 0;
for Y := 0 to H do
for X := 0 to W do
begin
if (Temp[Y, X] > Threshold) then
begin
Matrix[Y, X] := Hit;
if (X < Bounds.X1) then Bounds.X1 := X;
if (Y < Bounds.Y1) then Bounds.Y1 := Y;
if (X > Bounds.X2) then Bounds.X2 := X;
if (Y > Bounds.Y2) then Bounds.Y2 := Y;
end else
Matrix[Y, X] := Miss;
end;
Result := (Bounds.X1 <> $FFFFFF) and (Bounds.Y1 <> $FFFFFF) and (Bounds.X2 <> 0) and (Bounds.Y2 <> 0);
end;
function ApplyShadowFilter(Filter: TOCRFilter; var Matrix: TIntegerMatrix; out Bounds: TBox): Boolean;
function IsShadow(const X, Y: Integer): Boolean; inline;
begin
with TRGB32(Matrix[Y, X]) do
Result := ((R + G + B) div 3) < Filter.ShadowRule.MaxShadowValue;
end;
var
X, Y, Width, Height: Integer;
Size, Count: Integer;
Colors: TIntegerArray;
begin
Result := False;
Size := 0;
Count := 0;
Colors := [];
Height := High(Matrix);
Width := High(Matrix[0]);
for Y := 1 to Height do
for X := 1 to Width do
begin
if IsShadow(X, Y) and (not IsShadow(X-1, Y-1)) then
begin
if (Count = Size) then
begin
Size := (Size + 32) * 2;
SetLength(Colors, Size);
end;
Colors[Count] := Matrix[Y-1, X-1];
Inc(Count);
end;
end;
if (Count > 0) then
begin
SetLength(Filter.ColorRule.Colors, 1);
with Filter.ColorRule.Colors[0] do
begin
Color := Mode(Colors, Count - 1);
Tolerance := Filter.ShadowRule.Tolerance;
end;
Result := ApplyColorFilter(Filter, Matrix, Bounds);
end;
end;
end.