File: osra_ocr.cpp

package info (click to toggle)
osra 2.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,468 kB
  • sloc: cpp: 52,893; sh: 3,085; perl: 390; makefile: 328; python: 279; xml: 169; java: 160
file content (177 lines) | stat: -rw-r--r-- 3,763 bytes parent folder | download | duplicates (2)
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

#include <iostream>

#include <stdlib.h>
#include <string.h>

#include "ocradlib.h"

// Required by OCRAD, not used here:
#include <vector>
#include <stdio.h>

#include "common.h"
#include "rectangle.h"
#include "bitmap.h"
#include "blob.h"
#include "character.h"

using namespace std;

/* Actual max height is 12, but we leave some more for extensions: */
const char* TESTS[][12] =
{
  /* Test1: "N" is not detected */
  {
    "11100001",
    "11110001",
    "11110001",
    "11011001",
    "11011101",
    "11001111",
    "11000111",
    "11000111",
    "11000011",
  },
  /* Test2: "N" is not detected */
  {
    "11000011",
    "11100011",
    "11110011",
    "11110011",
    "10011011",
    "10011111",
    "10001111",
    "10000111",
    "10000111",
  },
  /* Test3: Detected as "r": */
  {
    "000000010",
    "000000111",
    "000001110",
    "000011100",
    "000111000",
    "001111000",
    "011110000",
    "111100000",
    "011000000",
  },
  /* Test4: Detected as "r": */
  {
    "00000011111",
    "00000111100",
    "00011110000",
    "01111100000",
    "11110000000",
    "11000000000",
  },
  /* Test5: Detected as "r": */
  {
    "00111",
    "01110",
    "01110",
    "01110",
    "11100",
    "11100",
    "11100",
    "11000",
    "11000",
  },
  /* Test6: Detected as "t": */
  {
    "00111",
    "00111",
    "00110",
    "01110",
    "01110",
    "01110",
    "11100",
    "11100",
    "11100"
  }
};

char run_test(int n)
{
  int height = 0;
  int width = strlen(TESTS[n][0]);

  while (TESTS[n][height] != NULL)
    {
      height++;
    }

  const char** image = TESTS[n];

  cout << "Test " << n + 1 << ": width x height = " << width << "x" << height << endl;

  // Blob for recognition attempt via Character::recognize1():
  Blob* b = new Blob(0, 0, width-1, height-1);

  // OCRAD_Pixmap for recognition attempt via OCRAD_result_first_character():
  struct OCRAD_Pixmap* opix = new OCRAD_Pixmap();

  unsigned char* bitmap_data = (unsigned char*) malloc(width * height);
  unsigned char* greymap_data = (unsigned char*) malloc(width * height);

  memset(bitmap_data, 0, width * height);
  memset(greymap_data, 255, width * height);

  opix->height = height;
  opix->width = width;

  opix->mode = OCRAD_bitmap;
  opix->data = bitmap_data;
//	opix->mode = OCRAD_greymap; opix->data = greymap_data;

  for (int row = 0; row < height; row++)
    {
      for (int col = 0; col < width; col++)
        if (image[row][col] == '1')
          {
            b->set_bit(row, col, true);
            bitmap_data[row * width + col] = 1;
            greymap_data[row * width + col] = 0;
          }
    }

  b->find_holes();

  Control control;
  Character a(b);

  // The Blob object was delegated to Character, which will free it on destruction:
  b = NULL;

  a.recognize1(control.charset, Rectangle::Rectangle(a.left(), a.top(), a.right(), a.bottom()));
  char c1 = a.byte_result();

  // Was the character recognised by OCRAD?
  cout << "+ recognised via Character::recognize1(): " << c1 << endl;

  char c2 = 0;
  OCRAD_Descriptor * const ocrdes = OCRAD_open();

  if (ocrdes && OCRAD_get_errno(ocrdes) == OCRAD_ok &&
      OCRAD_set_image(ocrdes, opix, 0) == 0 &&
      ( height >= 10 || OCRAD_scale( ocrdes, 2 ) == 0 ) &&
      OCRAD_recognize(ocrdes, 0) == 0 )
    c2 = OCRAD_result_first_character(ocrdes);

  OCRAD_close(ocrdes);

  delete opix;
  free(bitmap_data);
  free(greymap_data);

  cout << "+ recognised via OCRAD_result_first_character(): " << c2 << endl;
}

int main()
{
  for (unsigned int n = 0; n < 6; n++)
    {
      run_test(n);
    }
}