Tesseract  3.02
tesseract-ocr/wordrec/associate.h
Go to the documentation of this file.
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 class WERD_RES;
00033 
00034 typedef inT16 BLOB_WEIGHTS[MAX_NUM_CHUNKS];
00035 
00036 // Each unichar evaluated.
00037 struct EVALUATION_RECORD {
00038   float match;
00039   float certainty;
00040   char character;
00041   int width;
00042   int gap;
00043 };
00044 
00045 typedef EVALUATION_RECORD EVALUATION_ARRAY[MAX_NUM_CHUNKS];
00046 
00047 // Classification info for chunks.
00048 //
00049 // TODO(daria): move to tesseract namespace when obsolete code using
00050 // this struct that is not in tesseract namespace is deprecated.
00051 struct CHUNKS_RECORD {
00052   MATRIX *ratings;
00053   TBLOB *chunks;
00054   WERD_RES* word_res;  // Borrowed pointer - do not delete!
00055   SEAMS splits;
00056   int x_height;
00057   WIDTH_RECORD *chunk_widths;
00058   WIDTH_RECORD *char_widths;
00059   inT16 *weights;
00060 };
00061 
00062 namespace tesseract {
00063 
00064 // Statisitcs about character widths, gaps and seams.
00065 struct AssociateStats {
00066   AssociateStats() { Clear(); }
00067 
00068   void Clear() {
00069     shape_cost = 0.0f;
00070     bad_shape = false;
00071     full_wh_ratio = 0.0f;
00072     full_wh_ratio_total = 0.0f;
00073     full_wh_ratio_var = 0.0f;
00074     bad_fixed_pitch_right_gap = false;
00075     bad_fixed_pitch_wh_ratio = false;
00076   }
00077 
00078   void Print() {
00079     tprintf("AssociateStats: w(%g %d) s(%g %d)\n", shape_cost, bad_shape);
00080   }
00081 
00082   float shape_cost;           // cost of blob shape
00083   bool bad_shape;             // true if the shape of the blob is unacceptable
00084   float full_wh_ratio;        // width-to-hight ratio + gap on the right
00085   float full_wh_ratio_total;  // sum of width-to-hight ratios
00086                               // on the path terminating at this blob
00087   float full_wh_ratio_var;    // variance of full_wh_ratios on the path
00088   bool bad_fixed_pitch_right_gap;  // true if there is no gap before
00089                                    // the blob on the right
00090   bool bad_fixed_pitch_wh_ratio;   // true if the blobs has width-to-hight
00091                                    // ratio > kMaxFixedPitchCharAspectRatio
00092 };
00093 
00094 // Utility functions for scoring segmentation paths according to their
00095 // character widths, gap widths, seam characteristics.
00096 class AssociateUtils {
00097  public:
00098   static const float kMaxFixedPitchCharAspectRatio;
00099   static const float kMinGap;
00100 
00101   // Computes character widths, gaps and seams stats given the
00102   // AssociateStats of the path so far, col, row of the blob that
00103   // is being added to the path, and CHUNKS_RECORD containing information
00104   // about character widths, gaps and seams.
00105   // Fills associate_cost with the combined shape, gap and seam cost
00106   // of adding a unichar from (col, row) to the path (note that since
00107   // this function could be used to compute the prioritization for
00108   // pain points, (col, row) entry might not be classified yet; thus
00109   // information in the (col, row) entry of the ratings matrix is not used).
00110   //
00111   // Note: the function assumes that chunks_record, stats and
00112   // associate_cost pointers are not NULL.
00113   static void ComputeStats(int col, int row,
00114                            const AssociateStats *parent_stats,
00115                            int parent_path_length,
00116                            bool fixed_pitch,
00117                            float max_char_wh_ratio,
00118                            const DENORM *denorm,
00119                            CHUNKS_RECORD *chunks_record,
00120                            int debug_level,
00121                            AssociateStats *stats);
00122 
00123   // Returns the width of a chunk which is a composed of several blobs
00124   // blobs[start_blob..last_blob] inclusively.
00125   // Widths/gaps records are in the form:
00126   // width_record->num_char = n
00127   // width_record->widths[2*n-1] = w0,g0,w1,g1..w(n-1),g(n-1)
00128   static int GetChunksWidth(WIDTH_RECORD *width_record,
00129                             int start_blob, int last_blob);
00130 
00131   // Returns the width of a gap between the specified chunk and the next one.
00132   static inline int GetChunksGap(WIDTH_RECORD *width_record, int last_chunk) {
00133     return (last_chunk >= 0 && last_chunk < width_record->num_chars - 1) ?
00134       width_record->widths[last_chunk * 2 + 1] : 0;
00135   }
00136 
00137   // Returns the width cost for fixed-pitch text.
00138   static float FixedPitchWidthCost(float norm_width, float right_gap,
00139                                    bool end_pos, float max_char_wh_ratio);
00140 
00141   // Returns the gap cost for fixed-pitch text (penalizes vertically
00142   // overlapping components).
00143   static inline float FixedPitchGapCost(float norm_gap, bool end_pos) {
00144     return (norm_gap < 0.05 && !end_pos) ? 5.0f : 0.0f;
00145   }
00146 };
00147 
00148 }  // namespace tesseract
00149 
00150 #endif