Tesseract 3.01
|
00001 00002 // File: associate.h 00003 // Description: Structs, classes, typedefs useful for the segmentation 00004 // search. Functions for scoring segmentation paths according 00005 // to their character widths, gap widths and seam cuts. 00006 // Author: Daria Antonova 00007 // Created: Mon Mar 8 11:26:43 PDT 2010 00008 // 00009 // (C) Copyright 2010, Google Inc. 00010 // Licensed under the Apache License, Version 2.0 (the "License"); 00011 // you may not use this file except in compliance with the License. 00012 // You may obtain a copy of the License at 00013 // http://www.apache.org/licenses/LICENSE-2.0 00014 // Unless required by applicable law or agreed to in writing, software 00015 // distributed under the License is distributed on an "AS IS" BASIS, 00016 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00017 // See the License for the specific language governing permissions and 00018 // limitations under the License. 00019 // 00021 00022 #ifndef ASSOCIATE_H 00023 #define ASSOCIATE_H 00024 00025 #include "blobs.h" 00026 #include "elst.h" 00027 #include "matrix.h" 00028 #include "seam.h" 00029 #include "split.h" 00030 #include "states.h" 00031 00032 typedef inT16 BLOB_WEIGHTS[MAX_NUM_CHUNKS]; 00033 00034 // Each unichar evaluated. 00035 struct EVALUATION_RECORD { 00036 float match; 00037 float certainty; 00038 char character; 00039 int width; 00040 int gap; 00041 }; 00042 00043 typedef EVALUATION_RECORD EVALUATION_ARRAY[MAX_NUM_CHUNKS]; 00044 00045 // Classification info for chunks. 00046 // 00047 // TODO(daria): move to tesseract namespace when obsolete code using 00048 // this struct that is not in tesseract namespace is deprecated. 00049 struct CHUNKS_RECORD { 00050 MATRIX *ratings; 00051 TBLOB *chunks; 00052 SEAMS splits; 00053 int x_height; 00054 WIDTH_RECORD *chunk_widths; 00055 WIDTH_RECORD *char_widths; 00056 inT16 *weights; 00057 }; 00058 00059 namespace tesseract { 00060 00061 // Statisitcs about character widths, gaps and seams. 00062 struct AssociateStats { 00063 AssociateStats() { Clear(); } 00064 00065 void Clear() { 00066 shape_cost = 0.0f; 00067 bad_shape = false; 00068 full_wh_ratio = 0.0f; 00069 full_wh_ratio_total = 0.0f; 00070 full_wh_ratio_var = 0.0f; 00071 bad_fixed_pitch_right_gap = false; 00072 bad_fixed_pitch_wh_ratio = false; 00073 } 00074 00075 void Print() { 00076 tprintf("AssociateStats: w(%g %d) s(%g %d)\n", shape_cost, bad_shape); 00077 } 00078 00079 float shape_cost; // cost of blob shape 00080 bool bad_shape; // true if the shape of the blob is unacceptable 00081 float full_wh_ratio; // width-to-hight ratio + gap on the right 00082 float full_wh_ratio_total; // sum of width-to-hight ratios 00083 // on the path terminating at this blob 00084 float full_wh_ratio_var; // variance of full_wh_ratios on the path 00085 bool bad_fixed_pitch_right_gap; // true if there is no gap before 00086 // the blob on the right 00087 bool bad_fixed_pitch_wh_ratio; // true if the blobs has width-to-hight 00088 // ratio > kMaxFixedPitchCharAspectRatio 00089 }; 00090 00091 // Utility functions for scoring segmentation paths according to their 00092 // character widths, gap widths, seam characteristics. 00093 class AssociateUtils { 00094 public: 00095 static const float kMaxFixedPitchCharAspectRatio; 00096 static const float kMinGap; 00097 00098 // Computes character widths, gaps and seams stats given the 00099 // AssociateStats of the path so far, col, row of the blob that 00100 // is being added to the path, and CHUNKS_RECORD containing information 00101 // about character widths, gaps and seams. 00102 // Fills associate_cost with the combined shape, gap and seam cost 00103 // of adding a unichar from (col, row) to the path (note that since 00104 // this function could be used to compute the prioritization for 00105 // pain points, (col, row) entry might not be classified yet; thus 00106 // information in the (col, row) entry of the ratings matrix is not used). 00107 // 00108 // Note: the function assumes that chunks_record, stats and 00109 // associate_cost pointers are not NULL. 00110 static void ComputeStats(int col, int row, 00111 const AssociateStats *parent_stats, 00112 int parent_path_length, 00113 bool fixed_pitch, 00114 float max_char_wh_ratio, 00115 const DENORM *denorm, 00116 CHUNKS_RECORD *chunks_record, 00117 int debug_level, 00118 AssociateStats *stats); 00119 00120 // Returns the width of a chunk which is a composed of several blobs 00121 // blobs[start_blob..last_blob] inclusively. 00122 // Widths/gaps records are in the form: 00123 // width_record->num_char = n 00124 // width_record->widths[2*n-1] = w0,g0,w1,g1..w(n-1),g(n-1) 00125 static int GetChunksWidth(WIDTH_RECORD *width_record, 00126 int start_blob, int last_blob); 00127 00128 // Returns the width of a gap between the specified chunk and the next one. 00129 static inline int GetChunksGap(WIDTH_RECORD *width_record, int last_chunk) { 00130 return (last_chunk >= 0 && last_chunk < width_record->num_chars - 1) ? 00131 width_record->widths[last_chunk * 2 + 1] : 0; 00132 } 00133 00134 // Returns the width cost for fixed-pitch text. 00135 static float FixedPitchWidthCost(float norm_width, float right_gap, 00136 bool end_pos, float max_char_wh_ratio); 00137 00138 // Returns the gap cost for fixed-pitch text (penalizes vertically 00139 // overlapping components). 00140 static inline float FixedPitchGapCost(float norm_gap, bool end_pos) { 00141 return (norm_gap < 0.05 && !end_pos) ? 5.0f : 0.0f; 00142 } 00143 }; 00144 00145 } // namespace tesseract 00146 00147 #endif