File: ocrad.texi

package info (click to toggle)
ocrad 0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 812 kB
  • sloc: cpp: 9,710; sh: 388; makefile: 155
file content (743 lines) | stat: -rw-r--r-- 27,597 bytes parent folder | download
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
\input texinfo @c -*-texinfo-*-
@c %**start of header
@setfilename ocrad.info
@documentencoding ISO-8859-15
@settitle GNU Ocrad Manual
@finalout
@c %**end of header

@set UPDATED 18 January 2024
@set VERSION 0.29

@dircategory GNU Packages
@direntry
* Ocrad: (ocrad).               The GNU OCR program
@end direntry


@ifnothtml
@titlepage
@title GNU Ocrad
@subtitle The GNU OCR Program
@subtitle for GNU Ocrad version @value{VERSION}, @value{UPDATED}
@author by Antonio Diaz Diaz

@page
@vskip 0pt plus 1filll
@end titlepage

@contents
@end ifnothtml

@ifnottex
@node Top
@top

This manual is for GNU Ocrad (version @value{VERSION}, @value{UPDATED}).

@menu
* Introduction::                Purpose and features of GNU Ocrad
* Character sets::              Input charsets and output formats
* Invoking ocrad::              Command-line interface
* Filters::                     Postprocessing the text produced
* Library version::             Checking library version
* Library functions::           Descriptions of the library functions
* Library error codes::         Meaning of codes returned by functions
* Image format conversion::     How to convert other formats to png or pnm
* Algorithm::                   How ocrad does its job
* OCR results file::            Description of the ORF file format
* Problems::                    Reporting bugs
* Concept index::               Index of concepts
@end menu

@sp 1
Copyright @copyright{} 2003-2024 Antonio Diaz Diaz.

This manual is free documentation: you have unlimited permission to copy,
distribute, and modify it.
@end ifnottex


@node Introduction
@chapter Introduction
@cindex introduction

@uref{http://www.gnu.org/software/ocrad/ocrad.html,,GNU Ocrad}
is an OCR (Optical Character Recognition) program and library
based on a feature extraction method. It reads images in png or pnm formats
and produces text in @w{byte (8-bit)} or UTF-8 formats. The formats pbm
(bitmap), pgm (greyscale), and ppm (color) are collectively known as pnm.

Ocrad includes a layout analyser able to separate the columns and blocks
of text normally found on printed pages.

For best results the characters should be at least 20 pixels high. If they
are smaller, try the option @option{--scale}. Scanning the image at 300 dpi
usually produces a character size good enough for ocrad.


@node Character sets
@chapter Character sets
@cindex input charsets
@cindex output format

The character set internally used by ocrad is ISO 10646, also known as
UCS (Universal Character Set), which can represent over two thousand
million characters (2^31).

As it is unpractical to try to recognize one among so many different
characters, you can tell ocrad what character sets to recognize. You do
this with the option @option{--charset}.

If the input page contains characters from only one character set, say
@samp{ISO-8859-15}, you can use the default @samp{byte} output format. But
in a page with @samp{ISO-8859-9} and @samp{ISO-8859-15} characters, you
can't tell if a code of 0xFD represents a @samp{latin small letter i
dotless} or a @samp{latin small letter y with acute}. You should use
@option{--format=utf8} instead. Of course, you may request UTF-8 output in
any case.

@sp 1
NOTE: 10^9 is a thousand millions, a billion is a million millions
(million^2), a trillion is a million million millions (million^3), and so
on. Please, don't "embrace and extend" the meaning of prefixes, making
communication among all people difficult. Thanks.


@node Invoking ocrad
@chapter Invoking ocrad
@cindex invoking
@cindex options
@cindex usage
@cindex version

The format for running ocrad is:

@example
ocrad [@var{options}] [@var{files}]
@end example

@noindent
A hyphen @samp{-} used as a @var{file} argument means standard input. It can
be mixed with other @var{files} and is read just once, the first time it
appears in the command line. Ocrad can read concatenated files from standard
input. Remember to prepend @file{./} to any file name beginning with a
hyphen, or use @samp{--}.

ocrad supports the following
@uref{http://www.nongnu.org/arg-parser/manual/arg_parser_manual.html#Argument-syntax,,options}:
@ifnothtml
@xref{Argument syntax,,,arg_parser}.
@end ifnothtml

@table @code
@item -h
@itemx --help
Print an informative help message describing the options and exit.
@w{@samp{ocrad --verbose --help}} describes also hidden options.

@item -V
@itemx --version
Print the version number of ocrad on the standard output and exit.
This version number should be included in all bug reports.

@item -a
@itemx --append
Append generated text to the output file instead of overwriting it.

@item -c @var{name}
@itemx --charset=@var{name}
Enable recognition of the characters belonging to the character set given.
You can repeat this option multiple times with different names for
processing a page with characters from different character sets.@*
If no charset is specified, @samp{iso-8859-15} (latin9) is assumed.@*
Try @option{--charset=help} for a list of valid charset names.

@anchor{--filter}
@item -e @var{name}
@itemx --filter=@var{name}
Pass the output text through the built-in postprocessing filter given
(@pxref{Filters}). Several filters can be applied in sequence using as many
@option{--filter} and @option{--user-filter} options as needed. The filters
are applied in the order they appear on the command line. Try
@option{--filter=help} for a list of valid filter names.

@anchor{--user-filter}
@item -E @var{file}
@itemx --user-filter=@var{file}
Pass the output text through the postprocessing filter defined in
@var{file}. @xref{Filters}, for a description of the format of @var{file}.
Several filters can be applied in sequence using as many @option{--filter}
and @option{--user-filter} options as needed. The filters are applied in the
order they appear on the command line.

@item -f
@itemx --force
Force overwrite of output files.

@item -F @var{name}
@itemx --format=@var{name}
Select the output format. The valid names are @samp{byte} and @samp{utf8}.@*
If no output format is specified, @samp{byte} (8 bit) is assumed.

@item -i
@itemx --invert
Invert image levels (white on black).

@item -l
@itemx --layout
Enable page layout analysis. Ocrad is able to separate blocks of text of
arbitrary shape as long as they are clearly delimited by white space.

@item -o @var{file}
@itemx --output=@var{file}
Place the output into @var{file} instead of into the standard output. Any
missing parent directories are automatically created.

@item -q
@itemx --quiet
Quiet operation.

@item -s @var{value}
@itemx --scale=@var{value}
Scale up the input image by @var{value} before layout analysis and
recognition. If @var{value} is negative, the input image is scaled down
by @var{-value}.

@item -t @var{name}
@itemx --transform=@var{name}
Perform the transformation given (rotation or mirroring) on the input image
before scaling, layout analysis, and recognition. Rotations are made
counter-clockwise.@*
Try @option{--transform=help} for a list of valid transformation names.

@item -T @var{value}
@itemx --threshold=@var{value}
Set binarization threshold for png, pgm, and ppm files or for option
@option{--scale} (only for scaled down images). @var{value} should be a
rational number between 0 and 1, and may be given as a percentage (50%), a
fraction (1/2), or a decimal value (0.5). Pixel values greater than
threshold are converted to white. The default value is 0.5.

@item -u @var{left},@var{top},@var{width},@var{height}
@itemx --cut=@var{left},@var{top},@var{width},@var{height}
Cut the input image by the rectangle defined by @var{left}, @var{top},
@var{width}, and @var{height}. Values may be relative to the image size
@w{(-1.0 <= value <= +1.0)}, or absolute @w{(abs( value ) > 1)}.
Negative values of @var{left}, @var{top} are relative to the
right-bottom corner of the image. Values of @var{width} and @var{height}
must be positive. Absolute and relative values can be mixed. For example
@w{@samp{ocrad --cut 700,960,1,1}} will extract from @samp{700,960} to
the right-bottom corner of the image.@*
The cutting is performed before any other transformation (rotation or
mirroring) on the input image, and before scaling, layout analysis, and
recognition.

@item -v
@itemx --verbose
Verbose mode.

@item -x @var{file}
@itemx --export=@var{file}
Write (export) OCR results file to @var{file} (@pxref{OCR results file}).
@w{@option{-x -}} writes to stdout, overriding text output except if
output has been also redirected with the option @option{-o}.

@end table

Exit status: 0 for a normal exit, 1 for environmental problems
(file not found, invalid command-line options, I/O errors, etc), 2 to
indicate a corrupt or invalid input file, 3 for an internal consistency
error (e.g., bug) which caused ocrad to panic.


@node Filters
@chapter Postprocessing the text produced
@cindex filters

Filters replace some characters in the text output with different
characters and remove some other characters from the output. For
example, when recognizing a text that is known to contain just numbers,
any character recognized as a @samp{Z} will probably be a @samp{2}.

Filters don't enable the recognition of characters, just filter them
from the output. Use @option{--charset} to enable the recognition of a
character set different from the default ISO-8859-15.

Ocrad provides both built-in filters and user-defined filters.

@section User-defined filters

The format of a user-defined filter file (@pxref{--user-filter}) is very
simple. Each line contains either a character conversion or a word that
specifies the default behaviour for unlisted characters.

A character conversion is a comma-separated list of quoted characters
('c'), character sets ([0-9A-Z]), character codes (U0063), or character
ranges (U0000 - UFFFF), and an optional conversion (an equal sign (=)
followed by a quoted character or a character code). The characters in
the list are converted to the character in the conversion. If no
conversion is specified, the character is left unmodified (converted to
itself).

The default behaviour is to discard unlisted characters, i.e. those
characters not appearing in the file, either by themselves or included
in a set or range. If a line containing just the word @samp{leave} is
found in the file, unlisted characters are left unmodified. If the word
is @samp{mark}, unlisted characters are marked as unrecognized.

The destination character of a conversion is considered as listed by
default. Every character may be listed more than once, even as part of
different conversions. The last conversion affecting a given character
is the one that is performed.

Character sets and quoted characters may contain escape sequences.

The character @samp{#} at begin of line or after whitespace starts a
comment that extends to the end of the line.

Ranges of characters may be specified in character sets by writing the
starting and ending characters with a @samp{-} between them. Thus,
@samp{[A-Z]} matches any ASCII uppercase letter. @samp{-} may be
specified by placing it first or last. @samp{]} may be specified by
placing it first. If the first character after the left bracket is
@samp{^}, it indicates a "complemented set", which matches any character
except the ones between the brackets.

Literals (quoted characters and character sets) are decoded as
ISO-8859-15. Character codes are decoded as UCS2. Thus, a @samp{latin
capital letter y with diaeresis} is specified in a set as @samp{[\xBE]},
but its code is @samp{U0178}.

Spaces and control characters are unaffected by filters, except that
leadind, trailing, and duplicate spaces produced by the removal of other
characters will be themselves removed.

@noindent
Here is an example user-defined filter file equivalent to the built-in
filter @samp{numbers}:

@example
leave                      # remove this line to get @samp{numbers_only}
'D', 'O', 'Q', 'o' = '0'
'I', 'L', 'l', '|' = '1'
'Z', 'z'           = '2'
                     '3'
'A', 'q'           = '4'
'S', 's'           = '5'
'G', 'b', U00F3    = '6'   # U00F3 = latin small letter o with acute
'J', 'T'           = '7'
'&', 'B'           = '8'
'g'                = '9'
@end example


@section Built-in filters

Ocrad provides the following built-in filters (@pxref{--filter}):

@table @code
@item --filter=letters
Forces every character that resembles a letter to be recognized as a
letter. Other characters will be output without change.

@item --filter=letters_only
Same as @option{--filter=letters}, but other characters will be discarded.

@item --filter=numbers
Forces every character that resembles a number to be recognized as a
number. Other characters will be output without change.

@item --filter=numbers_only
Same as @option{--filter=numbers} but other characters will be discarded.

@item --filter=same_height
Discards any character (or noise) whose height differs in more than 10
percent from the median height of the characters in the line.

@item --filter=text_block
Discards any character (or noise) outside of a rectangular block of text
lines.

@item --filter=upper_num
Forces every character that resembles a uppercase letter or a number to
be recognized as such. Other characters will be output without change.

@item --filter=upper_num_mark
Same as @option{--filter=upper_num}, but other characters will be marked
as unrecognized.

@item --filter=upper_num_only
Same as @option{--filter=upper_num}, but other characters will be discarded.

@end table


@node Library version
@chapter Library version
@cindex library version

@defvr Constant OCRAD_API_VERSION
This constant is defined in @samp{ocradlib.h} and works as a version test
macro. The application should check at compile time that OCRAD_API_VERSION
is equal to the version required by the application:

@example
#if !defined OCRAD_API_VERSION || OCRAD_API_VERSION != 28
#error "ocradlib 0.28 needed."
#endif
@end example

Before version 0.28, ocradlib didn't define OCRAD_API_VERSION.@*
OCRAD_API_VERSION is defined as (major * 1000 + minor).
@end defvr

NOTE: Version test macros are the library's way of announcing functionality
to the application. They should not be confused with feature test macros,
which allow the application to announce to the library its desire to have
certain symbols and prototypes exposed.

@deftypefun int OCRAD_api_version ( void )
If OCRAD_API_VERSION >= 28, this function is declared in @samp{ocradlib.h}
(else it doesn't exist). It returns the OCRAD_API_VERSION of the library
object code being used. The application should check at run time that the
value returned by @code{OCRAD_api_version} is equal to the version required
by the application.

@example
#if defined OCRAD_API_VERSION && OCRAD_API_VERSION >= 28
  if( OCRAD_api_version() != 28 )
    show_error( "ocradlib 0.28 needed." );
#endif
@end example
@end deftypefun

@deftypevr Constant {const char *} OCRAD_version_string
This string constant is defined in the header file @samp{ocradlib.h} and
represents the version of the library being used at compile time.
@end deftypevr

@deftypefun {const char *} OCRAD_version ( void )
This function returns a string representing the version of the library being
used at run time.
@end deftypefun


@node Library functions
@chapter Library functions
@cindex library functions

These are the OCRAD library functions. In case of error, all of them
return -1 or a null pointer, except @samp{OCRAD_open} whose return value
must be checked by calling @samp{OCRAD_get_errno} before using it.


@deftypefun {struct OCRAD_Descriptor *} OCRAD_open ( void )
Initialize the internal library state and return a pointer that can only be
used as the @var{ocrdes} argument for the other OCRAD functions, or a null
pointer if the descriptor could not be allocated.

The pointer returned must be checked by calling @samp{OCRAD_get_errno}
before using it. If @samp{OCRAD_get_errno} does not return @samp{OCRAD_ok},
the pointer returned must not be used and should be freed with
@samp{OCRAD_close} to avoid memory leaks.
@end deftypefun


@deftypefun int OCRAD_close ( struct OCRAD_Descriptor * const @var{ocrdes} )
Free all dynamically allocated data structures for this descriptor.
After a call to @samp{OCRAD_close}, @var{ocrdes} can no more be used as
an argument to any OCRAD function.
@end deftypefun


@deftypefun {enum OCRAD_Errno} OCRAD_get_errno ( struct OCRAD_Descriptor * const @var{ocrdes} )
Return the current error code for @var{ocrdes}. @xref{Library error codes}.
@end deftypefun


@deftypefun int OCRAD_set_image ( struct OCRAD_Descriptor * const @var{ocrdes}, const struct OCRAD_Pixmap * const @var{image}, const bool @var{invert} )
Load @var{image} into the internal buffer. If @var{invert} is true, image
levels are inverted (white on black). Loading a new image deletes any
previous text results.
@end deftypefun


@deftypefun int OCRAD_set_image_from_file ( struct OCRAD_Descriptor * const @var{ocrdes}, const char * const @var{filename}, const bool @var{invert} )
Load a image from the file @var{filename} into the internal buffer. If
@var{invert} is true, image levels are inverted (white on black).
Loading a new image deletes any previous text results.
@end deftypefun


@deftypefun int OCRAD_set_utf8_format ( struct OCRAD_Descriptor * const @var{ocrdes}, const bool @var{utf8} )
Set the output format to @samp{byte} (if @var{utf8}=false) or to @samp{utf8}
(if @var{utf8}=true). By default ocrad produces @samp{byte} (8 bit) output.
@end deftypefun


@deftypefun int OCRAD_set_threshold ( struct OCRAD_Descriptor * const @var{ocrdes}, const int @var{threshold} )
Set the binarization threshold for greymap and RGB images. @var{threshold}
values between 0 and 255 set a fixed threshold. A value of -1 sets an
automatic threshold. Pixel values greater than the resulting threshold are
converted to white. The default threshold value if this function is not
called is 127.
@end deftypefun


@deftypefun int OCRAD_scale ( struct OCRAD_Descriptor * const @var{ocrdes}, const int @var{value} )
Scale up the image in the internal buffer by @var{value}. If @var{value}
is negative, the image is scaled down by @var{-value}.
@end deftypefun


@deftypefun int OCRAD_recognize ( struct OCRAD_Descriptor * const @var{ocrdes}, const bool @var{layout} )
Recognize the image loaded in the internal buffer and produce text results
which can be later retrieved with the functions @samp{OCRAD_result_*}. The
same image can be recognized as many times as desired, for example setting a
new threshold each time for 3D greymap recognition. Every time this function
is called, the text results produced replace any previous ones. If
@var{layout} is true, page layout analysis is enabled, probably producing
more than one text block.
@end deftypefun


@deftypefun int OCRAD_result_blocks ( struct OCRAD_Descriptor * const @var{ocrdes} )
Return the number of text blocks found in the image, or 0 if no text was
found. The value returned is usually 1, but can be larger if layout analysis
was requested.
@end deftypefun


@deftypefun int OCRAD_result_lines ( struct OCRAD_Descriptor * const @var{ocrdes}, const int @var{blocknum} )
Return the number of text lines contained in the text block given.
@end deftypefun


@deftypefun int OCRAD_result_chars_total ( struct OCRAD_Descriptor * const @var{ocrdes} )
Return the total number of text characters contained in the image recognized.
@end deftypefun


@deftypefun int OCRAD_result_chars_block ( struct OCRAD_Descriptor * const @var{ocrdes}, const int @var{blocknum} )
Return the number of text characters contained in the text block given.
@end deftypefun


@deftypefun int OCRAD_result_chars_line ( struct OCRAD_Descriptor * const @var{ocrdes}, const int @var{blocknum}, const int @var{linenum} )
Return the number of text characters contained in the text line given.
@end deftypefun



@deftypefun {const char *} OCRAD_result_line ( struct OCRAD_Descriptor * const @var{ocrdes}, const int @var{blocknum}, const int @var{linenum} )
Return the line of text specified by @var{blocknum} and @var{linenum}.
@end deftypefun


@deftypefun int OCRAD_result_first_character ( struct OCRAD_Descriptor * const @var{ocrdes} )
Return the byte result for the first character in the image. Return 0 if the
image has no characters or if the first character could not be recognized.
This function is a convenient short cut to the result for images containing
a single character.
@end deftypefun


@node Library error codes
@chapter Library error codes
@cindex library error codes

Most library functions return -1 or a null pointer to indicate that they
have failed. But this return value only tells you that an error has
occurred. To find out what kind of error it was, you need to check the
error code by calling @samp{OCRAD_get_errno}.

Library functions don't change the value returned by @samp{OCRAD_get_errno}
when they succeed; thus, the value returned by @samp{OCRAD_get_errno} after
a successful call is not necessarily OCRAD_ok, and you should not use
@samp{OCRAD_get_errno} to determine whether a call failed. If the call
failed, then you can examine @samp{OCRAD_get_errno}.

The error codes are defined in the header file @samp{ocradlib.h}.

@deftypevr Constant {enum OCRAD_Errno} OCRAD_ok
The value of this constant is 0 and is used to indicate that there is no
error.
@end deftypevr

@deftypevr Constant {enum OCRAD_Errno} OCRAD_bad_argument
At least one of the arguments passed to the library function was invalid.
@end deftypevr

@deftypevr Constant {enum OCRAD_Errno} OCRAD_mem_error
No memory available. The system cannot allocate more virtual memory because
its capacity is full.
@end deftypevr

@deftypevr Constant {enum OCRAD_Errno} OCRAD_sequence_error
A library function was called in the wrong order. For example
@samp{OCRAD_result_line} was called before @samp{OCRAD_recognize}.
@end deftypevr

@deftypevr Constant {enum OCRAD_Errno} OCRAD_library_error
A bug was detected in the library. Please, report it. @xref{Problems}.
@end deftypevr


@node Image format conversion
@chapter Image format conversion
@cindex image format conversion

There are a lot of image formats, but ocrad is able to decode only four of
them; png, pbm, pgm, and ppm. In this chapter you will find command examples
and advice about how to convert image files to a format that ocrad can
manage.

@table @samp
@item .ps
@itemx .pdf
Postscript or Portable Document Format file. Use the command
@w{@samp{gs -sPAPERSIZE=a4 -sDEVICE=pnmraw -r300 -dNOPAUSE -dBATCH -sOutputFile=- -q file.ps | ocrad}}.@*
You may also use the command
@w{@samp{pstopnm -stdout -dpi=300 -pgm file.ps | ocrad}}, but it seems not
to work with pdf files. Also old versions of @samp{pstopnm} don't recognize
the option @option{-dpi} and produce an image too small for OCR.

@item .tiff
TIFF file. Use the command @w{@samp{tifftopnm file.tiff | ocrad}}.

@item .jpg
JPEG file. Use the command
@w{@samp{djpeg -greyscale -pnm file.jpg | ocrad}}.@*
JPEG is a lossy format and is in general not recommended for text images.

@item .pnm.gz
Pnm file compressed with gzip.
Use the command @w{@samp{gzip -cd file.pnm.gz | ocrad}}.

@item .pnm.lz
Pnm file compressed with @uref{http://www.nongnu.org/lzip/lzip.html,,lzip}.
Use the command @w{@samp{lzip -cd file.pnm.lz | ocrad}}.

@end table


@node Algorithm
@chapter Algorithm
@cindex algorithm

Ocrad is mainly a research project. Many of the algorithms ocrad uses
are ad hoc, and will change in successive releases as I myself gain
understanding about OCR issues.

The overall working of ocrad may be described as follows:@*
1) Read the image.@*
2) Optionally, perform some transformations (cut, rotate, scale, etc).@*
3) Optionally, perform layout detection.@*
4) Remove frames and pictures.@*
5) Detect characters and group them in lines.@*
6) Recognize characters (very ad hoc; one algorithm per character).@*
7) Correct some ambiguities (transform l.OOO into 1.000, etc).@*
8) Optionally, apply one or more filters to the text.@*
9) Output text result.

@sp 1
Ocrad recognizes characters by its shape, and the reason it is so fast
is that it does not compare the shape of every character against some
sort of database of shapes and then chooses the best match. Instead of
this, ocrad only compares the shape differences that are relevant to
choose between two character categories, mostly like a binary search.

As there is no such thing as a free lunch, this approach has some
drawbacks. It makes ocrad very sensitive to character defects, and makes
difficult to modify ocrad to recognize new characters.


@node OCR results file
@chapter OCR results file
@cindex OCR results file

Calling ocrad with option @option{-x} produces an OCR results file (ORF),
that is, a parsable file containing the OCR results. The ORF format is
as follows:

@itemize @minus
@item
Any line beginning with @samp{#} is a comment line.
@item
The first non-comment line has the form
@w{@samp{source file @var{filename}}}, where @var{filename} is the name
of the file being processed (@samp{-} for standard input). This is the
only line guaranteed to exist for every input file read without errors.
If the file, or any block or line, has no text, the corresponding part
in the ORF file will be missing.
@item
The second non-comment line has the form
@w{@samp{total text blocks @var{n}}}, where @var{n} is the total number
of text blocks in the source image.
@end itemize

@noindent
For each text block in the source image, the following data follows:

@itemize @minus
@item
A line in the form @w{@samp{text block @var{i} @var{x y w h}}}. Where
@var{i} is the block number and @var{x y w h} are the block position and
size as described below for character boxes.
@item
A line in the form @samp{lines @var{n}}. Where @var{n} is the number of
lines in this block.
@end itemize

@noindent
For each line in every text block, the following data follows:

@itemize @minus
@item
A line in the form @samp{line @var{i} chars @var{n} height @var{h}},
where @var{i} is the line number, @var{n} is the number of characters in
this line, and @var{h} is the mean height of the characters in this line
(in pixels).
@item
N lines (one for every character) in the form
@w{@samp{@var{x} @var{y} @var{w} @var{h}; @var{g}[, '@var{c}'@var{v}]...}},
where:@*
@var{x} is the left border (x-coordinate) of the char bounding box in the
source image (in pixels).@*
@var{y} is the top border (y-coordinate).@*
@var{w} is the width of the bounding box.@*
@var{h} is the height of the bounding box.@*
@var{g} is the number of different recognition guesses for this character.@*
The result characters follow after the number of guesses in the form of
a comma-separated list of pairs. Every pair is formed by the actual
recognised char @var{c} enclosed in single quotes, followed by the
confidence value @var{v}, without space between them. The higher the
value of confidence, the more confident is the result.
@end itemize

Running @samp{./ocrad -x test.orf testsuite/test.pbm} in the source
directory will give you an example ORF file.


@node Problems
@chapter Reporting bugs
@cindex bugs
@cindex getting help

There are probably bugs in ocrad. There are certainly errors and
omissions in this manual. If you report them, they will get fixed. If
you don't, no one will ever know about them and they will remain unfixed
for all eternity, if not longer.

If you find a bug in GNU Ocrad, please send electronic mail to
@email{bug-ocrad@@gnu.org}. Include the version number, which you can
find by running @w{@samp{ocrad --version}}.


@node Concept index
@unnumbered Concept index

@printindex cp

@bye