tesseract  4.0.0-1-g2a2b
tesseract::TrainingSample Class Reference

#include <trainingsample.h>

Inheritance diagram for tesseract::TrainingSample:
ELIST_LINK

Public Member Functions

 TrainingSample ()
 
 ~TrainingSample ()
 
FEATURE_STRUCTGetCNFeature () const
 
TrainingSampleRandomizedCopy (int index) const
 
TrainingSampleCopy () const
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
void ExtractCharDesc (int feature_type, int micro_type, int cn_type, int geo_type, CHAR_DESC_STRUCT *char_desc)
 
void IndexFeatures (const IntFeatureSpace &feature_space)
 
void MapFeatures (const IntFeatureMap &feature_map)
 
Pix * RenderToPix (const UNICHARSET *unicharset) const
 
void DisplayFeatures (ScrollView::Color color, ScrollView *window) const
 
Pix * GetSamplePix (int padding, Pix *page_pix) const
 
UNICHAR_ID class_id () const
 
void set_class_id (int id)
 
int font_id () const
 
void set_font_id (int id)
 
int page_num () const
 
void set_page_num (int page)
 
const TBOXbounding_box () const
 
void set_bounding_box (const TBOX &box)
 
uint32_t num_features () const
 
const INT_FEATURE_STRUCTfeatures () const
 
uint32_t num_micro_features () const
 
const MicroFeaturemicro_features () const
 
int outline_length () const
 
float cn_feature (int index) const
 
int geo_feature (int index) const
 
double weight () const
 
void set_weight (double value)
 
double max_dist () const
 
void set_max_dist (double value)
 
int sample_index () const
 
void set_sample_index (int value)
 
bool features_are_mapped () const
 
const GenericVector< int > & mapped_features () const
 
const GenericVector< int > & indexed_features () const
 
bool is_error () const
 
void set_is_error (bool value)
 
- Public Member Functions inherited from ELIST_LINK
 ELIST_LINK ()
 
 ELIST_LINK (const ELIST_LINK &)
 
void operator= (const ELIST_LINK &)
 

Static Public Member Functions

static TrainingSampleCopyFromFeatures (const INT_FX_RESULT_STRUCT &fx_info, const TBOX &bounding_box, const INT_FEATURE_STRUCT *features, int num_features)
 
static TrainingSampleDeSerializeCreate (bool swap, FILE *fp)
 

Detailed Description

Definition at line 53 of file trainingsample.h.

Constructor & Destructor Documentation

◆ TrainingSample()

tesseract::TrainingSample::TrainingSample ( )
inline

Definition at line 55 of file trainingsample.h.

56  : class_id_(INVALID_UNICHAR_ID), font_id_(0), page_num_(0),
57  num_features_(0), num_micro_features_(0), outline_length_(0),
58  features_(nullptr), micro_features_(nullptr), weight_(1.0),
59  max_dist_(0.0), sample_index_(0),
60  features_are_indexed_(false), features_are_mapped_(false),
61  is_error_(false) {
62  }

◆ ~TrainingSample()

tesseract::TrainingSample::~TrainingSample ( )

Definition at line 45 of file trainingsample.cpp.

45  {
46  delete [] features_;
47  delete [] micro_features_;
48 }

Member Function Documentation

◆ bounding_box()

const TBOX& tesseract::TrainingSample::bounding_box ( ) const
inline

Definition at line 134 of file trainingsample.h.

134  {
135  return bounding_box_;
136  }

◆ class_id()

UNICHAR_ID tesseract::TrainingSample::class_id ( ) const
inline

Definition at line 116 of file trainingsample.h.

116  {
117  return class_id_;
118  }

◆ cn_feature()

float tesseract::TrainingSample::cn_feature ( int  index) const
inline

Definition at line 155 of file trainingsample.h.

155  {
156  return cn_feature_[index];
157  }

◆ Copy()

TrainingSample * tesseract::TrainingSample::Copy ( ) const

Definition at line 181 of file trainingsample.cpp.

181  {
183  sample->class_id_ = class_id_;
184  sample->font_id_ = font_id_;
185  sample->weight_ = weight_;
186  sample->sample_index_ = sample_index_;
187  sample->num_features_ = num_features_;
188  if (num_features_ > 0) {
189  sample->features_ = new INT_FEATURE_STRUCT[num_features_];
190  memcpy(sample->features_, features_, num_features_ * sizeof(features_[0]));
191  }
192  sample->num_micro_features_ = num_micro_features_;
193  if (num_micro_features_ > 0) {
194  sample->micro_features_ = new MicroFeature[num_micro_features_];
195  memcpy(sample->micro_features_, micro_features_,
196  num_micro_features_ * sizeof(micro_features_[0]));
197  }
198  memcpy(sample->cn_feature_, cn_feature_, sizeof(*cn_feature_) * kNumCNParams);
199  memcpy(sample->geo_feature_, geo_feature_, sizeof(*geo_feature_) * GeoCount);
200  return sample;
201 }
Definition: cluster.h:32
float MicroFeature[MFCount]
Definition: mf.h:33

◆ CopyFromFeatures()

TrainingSample * tesseract::TrainingSample::CopyFromFeatures ( const INT_FX_RESULT_STRUCT fx_info,
const TBOX bounding_box,
const INT_FEATURE_STRUCT features,
int  num_features 
)
static

Definition at line 124 of file trainingsample.cpp.

128  {
130  sample->num_features_ = num_features;
131  sample->features_ = new INT_FEATURE_STRUCT[num_features];
132  sample->outline_length_ = fx_info.Length;
133  memcpy(sample->features_, features, num_features * sizeof(features[0]));
134  sample->geo_feature_[GeoBottom] = bounding_box.bottom();
135  sample->geo_feature_[GeoTop] = bounding_box.top();
136  sample->geo_feature_[GeoWidth] = bounding_box.width();
137 
138  // Generate the cn_feature_ from the fx_info.
139  sample->cn_feature_[CharNormY] =
141  sample->cn_feature_[CharNormLength] =
143  sample->cn_feature_[CharNormRx] = MF_SCALE_FACTOR * fx_info.Rx;
144  sample->cn_feature_[CharNormRy] = MF_SCALE_FACTOR * fx_info.Ry;
145 
146  sample->features_are_indexed_ = false;
147  sample->features_are_mapped_ = false;
148  return sample;
149 }
Definition: cluster.h:32
#define MF_SCALE_FACTOR
Definition: mfoutline.h:64
const int kBlnBaselineOffset
Definition: normalis.h:25
int16_t width() const
Definition: rect.h:115
int16_t top() const
Definition: rect.h:58
#define LENGTH_COMPRESSION
Definition: normfeat.h:27
const TBOX & bounding_box() const
const INT_FEATURE_STRUCT * features() const
uint32_t num_features() const
int32_t Length
Definition: intfx.h:36
int16_t Ymean
Definition: intfx.h:37
int16_t bottom() const
Definition: rect.h:65

◆ DeSerialize()

bool tesseract::TrainingSample::DeSerialize ( bool  swap,
FILE *  fp 
)

Definition at line 87 of file trainingsample.cpp.

