tesseract  4.00.00dev
pieces.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: pieces.c (Formerly pieces.c)
5  * Description:
6  * Author: Mark Seaman, OCR Technology
7  * Created: Fri Oct 16 14:37:00 1987
8  * Modified: Mon May 20 12:12:35 1991 (Mark Seaman) marks@hpgrlt
9  * Language: C
10  * Package: N/A
11  * Status: Reusable Software Component
12  *
13  * (c) Copyright 1987, Hewlett-Packard Company.
14  ** Licensed under the Apache License, Version 2.0 (the "License");
15  ** you may not use this file except in compliance with the License.
16  ** You may obtain a copy of the License at
17  ** http://www.apache.org/licenses/LICENSE-2.0
18  ** Unless required by applicable law or agreed to in writing, software
19  ** distributed under the License is distributed on an "AS IS" BASIS,
20  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  ** See the License for the specific language governing permissions and
22  ** limitations under the License.
23  *
24  *********************************************************************************/
25 /*----------------------------------------------------------------------
26  I n c l u d e s
27 ----------------------------------------------------------------------*/
28 
29 #include "blobs.h"
30 #include "helpers.h"
31 #include "matrix.h"
32 #include "ndminx.h"
33 #include "ratngs.h"
34 #include "seam.h"
35 #include "wordrec.h"
36 
37 // Include automatically generated configuration file if running autoconf.
38 #ifdef HAVE_CONFIG_H
39 #include "config_auto.h"
40 #endif
41 
43 
44 /*----------------------------------------------------------------------
45  F u n c t i o n s
46 ----------------------------------------------------------------------*/
47 
48 /**********************************************************************
49  * classify_piece
50  *
51  * Create a larger piece from a collection of smaller ones. Classify
52  * it and return the results. Take the large piece apart to leave
53  * the collection of small pieces un modified.
54  **********************************************************************/
55 namespace tesseract {
56 BLOB_CHOICE_LIST *Wordrec::classify_piece(const GenericVector<SEAM*>& seams,
57  inT16 start,
58  inT16 end,
59  const char* description,
60  TWERD *word,
61  BlamerBundle *blamer_bundle) {
62  if (end > start) SEAM::JoinPieces(seams, word->blobs, start, end);
63  BLOB_CHOICE_LIST *choices = classify_blob(word->blobs[start], description,
64  White, blamer_bundle);
65  // Set the matrix_cell_ entries in all the BLOB_CHOICES.
66  BLOB_CHOICE_IT bc_it(choices);
67  for (bc_it.mark_cycle_pt(); !bc_it.cycled_list(); bc_it.forward()) {
68  bc_it.data()->set_matrix_cell(start, end);
69  }
70 
71  if (end > start) SEAM::BreakPieces(seams, word->blobs, start, end);
72 
73  return (choices);
74 }
75 
76 template<class BLOB_CHOICE>
77 int SortByUnicharID(const void *void1, const void *void2) {
78  const BLOB_CHOICE *p1 = *static_cast<const BLOB_CHOICE * const *>(void1);
79  const BLOB_CHOICE *p2 = *static_cast<const BLOB_CHOICE * const *>(void2);
80 
81  return p1->unichar_id() - p2->unichar_id();
82 }
83 
84 template<class BLOB_CHOICE>
85 int SortByRating(const void *void1, const void *void2) {
86  const BLOB_CHOICE *p1 = *static_cast<const BLOB_CHOICE * const *>(void1);
87  const BLOB_CHOICE *p2 = *static_cast<const BLOB_CHOICE * const *>(void2);
88 
89  if (p1->rating() < p2->rating())
90  return 1;
91  return -1;
92 }
93 
94 
95 /**********************************************************************
96  * fill_filtered_fragment_list
97  *
98  * Filter the fragment list so that the filtered_choices only contain
99  * fragments that are in the correct position. choices is the list
100  * that we are going to filter. fragment_pos is the position in the
101  * fragment that we are looking for and num_frag_parts is the the
102  * total number of pieces. The result will be appended to
103  * filtered_choices.
104  **********************************************************************/
105 void Wordrec::fill_filtered_fragment_list(BLOB_CHOICE_LIST *choices,
106  int fragment_pos,
107  int num_frag_parts,
108  BLOB_CHOICE_LIST *filtered_choices) {
109  BLOB_CHOICE_IT filtered_choices_it(filtered_choices);
110  BLOB_CHOICE_IT choices_it(choices);
111 
112  for (choices_it.mark_cycle_pt(); !choices_it.cycled_list();
113  choices_it.forward()) {
114  UNICHAR_ID choice_unichar_id = choices_it.data()->unichar_id();
115  const CHAR_FRAGMENT *frag = unicharset.get_fragment(choice_unichar_id);
116 
117  if (frag != NULL && frag->get_pos() == fragment_pos &&
118  frag->get_total() == num_frag_parts) {
119  // Recover the unichar_id of the unichar that this fragment is
120  // a part of
121  BLOB_CHOICE *b = new BLOB_CHOICE(*choices_it.data());
122  int original_unichar = unicharset.unichar_to_id(frag->get_unichar());
123  b->set_unichar_id(original_unichar);
124  filtered_choices_it.add_to_end(b);
125  }
126  }
127 
128  filtered_choices->sort(SortByUnicharID<BLOB_CHOICE>);
129 }
130 
131 
132 /**********************************************************************
133  * merge_and_put_fragment_lists
134  *
135  * Merge the fragment lists in choice_lists and append it to the
136  * ratings matrix.
137  **********************************************************************/
139  inT16 num_frag_parts,
140  BLOB_CHOICE_LIST *choice_lists,
141  MATRIX *ratings) {
142  BLOB_CHOICE_IT *choice_lists_it = new BLOB_CHOICE_IT[num_frag_parts];
143 
144  for (int i = 0; i < num_frag_parts; i++) {
145  choice_lists_it[i].set_to_list(&choice_lists[i]);
146  choice_lists_it[i].mark_cycle_pt();
147  }
148 
149  BLOB_CHOICE_LIST *merged_choice = ratings->get(row, column);
150  if (merged_choice == NULL)
151  merged_choice = new BLOB_CHOICE_LIST;
152 
153  bool end_of_list = false;
154  BLOB_CHOICE_IT merged_choice_it(merged_choice);
155  while (!end_of_list) {
156  // Find the maximum unichar_id of the current entry the iterators
157  // are pointing at
158  UNICHAR_ID max_unichar_id = choice_lists_it[0].data()->unichar_id();
159  for (int i = 0; i < num_frag_parts; i++) {
160  UNICHAR_ID unichar_id = choice_lists_it[i].data()->unichar_id();
161  if (max_unichar_id < unichar_id) {
162  max_unichar_id = unichar_id;
163  }
164  }
165 
166  // Move the each iterators until it gets to an entry that has a
167  // value greater than or equal to max_unichar_id
168  for (int i = 0; i < num_frag_parts; i++) {
169  UNICHAR_ID unichar_id = choice_lists_it[i].data()->unichar_id();
170  while (!choice_lists_it[i].cycled_list() &&
171  unichar_id < max_unichar_id) {
172  choice_lists_it[i].forward();
173  unichar_id = choice_lists_it[i].data()->unichar_id();
174  }
175  if (choice_lists_it[i].cycled_list()) {
176  end_of_list = true;
177  break;
178  }
179  }
180 
181  if (end_of_list)
182  break;
183 
184  // Checks if the fragments are parts of the same character
185  UNICHAR_ID first_unichar_id = choice_lists_it[0].data()->unichar_id();
186  bool same_unichar = true;
187  for (int i = 1; i < num_frag_parts; i++) {
188  UNICHAR_ID unichar_id = choice_lists_it[i].data()->unichar_id();
189  if (unichar_id != first_unichar_id) {
190  same_unichar = false;
191  break;
192  }
193  }
194 
195  if (same_unichar) {
196  // Add the merged character to the result
197  UNICHAR_ID merged_unichar_id = first_unichar_id;
198  GenericVector<ScoredFont> merged_fonts =
199  choice_lists_it[0].data()->fonts();
200  float merged_min_xheight = choice_lists_it[0].data()->min_xheight();
201  float merged_max_xheight = choice_lists_it[0].data()->max_xheight();
202  float positive_yshift = 0, negative_yshift = 0;
203  int merged_script_id = choice_lists_it[0].data()->script_id();
204  BlobChoiceClassifier classifier = choice_lists_it[0].data()->classifier();
205 
206  float merged_rating = 0, merged_certainty = 0;
207  for (int i = 0; i < num_frag_parts; i++) {
208  float rating = choice_lists_it[i].data()->rating();
209  float certainty = choice_lists_it[i].data()->certainty();
210 
211  if (i == 0 || certainty < merged_certainty)
212  merged_certainty = certainty;
213  merged_rating += rating;
214 
215  choice_lists_it[i].forward();
216  if (choice_lists_it[i].cycled_list())
217  end_of_list = true;
218  IntersectRange(choice_lists_it[i].data()->min_xheight(),
219  choice_lists_it[i].data()->max_xheight(),
220  &merged_min_xheight, &merged_max_xheight);
221  float yshift = choice_lists_it[i].data()->yshift();
222  if (yshift > positive_yshift) positive_yshift = yshift;
223  if (yshift < negative_yshift) negative_yshift = yshift;
224  // Use the min font rating over the parts.
225  // TODO(rays) font lists are unsorted. Need to be faster?
226  const GenericVector<ScoredFont>& frag_fonts =
227  choice_lists_it[i].data()->fonts();
228  for (int f = 0; f < frag_fonts.size(); ++f) {
229  int merged_f = 0;
230  for (merged_f = 0; merged_f < merged_fonts.size() &&
231  merged_fonts[merged_f].fontinfo_id != frag_fonts[f].fontinfo_id;
232  ++merged_f) {}
233  if (merged_f == merged_fonts.size()) {
234  merged_fonts.push_back(frag_fonts[f]);
235  } else if (merged_fonts[merged_f].score > frag_fonts[f].score) {
236  merged_fonts[merged_f].score = frag_fonts[f].score;
237  }
238  }
239  }
240 
241  float merged_yshift = positive_yshift != 0
242  ? (negative_yshift != 0 ? 0 : positive_yshift)
243  : negative_yshift;
244  BLOB_CHOICE* choice = new BLOB_CHOICE(merged_unichar_id,
245  merged_rating,
246  merged_certainty,
247  merged_script_id,
248  merged_min_xheight,
249  merged_max_xheight,
250  merged_yshift,
251  classifier);
252  choice->set_fonts(merged_fonts);
253  merged_choice_it.add_to_end(choice);
254  }
255  }
256 
258  print_ratings_list("Merged Fragments", merged_choice,
259  unicharset);
260 
261  if (merged_choice->empty())
262  delete merged_choice;
263  else
264  ratings->put(row, column, merged_choice);
265 
266  delete [] choice_lists_it;
267 }
268 
269 /**********************************************************************
270  * get_fragment_lists
271  *
272  * Recursively go through the ratings matrix to find lists of fragments
273  * to be merged in the function merge_and_put_fragment_lists.
274  * current_frag is the position of the piece we are looking for.
275  * current_row is the row in the rating matrix we are currently at.
276  * start is the row we started initially, so that we can know where
277  * to append the results to the matrix. num_frag_parts is the total
278  * number of pieces we are looking for and num_blobs is the size of the
279  * ratings matrix.
280  **********************************************************************/
281 void Wordrec::get_fragment_lists(inT16 current_frag, inT16 current_row,
282  inT16 start, inT16 num_frag_parts,
283  inT16 num_blobs, MATRIX *ratings,
284  BLOB_CHOICE_LIST *choice_lists) {
285  if (current_frag == num_frag_parts) {
286  merge_and_put_fragment_lists(start, current_row - 1, num_frag_parts,
287  choice_lists, ratings);
288  return;
289  }
290 
291  for (inT16 x = current_row; x < num_blobs; x++) {
292  BLOB_CHOICE_LIST *choices = ratings->get(current_row, x);
293  if (choices == NULL)
294  continue;
295 
296  fill_filtered_fragment_list(choices, current_frag, num_frag_parts,
297  &choice_lists[current_frag]);
298  if (!choice_lists[current_frag].empty()) {
299  get_fragment_lists(current_frag + 1, x + 1, start, num_frag_parts,
300  num_blobs, ratings, choice_lists);
301  choice_lists[current_frag].clear();
302  }
303  }
304 }
305 
306 
307 /**********************************************************************
308  * merge_fragments
309  *
310  * Try to merge fragments in the ratings matrix and put the result in
311  * the corresponding row and column
312  **********************************************************************/
313 void Wordrec::merge_fragments(MATRIX *ratings, inT16 num_blobs) {
314  BLOB_CHOICE_LIST choice_lists[CHAR_FRAGMENT::kMaxChunks];
315  for (inT16 start = 0; start < num_blobs; start++) {
316  for (int frag_parts = 2; frag_parts <= CHAR_FRAGMENT::kMaxChunks;
317  frag_parts++) {
318  get_fragment_lists(0, start, start, frag_parts, num_blobs,
319  ratings, choice_lists);
320  }
321  }
322 
323  // Delete fragments from the rating matrix
324  for (inT16 x = 0; x < num_blobs; x++) {
325  for (inT16 y = x; y < num_blobs; y++) {
326  BLOB_CHOICE_LIST *choices = ratings->get(x, y);
327  if (choices != NULL) {
328  BLOB_CHOICE_IT choices_it(choices);
329  for (choices_it.mark_cycle_pt(); !choices_it.cycled_list();
330  choices_it.forward()) {
331  UNICHAR_ID choice_unichar_id = choices_it.data()->unichar_id();
332  const CHAR_FRAGMENT *frag =
333  unicharset.get_fragment(choice_unichar_id);
334  if (frag != NULL)
335  delete choices_it.extract();
336  }
337  }
338  }
339  }
340 }
341 
342 
343 } // namespace tesseract
Definition: callcpp.h:34
BLOB_CHOICE_LIST * classify_blob(TBLOB *blob, const char *string, C_COL color, BlamerBundle *blamer_bundle)
Definition: wordclass.cpp:56
static void BreakPieces(const GenericVector< SEAM *> &seams, const GenericVector< TBLOB *> &blobs, int first, int last)
Definition: seam.cpp:194
int UNICHAR_ID
Definition: unichar.h:33
const CHAR_FRAGMENT * get_fragment(UNICHAR_ID unichar_id) const
Definition: unicharset.h:694
static const int kMaxChunks
Definition: unicharset.h:49
int get_pos() const
Definition: unicharset.h:65
const char * get_unichar() const
Definition: unicharset.h:64
T get(ICOORD pos) const
Definition: matrix.h:223
int push_back(T object)
float rating() const
Definition: ratngs.h:79
void set_fonts(const GenericVector< tesseract::ScoredFont > &fonts)
Definition: ratngs.h:94
int size() const
Definition: genericvector.h:72
int16_t inT16
Definition: host.h:36
Definition: blobs.h:395
void fill_filtered_fragment_list(BLOB_CHOICE_LIST *choices, int fragment_pos, int num_frag_parts, BLOB_CHOICE_LIST *filtered_choices)
Definition: pieces.cpp:105
void get_fragment_lists(inT16 current_frag, inT16 current_row, inT16 start, inT16 num_frag_parts, inT16 num_blobs, MATRIX *ratings, BLOB_CHOICE_LIST *choice_lists)
Definition: pieces.cpp:281
int SortByUnicharID(const void *void1, const void *void2)
Definition: pieces.cpp:77
static void JoinPieces(const GenericVector< SEAM *> &seams, const GenericVector< TBLOB *> &blobs, int first, int last)
Definition: seam.cpp:216
int SortByRating(const void *void1, const void *void2)
Definition: pieces.cpp:85
int get_total() const
Definition: unicharset.h:66
void IntersectRange(const T &lower1, const T &upper1, T *lower2, T *upper2)
Definition: helpers.h:153
void set_unichar_id(UNICHAR_ID newunichar_id)
Definition: ratngs.h:144
UNICHARSET unicharset
Definition: ccutil.h:68
GenericVector< TBLOB * > blobs
Definition: blobs.h:436
void put(ICOORD pos, const T &thing)
Definition: matrix.h:215
Definition: matrix.h:563
void merge_fragments(MATRIX *ratings, inT16 num_blobs)
Definition: pieces.cpp:313
virtual BLOB_CHOICE_LIST * classify_piece(const GenericVector< SEAM *> &seams, inT16 start, inT16 end, const char *description, TWERD *word, BlamerBundle *blamer_bundle)
Definition: pieces.cpp:56
UNICHAR_ID unichar_id() const
Definition: ratngs.h:76
BlobChoiceClassifier
Definition: ratngs.h:40
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET &current_unicharset)
Definition: ratngs.cpp:819
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:194
void merge_and_put_fragment_lists(inT16 row, inT16 column, inT16 num_frag_parts, BLOB_CHOICE_LIST *choice_lists, MATRIX *ratings)
Definition: pieces.cpp:138