Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 template <typename T> class GenericVector;
27 struct Pix;
28 
29 namespace tesseract {
30 
31 class ShapeTable;
32 class TrainingSample;
33 
34 // Classifier result from a low-level classification is an index into some
35 // ShapeTable and a rating.
36 struct ShapeRating {
37  ShapeRating() : shape_id(0), rating(0.0f), raw(0.0f), font(0.0f) {}
38  ShapeRating(int s, float r)
39  : shape_id(s), rating(r), raw(1.0f), font(0.0f) {}
40 
41  // Sort function to sort ratings appropriately by descending rating.
42  static int SortDescendingRating(const void* t1, const void* t2) {
43  const ShapeRating* a = reinterpret_cast<const ShapeRating *>(t1);
44  const ShapeRating* b = reinterpret_cast<const ShapeRating *>(t2);
45  if (a->rating > b->rating) {
46  return -1;
47  } else if (a->rating < b->rating) {
48  return 1;
49  } else {
50  return a->shape_id - b->shape_id;
51  }
52  }
53 
54  // Index into some shape table indicates the class of the answer.
55  int shape_id;
56  // Rating from classifier with 1.0 perfect and 0.0 impossible.
57  // Call it a probability if you must.
58  float rating;
59  // Subsidiary rating that a classifier may use internally.
60  float raw;
61  // Subsidiary rating that a classifier may use internally.
62  float font;
63 };
64 
65 // Interface base class for classifiers that produce ShapeRating results.
67  public:
68  virtual ~ShapeClassifier() {}
69 
70  // Classifies the given [training] sample, writing to results.
71  // If page_pix is not NULL, the overriding function may call
72  // sample.GetSamplePix(padding, page_pix) to get an image of the sample
73  // padded (with real image data) by the given padding to extract features
74  // from the image of the character. Other members of TrainingSample:
75  // features(), micro_features(), cn_feature(), geo_feature() may be used
76  // to get the appropriate tesseract features.
77  // If debug is non-zero, then various degrees of classifier dependent debug
78  // information is provided.
79  // If keep_this (a shape index) is >= 0, then the results should always
80  // contain keep_this, and (if possible) anything of intermediate confidence.
81  // (Used for answering "Why didn't it get that right?" questions.)
82  // The return value is the number of classes saved in results.
83  // NOTE that overriding functions MUST clear results unless the classifier
84  // is working with a team of such classifiers.
85  virtual int ClassifySample(const TrainingSample& sample, Pix* page_pix,
86  int debug, int keep_this,
87  GenericVector<ShapeRating>* results) = 0;
88 
89  // Provides access to the ShapeTable that this classifier works with.
90  virtual const ShapeTable* GetShapeTable() const = 0;
91 };
92 
93 } // namespace tesseract.
94 
95 #endif // TESSERACT_CLASSIFY_SHAPECLASSIFIER_H_