tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/classify/mfx.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  **      Filename:       mfx.c
00003  **      Purpose:        Micro feature extraction routines
00004  **      Author:         Dan Johnson
00005  **      History:        7/21/89, DSJ, Created.
00006  **
00007  **      (c) Copyright Hewlett-Packard Company, 1988.
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  ******************************************************************************/
00021 #include "mfdefs.h"
00022 #include "mfoutline.h"
00023 #include "clusttool.h"           //NEEDED
00024 #include "const.h"
00025 #include "intfx.h"
00026 #include "normalis.h"
00027 #include "params.h"
00028 
00029 #include <math.h>
00030 
00035 /* old numbers corresponded to 10.0 degrees and 80.0 degrees */
00036 double_VAR(classify_min_slope, 0.414213562,
00037            "Slope below which lines are called horizontal");
00038 double_VAR(classify_max_slope, 2.414213562,
00039            "Slope above which lines are called vertical");
00040 
00044 /* miscellaneous macros */
00045 #define NormalizeAngle(A)       ( (((A)<0)?((A)+2*PI):(A)) / (2*PI) )
00046 
00047 /*----------------------------------------------------------------------------
00048           Private Function Prototypes
00049 -----------------------------------------------------------------------------*/
00050 FLOAT32 ComputeOrientation(MFEDGEPT *Start, MFEDGEPT *End);
00051 
00052 MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline,
00053                                      MICROFEATURES MicroFeatures);
00054 
00055 MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End);
00056 
00061 /*---------------------------------------------------------------------------*/
00062 CHAR_FEATURES BlobMicroFeatures(TBLOB *Blob, const DENORM& bl_denorm,
00063                                 const DENORM& cn_denorm,
00064                                 const INT_FX_RESULT_STRUCT& fx_info) {
00065 /*
00066  **      Parameters:
00067  **              Blob            blob to extract micro-features from
00068  **              denorm          control parameter to feature extractor
00069  **      Operation:
00070  **              This routine extracts micro-features from the specified
00071  **              blob and returns a list of the micro-features.  All
00072  **              micro-features are normalized according to the specified
00073  **              line statistics.
00074  **      Return: List of micro-features extracted from the blob.
00075  **      Exceptions: none
00076  **      History: 7/21/89, DSJ, Created.
00077  */
00078   MICROFEATURES MicroFeatures = NIL_LIST;
00079   LIST Outlines;
00080   LIST RemainingOutlines;
00081   MFOUTLINE Outline;
00082 
00083   if (Blob != NULL) {
00084     Outlines = ConvertBlob(Blob);
00085 
00086     RemainingOutlines = Outlines;
00087     iterate(RemainingOutlines) {
00088       Outline = (MFOUTLINE) first_node (RemainingOutlines);
00089       CharNormalizeOutline(Outline, cn_denorm);
00090     }
00091 
00092     RemainingOutlines = Outlines;
00093     iterate(RemainingOutlines) {
00094       Outline = (MFOUTLINE) first_node(RemainingOutlines);
00095       FindDirectionChanges(Outline, classify_min_slope, classify_max_slope);
00096       MarkDirectionChanges(Outline);
00097       MicroFeatures = ConvertToMicroFeatures(Outline, MicroFeatures);
00098     }
00099     FreeOutlines(Outlines);
00100   }
00101   return ((CHAR_FEATURES) MicroFeatures);
00102 }                                /* BlobMicroFeatures */
00103 
00104 
00105 /*---------------------------------------------------------------------------
00106             Private Code
00107 ---------------------------------------------------------------------------*/
00108 
00109 /*---------------------------------------------------------------------------*/
00110 FLOAT32 ComputeOrientation(MFEDGEPT *Start, MFEDGEPT *End) {
00111 /*
00112  **      Parameters:
00113  **              Start           starting edge point of micro-feature
00114  **              End             ending edge point of micro-feature
00115  **      Globals: none
00116  **      Operation:
00117  **              This routine computes the orientation parameter of the
00118  **              specified micro-feature.  The orientation is the angle of
00119  **              the vector from Start to End.  It is normalized to a number
00120  **              between 0 and 1 where 0 corresponds to 0 degrees and 1
00121  **              corresponds to 360 degrees.  The actual range is [0,1), i.e.
00122  **              1 is excluded from the range (since it is actual the
00123  **              same orientation as 0).  This routine assumes that Start
00124  **              and End are not the same point.
00125  **      Return: Orientation parameter for the specified micro-feature.
00126  **      Exceptions: none
00127  **      History: 7/27/89, DSJ, Created.
00128  */
00129   FLOAT32 Orientation;
00130 
00131   Orientation = NormalizeAngle (AngleFrom (Start->Point, End->Point));
00132 
00133   /* ensure that round-off errors do not put circular param out of range */
00134   if ((Orientation < 0) || (Orientation >= 1))
00135     Orientation = 0;
00136   return (Orientation);
00137 }                                /* ComputeOrientation */
00138 
00139 
00140 /*---------------------------------------------------------------------------*/
00141 MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline,
00142                                      MICROFEATURES MicroFeatures) {
00143 /*
00144  **      Parameters:
00145  **              Outline         outline to extract micro-features from
00146  **              MicroFeatures   list of micro-features to add to
00147  **      Globals: none
00148  **      Operation:
00149  **              This routine
00150  **      Return: List of micro-features with new features added to front.
00151  **      Exceptions: none
00152  **      History: 7/26/89, DSJ, Created.
00153  */
00154   MFOUTLINE Current;
00155   MFOUTLINE Last;
00156   MFOUTLINE First;
00157   MICROFEATURE NewFeature;
00158 
00159   if (DegenerateOutline (Outline))
00160     return (MicroFeatures);
00161 
00162   First = NextExtremity (Outline);
00163   Last = First;
00164   do {
00165     Current = NextExtremity (Last);
00166     if (!PointAt(Current)->Hidden) {
00167       NewFeature = ExtractMicroFeature (Last, Current);
00168       if (NewFeature != NULL)
00169         MicroFeatures = push (MicroFeatures, NewFeature);
00170     }
00171     Last = Current;
00172   }
00173   while (Last != First);
00174 
00175   return (MicroFeatures);
00176 }                                /* ConvertToMicroFeatures */
00177 
00178 
00179 /*---------------------------------------------------------------------------*/
00180 MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End) {
00181 /*
00182  **      Parameters:
00183  **              Start           starting point of micro-feature
00184  **              End             ending point of micro-feature
00185  **      Globals: none
00186  **      Operation:
00187  **              This routine computes the feature parameters which describe
00188  **              the micro-feature that starts and Start and ends at End.
00189  **              A new micro-feature is allocated, filled with the feature
00190  **              parameters, and returned.  The routine assumes that
00191  **              Start and End are not the same point.  If they are the
00192  **              same point, NULL is returned, a warning message is
00193  **              printed, and the current outline is dumped to stdout.
00194  **      Return: New micro-feature or NULL if the feature was rejected.
00195  **      Exceptions: none
00196  **      History: 7/26/89, DSJ, Created.
00197  **              11/17/89, DSJ, Added handling for Start and End same point.
00198  */
00199   MICROFEATURE NewFeature;
00200   MFEDGEPT *P1, *P2;
00201 
00202   P1 = PointAt(Start);
00203   P2 = PointAt(End);
00204 
00205   NewFeature = NewMicroFeature ();
00206   NewFeature[XPOSITION] = AverageOf(P1->Point.x, P2->Point.x);
00207   NewFeature[YPOSITION] = AverageOf(P1->Point.y, P2->Point.y);
00208   NewFeature[MFLENGTH] = DistanceBetween(P1->Point, P2->Point);
00209   NewFeature[ORIENTATION] = NormalizedAngleFrom(&P1->Point, &P2->Point, 1.0);
00210   NewFeature[FIRSTBULGE] = 0.0f;  // deprecated
00211   NewFeature[SECONDBULGE] = 0.0f;  // deprecated
00212 
00213   return NewFeature;
00214 }                                /* ExtractMicroFeature */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines