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