Tesseract  3.02
tesseract-ocr/ccstruct/linlsq.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        linlsq.h  (Formerly llsq.h)
00003  * Description: Linear Least squares fitting code.
00004  * Author:              Ray Smith
00005  * Created:             Thu Sep 12 08:44:51 BST 1991
00006  *
00007  * (C) Copyright 1991, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef TESSERACT_CCSTRUCT_LINLSQ_H_
00021 #define TESSERACT_CCSTRUCT_LINLSQ_H_
00022 
00023 #include "points.h"
00024 #include "params.h"
00025 
00026 class LLSQ {
00027  public:
00028   LLSQ() {  // constructor
00029     clear();  // set to zeros
00030   }
00031   void clear();  // initialize
00032 
00033   // Adds an element with a weight of 1.
00034   void add(double x, double y);
00035   // Adds an element with a specified weight.
00036   void add(double x, double y, double weight);
00037   // Adds a whole LLSQ.
00038   void add(const LLSQ& other);
00039   // Deletes an element with a weight of 1.
00040   void remove(double x, double y);
00041   inT32 count() const {  // no of elements
00042     return static_cast<int>(total_weight + 0.5);
00043   }
00044 
00045   double m() const;  // get gradient
00046   double c(double m) const;            // get constant
00047   double rms(double m, double c) const;            // get error
00048   double pearson() const;  // get correlation coefficient.
00049 
00050   // Returns the x,y means as an FCOORD.
00051   FCOORD mean_point() const;
00052   // Returns the direction of the fitted line as a unit vector, using the
00053   // least mean squared perpendicular distance. The line runs through the
00054   // mean_point, i.e. a point p on the line is given by:
00055   // p = mean_point() + lambda * vector_fit() for some real number lambda.
00056   // Note that the result (0<=x<=1, -1<=y<=-1) is directionally ambiguous
00057   // and may be negated without changing its meaning.
00058   FCOORD vector_fit() const;
00059   // Returns the covariance.
00060   double covariance() const {
00061     if (total_weight > 0.0)
00062       return (sigxy - sigx * sigy / total_weight) / total_weight;
00063     else
00064       return 0.0;
00065   }
00066   double x_variance() const {
00067     if (total_weight > 0.0)
00068       return (sigxx - sigx * sigx / total_weight) / total_weight;
00069     else
00070       return 0.0;
00071   }
00072   double y_variance() const {
00073     if (total_weight > 0.0)
00074       return (sigyy - sigy * sigy / total_weight) / total_weight;
00075     else
00076       return 0.0;
00077   }
00078 
00079  private:
00080   double total_weight;         // no of elements or sum of weights.
00081   double sigx;                 // sum of x
00082   double sigy;                 // sum of y
00083   double sigxx;                // sum x squared
00084   double sigxy;                // sum of xy
00085   double sigyy;                // sum y squared
00086 };
00087 
00088 #endif  // TESSERACT_CCSTRUCT_LINLSQ_H_