tesseract  3.05.02
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

◆ CubeSearchObject()

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
unsigned short Height() const
Definition: bmp_8.h:50
ReadOrder ReadingOrder() const
SearchObject(CubeRecoContext *cntxt)
Definition: search_object.h:38
unsigned short Width() const
Definition: bmp_8.h:48
TuningParams * Params() const
double MinSpaceHeightRatio() const
Definition: tuning_params.h:60
CubeRecoContext * cntxt_
Definition: search_object.h:51

◆ ~CubeSearchObject()

tesseract::CubeSearchObject::~CubeSearchObject ( )

Definition at line 50 of file cube_search_object.cpp.

50  {
51  Cleanup();
52 }

Member Function Documentation

◆ CharBox()

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

Implements tesseract::SearchObject.

Definition at line 206 of file cube_search_object.cpp.

206  {
207  if (!init_ && !Init())
208  return NULL;
209  if (!IsValidSegmentRange(start_pt, end_pt)) {
210  fprintf(stderr, "Cube ERROR (CubeSearchObject::CharBox): invalid "
211  "segment range (%d, %d)\n", start_pt, end_pt);
212  return NULL;
213  }
214 
215  // create a char samp object from the specified range of segments,
216  // extract its dimensions into a leptonica box, and delete it
217  bool left_most;
218  bool right_most;
219  CharSamp *samp = CharSamp::FromConComps(segments_, start_pt + 1,
220  end_pt - start_pt, NULL,
221  &left_most, &right_most, hgt_);
222  if (!samp)
223  return NULL;
224  if (kUseCroppedChars) {
225  CharSamp *cropped_samp = samp->Crop();
226  delete samp;
227  if (!cropped_samp) {
228  return NULL;
229  }
230  samp = cropped_samp;
231  }
232  Box *box = boxCreate(samp->Left(), samp->Top(),
233  samp->Width(), samp->Height());
234  delete samp;
235  return box;
236 }
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:439

◆ CharSample()

CharSamp * tesseract::CubeSearchObject::CharSample ( int  start_pt,
int  end_pt 
)
virtual

Implements tesseract::SearchObject.

Definition at line 146 of file cube_search_object.cpp.

146  {
147  // init if necessary
148  if (!init_ && !Init())
149  return NULL;
150  // validate segment range
151  if (!IsValidSegmentRange(start_pt, end_pt))
152  return NULL;
153 
154  // look for the samp in the cache
155  if (samp_cache_ && samp_cache_[start_pt + 1] &&
156  samp_cache_[start_pt + 1][end_pt]) {
157  return samp_cache_[start_pt + 1][end_pt];
158  }
159  // create a char samp object from the specified range of segments
160  bool left_most;
161  bool right_most;
162  CharSamp *samp = CharSamp::FromConComps(segments_, start_pt + 1,
163  end_pt - start_pt, NULL,
164  &left_most, &right_most, hgt_);
165  if (!samp)
166  return NULL;
167 
168  if (kUseCroppedChars) {
169  CharSamp *cropped_samp = samp->Crop();
170  // we no longer need the orig sample
171  delete samp;
172  if (!cropped_samp)
173  return NULL;
174  samp = cropped_samp;
175  }
176 
177  // get the dimensions of the new cropped sample
178  int char_top = samp->Top();
179  int char_wid = samp->Width();
180  int char_hgt = samp->Height();
181 
182  // for cursive languages, these features correspond to whether
183  // the charsamp is at the beginning or end of conncomp
184  if (cntxt_->Cursive() == true) {
185  // first and last char flags depend on reading order
186  bool first_char = rtl_ ? right_most : left_most;
187  bool last_char = rtl_ ? left_most : right_most;
188 
189  samp->SetFirstChar(first_char ? 255 : 0);
190  samp->SetLastChar(last_char ? 255 : 0);
191  } else {
192  // for non cursive languages, these features correspond
193  // to whether the charsamp is at the beginning or end of the word
194  samp->SetFirstChar((start_pt == -1) ? 255 : 0);
195  samp->SetLastChar((end_pt == (segment_cnt_ - 1)) ? 255 : 0);
196  }
197  samp->SetNormTop(255 * char_top / hgt_);
198  samp->SetNormBottom(255 * (char_top + char_hgt) / hgt_);
199  samp->SetNormAspectRatio(255 * char_wid / (char_wid + char_hgt));
200 
201  // add to cache & return
202  samp_cache_[start_pt + 1][end_pt] = samp;
203  return samp;
204 }
CubeRecoContext * cntxt_
Definition: search_object.h:51
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:439

◆ NoSpaceCost() [1/2]

