All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::CubeSearchObject Class Reference

#include <cube_search_object.h>

Inheritance diagram for tesseract::CubeSearchObject:
tesseract::SearchObject

Public Member Functions

 CubeSearchObject (CubeRecoContext *cntxt, CharSamp *samp)
 
 ~CubeSearchObject ()
 
int SegPtCnt ()
 
CharAltListRecognizeSegment (int start_pt, int end_pt)
 
CharSampCharSample (int start_pt, int end_pt)
 
Box * CharBox (int start_pt, int end_pt)
 
int SpaceCost (int seg_pt)
 
int NoSpaceCost (int seg_pt)
 
int NoSpaceCost (int seg_pt, int end_pt)
 
- Public Member Functions inherited from tesseract::SearchObject
 SearchObject (CubeRecoContext *cntxt)
 
virtual ~SearchObject ()
 

Additional Inherited Members

- Protected Attributes inherited from tesseract::SearchObject
CubeRecoContextcntxt_
 

Detailed Description

Definition at line 41 of file cube_search_object.h.

Constructor & Destructor Documentation

tesseract::CubeSearchObject::CubeSearchObject ( CubeRecoContext cntxt,
CharSamp samp 
)

Definition at line 28 of file cube_search_object.cpp.

29  : SearchObject(cntxt) {
30  init_ = false;
31  reco_cache_ = NULL;
32  samp_cache_ = NULL;
33  segments_ = NULL;
34  segment_cnt_ = 0;
35  samp_ = samp;
36  left_ = 0;
37  itop_ = 0;
38  space_cost_ = NULL;
39  no_space_cost_ = NULL;
40  wid_ = samp_->Width();
41  hgt_ = samp_->Height();
42  max_seg_per_char_ = cntxt_->Params()->MaxSegPerChar();
44  min_spc_gap_ =
45  static_cast<int>(hgt_ * cntxt_->Params()->MinSpaceHeightRatio());
46  max_spc_gap_ =
47  static_cast<int>(hgt_ * cntxt_->Params()->MaxSpaceHeightRatio());
48 }
double MaxSpaceHeightRatio() const
Definition: tuning_params.h:61
double MinSpaceHeightRatio() const
Definition: tuning_params.h:60
int MaxSegPerChar() const
Definition: tuning_params.h:52
SearchObject(CubeRecoContext *cntxt)
Definition: search_object.h:38
ReadOrder ReadingOrder() const
unsigned short Width() const
Definition: bmp_8.h:48
TuningParams * Params() const
CubeRecoContext * cntxt_
Definition: search_object.h:51
unsigned short Height() const
Definition: bmp_8.h:50
#define NULL
Definition: host.h:144
tesseract::CubeSearchObject::~CubeSearchObject ( )

Definition at line 50 of file cube_search_object.cpp.

50  {
51  Cleanup();
52 }

Member Function Documentation

Box * tesseract::CubeSearchObject::CharBox ( int  start_pt,
int  end_pt 
)
virtual

Implements tesseract::SearchObject.

Definition at line 228 of file cube_search_object.cpp.

228  {
229  if (!init_ && !Init())
230  return NULL;
231  if (!IsValidSegmentRange(start_pt, end_pt)) {
232  fprintf(stderr, "Cube ERROR (CubeSearchObject::CharBox): invalid "
233  "segment range (%d, %d)\n", start_pt, end_pt);
234  return NULL;
235  }
236 
237  // create a char samp object from the specified range of segments,
238  // extract its dimensions into a leptonica box, and delete it
239  bool left_most;
240  bool right_most;
241  CharSamp *samp = CharSamp::FromConComps(segments_, start_pt + 1,
242  end_pt - start_pt, NULL,
243  &left_most, &right_most, hgt_);
244  if (!samp)
245  return NULL;
246  if (kUseCroppedChars) {
247  CharSamp *cropped_samp = samp->Crop();
248  delete samp;
249  if (!cropped_samp) {
250  return NULL;
251  }
252  samp = cropped_samp;
253  }
254  Box *box = boxCreate(samp->Left(), samp->Top(),
255  samp->Width(), samp->Height());
256  delete samp;
257  return box;
258 }
static CharSamp * FromConComps(ConComp **concomp_array, int strt_concomp, int seg_flags_size, int *seg_flags, bool *left_most, bool *right_most, int word_hgt)
Definition: char_samp.cpp:457
#define NULL
Definition: host.h:144
CharSamp * tesseract::CubeSearchObject::CharSample ( int  start_pt,
int  end_pt 
)
virtual

