tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/wordrec/lm_consistency.cpp
Go to the documentation of this file.
00001 
00002 // File:        lm_consistency.cpp
00003 // Description: Struct for recording consistency of the paths  representing
00004 //              OCR hypotheses.
00005 // Author:      Rika Antonova
00006 // Created:     Mon Jun 20 11:26:43 PST 2012
00007 //
00008 // (C) Copyright 2012, Google Inc.
00009 // Licensed under the Apache License, Version 2.0 (the "License");
00010 // you may not use this file except in compliance with the License.
00011 // You may obtain a copy of the License at
00012 // http://www.apache.org/licenses/LICENSE-2.0
00013 // Unless required by applicable law or agreed to in writing, software
00014 // distributed under the License is distributed on an "AS IS" BASIS,
00015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016 // See the License for the specific language governing permissions and
00017 // limitations under the License.
00018 //
00020 
00021 #include "lm_consistency.h"
00022 
00023 #include "associate.h"
00024 #include "dict.h"
00025 #include "ratngs.h"
00026 
00027 namespace tesseract {
00028 
00029 void LMConsistencyInfo::ComputeXheightConsistency(
00030     const BLOB_CHOICE *b, bool is_punc) {
00031   if (xht_decision == XH_INCONSISTENT)
00032     return;  // It isn't going to get any better.
00033 
00034   // Compute xheight consistency.
00035   bool parent_null = xht_sp < 0;
00036   int parent_sp = xht_sp;
00037   // Debug strings.
00038   if (b->yshift() > LMConsistencyInfo::kShiftThresh) {
00039     xht_sp = LMConsistencyInfo::kSUP;
00040   } else if (b->yshift() < -LMConsistencyInfo::kShiftThresh) {
00041     xht_sp = LMConsistencyInfo::kSUB;
00042   } else {
00043     xht_sp = LMConsistencyInfo::kNORM;
00044   }
00045   xht_count[xht_sp]++;
00046   if (is_punc) xht_count_punc[xht_sp]++;
00047   if (!parent_null) {
00048     xpos_entropy += abs(parent_sp - xht_sp);
00049   }
00050   // TODO(eger): Figure out a better way to account for small caps.
00051   // For the first character not y-shifted, we only care if it is too small.
00052   // Too large is common in drop caps and small caps.
00053   // inT16 small_xht = b->min_xheight();
00054   //  if (parent_vse == NULL && sp == LanguageModelConsistencyInfo::kNORM) {
00055   //  small_xht = 0;
00056   // }
00057   IntersectRange(b->min_xheight(), b->max_xheight(),
00058                  &(xht_lo[xht_sp]), &(xht_hi[xht_sp]));
00059 
00060 
00061   // Compute xheight inconsistency kinds.
00062   if (parent_null) {
00063     if (xht_count[kNORM] == 1) {
00064       xht_decision = XH_GOOD;
00065     } else {
00066       xht_decision = XH_SUBNORMAL;
00067     }
00068     return;
00069   }
00070 
00071   // When we intersect the ranges of xheights in pixels for all characters in
00072   // each position (subscript, normal, superscript),
00073   // How much range must be left?  0? [exactly one pixel height for xheight] 1?
00074   // TODO(eger): Extend this code to take a prior for the rest of the line.
00075   const int kMinIntersectedXHeightRange = 0;
00076   for (int i = 0; i < kNumPos; i++) {
00077     if (xht_lo[i] > xht_hi[i] - kMinIntersectedXHeightRange) {
00078       xht_decision = XH_INCONSISTENT;
00079       return;
00080     }
00081   }
00082 
00083   // Reject as improbable anything where there's much punctuation in subscript
00084   // or superscript regions.
00085   if (xht_count_punc[kSUB] > xht_count[kSUB] * 0.4 ||
00086       xht_count_punc[kSUP] > xht_count[kSUP] * 0.4) {
00087     xht_decision = XH_INCONSISTENT;
00088     return;
00089   }
00090 
00091   // Now check that the subscript and superscript aren't too small relative to
00092   // the mainline.
00093   double mainline_xht = static_cast<double>(xht_lo[kNORM]);
00094   double kMinSizeRatio = 0.4;
00095   if (mainline_xht > 0.0 &&
00096       (static_cast<double>(xht_hi[kSUB]) / mainline_xht < kMinSizeRatio ||
00097        static_cast<double>(xht_hi[kSUP]) / mainline_xht < kMinSizeRatio)) {
00098     xht_decision = XH_INCONSISTENT;
00099     return;
00100   }
00101   // TODO(eger): Check into inconsistency of super/subscript y offsets.
00102   if (xpos_entropy > kMaxEntropy) {
00103     xht_decision = XH_INCONSISTENT;
00104     return;
00105   }
00106   if (xht_count[kSUB] == 0 && xht_count[kSUP] == 0) {
00107     xht_decision = XH_GOOD;
00108     return;
00109   }
00110   xht_decision = XH_SUBNORMAL;
00111 }
00112 
00113 }  // namespace tesseract
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines