Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trainingsample.cpp
Go to the documentation of this file.
1 // Copyright 2010 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 //
15 
16 // Include automatically generated configuration file if running autoconf.
17 #ifdef HAVE_CONFIG_H
18 #include "config_auto.h"
19 #endif
20 
21 #include "trainingsample.h"
22 
23 #include <math.h>
24 #include "allheaders.h"
25 #include "helpers.h"
26 #include "intfeaturemap.h"
27 #include "normfeat.h"
28 #include "shapetable.h"
29 
30 namespace tesseract {
31 
32 ELISTIZE(TrainingSample)
33 
34 // Center of randomizing operations.
35 const int kRandomizingCenter = 128;
36 
37 // Randomizing factors.
38 const int TrainingSample::kYShiftValues[kSampleYShiftSize] = {
39  6, 3, -3, -6, 0
40 };
41 const double TrainingSample::kScaleValues[kSampleScaleSize] = {
42  1.0625, 0.9375, 1.0
43 };
44 
46  delete [] features_;
47  delete [] micro_features_;
48 }
49 
50 // WARNING! Serialize/DeSerialize do not save/restore the "cache" data
51 // members, which is mostly the mapped features, and the weight.
52 // It is assumed these can all be reconstructed from what is saved.
53 // Writes to the given file. Returns false in case of error.
54 bool TrainingSample::Serialize(FILE* fp) const {
55  if (fwrite(&class_id_, sizeof(class_id_), 1, fp) != 1) return false;
56  if (fwrite(&font_id_, sizeof(font_id_), 1, fp) != 1) return false;
57  if (fwrite(&page_num_, sizeof(page_num_), 1, fp) != 1) return false;
58  if (!bounding_box_.Serialize(fp)) return false;
59  if (fwrite(&num_features_, sizeof(num_features_), 1, fp) != 1) return false;
60  if (fwrite(&num_micro_features_, sizeof(num_micro_features_), 1, fp) != 1)
61  return false;
62  if (fwrite(features_, sizeof(*features_), num_features_, fp) != num_features_)
63  return false;
64  if (fwrite(micro_features_, sizeof(*micro_features_), num_micro_features_,
65  fp) != num_micro_features_)
66  return false;
67  if (fwrite(cn_feature_, sizeof(*cn_feature_), kNumCNParams, fp) !=
68  kNumCNParams) return false;
69  if (fwrite(geo_feature_, sizeof(*geo_feature_), GeoCount, fp) != GeoCount)
70  return false;
71  return true;
72 }
73 
74 // Creates from the given file. Returns NULL in case of error.
75 // If swap is true, assumes a big/little-endian swap is needed.
78  if (sample->DeSerialize(swap, fp)) return sample;
79  delete sample;
80  return NULL;
81 }
82 
83 // Reads from the given file. Returns false in case of error.
84 // If swap is true, assumes a big/little-endian swap is needed.
85 bool TrainingSample::DeSerialize(bool swap, FILE* fp) {
86  if (fread(&class_id_, sizeof(class_id_), 1, fp) != 1) return false;
87  if (fread(&font_id_, sizeof(font_id_), 1, fp) != 1) return false;
88  if (fread(&page_num_, sizeof(page_num_), 1, fp) != 1) return false;
89  if (!bounding_box_.DeSerialize(swap, fp)) return false;
90  if (fread(&num_features_, sizeof(num_features_), 1, fp) != 1) return false;
91  if (fread(&num_micro_features_, sizeof(num_micro_features_), 1, fp) != 1)
92  return false;
93  if (swap) {
94  ReverseN(&class_id_, sizeof(class_id_));
95  ReverseN(&num_features_, sizeof(num_features_));
96  ReverseN(&num_micro_features_, sizeof(num_micro_features_));
97  }
98  delete [] features_;
99  features_ = new INT_FEATURE_STRUCT[num_features_];
100  if (fread(features_, sizeof(*features_), num_features_, fp) != num_features_)
101  return false;
102  delete [] micro_features_;
103  micro_features_ = new MicroFeature[num_micro_features_];
104  if (fread(micro_features_, sizeof(*micro_features_), num_micro_features_,
105  fp) != num_micro_features_)
106  return false;
107  if (fread(cn_feature_, sizeof(*cn_feature_), kNumCNParams, fp) !=
108  kNumCNParams) return false;
109  if (fread(geo_feature_, sizeof(*geo_feature_), GeoCount, fp) != GeoCount)
110  return false;
111  return true;
112 }
113 
114 // Saves the given features into a TrainingSample.
116  const INT_FX_RESULT_STRUCT& fx_info, const INT_FEATURE_STRUCT* features,
117  int num_features) {
119  sample->num_features_ = num_features;
120  sample->features_ = new INT_FEATURE_STRUCT[num_features];
121  memcpy(sample->features_, features, num_features * sizeof(features[0]));
122  sample->geo_feature_[GeoBottom] = fx_info.YBottom;
123  sample->geo_feature_[GeoTop] = fx_info.YTop;
124  sample->geo_feature_[GeoWidth] = fx_info.Width;
125  sample->features_are_indexed_ = false;
126  sample->features_are_mapped_ = false;
127  return sample;
128 }
129 
130 // Constructs and returns a copy randomized by the method given by
131 // the randomizer index. If index is out of [0, kSampleRandomSize) then
132 // an exact copy is returned.
135  if (index >= 0 && index < kSampleRandomSize) {
136  ++index; // Remove the first combination.
137  int yshift = kYShiftValues[index / kSampleScaleSize];
138  double scaling = kScaleValues[index % kSampleScaleSize];
139  for (int i = 0; i < num_features_; ++i) {
140  double result = (features_[i].X - kRandomizingCenter) * scaling;
141  result += kRandomizingCenter;
142  sample->features_[i].X = ClipToRange(static_cast<int>(result + 0.5), 0,
143  MAX_UINT8);
144  result = (features_[i].Y - kRandomizingCenter) * scaling;
145  result += kRandomizingCenter + yshift;
146  sample->features_[i].Y = ClipToRange(static_cast<int>(result + 0.5), 0,
147  MAX_UINT8);
148  }
149  }
150  return sample;
151 }
152 
153 // Constructs and returns an exact copy.
156  sample->class_id_ = class_id_;
157  sample->font_id_ = font_id_;
158  sample->weight_ = weight_;
159  sample->sample_index_ = sample_index_;
160  sample->num_features_ = num_features_;
161  if (num_features_ > 0) {
162  sample->features_ = new INT_FEATURE_STRUCT[num_features_];
163  memcpy(sample->features_, features_, num_features_ * sizeof(features_[0]));
164  }
165  sample->num_micro_features_ = num_micro_features_;
166  if (num_micro_features_ > 0) {
167  sample->micro_features_ = new MicroFeature[num_micro_features_];
168  memcpy(sample->micro_features_, micro_features_,
169  num_micro_features_ * sizeof(micro_features_[0]));
170  }
171  memcpy(sample->cn_feature_, cn_feature_, sizeof(*cn_feature_) * kNumCNParams);
172  memcpy(sample->geo_feature_, geo_feature_, sizeof(*geo_feature_) * GeoCount);
173  return sample;
174 }
175 
176 // Extracts the needed information from the CHAR_DESC_STRUCT.
177 void TrainingSample::ExtractCharDesc(int int_feature_type,
178  int micro_type,
179  int cn_type,
180  int geo_type,
181  CHAR_DESC_STRUCT* char_desc) {
182  // Extract the INT features.
183  if (features_ != NULL) delete [] features_;
184  FEATURE_SET_STRUCT* char_features = char_desc->FeatureSets[int_feature_type];
185  if (char_features == NULL) {
186  tprintf("Error: no features to train on of type %s\n",
188  num_features_ = 0;
189  features_ = NULL;
190  } else {
191  num_features_ = char_features->NumFeatures;
192  features_ = new INT_FEATURE_STRUCT[num_features_];
193  for (int f = 0; f < num_features_; ++f) {
194  features_[f].X =
195  static_cast<uinT8>(char_features->Features[f]->Params[IntX]);
196  features_[f].Y =
197  static_cast<uinT8>(char_features->Features[f]->Params[IntY]);
198  features_[f].Theta =
199  static_cast<uinT8>(char_features->Features[f]->Params[IntDir]);
200  features_[f].CP_misses = 0;
201  }
202  }
203  // Extract the Micro features.
204  if (micro_features_ != NULL) delete [] micro_features_;
205  char_features = char_desc->FeatureSets[micro_type];
206  if (char_features == NULL) {
207  tprintf("Error: no features to train on of type %s\n",
209  num_micro_features_ = 0;
210  micro_features_ = NULL;
211  } else {
212  num_micro_features_ = char_features->NumFeatures;
213  micro_features_ = new MicroFeature[num_micro_features_];
214  for (int f = 0; f < num_micro_features_; ++f) {
215  for (int d = 0; d < MFCount; ++d) {
216  micro_features_[f][d] = char_features->Features[f]->Params[d];
217  }
218  }
219  }
220  // Extract the CN feature.
221  char_features = char_desc->FeatureSets[cn_type];
222  if (char_features == NULL) {
223  tprintf("Error: no CN feature to train on.\n");
224  } else {
225  ASSERT_HOST(char_features->NumFeatures == 1);
226  cn_feature_[CharNormY] = char_features->Features[0]->Params[CharNormY];
227  cn_feature_[CharNormLength] =
228  char_features->Features[0]->Params[CharNormLength];
229  cn_feature_[CharNormRx] = char_features->Features[0]->Params[CharNormRx];
230  cn_feature_[CharNormRy] = char_features->Features[0]->Params[CharNormRy];
231  }
232  // Extract the Geo feature.
233  char_features = char_desc->FeatureSets[geo_type];
234  if (char_features == NULL) {
235  tprintf("Error: no Geo feature to train on.\n");
236  } else {
237  ASSERT_HOST(char_features->NumFeatures == 1);
238  geo_feature_[GeoBottom] = char_features->Features[0]->Params[GeoBottom];
239  geo_feature_[GeoTop] = char_features->Features[0]->Params[GeoTop];
240  geo_feature_[GeoWidth] = char_features->Features[0]->Params[GeoWidth];
241  }
242  features_are_indexed_ = false;
243  features_are_mapped_ = false;
244 }
245 
246 // Sets the mapped_features_ from the features_ using the provided
247 // feature_space to the indexed versions of the features.
248 void TrainingSample::IndexFeatures(const IntFeatureSpace& feature_space) {
250  feature_space.IndexAndSortFeatures(features_, num_features_,
251  &mapped_features_);
252  features_are_indexed_ = true;
253  features_are_mapped_ = false;
254 }
255 
256 // Sets the mapped_features_ from the features using the provided
257 // feature_map.
258 void TrainingSample::MapFeatures(const IntFeatureMap& feature_map) {
260  feature_map.feature_space().IndexAndSortFeatures(features_, num_features_,
261  &indexed_features);
262  feature_map.MapIndexedFeatures(indexed_features, &mapped_features_);
263  features_are_indexed_ = false;
264  features_are_mapped_ = true;
265 }
266 
267 // Returns a pix representing the sample. (Int features only.)
268 Pix* TrainingSample::RenderToPix(const UNICHARSET* unicharset) const {
269  Pix* pix = pixCreate(kIntFeatureExtent, kIntFeatureExtent, 1);
270  for (int f = 0; f < num_features_; ++f) {
271  int start_x = features_[f].X;
272  int start_y = kIntFeatureExtent - features_[f].Y;
273  double dx = cos((features_[f].Theta / 256.0) * 2.0 * PI - PI);
274  double dy = -sin((features_[f].Theta / 256.0) * 2.0 * PI - PI);
275  for (int i = 0; i <= 5; ++i) {
276  int x = static_cast<int>(start_x + dx * i);
277  int y = static_cast<int>(start_y + dy * i);
278  if (x >= 0 && x < 256 && y >= 0 && y < 256)
279  pixSetPixel(pix, x, y, 1);
280  }
281  }
282  if (unicharset != NULL)
283  pixSetText(pix, unicharset->id_to_unichar(class_id_));
284  return pix;
285 }
286 
287 // Displays the features in the given window with the given color.
289  ScrollView* window) const {
290  #ifndef GRAPHICS_DISABLED
291  for (int f = 0; f < num_features_; ++f) {
292  RenderIntFeature(window, &features_[f], color);
293  }
294  #endif // GRAPHICS_DISABLED
295 }
296 
297 // Returns a pix of the original sample image. The pix is padded all round
298 // by padding wherever possible.
299 // The returned Pix must be pixDestroyed after use.
300 // If the input page_pix is NULL, NULL is returned.
301 Pix* TrainingSample::GetSamplePix(int padding, Pix* page_pix) const {
302  if (page_pix == NULL)
303  return NULL;
304  int page_width = pixGetWidth(page_pix);
305  int page_height = pixGetHeight(page_pix);
306  TBOX padded_box = bounding_box();
307  padded_box.pad(padding, padding);
308  // Clip the padded_box to the limits of the page
309  TBOX page_box(0, 0, page_width, page_height);
310  padded_box &= page_box;
311  Box* box = boxCreate(page_box.left(), page_height - page_box.top(),
312  page_box.width(), page_box.height());
313  Pix* sample_pix = pixClipRectangle(page_pix, box, NULL);
314  boxDestroy(&box);
315  return sample_pix;
316 }
317 
318 } // namespace tesseract