Tesseract  3.02
tesseract::CubeSearchObject Class Reference

#include <cube_search_object.h>

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

List of all members.

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)

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.

    : SearchObject(cntxt) {
  init_ = false;
  reco_cache_ = NULL;
  samp_cache_ = NULL;
  segments_ = NULL;
  segment_cnt_ = 0;
  samp_ = samp;
  left_ = 0;
  itop_ = 0;
  space_cost_ = NULL;
  no_space_cost_ = NULL;
  wid_ = samp_->Width();
  hgt_ = samp_->Height();
  max_seg_per_char_ = cntxt_->Params()->MaxSegPerChar();
  rtl_ = (cntxt_->ReadingOrder() == CubeRecoContext::R2L);
  min_spc_gap_ =
      static_cast<int>(hgt_ * cntxt_->Params()->MinSpaceHeightRatio());
  max_spc_gap_ =
      static_cast<int>(hgt_ * cntxt_->Params()->MaxSpaceHeightRatio());
}
tesseract::CubeSearchObject::~CubeSearchObject ( )

Definition at line 50 of file cube_search_object.cpp.

                                    {
  Cleanup();
}

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.

                                                       {
  if (!init_ && !Init())
    return NULL;
  if (!IsValidSegmentRange(start_pt, end_pt)) {
    fprintf(stderr, "Cube ERROR (CubeSearchObject::CharBox): invalid "
            "segment range (%d, %d)\n", start_pt, end_pt);
    return NULL;
  }

  // create a char samp object from the specified range of segments,
  // extract its dimensions into a leptonica box, and delete it
  bool left_most;
  bool right_most;
  CharSamp *samp = CharSamp::FromConComps(segments_, start_pt + 1,
                                          end_pt - start_pt, NULL,
                                          &left_most, &right_most, hgt_);
  if (!samp)
    return NULL;
  if (kUseCroppedChars) {
    CharSamp *cropped_samp = samp->Crop();
    delete samp;
    if (!cropped_samp) {
      return NULL;
    }
    samp = cropped_samp;
  }
  Box *box = boxCreate(samp->Left(), samp->Top(),
                       samp->Width(), samp->Height());
  delete samp;
  return box;
}
CharSamp * tesseract::CubeSearchObject::CharSample ( int  start_pt,
int  end_pt 
) [virtual]

Implements tesseract::SearchObject.

Definition at line 168 of file cube_search_object.cpp.

                                                               {
  // init if necessary
  if (!init_ && !Init())
    return NULL;
  // validate segment range
  if (!IsValidSegmentRange(start_pt, end_pt))
    return NULL;

  // look for the samp in the cache
  if (samp_cache_ && samp_cache_[start_pt + 1] &&
      samp_cache_[start_pt + 1][end_pt]) {
    return samp_cache_[start_pt + 1][end_pt];
  }
  // create a char samp object from the specified range of segments
  bool left_most;
  bool right_most;
  CharSamp *samp = CharSamp::FromConComps(segments_, start_pt + 1,
                                          end_pt - start_pt, NULL,
                                          &left_most, &right_most, hgt_);
  if (!samp)
    return NULL;

  if (kUseCroppedChars) {
    CharSamp *cropped_samp = samp->Crop();
    // we no longer need the orig sample
    delete samp;
    if (!cropped_samp)
      return NULL;
    samp = cropped_samp;
  }

  // get the dimensions of the new cropped sample
  int char_top = samp->Top();
  int char_wid = samp->Width();
  int char_hgt = samp->Height();

  // for cursive languages, these features correspond to whether
  // the charsamp is at the beginning or end of conncomp
  if (cntxt_->Cursive() == true) {
    // first and last char flags depend on reading order
    bool first_char = rtl_ ? right_most : left_most;
    bool last_char = rtl_ ? left_most : right_most;

    samp->SetFirstChar(first_char ? 255 : 0);
    samp->SetLastChar(last_char ? 255 : 0);
  } else {
    // for non cursive languages, these features correspond
    // to whether the charsamp is at the begining or end of the word
    samp->SetFirstChar((start_pt == -1) ? 255 : 0);
    samp->SetLastChar((end_pt == (segment_cnt_ - 1)) ? 255 : 0);
  }
  samp->SetNormTop(255 * char_top / hgt_);
  samp->SetNormBottom(255 * (char_top + char_hgt) / hgt_);
  samp->SetNormAspectRatio(255 * char_wid / (char_wid + char_hgt));

  // add to cache & return
  samp_cache_[start_pt + 1][end_pt] = samp;
  return samp;
}
int tesseract::CubeSearchObject::NoSpaceCost ( int  seg_pt) [virtual]