Implements tesseract::SearchObject.

Definition at line 168 of file cube_search_object.cpp.

168  {
169  // init if necessary
170  if (!init_ && !Init())
171  return NULL;
172  // validate segment range
173  if (!IsValidSegmentRange(start_pt, end_pt))
174  return NULL;
175 
176  // look for the samp in the cache
177  if (samp_cache_ && samp_cache_[start_pt + 1] &&
178  samp_cache_[start_pt + 1][end_pt]) {
179  return samp_cache_[start_pt + 1][end_pt];
180  }
181  // create a char samp object from the specified range of segments
182  bool left_most;
183  bool right_most;
184  CharSamp *samp = CharSamp::FromConComps(segments_, start_pt + 1,
185  end_pt - start_pt, NULL,
186  &left_most, &right_most, hgt_);
187  if (!samp)
188  return NULL;
189 
190  if (kUseCroppedChars) {
191  CharSamp *cropped_samp = samp->Crop();
192  // we no longer need the orig sample
193  delete samp;
194  if (!cropped_samp)
195  return NULL;
196  samp = cropped_samp;
197  }
198 
199  // get the dimensions of the new cropped sample
200  int char_top = samp->Top();
201  int char_wid = samp->Width();
202  int char_hgt = samp->Height();
203 
204  // for cursive languages, these features correspond to whether
205  // the charsamp is at the beginning or end of conncomp
206  if (cntxt_->Cursive() == true) {
207  // first and last char flags depend on reading order
208  bool first_char = rtl_ ? right_most : left_most;
209  bool last_char = rtl_ ? left_most : right_most;
210 
211  samp->SetFirstChar(first_char ? 255 : 0);
212  samp->SetLastChar(last_char ? 255 : 0);
213  } else {
214  // for non cursive languages, these features correspond
215  // to whether the charsamp is at the begining or end of the word
216  samp->SetFirstChar((start_pt == -1) ? 255 : 0);
217  samp->SetLastChar((end_pt == (segment_cnt_ - 1)) ? 255 : 0);
218  }
219  samp->SetNormTop(255 * char_top / hgt_);
220  samp->SetNormBottom(255 * (char_top + char_hgt) / hgt_);
221  samp->SetNormAspectRatio(255 * char_wid / (char_wid + char_hgt));
222 
223  // add to cache & return
224  samp_cache_[start_pt + 1][end_pt] = samp;
225  return samp;
226 }
static CharSamp * FromConComps(ConComp **concomp_array, int strt_concomp, int seg_flags_size, int *seg_flags, bool *left_most, bool *right_most, int word_hgt)
Definition: char_samp.cpp:457
CubeRecoContext * cntxt_
Definition: search_object.h:51
#define NULL
Definition: host.h:144
int tesseract::CubeSearchObject::NoSpaceCost ( int  seg_pt)
virtual

Implements tesseract::SearchObject.

Definition at line 437 of file cube_search_object.cpp.

437  {
438  // If failed to compute costs, return a 1.0 prob
439  if (!space_cost_ && !ComputeSpaceCosts())
440  return CubeUtils::Prob2Cost(0.0);
441  return no_space_cost_[pt_idx];
442 }
static int Prob2Cost(double prob_val)
Definition: cube_utils.cpp:37
int tesseract::CubeSearchObject::NoSpaceCost ( int  seg_pt,
int  end_pt 
)
virtual

Implements tesseract::SearchObject.

Definition at line 446 of file cube_search_object.cpp.