int tesseract::CubeSearchObject::NoSpaceCost ( int  seg_pt)
virtual

Implements tesseract::SearchObject.

Definition at line 403 of file cube_search_object.cpp.

403  {
404  // If failed to compute costs, return a 1.0 prob
405  if (!space_cost_ && !ComputeSpaceCosts())
406  return CubeUtils::Prob2Cost(0.0);
407  return no_space_cost_[pt_idx];
408 }
static int Prob2Cost(double prob_val)
Definition: cube_utils.cpp:37

◆ NoSpaceCost() [2/2]

int tesseract::CubeSearchObject::NoSpaceCost ( int  seg_pt,
int  end_pt 
)
virtual

Implements tesseract::SearchObject.

Definition at line 412 of file cube_search_object.cpp.

412  {
413  // If fail to compute costs, return a 1.0 prob
414  if (!space_cost_ && !ComputeSpaceCosts())
415  return CubeUtils::Prob2Cost(1.0);
416  int no_spc_cost = 0;
417  for (int pt_idx = st_pt + 1; pt_idx < end_pt; pt_idx++)
418  no_spc_cost += NoSpaceCost(pt_idx);
419  return no_spc_cost;
420 }
static int Prob2Cost(double prob_val)
Definition: cube_utils.cpp:37

◆ RecognizeSegment()

CharAltList * tesseract::CubeSearchObject::RecognizeSegment ( int  start_pt,
int  end_pt 
)
virtual

Implements tesseract::SearchObject.

Definition at line 240 of file cube_search_object.cpp.

240  {
241  // init if necessary
242  if (!init_ && !Init()) {
243  fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): could "
244  "not initialize CubeSearchObject\n");
245  return NULL;
246  }
247 
248  // validate segment range
249  if (!IsValidSegmentRange(start_pt, end_pt)) {
250  fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): invalid "
251  "segment range (%d, %d)\n", start_pt, end_pt);
252  return NULL;
253  }
254 
255  // look for the recognition results in cache in the cache
256  if (reco_cache_ && reco_cache_[start_pt + 1] &&
257  reco_cache_[start_pt + 1][end_pt]) {
258  return reco_cache_[start_pt + 1][end_pt];
259  }
260 
261  // create the char sample corresponding to the blob
262  CharSamp *samp = CharSample(start_pt, end_pt);
263  if (!samp) {
264  fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): could "
265  "not construct CharSamp\n");
266  return NULL;
267  }
268 
269  // recognize the char sample
270  CharClassifier *char_classifier = cntxt_->Classifier();
271  if (char_classifier) {
272  reco_cache_[start_pt + 1][end_pt] = char_classifier->Classify(samp);
273  } else {
274  // no classifer: all characters are equally probable; add a penalty
275  // that favors 2-segment characters and aspect ratios (w/h) > 1
276  fprintf(stderr, "Cube WARNING (CubeSearchObject::RecognizeSegment): cube "
277  "context has no character classifier!! Inventing a probability "
278  "distribution.\n");
279  int class_cnt = cntxt_->CharacterSet()->ClassCount();
280  CharAltList *alt_list = new CharAltList(cntxt_->CharacterSet(), class_cnt);
281  int seg_cnt = end_pt - start_pt;
282  double prob_val = (1.0 / class_cnt) *
283  exp(-fabs(seg_cnt - 2.0)) *
284  exp(-samp->Width() / static_cast<double>(samp->Height()));
285 
286  for (int class_idx = 0; class_idx < class_cnt; class_idx++) {
287  alt_list->Insert(class_idx, CubeUtils::Prob2Cost(prob_val));
288  }
289  reco_cache_[start_pt + 1][end_pt] = alt_list;
290  }
291 
292  return reco_cache_[start_pt + 1][end_pt];
293 }
CharClassifier * Classifier() const
static int Prob2Cost(double prob_val)
Definition: cube_utils.cpp:37
int ClassCount() const
Definition: char_set.h:111
CharSet * CharacterSet() const
CharSamp * CharSample(int start_pt, int end_pt)
CubeRecoContext * cntxt_
Definition: search_object.h:51

◆ SegPtCnt()

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 }

◆ SpaceCost()

int tesseract::CubeSearchObject::SpaceCost ( int  seg_pt)
virtual

Implements tesseract::SearchObject.

Definition at line 393 of file cube_search_object.cpp.

393  {
394  if (!space_cost_ && !ComputeSpaceCosts()) {
395  // Failed to compute costs return a zero prob
396  return CubeUtils::Prob2Cost(0.0);
397  }
398  return space_cost_[pt_idx];
399 }
static int Prob2Cost(double prob_val)
Definition: cube_utils.cpp:37

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