tesseract  4.00.00dev
tesseract::LMPainPoints Class Reference

#include <lm_pain_points.h>

Public Member Functions

 LMPainPoints (int max, float rat, bool fp, const Dict *d, int deb)
 
 ~LMPainPoints ()
 
bool HasPainPoints (LMPainPointsType pp_type) const
 
LMPainPointsType Deque (MATRIX_COORD *pp, float *priority)
 
void Clear ()
 
void GenerateInitial (WERD_RES *word_res)
 
void GenerateFromPath (float rating_cert_scale, ViterbiStateEntry *vse, WERD_RES *word_res)
 
void GenerateFromAmbigs (const DANGERR &fixpt, ViterbiStateEntry *vse, WERD_RES *word_res)
 
bool GenerateForBlamer (double max_char_wh_ratio, WERD_RES *word_res, int col, int row)
 
bool GeneratePainPoint (int col, int row, LMPainPointsType pp_type, float special_priority, bool ok_to_extend, float max_char_wh_ratio, WERD_RES *word_res)
 
void RemapForSplit (int index)
 

Static Public Member Functions

static const char * PainPointDescription (LMPainPointsType type)
 

Static Public Attributes

static const float kDefaultPainPointPriorityAdjustment = 2.0f
 
static const float kLooseMaxCharWhRatio = 2.5f
 

Detailed Description

Definition at line 53 of file lm_pain_points.h.

Constructor & Destructor Documentation

◆ LMPainPoints()

tesseract::LMPainPoints::LMPainPoints ( int  max,
float  rat,
bool  fp,
const Dict d,
int  deb 
)
inline

Definition at line 66 of file lm_pain_points.h.

66  :
67  max_heap_size_(max), max_char_wh_ratio_(rat), fixed_pitch_(fp),
68  dict_(d), debug_level_(deb) {}
const int max

◆ ~LMPainPoints()

tesseract::LMPainPoints::~LMPainPoints ( )
inline

Definition at line 69 of file lm_pain_points.h.

69 {}

Member Function Documentation

◆ Clear()

void tesseract::LMPainPoints::Clear ( )
inline

Definition at line 82 of file lm_pain_points.h.

82  {
83  for (int h = 0; h < LM_PPTYPE_NUM; ++h) pain_points_heaps_[h].clear();
84  }

◆ Deque()

LMPainPointsType tesseract::LMPainPoints::Deque ( MATRIX_COORD pp,
float *  priority 
)

Definition at line 37 of file lm_pain_points.cpp.

37  {
38  for (int h = 0; h < LM_PPTYPE_NUM; ++h) {
39  if (pain_points_heaps_[h].empty()) continue;
40  *priority = pain_points_heaps_[h].PeekTop().key;
41  *pp = pain_points_heaps_[h].PeekTop().data;
42  pain_points_heaps_[h].Pop(NULL);
43  return static_cast<LMPainPointsType>(h);
44  }
45  return LM_PPTYPE_NUM;
46 }
const Pair & PeekTop() const
Definition: genericheap.h:108
bool Pop(Pair *entry)
Definition: genericheap.h:118

◆ GenerateForBlamer()

bool tesseract::LMPainPoints::GenerateForBlamer ( double  max_char_wh_ratio,
WERD_RES word_res,
int  col,
int  row 
)
inline

Definition at line 99 of file lm_pain_points.h.

100  {
101  return GeneratePainPoint(col, row, LM_PPTYPE_BLAMER, 0.0, false,
102  max_char_wh_ratio, word_res);
103  }
bool GeneratePainPoint(int col, int row, LMPainPointsType pp_type, float special_priority, bool ok_to_extend, float max_char_wh_ratio, WERD_RES *word_res)

◆ GenerateFromAmbigs()

void tesseract::LMPainPoints::GenerateFromAmbigs ( const DANGERR fixpt,
ViterbiStateEntry vse,
WERD_RES word_res 
)

Definition at line 130 of file lm_pain_points.cpp.

132  {
133  // Begins and ends in DANGERR vector now record the blob indices as used
134  // by the ratings matrix.
135  for (int d = 0; d < fixpt.size(); ++d) {
136  const DANGERR_INFO &danger = fixpt[d];
137  // Only use dangerous ambiguities.
138  if (danger.dangerous) {
139  GeneratePainPoint(danger.begin, danger.end - 1,
140  LM_PPTYPE_AMBIG, vse->cost, true,
141  kLooseMaxCharWhRatio, word_res);
142  }
143  }
144 }
bool GeneratePainPoint(int col, int row, LMPainPointsType pp_type, float special_priority, bool ok_to_extend, float max_char_wh_ratio, WERD_RES *word_res)
int size() const
Definition: genericvector.h:72
int begin
Definition: stopper.h:40
bool dangerous
Definition: stopper.h:42
static const float kLooseMaxCharWhRatio

◆ GenerateFromPath()

void tesseract::LMPainPoints::GenerateFromPath ( float  rating_cert_scale,
ViterbiStateEntry vse,
WERD_RES word_res 
)

Definition at line 68 of file lm_pain_points.cpp.

70  {
71  ViterbiStateEntry *curr_vse = vse;
72  BLOB_CHOICE *curr_b = vse->curr_b;
73  // The following pain point generation and priority calculation approaches
74  // prioritize exploring paths with low average rating of the known part of
75  // the path, while not relying on the ratings of the pieces to be combined.
76  //
77  // A pain point to combine the neighbors is generated for each pair of
78  // neighboring blobs on the path (the path is represented by vse argument
79  // given to GenerateFromPath()). The priority of each pain point is set to
80  // the average rating (per outline length) of the path, not including the
81  // ratings of the blobs to be combined.
82  // The ratings of the blobs to be combined are not used to calculate the
83  // priority, since it is not possible to determine from their magnitude
84  // whether it will be beneficial to combine the blobs. The reason is that
85  // chopped junk blobs (/ | - ') can have very good (low) ratings, however
86  // combining them will be beneficial. Blobs with high ratings might be
87  // over-joined pieces of characters, but also could be blobs from an unseen
88  // font or chopped pieces of complex characters.
89  while (curr_vse->parent_vse != NULL) {
90  ViterbiStateEntry* parent_vse = curr_vse->parent_vse;
91  const MATRIX_COORD& curr_cell = curr_b->matrix_cell();
92  const MATRIX_COORD& parent_cell = parent_vse->curr_b->matrix_cell();
93  MATRIX_COORD pain_coord(parent_cell.col, curr_cell.row);
94  if (!pain_coord.Valid(*word_res->ratings) ||
95  !word_res->ratings->Classified(parent_cell.col, curr_cell.row,
96  dict_->WildcardID())) {
97  // rat_subtr contains ratings sum of the two adjacent blobs to be merged.
98  // rat_subtr will be subtracted from the ratings sum of the path, since
99  // the blobs will be joined into a new blob, whose rating is yet unknown.
100  float rat_subtr = curr_b->rating() + parent_vse->curr_b->rating();
101  // ol_subtr contains the outline length of the blobs that will be joined.
102  float ol_subtr =
103  AssociateUtils::ComputeOutlineLength(rating_cert_scale, *curr_b) +
104  AssociateUtils::ComputeOutlineLength(rating_cert_scale,
105  *(parent_vse->curr_b));
106  // ol_dif is the outline of the path without the two blobs to be joined.
107  float ol_dif = vse->outline_length - ol_subtr;
108  // priority is set to the average rating of the path per unit of outline,
109  // not counting the ratings of the pieces to be joined.
110  float priority = ol_dif > 0 ? (vse->ratings_sum-rat_subtr)/ol_dif : 0.0;
111  GeneratePainPoint(pain_coord.col, pain_coord.row, LM_PPTYPE_PATH,
112  priority, true, max_char_wh_ratio_, word_res);
113  } else if (debug_level_ > 3) {
114  tprintf("NO pain point (Classified) for col=%d row=%d type=%s\n",
115  pain_coord.col, pain_coord.row,
116  LMPainPointsTypeName[LM_PPTYPE_PATH]);
117  BLOB_CHOICE_IT b_it(word_res->ratings->get(pain_coord.col,
118  pain_coord.row));
119  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
120  BLOB_CHOICE* choice = b_it.data();
121  choice->print_full();
122  }
123  }
124 
125  curr_vse = parent_vse;
126  curr_b = curr_vse->curr_b;
127  }
128 }
bool GeneratePainPoint(int col, int row, LMPainPointsType pp_type, float special_priority, bool ok_to_extend, float max_char_wh_ratio, WERD_RES *word_res)
static float ComputeOutlineLength(float rating_cert_scale, const BLOB_CHOICE &b)
Definition: associate.h:80
const MATRIX_COORD & matrix_cell()
Definition: ratngs.h:114
T get(ICOORD pos) const
Definition: matrix.h:223
float rating() const
Definition: ratngs.h:79
#define tprintf(...)
Definition: tprintf.h:31
MATRIX * ratings
Definition: pageres.h:215
void print_full() const
Definition: ratngs.h:186
bool Classified(int col, int row, int wildcard_id) const
Definition: matrix.cpp:36
UNICHAR_ID WildcardID() const
Definition: dict.h:410