446  {
447  // If fail to compute costs, return a 1.0 prob
448  if (!space_cost_ && !ComputeSpaceCosts())
449  return CubeUtils::Prob2Cost(1.0);
450  int no_spc_cost = 0;
451  for (int pt_idx = st_pt + 1; pt_idx < end_pt; pt_idx++)
452  no_spc_cost += NoSpaceCost(pt_idx);
453  return no_spc_cost;
454 }
static int Prob2Cost(double prob_val)
Definition: cube_utils.cpp:37
CharAltList * tesseract::CubeSearchObject::RecognizeSegment ( int  start_pt,
int  end_pt 
)
virtual

Implements tesseract::SearchObject.

Definition at line 262 of file cube_search_object.cpp.

262  {
263  // init if necessary
264  if (!init_ && !Init()) {
265  fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): could "
266  "not initialize CubeSearchObject\n");
267  return NULL;
268  }
269 
270  // validate segment range
271  if (!IsValidSegmentRange(start_pt, end_pt)) {
272  fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): invalid "
273  "segment range (%d, %d)\n", start_pt, end_pt);
274  return NULL;
275  }
276 
277  // look for the recognition results in cache in the cache
278  if (reco_cache_ && reco_cache_[start_pt + 1] &&
279  reco_cache_[start_pt + 1][end_pt]) {
280  return reco_cache_[start_pt + 1][end_pt];
281  }
282 
283  // create the char sample corresponding to the blob
284  CharSamp *samp = CharSample(start_pt, end_pt);
285  if (!samp) {
286  fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): could "
287  "not construct CharSamp\n");
288  return NULL;
289  }
290 
291  // recognize the char sample
292  CharClassifier *char_classifier = cntxt_->Classifier();
293  if (char_classifier) {
294  reco_cache_[start_pt + 1][end_pt] = char_classifier->Classify(samp);
295  } else {
296  // no classifer: all characters are equally probable; add a penalty
297  // that favors 2-segment characters and aspect ratios (w/h) > 1
298  fprintf(stderr, "Cube WARNING (CubeSearchObject::RecognizeSegment): cube "
299  "context has no character classifier!! Inventing a probability "
300  "distribution.\n");
301  int class_cnt = cntxt_->CharacterSet()->ClassCount();
302  CharAltList *alt_list = new CharAltList(cntxt_->CharacterSet(), class_cnt);
303  int seg_cnt = end_pt - start_pt;
304  double prob_val = (1.0 / class_cnt) *
305  exp(-fabs(seg_cnt - 2.0)) *
306  exp(-samp->Width() / static_cast<double>(samp->Height()));
307 
308  if (alt_list) {
309  for (int class_idx = 0; class_idx < class_cnt; class_idx++) {
310  alt_list->Insert(class_idx, CubeUtils::Prob2Cost(prob_val));
311  }
312  reco_cache_[start_pt + 1][end_pt] = alt_list;
313  }
314  }
315 
316  return reco_cache_[start_pt + 1][end_pt];
317 }
static int Prob2Cost(double prob_val)
Definition: cube_utils.cpp:37
CharSamp * CharSample(int start_pt, int end_pt)
CharClassifier * Classifier() const
CharSet * CharacterSet() const
CubeRecoContext * cntxt_
Definition: search_object.h:51
int ClassCount() const
Definition: char_set.h:111
#define NULL
Definition: host.h:144
int tesseract::CubeSearchObject::SegPtCnt ( )
virtual

Implements tesseract::SearchObject.

Definition at line 114 of file cube_search_object.cpp.

114  {
115  if (!init_ && !Init())
116  return -1;
117  return segment_cnt_ - 1;
118 }
int tesseract::CubeSearchObject::SpaceCost ( int  seg_pt)
virtual

Implements tesseract::SearchObject.

Definition at line 427 of file cube_search_object.cpp.

427  {
428  if (!space_cost_ && !ComputeSpaceCosts()) {
429  // Failed to compute costs return a zero prob
430  return CubeUtils::Prob2Cost(0.0);
431  }
432  return space_cost_[pt_idx];
433 }
static int Prob2Cost(double prob_val)
Definition: cube_utils.cpp:37

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