tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/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 "ratngs.h"
00028 #include "seam.h"
00029 #include "split.h"
00030 
00031 class WERD_RES;
00032 
00033 namespace tesseract {
00034 
00035 // Statisitcs about character widths, gaps and seams.
00036 struct AssociateStats {
00037   AssociateStats() { Clear(); }
00038 
00039   void Clear() {
00040     shape_cost = 0.0f;
00041     bad_shape = false;
00042     full_wh_ratio = 0.0f;
00043     full_wh_ratio_total = 0.0f;
00044     full_wh_ratio_var = 0.0f;
00045     bad_fixed_pitch_right_gap = false;
00046     bad_fixed_pitch_wh_ratio = false;
00047     gap_sum = 0;
00048   }
00049 
00050   void Print() {
00051     tprintf("AssociateStats: w(%g %d) s(%g %d)\n", shape_cost, bad_shape);
00052   }
00053 
00054   float shape_cost;           // cost of blob shape
00055   bool bad_shape;             // true if the shape of the blob is unacceptable
00056   float full_wh_ratio;        // width-to-hight ratio + gap on the right
00057   float full_wh_ratio_total;  // sum of width-to-hight ratios
00058                               // on the path terminating at this blob
00059   float full_wh_ratio_var;    // variance of full_wh_ratios on the path
00060   bool bad_fixed_pitch_right_gap;  // true if there is no gap before
00061                                    // the blob on the right
00062   bool bad_fixed_pitch_wh_ratio;   // true if the blobs has width-to-hight
00063                                    // ratio > kMaxFixedPitchCharAspectRatio
00064   int gap_sum;  // sum of gaps within the blob
00065 };
00066 
00067 // Utility functions for scoring segmentation paths according to their
00068 // character widths, gap widths, seam characteristics.
00069 class AssociateUtils {
00070  public:
00071   static const float kMaxFixedPitchCharAspectRatio;
00072   static const float kMinGap;
00073 
00074   // Returns outline length of the given blob is computed as:
00075   // rating_cert_scale * rating / certainty
00076   // Since from Wordrec::SegSearch() in segsearch.cpp
00077   // rating_cert_scale = -1.0 * getDict().certainty_scale / rating_scale
00078   // And from Classify::ConvertMatchesToChoices() in adaptmatch.cpp
00079   // Rating = Certainty = next.rating
00080   // Rating *= rating_scale * Results->BlobLength
00081   // Certainty *= -(getDict().certainty_scale)
00082   static inline float ComputeOutlineLength(float rating_cert_scale,
00083                                            const BLOB_CHOICE &b) {
00084     return rating_cert_scale * b.rating() / b.certainty();
00085   }
00086   static inline float ComputeRating(float rating_cert_scale,
00087                                     float cert, int width) {
00088     return static_cast<float>(width) * cert / rating_cert_scale;
00089   }
00090 
00091   // Computes character widths, gaps and seams stats given the
00092   // AssociateStats of the path so far, col, row of the blob that
00093   // is being added to the path, and WERD_RES containing information
00094   // about character widths, gaps and seams.
00095   // Fills associate_cost with the combined shape, gap and seam cost
00096   // of adding a unichar from (col, row) to the path (note that since
00097   // this function could be used to compute the prioritization for
00098   // pain points, (col, row) entry might not be classified yet; thus
00099   // information in the (col, row) entry of the ratings matrix is not used).
00100   //
00101   // Note: the function assumes that word_res, stats and
00102   // associate_cost pointers are not NULL.
00103   static void ComputeStats(int col, int row,
00104                            const AssociateStats *parent_stats,
00105                            int parent_path_length,
00106                            bool fixed_pitch,
00107                            float max_char_wh_ratio,
00108                            WERD_RES *word_res,
00109                            bool debug,
00110                            AssociateStats *stats);
00111 
00112   // Returns the width cost for fixed-pitch text.
00113   static float FixedPitchWidthCost(float norm_width, float right_gap,
00114                                    bool end_pos, float max_char_wh_ratio);
00115 
00116   // Returns the gap cost for fixed-pitch text (penalizes vertically
00117   // overlapping components).
00118   static inline float FixedPitchGapCost(float norm_gap, bool end_pos) {
00119     return (norm_gap < 0.05 && !end_pos) ? 5.0f : 0.0f;
00120   }
00121 };
00122 
00123 }  // namespace tesseract
00124 
00125 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines