All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
associate.h
Go to the documentation of this file.
1 // File: associate.h
3 // Description: Structs, classes, typedefs useful for the segmentation
4 // search. Functions for scoring segmentation paths according
5 // to their character widths, gap widths and seam cuts.
6 // Author: Daria Antonova
7 // Created: Mon Mar 8 11:26:43 PDT 2010
8 //
9 // (C) Copyright 2010, Google Inc.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
21 
22 #ifndef ASSOCIATE_H
23 #define ASSOCIATE_H
24 
25 #include "blobs.h"
26 #include "elst.h"
27 #include "ratngs.h"
28 #include "seam.h"
29 #include "split.h"
30 
31 class WERD_RES;
32 
33 namespace tesseract {
34 
35 // Statisitcs about character widths, gaps and seams.
38 
39  void Clear() {
40  shape_cost = 0.0f;
41  bad_shape = false;
42  full_wh_ratio = 0.0f;
43  full_wh_ratio_total = 0.0f;
44  full_wh_ratio_var = 0.0f;
47  gap_sum = 0;
48  }
49 
50  void Print() {
51  tprintf("AssociateStats: w(%g %d) s(%g %d)\n", shape_cost, bad_shape);
52  }
53 
54  float shape_cost; // cost of blob shape
55  bool bad_shape; // true if the shape of the blob is unacceptable
56  float full_wh_ratio; // width-to-hight ratio + gap on the right
57  float full_wh_ratio_total; // sum of width-to-hight ratios
58  // on the path terminating at this blob
59  float full_wh_ratio_var; // variance of full_wh_ratios on the path
60  bool bad_fixed_pitch_right_gap; // true if there is no gap before
61  // the blob on the right
62  bool bad_fixed_pitch_wh_ratio; // true if the blobs has width-to-hight
63  // ratio > kMaxFixedPitchCharAspectRatio
64  int gap_sum; // sum of gaps within the blob
65 };
66 
67 // Utility functions for scoring segmentation paths according to their
68 // character widths, gap widths, seam characteristics.
70  public:
71  static const float kMaxFixedPitchCharAspectRatio;
72  static const float kMinGap;
73 
74  // Returns outline length of the given blob is computed as:
75  // rating_cert_scale * rating / certainty
76  // Since from Wordrec::SegSearch() in segsearch.cpp
77  // rating_cert_scale = -1.0 * getDict().certainty_scale / rating_scale
78  // And from Classify::ConvertMatchesToChoices() in adaptmatch.cpp
79  // Rating = Certainty = next.rating
80  // Rating *= rating_scale * Results->BlobLength
81  // Certainty *= -(getDict().certainty_scale)
82  static inline float ComputeOutlineLength(float rating_cert_scale,
83  const BLOB_CHOICE &b) {
84  return rating_cert_scale * b.rating() / b.certainty();
85  }
86  static inline float ComputeRating(float rating_cert_scale,
87  float cert, int width) {
88  return static_cast<float>(width) * cert / rating_cert_scale;
89  }
90 
91  // Computes character widths, gaps and seams stats given the
92  // AssociateStats of the path so far, col, row of the blob that
93  // is being added to the path, and WERD_RES containing information
94  // about character widths, gaps and seams.
95  // Fills associate_cost with the combined shape, gap and seam cost
96  // of adding a unichar from (col, row) to the path (note that since
97  // this function could be used to compute the prioritization for
98  // pain points, (col, row) entry might not be classified yet; thus
99  // information in the (col, row) entry of the ratings matrix is not used).
100  //
101  // Note: the function assumes that word_res, stats and
102  // associate_cost pointers are not NULL.
103  static void ComputeStats(int col, int row,
104  const AssociateStats *parent_stats,
105  int parent_path_length,
106  bool fixed_pitch,
107  float max_char_wh_ratio,
108  WERD_RES *word_res,
109  bool debug,
110  AssociateStats *stats);
111 
112  // Returns the width cost for fixed-pitch text.
113  static float FixedPitchWidthCost(float norm_width, float right_gap,
114  bool end_pos, float max_char_wh_ratio);
115 
116  // Returns the gap cost for fixed-pitch text (penalizes vertically
117  // overlapping components).
118  static inline float FixedPitchGapCost(float norm_gap, bool end_pos) {
119  return (norm_gap < 0.05 && !end_pos) ? 5.0f : 0.0f;
120  }
121 };
122 
123 } // namespace tesseract
124 
125 #endif
static float FixedPitchWidthCost(float norm_width, float right_gap, bool end_pos, float max_char_wh_ratio)
Definition: associate.cpp:159
static const float kMinGap
Definition: associate.h:72
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
#define tprintf(...)
Definition: tprintf.h:31
float rating() const
Definition: ratngs.h:79
static float ComputeRating(float rating_cert_scale, float cert, int width)
Definition: associate.h:86
static float ComputeOutlineLength(float rating_cert_scale, const BLOB_CHOICE &b)
Definition: associate.h:82
static const float kMaxFixedPitchCharAspectRatio
Definition: associate.h:71
static float FixedPitchGapCost(float norm_gap, bool end_pos)
Definition: associate.h:118
float certainty() const
Definition: ratngs.h:82