87  {
88  if (fread(&class_id_, sizeof(class_id_), 1, fp) != 1) return false;
89  if (fread(&font_id_, sizeof(font_id_), 1, fp) != 1) return false;
90  if (fread(&page_num_, sizeof(page_num_), 1, fp) != 1) return false;
91  if (!bounding_box_.DeSerialize(swap, fp)) return false;
92  if (fread(&num_features_, sizeof(num_features_), 1, fp) != 1) return false;
93  if (fread(&num_micro_features_, sizeof(num_micro_features_), 1, fp) != 1)
94  return false;
95  if (fread(&outline_length_, sizeof(outline_length_), 1, fp) != 1)
96  return false;
97  if (swap) {
98  ReverseN(&class_id_, sizeof(class_id_));
99  ReverseN(&num_features_, sizeof(num_features_));
100  ReverseN(&num_micro_features_, sizeof(num_micro_features_));
101  ReverseN(&outline_length_, sizeof(outline_length_));
102  }
103  // Arbitrarily limit the number of elements to protect against bad data.
104  if (num_features_ > UINT16_MAX) return false;
105  if (num_micro_features_ > UINT16_MAX) return false;
106  delete [] features_;
107  features_ = new INT_FEATURE_STRUCT[num_features_];
108  if (fread(features_, sizeof(*features_), num_features_, fp)
109  != num_features_)
110  return false;
111  delete [] micro_features_;
112  micro_features_ = new MicroFeature[num_micro_features_];
113  if (fread(micro_features_, sizeof(*micro_features_), num_micro_features_,
114  fp) != num_micro_features_)
115  return false;
116  if (fread(cn_feature_, sizeof(*cn_feature_), kNumCNParams, fp) !=
117  kNumCNParams) return false;
118  if (fread(geo_feature_, sizeof(*geo_feature_), GeoCount, fp) != GeoCount)
119  return false;
120  return true;
121 }
bool DeSerialize(bool swap, FILE *fp)
Definition: rect.cpp:192
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178
float MicroFeature[MFCount]
Definition: mf.h:33

◆ DeSerializeCreate()

TrainingSample * tesseract::TrainingSample::DeSerializeCreate ( bool  swap,
FILE *  fp 
)
static

Definition at line 78 of file trainingsample.cpp.

78  {
80  if (sample->DeSerialize(swap, fp)) return sample;
81  delete sample;
82  return nullptr;
83 }
Definition: cluster.h:32

◆ DisplayFeatures()

void tesseract::TrainingSample::DisplayFeatures ( ScrollView::Color  color,
ScrollView window 
) const

Definition at line 315 of file trainingsample.cpp.

316  {
317  #ifndef GRAPHICS_DISABLED
318  for (uint32_t f = 0; f < num_features_; ++f) {
319  RenderIntFeature(window, &features_[f], color);
320  }
321  #endif // GRAPHICS_DISABLED
322 }
void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT *Feature, ScrollView::Color color)
Definition: intproto.cpp:1628

◆ ExtractCharDesc()

void tesseract::TrainingSample::ExtractCharDesc ( int  feature_type,
int  micro_type,
int  cn_type,
int  geo_type,
CHAR_DESC_STRUCT char_desc 
)

Definition at line 204 of file trainingsample.cpp.

