tesseract  5.0.0-alpha-619-ge9db
shapeclassifier.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
4 // File: shapeclassifier.h
5 // Description: Base interface class for classifiers that return a
6 // shape index.
7 // Author: Ray Smith
8 // Created: Tue Sep 13 11:26:32 PDT 2011
9 //
10 // (C) Copyright 2011, Google Inc.
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
22 
23 #ifndef TESSERACT_CLASSIFY_SHAPECLASSIFIER_H_
24 #define TESSERACT_CLASSIFY_SHAPECLASSIFIER_H_
25 
26 #include <tesseract/unichar.h>
27 
28 template <typename T> class GenericVector;
29 struct Pix;
30 class ScrollView;
31 class UNICHARSET;
32 
33 namespace tesseract {
34 
35 template <typename T> class PointerVector;
36 struct ShapeRating;
37 class ShapeTable;
38 class TrainingSample;
39 class TrainingSampleSet;
40 struct UnicharRating;
41 
42 // Interface base class for classifiers that produce ShapeRating results.
44  public:
45  virtual ~ShapeClassifier() = default;
46 
47  // Classifies the given [training] sample, writing to results.
48  // If page_pix is not nullptr, the overriding function may call
49  // sample.GetSamplePix(padding, page_pix) to get an image of the sample
50  // padded (with real image data) by the given padding to extract features
51  // from the image of the character. Other members of TrainingSample:
52  // features(), micro_features(), cn_feature(), geo_feature() may be used
53  // to get the appropriate tesseract features.
54  // If debug is non-zero, then various degrees of classifier dependent debug
55  // information is provided.
56  // If keep_this (a UNICHAR_ID) is >= 0, then the results should always
57  // contain keep_this, and (if possible) anything of intermediate confidence.
58  // (Used for answering "Why didn't it get that right?" questions.) It must
59  // be a UNICHAR_ID as the callers have no clue how to choose the best shape
60  // that may contain a desired answer.
61  // The return value is the number of classes saved in results.
62  // NOTE that overriding functions MUST clear and sort the results by
63  // descending rating unless the classifier is working with a team of such
64  // classifiers.
65  // NOTE: Neither overload of ClassifySample is pure, but at least one must
66  // be overridden by a classifier in order for it to do anything.
67  virtual int UnicharClassifySample(const TrainingSample& sample, Pix* page_pix,
68  int debug, UNICHAR_ID keep_this,
70 
71  protected:
72  virtual int ClassifySample(const TrainingSample& sample, Pix* page_pix,
73  int debug, UNICHAR_ID keep_this,
75 
76  public:
77  // Returns the shape that contains unichar_id that has the best result.
78  // If result is not nullptr, it is set with the shape_id and rating.
79  // Returns -1 if ClassifySample fails to provide any result containing
80  // unichar_id. BestShapeForUnichar does not need to be overridden if
81  // ClassifySample respects the keep_this rule.
82  virtual int BestShapeForUnichar(const TrainingSample& sample, Pix* page_pix,
83  UNICHAR_ID unichar_id, ShapeRating* result);
84 
85  // Provides access to the ShapeTable that this classifier works with.
86  virtual const ShapeTable* GetShapeTable() const = 0;
87  // Provides access to the UNICHARSET that this classifier works with.
88  // Must be overridden IFF GetShapeTable() returns nullptr.
89  virtual const UNICHARSET& GetUnicharset() const;
90 
91  // Visual debugger classifies the given sample, displays the results and
92  // solicits user input to display other classifications. Returns when
93  // the user has finished with debugging the sample.
94  // Probably doesn't need to be overridden if the subclass provides
95  // DisplayClassifyAs.
96  virtual void DebugDisplay(const TrainingSample& sample, Pix* page_pix,
97  UNICHAR_ID unichar_id);
98 
99 
100  // Displays classification as the given unichar_id. Creates as many windows
101  // as it feels fit, using index as a guide for placement. Adds any created
102  // windows to the windows output and returns a new index that may be used
103  // by any subsequent classifiers. Caller waits for the user to view and
104  // then destroys the windows by clearing the vector.
105  virtual int DisplayClassifyAs(const TrainingSample& sample, Pix* page_pix,
106  UNICHAR_ID unichar_id, int index,
107  PointerVector<ScrollView>* windows);
108 
109  // Prints debug information on the results. context is some introductory/title
110  // message.
111  virtual void UnicharPrintResults(
112  const char* context, const GenericVector<UnicharRating>& results) const;
113  virtual void PrintResults(const char* context,
114  const GenericVector<ShapeRating>& results) const;
115 
116  protected:
117  // Removes any result that has all its unichars covered by a better choice,
118  // regardless of font.
120 };
121 
122 } // namespace tesseract.
123 
124 #endif // TESSERACT_CLASSIFY_SHAPECLASSIFIER_H_
tesseract::ShapeClassifier::DebugDisplay
virtual void DebugDisplay(const TrainingSample &sample, Pix *page_pix, UNICHAR_ID unichar_id)
Definition: shapeclassifier.cpp:96
ScrollView
Definition: scrollview.h:97
tesseract::ShapeClassifier::UnicharPrintResults
virtual void UnicharPrintResults(const char *context, const GenericVector< UnicharRating > &results) const
Definition: shapeclassifier.cpp:172
tesseract::PointerVector
Definition: genericvector.h:417
tesseract::ShapeClassifier::GetShapeTable
virtual const ShapeTable * GetShapeTable() const =0
tesseract::ShapeClassifier::UnicharClassifySample
virtual int UnicharClassifySample(const TrainingSample &sample, Pix *page_pix, int debug, UNICHAR_ID keep_this, GenericVector< UnicharRating > *results)
Definition: shapeclassifier.cpp:39
tesseract::ShapeClassifier::DisplayClassifyAs
virtual int DisplayClassifyAs(const TrainingSample &sample, Pix *page_pix, UNICHAR_ID unichar_id, int index, PointerVector< ScrollView > *windows)
Definition: shapeclassifier.cpp:163
tesseract::ShapeClassifier
Definition: shapeclassifier.h:43
tesseract::ShapeClassifier::BestShapeForUnichar
virtual int BestShapeForUnichar(const TrainingSample &sample, Pix *page_pix, UNICHAR_ID unichar_id, ShapeRating *result)
Definition: shapeclassifier.cpp:69
tesseract::ShapeClassifier::FilterDuplicateUnichars
void FilterDuplicateUnichars(GenericVector< ShapeRating > *results) const
Definition: shapeclassifier.cpp:202
tesseract::ShapeClassifier::PrintResults
virtual void PrintResults(const char *context, const GenericVector< ShapeRating > &results) const
Definition: shapeclassifier.cpp:187
UNICHARSET
Definition: unicharset.h:145
tesseract::ShapeRating
Definition: shapetable.h:92
tesseract
Definition: baseapi.h:65
tesseract::ShapeClassifier::ClassifySample
virtual int ClassifySample(const TrainingSample &sample, Pix *page_pix, int debug, UNICHAR_ID keep_this, GenericVector< ShapeRating > *results)
Definition: shapeclassifier.cpp:58
tesseract::ShapeClassifier::~ShapeClassifier
virtual ~ShapeClassifier()=default
UNICHAR_ID
int UNICHAR_ID
Definition: unichar.h:36
sample
Definition: cluster.h:31
GenericVector
Definition: baseapi.h:40
tesseract::TrainingSample
Definition: trainingsample.h:53
unichar.h
tesseract::ShapeClassifier::GetUnicharset
virtual const UNICHARSET & GetUnicharset() const
Definition: shapeclassifier.cpp:87
tesseract::ShapeTable
Definition: shapetable.h:261