Tesseract  3.02
tesseract-ocr/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 template <typename T> class GenericVector;
00027 struct Pix;
00028 
00029 namespace tesseract {
00030 
00031 class ShapeTable;
00032 class TrainingSample;
00033 
00034 // Classifier result from a low-level classification is an index into some
00035 // ShapeTable and a rating.
00036 struct ShapeRating {
00037   ShapeRating() : shape_id(0), rating(0.0f), raw(0.0f), font(0.0f) {}
00038   ShapeRating(int s, float r)
00039     : shape_id(s), rating(r), raw(1.0f), font(0.0f) {}
00040 
00041   // Sort function to sort ratings appropriately by descending rating.
00042   static int SortDescendingRating(const void* t1, const void* t2) {
00043     const ShapeRating* a = reinterpret_cast<const ShapeRating *>(t1);
00044     const ShapeRating* b = reinterpret_cast<const ShapeRating *>(t2);
00045     if (a->rating > b->rating) {
00046       return -1;
00047     } else if (a->rating < b->rating) {
00048       return 1;
00049     } else {
00050       return a->shape_id - b->shape_id;
00051     }
00052   }
00053 
00054   // Index into some shape table indicates the class of the answer.
00055   int shape_id;
00056   // Rating from classifier with 1.0 perfect and 0.0 impossible.
00057   // Call it a probability if you must.
00058   float rating;
00059   // Subsidiary rating that a classifier may use internally.
00060   float raw;
00061   // Subsidiary rating that a classifier may use internally.
00062   float font;
00063 };
00064 
00065 // Interface base class for classifiers that produce ShapeRating results.
00066 class ShapeClassifier {
00067  public:
00068   virtual ~ShapeClassifier() {}
00069 
00070   // Classifies the given [training] sample, writing to results.
00071   // If page_pix is not NULL, the overriding function may call
00072   // sample.GetSamplePix(padding, page_pix) to get an image of the sample
00073   // padded (with real image data) by the given padding to extract features
00074   // from the image of the character. Other members of TrainingSample:
00075   // features(), micro_features(), cn_feature(), geo_feature() may be used
00076   // to get the appropriate tesseract features.
00077   // If debug is non-zero, then various degrees of classifier dependent debug
00078   // information is provided.
00079   // If keep_this (a shape index) is >= 0, then the results should always
00080   // contain keep_this, and (if possible) anything of intermediate confidence.
00081   // (Used for answering "Why didn't it get that right?" questions.)
00082   // The return value is the number of classes saved in results.
00083   // NOTE that overriding functions MUST clear results unless the classifier
00084   // is working with a team of such classifiers.
00085   virtual int ClassifySample(const TrainingSample& sample, Pix* page_pix,
00086                              int debug, int keep_this,
00087                              GenericVector<ShapeRating>* results) = 0;
00088 
00089   // Provides access to the ShapeTable that this classifier works with.
00090   virtual const ShapeTable* GetShapeTable() const = 0;
00091 };
00092 
00093 }  // namespace tesseract.
00094 
00095 #endif  // TESSERACT_CLASSIFY_SHAPECLASSIFIER_H_