◆ GenerateInitial()

void tesseract::LMPainPoints::GenerateInitial ( WERD_RES word_res)

Definition at line 48 of file lm_pain_points.cpp.

48  {
49  MATRIX *ratings = word_res->ratings;
50  AssociateStats associate_stats;
51  for (int col = 0; col < ratings->dimension(); ++col) {
52  int row_end = MIN(ratings->dimension(), col + ratings->bandwidth() + 1);
53  for (int row = col + 1; row < row_end; ++row) {
54  MATRIX_COORD coord(col, row);
55  if (coord.Valid(*ratings) &&
56  ratings->get(col, row) != NOT_CLASSIFIED) continue;
57  // Add an initial pain point if needed.
58  if (ratings->Classified(col, row - 1, dict_->WildcardID()) ||
59  (col + 1 < ratings->dimension() &&
60  ratings->Classified(col + 1, row, dict_->WildcardID()))) {
61  GeneratePainPoint(col, row, LM_PPTYPE_SHAPE, 0.0,
62  true, max_char_wh_ratio_, word_res);
63  }
64  }
65  }
66 }
bool GeneratePainPoint(int col, int row, LMPainPointsType pp_type, float special_priority, bool ok_to_extend, float max_char_wh_ratio, WERD_RES *word_res)
T get(ICOORD pos) const
Definition: matrix.h:223
#define NOT_CLASSIFIED
Definition: matrix.h:41
MATRIX * ratings
Definition: pageres.h:215
int bandwidth() const
Definition: matrix.h:523
int dimension() const
Definition: matrix.h:521
#define MIN(x, y)
Definition: ndminx.h:28
bool Classified(int col, int row, int wildcard_id) const
Definition: matrix.cpp:36
Definition: matrix.h:563
UNICHAR_ID WildcardID() const
Definition: dict.h:410

◆ GeneratePainPoint()

bool tesseract::LMPainPoints::GeneratePainPoint ( int  col,
int  row,
LMPainPointsType  pp_type,
float  special_priority,
bool  ok_to_extend,
float  max_char_wh_ratio,
WERD_RES word_res 
)

Definition at line 146 of file lm_pain_points.cpp.

149  {
150  MATRIX_COORD coord(col, row);
151  if (coord.Valid(*word_res->ratings) &&
152  word_res->ratings->Classified(col, row, dict_->WildcardID())) {
153  return false;
154  }
155  if (debug_level_ > 3) {
156  tprintf("Generating pain point for col=%d row=%d type=%s\n",
157  col, row, LMPainPointsTypeName[pp_type]);
158  }
159  // Compute associate stats.
160  AssociateStats associate_stats;
161  AssociateUtils::ComputeStats(col, row, NULL, 0, fixed_pitch_,
162  max_char_wh_ratio, word_res, debug_level_,
163  &associate_stats);
164  // For fixed-pitch fonts/languages: if the current combined blob overlaps
165  // the next blob on the right and it is ok to extend the blob, try extending
166  // the blob until there is no overlap with the next blob on the right or
167  // until the width-to-height ratio becomes too large.
168  if (ok_to_extend) {
169  while (associate_stats.bad_fixed_pitch_right_gap &&
170  row + 1 < word_res->ratings->dimension() &&
171  !associate_stats.bad_fixed_pitch_wh_ratio) {
172  AssociateUtils::ComputeStats(col, ++row, NULL, 0, fixed_pitch_,
173  max_char_wh_ratio, word_res, debug_level_,
174  &associate_stats);
175  }
176  }
177  if (associate_stats.bad_shape) {
178  if (debug_level_ > 3) {
179  tprintf("Discarded pain point with a bad shape\n");
180  }
181  return false;
182  }
183 
184  // Insert the new pain point into pain_points_heap_.
185  if (pain_points_heaps_[pp_type].size() < max_heap_size_) {
186  // Compute pain point priority.
187  float priority;
188  if (pp_type == LM_PPTYPE_PATH) {
189  priority = special_priority;
190  } else {
191  priority = associate_stats.gap_sum;
192  }
193  MatrixCoordPair pain_point(priority, MATRIX_COORD(col, row));
194  pain_points_heaps_[pp_type].Push(&pain_point);
195  if (debug_level_) {
196  tprintf("Added pain point with priority %g\n", priority);
197  }
198  return true;
199  } else {
200  if (debug_level_) tprintf("Pain points heap is full\n");
201  return false;
202  }
203 }
void Push(Pair *entry)
Definition: genericheap.h:95
voidpf void uLong size
Definition: ioapi.h:39
#define tprintf(...)
Definition: tprintf.h:31
MATRIX * ratings
Definition: pageres.h:215
static void ComputeStats(int col, int row, const AssociateStats *parent_stats, int parent_path_length, bool fixed_pitch, float max_char_wh_ratio, WERD_RES *word_res, bool debug, AssociateStats *stats)
Definition: associate.cpp:37
int dimension() const
Definition: matrix.h:521
bool Classified(int col, int row, int wildcard_id) const
Definition: matrix.cpp:36
UNICHAR_ID WildcardID() const
Definition: dict.h:410

◆ HasPainPoints()

bool tesseract::LMPainPoints::HasPainPoints ( LMPainPointsType  pp_type) const
inline

Definition at line 72 of file lm_pain_points.h.

72  {
73  return !pain_points_heaps_[pp_type].empty();
74  }
bool empty() const
Definition: genericheap.h:68

◆ PainPointDescription()

static const char* tesseract::LMPainPoints::PainPointDescription ( LMPainPointsType  type)
inlinestatic

Definition at line 62 of file lm_pain_points.h.

62  {
63  return LMPainPointsTypeName[type];
64  }

◆ RemapForSplit()

void tesseract::LMPainPoints::RemapForSplit ( int  index)

Adjusts the pain point coordinates to cope with expansion of the ratings matrix due to a split of the blob with the given index.

Definition at line 209 of file lm_pain_points.cpp.

209  {
210  for (int i = 0; i < LM_PPTYPE_NUM; ++i) {
211  GenericVector<MatrixCoordPair>* heap = pain_points_heaps_[i].heap();
212  for (int j = 0; j < heap->size(); ++j)
213  (*heap)[j].data.MapForSplit(index);
214  }
215 }
int size() const
Definition: genericvector.h:72
GenericVector< Pair > * heap()
Definition: genericheap.h:83

Member Data Documentation

◆ kDefaultPainPointPriorityAdjustment

const float tesseract::LMPainPoints::kDefaultPainPointPriorityAdjustment = 2.0f
static

Definition at line 56 of file lm_pain_points.h.

◆ kLooseMaxCharWhRatio

const float tesseract::LMPainPoints::kLooseMaxCharWhRatio = 2.5f
static

Definition at line 60 of file lm_pain_points.h.


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