Implements tesseract::SearchObject.

Definition at line 437 of file cube_search_object.cpp.

                                            {
  // If failed to compute costs, return a 1.0 prob
  if (!space_cost_ && !ComputeSpaceCosts())
    return CubeUtils::Prob2Cost(0.0);
  return no_space_cost_[pt_idx];
}
int tesseract::CubeSearchObject::NoSpaceCost ( int  seg_pt,
int  end_pt 
) [virtual]

Implements tesseract::SearchObject.

Definition at line 446 of file cube_search_object.cpp.

                                                       {
  // If fail to compute costs, return a 1.0 prob
  if (!space_cost_ && !ComputeSpaceCosts())
    return CubeUtils::Prob2Cost(1.0);
  int no_spc_cost = 0;
  for (int pt_idx = st_pt + 1; pt_idx < end_pt; pt_idx++)
    no_spc_cost += NoSpaceCost(pt_idx);
  return no_spc_cost;
}
CharAltList * tesseract::CubeSearchObject::RecognizeSegment ( int  start_pt,
int  end_pt 
) [virtual]

Implements tesseract::SearchObject.

Definition at line 262 of file cube_search_object.cpp.

                                                                         {
  // init if necessary
  if (!init_ && !Init()) {
    fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): could "
            "not initialize CubeSearchObject\n");
    return NULL;
  }

  // validate segment range
  if (!IsValidSegmentRange(start_pt, end_pt)) {
    fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): invalid "
            "segment range (%d, %d)\n", start_pt, end_pt);
    return NULL;
  }

  // look for the recognition results in cache in the cache
  if (reco_cache_ && reco_cache_[start_pt + 1] &&
      reco_cache_[start_pt + 1][end_pt]) {
    return reco_cache_[start_pt + 1][end_pt];
  }

  // create the char sample corresponding to the blob
  CharSamp *samp = CharSample(start_pt, end_pt);
  if (!samp) {
    fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): could "
            "not construct CharSamp\n");
    return NULL;
  }

  // recognize the char sample
  CharClassifier *char_classifier = cntxt_->Classifier();
  if (char_classifier) {
    reco_cache_[start_pt + 1][end_pt] = char_classifier->Classify(samp);
  } else {
    // no classifer: all characters are equally probable; add a penalty
    // that favors 2-segment characters and aspect ratios (w/h) > 1
    fprintf(stderr, "Cube WARNING (CubeSearchObject::RecognizeSegment): cube "
            "context has no character classifier!! Inventing a probability "
            "distribution.\n");
    int class_cnt = cntxt_->CharacterSet()->ClassCount();
    CharAltList *alt_list = new CharAltList(cntxt_->CharacterSet(), class_cnt);
    int seg_cnt = end_pt - start_pt;
    double prob_val = (1.0 / class_cnt) *
        exp(-abs(seg_cnt - 2.0)) *
        exp(-samp->Width() / static_cast<double>(samp->Height()));

    if (alt_list) {
      for (int class_idx = 0; class_idx < class_cnt; class_idx++) {
        alt_list->Insert(class_idx, CubeUtils::Prob2Cost(prob_val));
      }
      reco_cache_[start_pt + 1][end_pt] = alt_list;
    }
  }

  return reco_cache_[start_pt + 1][end_pt];
}
int tesseract::CubeSearchObject::SegPtCnt ( ) [virtual]

Implements tesseract::SearchObject.

Definition at line 114 of file cube_search_object.cpp.

                               {
  if (!init_ && !Init())
    return -1;
  return segment_cnt_ - 1;
}
int tesseract::CubeSearchObject::SpaceCost ( int  seg_pt) [virtual]

Implements tesseract::SearchObject.

Definition at line 427 of file cube_search_object.cpp.

                                          {
  if (!space_cost_ && !ComputeSpaceCosts()) {
    // Failed to compute costs return a zero prob
    return CubeUtils::Prob2Cost(0.0);
  }
  return space_cost_[pt_idx];
}

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