208  {
209  // Extract the INT features.
210  delete[] features_;
211  FEATURE_SET_STRUCT* char_features = char_desc->FeatureSets[int_feature_type];
212  if (char_features == nullptr) {
213  tprintf("Error: no features to train on of type %s\n",
215  num_features_ = 0;
216  features_ = nullptr;
217  } else {
218  num_features_ = char_features->NumFeatures;
219  features_ = new INT_FEATURE_STRUCT[num_features_];
220  for (uint32_t f = 0; f < num_features_; ++f) {
221  features_[f].X =
222  static_cast<uint8_t>(char_features->Features[f]->Params[IntX]);
223  features_[f].Y =
224  static_cast<uint8_t>(char_features->Features[f]->Params[IntY]);
225  features_[f].Theta =
226  static_cast<uint8_t>(char_features->Features[f]->Params[IntDir]);
227  features_[f].CP_misses = 0;
228  }
229  }
230  // Extract the Micro features.
231  delete[] micro_features_;
232  char_features = char_desc->FeatureSets[micro_type];
233  if (char_features == nullptr) {
234  tprintf("Error: no features to train on of type %s\n",
236  num_micro_features_ = 0;
237  micro_features_ = nullptr;
238  } else {
239  num_micro_features_ = char_features->NumFeatures;
240  micro_features_ = new MicroFeature[num_micro_features_];
241  for (uint32_t f = 0; f < num_micro_features_; ++f) {
242  for (int d = 0; d < MFCount; ++d) {
243  micro_features_[f][d] = char_features->Features[f]->Params[d];
244  }
245  }
246  }
247  // Extract the CN feature.
248  char_features = char_desc->FeatureSets[cn_type];
249  if (char_features == nullptr) {
250  tprintf("Error: no CN feature to train on.\n");
251  } else {
252  ASSERT_HOST(char_features->NumFeatures == 1);
253  cn_feature_[CharNormY] = char_features->Features[0]->Params[CharNormY];
254  cn_feature_[CharNormLength] =
255  char_features->Features[0]->Params[CharNormLength];
256  cn_feature_[CharNormRx] = char_features->Features[0]->Params[CharNormRx];
257  cn_feature_[CharNormRy] = char_features->Features[0]->Params[CharNormRy];
258  }
259  // Extract the Geo feature.
260  char_features = char_desc->FeatureSets[geo_type];
261  if (char_features == nullptr) {
262  tprintf("Error: no Geo feature to train on.\n");
263  } else {
264  ASSERT_HOST(char_features->NumFeatures == 1);
265  geo_feature_[GeoBottom] = char_features->Features[0]->Params[GeoBottom];
266  geo_feature_[GeoTop] = char_features->Features[0]->Params[GeoTop];
267  geo_feature_[GeoWidth] = char_features->Features[0]->Params[GeoWidth];
268  }
269  features_are_indexed_ = false;
270  features_are_mapped_ = false;
271 }
Definition: picofeat.h:31
float Params[1]
Definition: ocrfeatures.h:62
FEATURE Features[1]
Definition: ocrfeatures.h:69
uint16_t NumFeatures
Definition: ocrfeatures.h:67
Definition: picofeat.h:30
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
float MicroFeature[MFCount]
Definition: mf.h:33
const char * kMicroFeatureType
Definition: featdefs.cpp:32
Definition: mf.h:30
const char * kIntFeatureType
Definition: featdefs.cpp:34
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ features()

const INT_FEATURE_STRUCT* tesseract::TrainingSample::features ( ) const
inline

Definition at line 143 of file trainingsample.h.

143  {
144  return features_;
145  }

◆ features_are_mapped()

bool tesseract::TrainingSample::features_are_mapped ( ) const
inline

Definition at line 179 of file trainingsample.h.

179  {
180  return features_are_mapped_;
181  }

◆ font_id()

int tesseract::TrainingSample::font_id ( ) const
inline

Definition at line 122 of file trainingsample.h.

122  {
123  return font_id_;
124  }

◆ geo_feature()

int tesseract::TrainingSample::geo_feature ( int  index) const
inline

Definition at line 158 of file trainingsample.h.

158  {
159  return geo_feature_[index];
160  }

◆ GetCNFeature()

FEATURE_STRUCT * tesseract::TrainingSample::GetCNFeature ( ) const

Definition at line 152 of file trainingsample.cpp.

152  {
153  FEATURE feature = NewFeature(&CharNormDesc);
154  for (int i = 0; i < kNumCNParams; ++i)
155  feature->Params[i] = cn_feature_[i];
156  return feature;
157 }
float Params[1]
Definition: ocrfeatures.h:62
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:81
const FEATURE_DESC_STRUCT CharNormDesc

◆ GetSamplePix()

Pix * tesseract::TrainingSample::GetSamplePix ( int  padding,
Pix *  page_pix 
) const

Definition at line 328 of file trainingsample.cpp.

328  {
329  if (page_pix == nullptr)
330  return nullptr;
331  int page_width = pixGetWidth(page_pix);
332  int page_height = pixGetHeight(page_pix);
333  TBOX padded_box = bounding_box();
334  padded_box.pad(padding, padding);
335  // Clip the padded_box to the limits of the page
336  TBOX page_box(0, 0, page_width, page_height);
337  padded_box &= page_box;
338  Box* box = boxCreate(page_box.left(), page_height - page_box.top(),
339  page_box.width(), page_box.height());
340  Pix* sample_pix = pixClipRectangle(page_pix, box, nullptr);
341  boxDestroy(&box);
342  return sample_pix;
343 }
Definition: rect.h:34
const TBOX & bounding_box() const
void pad(int xpad, int ypad)
Definition: rect.h:131

◆ indexed_features()

const GenericVector<int>& tesseract::TrainingSample::indexed_features ( ) const
inline

Definition at line 186 of file trainingsample.h.

186  {
187  ASSERT_HOST(features_are_indexed_);
188  return mapped_features_;
189  }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ IndexFeatures()

void tesseract::TrainingSample::IndexFeatures ( const IntFeatureSpace feature_space)

Definition at line 275 of file trainingsample.cpp.

275  {
277  feature_space.IndexAndSortFeatures(features_, num_features_,
278  &mapped_features_);
279  features_are_indexed_ = true;
280  features_are_mapped_ = false;
281 }
const GenericVector< int > & indexed_features() const

◆ is_error()

bool tesseract::TrainingSample::is_error ( ) const
inline

Definition at line 190 of file trainingsample.h.

190  {
191  return is_error_;
192  }

◆ MapFeatures()

void tesseract::TrainingSample::MapFeatures ( const IntFeatureMap feature_map)

Definition at line 285 of file trainingsample.cpp.

285  {
287  feature_map.feature_space().IndexAndSortFeatures(features_, num_features_,
289  feature_map.MapIndexedFeatures(indexed_features, &mapped_features_);
290  features_are_indexed_ = false;
291  features_are_mapped_ = true;
292 }
const GenericVector< int > & indexed_features() const

◆ mapped_features()

const GenericVector<int>& tesseract::TrainingSample::mapped_features ( ) const
inline

Definition at line 182 of file trainingsample.h.

182  {
183  ASSERT_HOST(features_are_mapped_);
184  return mapped_features_;
185  }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ max_dist()

double tesseract::TrainingSample::max_dist ( ) const
inline

Definition at line 167 of file trainingsample.h.

167  {
168  return max_dist_;
169  }

◆ micro_features()

const MicroFeature* tesseract::TrainingSample::micro_features ( ) const
inline

Definition at line 149 of file trainingsample.h.

149  {
150  return micro_features_;
151  }

◆ num_features()

uint32_t tesseract::TrainingSample::num_features ( ) const
inline

Definition at line 140 of file trainingsample.h.

140  {
141  return num_features_;
142  }

◆ num_micro_features()

uint32_t tesseract::TrainingSample::num_micro_features ( ) const
inline

Definition at line 146 of file trainingsample.h.

146  {
147  return num_micro_features_;
148  }

◆ outline_length()

int tesseract::TrainingSample::outline_length ( ) const
inline

Definition at line 152 of file trainingsample.h.

152  {
153  return outline_length_;
154  }

◆ page_num()

int tesseract::TrainingSample::page_num ( ) const
inline

Definition at line 128 of file trainingsample.h.

128  {
129  return page_num_;
130  }

◆ RandomizedCopy()

TrainingSample * tesseract::TrainingSample::RandomizedCopy ( int  index) const

Definition at line 162 of file trainingsample.cpp.

162  {
164  if (index >= 0 && index < kSampleRandomSize) {
165  ++index; // Remove the first combination.
166  const int yshift = kYShiftValues[index / kSampleScaleSize];
167  double scaling = kScaleValues[index % kSampleScaleSize];
168  for (uint32_t i = 0; i < num_features_; ++i) {
169  double result = (features_[i].X - kRandomizingCenter) * scaling;
170  result += kRandomizingCenter;
171  sample->features_[i].X = ClipToRange<int>(result + 0.5, 0, UINT8_MAX);
172  result = (features_[i].Y - kRandomizingCenter) * scaling;
173  result += kRandomizingCenter + yshift;
174  sample->features_[i].Y = ClipToRange<int>(result + 0.5, 0, UINT8_MAX);
175  }
176  }
177  return sample;
178 }
Definition: cluster.h:32
const int kRandomizingCenter
TrainingSample * Copy() const

◆ RenderToPix()

Pix * tesseract::TrainingSample::RenderToPix ( const UNICHARSET unicharset) const

Definition at line 295 of file trainingsample.cpp.

295  {
296  Pix* pix = pixCreate(kIntFeatureExtent, kIntFeatureExtent, 1);
297  for (uint32_t f = 0; f < num_features_; ++f) {
298  int start_x = features_[f].X;
299  int start_y = kIntFeatureExtent - features_[f].Y;
300  double dx = cos((features_[f].Theta / 256.0) * 2.0 * M_PI - M_PI);
301  double dy = -sin((features_[f].Theta / 256.0) * 2.0 * M_PI - M_PI);
302  for (int i = 0; i <= 5; ++i) {
303  int x = static_cast<int>(start_x + dx * i);
304  int y = static_cast<int>(start_y + dy * i);
305  if (x >= 0 && x < 256 && y >= 0 && y < 256)
306  pixSetPixel(pix, x, y, 1);
307  }
308  }
309  if (unicharset != nullptr)
310  pixSetText(pix, unicharset->id_to_unichar(class_id_));
311  return pix;
312 }
const int kIntFeatureExtent
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:290

◆ sample_index()

int tesseract::TrainingSample::sample_index ( ) const
inline

Definition at line 173 of file trainingsample.h.

173  {
174  return sample_index_;
175  }

◆ Serialize()

bool tesseract::TrainingSample::Serialize ( FILE *  fp) const

Definition at line 54 of file trainingsample.cpp.

54  {
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(&outline_length_, sizeof(outline_length_), 1, fp) != 1)
63  return false;
64  if (fwrite(features_, sizeof(*features_), num_features_, fp) != num_features_)
65  return false;
66  if (fwrite(micro_features_, sizeof(*micro_features_), num_micro_features_,
67  fp) != num_micro_features_)
68  return false;
69  if (fwrite(cn_feature_, sizeof(*cn_feature_), kNumCNParams, fp) !=
70  kNumCNParams) return false;
71  if (fwrite(geo_feature_, sizeof(*geo_feature_), GeoCount, fp) != GeoCount)
72  return false;
73  return true;
74 }
bool Serialize(FILE *fp) const
Definition: rect.cpp:185

◆ set_bounding_box()

void tesseract::TrainingSample::set_bounding_box ( const TBOX box)
inline

Definition at line 137 of file trainingsample.h.

137  {
138  bounding_box_ = box;
139  }

◆ set_class_id()

void tesseract::TrainingSample::set_class_id ( int  id)
inline

Definition at line 119 of file trainingsample.h.

119  {
120  class_id_ = id;
121  }

◆ set_font_id()

void tesseract::TrainingSample::set_font_id ( int  id)
inline

Definition at line 125 of file trainingsample.h.

125  {
126  font_id_ = id;
127  }

◆ set_is_error()

void tesseract::TrainingSample::set_is_error ( bool  value)
inline

Definition at line 193 of file trainingsample.h.

193  {
194  is_error_ = value;
195  }

◆ set_max_dist()

void tesseract::TrainingSample::set_max_dist ( double  value)
inline

Definition at line 170 of file trainingsample.h.

170  {
171  max_dist_ = value;
172  }

◆ set_page_num()

void tesseract::TrainingSample::set_page_num ( int  page)
inline

Definition at line 131 of file trainingsample.h.

131  {
132  page_num_ = page;
133  }

◆ set_sample_index()

void tesseract::TrainingSample::set_sample_index ( int  value)
inline

Definition at line 176 of file trainingsample.h.

176  {
177  sample_index_ = value;
178  }

◆ set_weight()

void tesseract::TrainingSample::set_weight ( double  value)
inline

Definition at line 164 of file trainingsample.h.

164  {
165  weight_ = value;
166  }

◆ weight()

double tesseract::TrainingSample::weight ( ) const
inline

Definition at line 161 of file trainingsample.h.

161  {
162  return weight_;
163  }

The documentation for this class was generated from the following files: