tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/classify/shapeclassifier.h
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 // Author: rays@google.com (Ray Smith)
00004 // File:        shapeclassifier.h
00005 // Description: Base interface class for classifiers that return a
00006 //              shape index.
00007 // Author:      Ray Smith
00008 // Created:     Tue Sep 13 11:26:32 PDT 2011
00009 //
00010 // (C) Copyright 2011, Google Inc.
00011 // Licensed under the Apache License, Version 2.0 (the "License");
00012 // you may not use this file except in compliance with the License.
00013 // You may obtain a copy of the License at
00014 // http://www.apache.org/licenses/LICENSE-2.0
00015 // Unless required by applicable law or agreed to in writing, software
00016 // distributed under the License is distributed on an "AS IS" BASIS,
00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018 // See the License for the specific language governing permissions and
00019 // limitations under the License.
00020 //
00022 
00023 #ifndef TESSERACT_CLASSIFY_SHAPECLASSIFIER_H_
00024 #define TESSERACT_CLASSIFY_SHAPECLASSIFIER_H_
00025 
00026 #include "unichar.h"
00027 
00028 template <typename T> class GenericVector;
00029 struct Pix;
00030 class ScrollView;
00031 class UNICHARSET;
00032 
00033 namespace tesseract {
00034 
00035 template <typename T> class PointerVector;
00036 struct ShapeRating;
00037 class ShapeTable;
00038 class TrainingSample;
00039 class TrainingSampleSet;
00040 struct UnicharRating;
00041 
00042 // Interface base class for classifiers that produce ShapeRating results.
00043 class ShapeClassifier {
00044  public:
00045   virtual ~ShapeClassifier() {}
00046 
00047   // Classifies the given [training] sample, writing to results.
00048   // If page_pix is not NULL, the overriding function may call
00049   // sample.GetSamplePix(padding, page_pix) to get an image of the sample
00050   // padded (with real image data) by the given padding to extract features
00051   // from the image of the character. Other members of TrainingSample:
00052   // features(), micro_features(), cn_feature(), geo_feature() may be used
00053   // to get the appropriate tesseract features.
00054   // If debug is non-zero, then various degrees of classifier dependent debug
00055   // information is provided.
00056   // If keep_this (a UNICHAR_ID) is >= 0, then the results should always
00057   // contain keep_this, and (if possible) anything of intermediate confidence.
00058   // (Used for answering "Why didn't it get that right?" questions.) It must
00059   // be a UNICHAR_ID as the callers have no clue how to choose the best shape
00060   // that may contain a desired answer.
00061   // The return value is the number of classes saved in results.
00062   // NOTE that overriding functions MUST clear and sort the results by
00063   // descending rating unless the classifier is working with a team of such
00064   // classifiers.
00065   // NOTE: Neither overload of ClassifySample is pure, but at least one must
00066   // be overridden by a classifier in order for it to do anything.
00067   virtual int UnicharClassifySample(const TrainingSample& sample, Pix* page_pix,
00068                                     int debug, UNICHAR_ID keep_this,
00069                                     GenericVector<UnicharRating>* results);
00070 
00071  protected:
00072   virtual int ClassifySample(const TrainingSample& sample, Pix* page_pix,
00073                              int debug, UNICHAR_ID keep_this,
00074                              GenericVector<ShapeRating>* results);
00075 
00076  public:
00077   // Returns the shape that contains unichar_id that has the best result.
00078   // If result is not NULL, it is set with the shape_id and rating.
00079   // Returns -1 if ClassifySample fails to provide any result containing
00080   // unichar_id. BestShapeForUnichar does not need to be overridden if
00081   // ClassifySample respects the keep_this rule.
00082   virtual int BestShapeForUnichar(const TrainingSample& sample, Pix* page_pix,
00083                                   UNICHAR_ID unichar_id, ShapeRating* result);
00084 
00085   // Provides access to the ShapeTable that this classifier works with.
00086   virtual const ShapeTable* GetShapeTable() const = 0;
00087   // Provides access to the UNICHARSET that this classifier works with.
00088   // Must be overridden IFF GetShapeTable() returns NULL.
00089   virtual const UNICHARSET& GetUnicharset() const;
00090 
00091   // Visual debugger classifies the given sample, displays the results and
00092   // solicits user input to display other classifications. Returns when
00093   // the user has finished with debugging the sample.
00094   // Probably doesn't need to be overridden if the subclass provides
00095   // DisplayClassifyAs.
00096   virtual void DebugDisplay(const TrainingSample& sample, Pix* page_pix,
00097                             UNICHAR_ID unichar_id);
00098 
00099 
00100   // Displays classification as the given unichar_id. Creates as many windows
00101   // as it feels fit, using index as a guide for placement. Adds any created
00102   // windows to the windows output and returns a new index that may be used
00103   // by any subsequent classifiers. Caller waits for the user to view and
00104   // then destroys the windows by clearing the vector.
00105   virtual int DisplayClassifyAs(const TrainingSample& sample,  Pix* page_pix,
00106                                 UNICHAR_ID unichar_id, int index,
00107                                 PointerVector<ScrollView>* windows);
00108 
00109   // Prints debug information on the results. context is some introductory/title
00110   // message.
00111   virtual void UnicharPrintResults(
00112       const char* context, const GenericVector<UnicharRating>& results) const;
00113   virtual void PrintResults(const char* context,
00114                             const GenericVector<ShapeRating>& results) const;
00115 
00116  protected:
00117   // Removes any result that has all its unichars covered by a better choice,
00118   // regardless of font.
00119   void FilterDuplicateUnichars(GenericVector<ShapeRating>* results) const;
00120 };
00121 
00122 }  // namespace tesseract.
00123 
00124 #endif  // TESSERACT_CLASSIFY_SHAPECLASSIFIER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines