classify/mfx.cpp File Reference

#include "mfdefs.h"
#include "variables.h"
#include "sigmenu.h"
#include "mfoutline.h"
#include "clusttool.h"
#include "const.h"
#include "intfx.h"
#include <math.h>

Go to the source code of this file.

Defines

Functions

Variables


Define Documentation

#define angle_of ( x1,
y1,
x2,
y2   ) 

Value:

((x2-x1) ?                                    \
   (atan2 (y2-y1, x2-x1)) :                     \
   ((y2<y1) ? (- PI / 2.0) : (PI / 2.0)))   \
Return the angle of the line between two points.

Definition at line 197 of file mfx.cpp.

#define MAX_FEATURE_LENGTH   (MAXFLOAT)

Definition at line 41 of file mfx.cpp.

#define MAX_SLOPE   2.414213562

Definition at line 37 of file mfx.cpp.

Referenced by InitMicroFxVars().

#define MIN_SLOPE   0.414213562

Note:
File: mfx.cpp
Micro feature extraction routines
Author:
Dan Johnson
Date:
7/21/89, DSJ, Created.
 ** (c) Copyright Hewlett-Packard Company, 1988.
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 ** http://www.apache.org/licenses/LICENSE-2.0
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.

Definition at line 35 of file mfx.cpp.

Referenced by InitMicroFxVars().

#define NOISE_SEGMENT_LENGTH   (0.00)

Definition at line 39 of file mfx.cpp.

Referenced by InitMicroFxVars().

#define NormalizeAngle (  )     ( (((A)<0)?((A)+2*PI):(A)) / (2*PI) )

Definition at line 47 of file mfx.cpp.

Referenced by ComputeOrientation().

#define scale_angle (  )     (((x<0) ? (2.0 * PI + x) : (x)) * 0.5 / PI) \

Make sure that the angle is non-negative.

Scale it to the right amount.

Definition at line 208 of file mfx.cpp.


Function Documentation

CHAR_FEATURES BlobMicroFeatures ( TBLOB Blob,
LINE_STATS LineStats 
)

Extracts micro-features from the specified blob.

Parameters:
Blob Blob to extract micro-features from
LineStats Statistics for text line normalization
Note:
Globals: XHeightAdjust Used for manually adjusting xheight
Returns:
List of micro-features extracted from the blob.
All micro-features are normalized according to the specified line statistics.
Note:
Exceptions: none
Date:
7/21/89, DSJ, Created.

Definition at line 150 of file mfx.cpp.

References CharNormalizeOutline(), ConvertBlob(), ConvertToMicroFeatures(), ExtractIntFeat(), FilterEdgeNoise(), FindDirectionChanges(), first, FreeOutlines(), iterate, MarkDirectionChanges(), MaxSlope, MinSlope, NIL, NoiseSegmentLength, NULL, Outline, INT_FX_RESULT_STRUCT::Rx, INT_FX_RESULT_STRUCT::Ry, SmearBulges(), SmearExtremities(), INT_FX_RESULT_STRUCT::Xmean, and INT_FX_RESULT_STRUCT::Ymean.

Referenced by ExtractMicros().

00150                                                                     { 
00151   MICROFEATURES MicroFeatures = NIL;
00152   FLOAT32 XScale, YScale;
00153   LIST Outlines;
00154   LIST RemainingOutlines;
00155   MFOUTLINE Outline;
00156   INT_FEATURE_ARRAY blfeatures;
00157   INT_FEATURE_ARRAY cnfeatures;
00158   INT_FX_RESULT_STRUCT results;
00159 
00160   if (Blob != NULL) {
00161     Outlines = ConvertBlob (Blob);
00162 //    NormalizeOutlines(Outlines, LineStats, &XScale, &YScale); 
00163     ExtractIntFeat(Blob, blfeatures, cnfeatures, &results);
00164     XScale = 0.2f / results.Ry;
00165     YScale = 0.2f / results.Rx;
00166 
00167     RemainingOutlines = Outlines;
00168     iterate(RemainingOutlines) { 
00169       Outline = (MFOUTLINE) first (RemainingOutlines);
00170       CharNormalizeOutline (Outline,
00171         results.Xmean, results.Ymean,
00172         XScale, YScale);
00173     }
00174 
00175     RemainingOutlines = Outlines;
00176     iterate(RemainingOutlines) {
00177       Outline = (MFOUTLINE) first (RemainingOutlines);
00178       FindDirectionChanges(Outline, MinSlope, MaxSlope);
00179       FilterEdgeNoise(Outline, NoiseSegmentLength);
00180       MarkDirectionChanges(Outline);
00181       SmearExtremities(Outline, XScale, YScale);
00182       MicroFeatures = ConvertToMicroFeatures (Outline, MicroFeatures);
00183     }
00184     SmearBulges(MicroFeatures, XScale, YScale);
00185     FreeOutlines(Outlines);
00186   }
00187   return ((CHAR_FEATURES) MicroFeatures);
00188 }                                /* BlobMicroFeatures */

void ComputeBulges ( MFOUTLINE  Start,
MFOUTLINE  End,
MICROFEATURE  MicroFeature 
)

Computes the size of the "bulges" of the specified micro-feature.

Parameters:
Start Starting point of micro-feature
End Ending point of micro-feature
MicroFeature Micro-feature whose bulges are to be computed
Note:
Globals: none
Returns:
none
The bulges are the deviations of the micro-features from a straight line at the 1/3 and 2/3 points along the straight line approximation of the micro-feature. The size of each bulge is normalized to the range -0.5 to 0.5. A positive bulge indicates a deviation in the counterclockwise direction and vice versa. A size of 0.5 (+ or -) corresponds to the largest bulge that could ever occur for the given feature independent of orientation.

This routine assumes that Start and End are not the same point. It also assumes that the orientation and length parameters of the micro-feature have already been computed.

Note:
Exceptions: none
Date:
7/27/89, DSJ, Created.

Definition at line 240 of file mfx.cpp.

References BULGENORMALIZER, CopyPoint, FillPoint, FirstBulgeOf, InitMatrix, LengthOf, MapPoint, NextPointAfter, OrientationOf, PI, PointAt, PositionOf, RotateMatrix(), SecondBulgeOf, TranslateMatrix, XIntersectionOf, Xof, XPositionOf, and YPositionOf.

Referenced by ExtractMicroFeature().

00240                                                                               { 
00241   MATRIX_2D Matrix;
00242   MFEDGEPT *Origin;
00243   MFOUTLINE SegmentStart, SegmentEnd;
00244   FPOINT CurrentPoint, LastPoint;
00245   FLOAT32 BulgePosition;
00246 
00247   /* check for simple case */
00248   if (End == NextPointAfter (Start))
00249     FirstBulgeOf (MicroFeature) = SecondBulgeOf (MicroFeature) = 0;
00250   else {
00251     Origin = PointAt (Start);
00252 
00253     InitMatrix(&Matrix);
00254     RotateMatrix (&Matrix, OrientationOf (MicroFeature) * -2.0 * PI);
00255     TranslateMatrix (&Matrix, -XPositionOf (Origin), -YPositionOf (Origin));
00256 
00257     SegmentEnd = Start;
00258     FillPoint (CurrentPoint, 0, 0);
00259     BulgePosition = LengthOf (MicroFeature) / 3;
00260     CopyPoint(CurrentPoint, LastPoint);
00261     while (Xof (CurrentPoint) < BulgePosition) {
00262       SegmentStart = SegmentEnd;
00263       SegmentEnd = NextPointAfter (SegmentStart);
00264       CopyPoint(CurrentPoint, LastPoint);
00265       MapPoint (&Matrix, PositionOf (PointAt (SegmentEnd)), CurrentPoint);
00266     }
00267     FirstBulgeOf (MicroFeature) =
00268       XIntersectionOf(LastPoint, CurrentPoint, BulgePosition);
00269 
00270     BulgePosition *= 2;
00271 
00272     // Prevents from copying the points before computing the bulge if
00273     // CurrentPoint will not change. (Which would cause to output nan
00274     // for the SecondBulge.)
00275     if (Xof (CurrentPoint) < BulgePosition)
00276       CopyPoint(CurrentPoint, LastPoint);
00277     while (Xof (CurrentPoint) < BulgePosition) {
00278       SegmentStart = SegmentEnd;
00279       SegmentEnd = NextPointAfter (SegmentStart);
00280       CopyPoint(CurrentPoint, LastPoint);
00281       MapPoint (&Matrix, PositionOf (PointAt (SegmentEnd)), CurrentPoint);
00282     }
00283     SecondBulgeOf (MicroFeature) =
00284       XIntersectionOf(LastPoint, CurrentPoint, BulgePosition);
00285 
00286     FirstBulgeOf (MicroFeature) /= BULGENORMALIZER *
00287       LengthOf(MicroFeature);
00288     SecondBulgeOf (MicroFeature) /= BULGENORMALIZER *
00289       LengthOf(MicroFeature);
00290   }
00291 }                                /* ComputeBulges */

FLOAT32 ComputeOrientation ( MFEDGEPT Start,
MFEDGEPT End 
)

Computes the orientation parameter of the specified micro-feature.

Parameters:
Start Starting edge point of micro-feature
End Ending edge point of micro-feature
Returns:
Orientation parameter for the specified micro-feature.
The orientation is the angle of the vector from Start to End. It is normalized to a number between 0 and 1 where 0 corresponds to 0 degrees and 1 corresponds to 360 degrees. The actual range is [0,1), i.e. 1 is excluded from the range (since it is actual the same orientation as 0).

This routine assumes that Start and End are not the same point.

Note:
Exceptions: none
Date:
7/27/89, DSJ, Created.

Definition at line 314 of file mfx.cpp.

References AngleFrom, NormalizeAngle, and PositionOf.

00314                                                            { 
00315   FLOAT32 Orientation;
00316 
00317   Orientation = NormalizeAngle (AngleFrom (PositionOf (Start),
00318     PositionOf (End)));
00319 
00320   /* ensure that round-off errors do not put circular param out of range */
00321   if ((Orientation < 0) || (Orientation >= 1))
00322     Orientation = 0;
00323   return (Orientation);
00324 }                                /* ComputeOrientation */

MICROFEATURES ConvertToMicroFeatures ( MFOUTLINE  Outline,
MICROFEATURES  MicroFeatures 
)

Extracts micro-features from Outline and adds them to MicroFeatures.

Parameters:
Outline Outline to extract micro-features from
MicroFeatures List of micro-features to add to
Returns:
List of micro-features with new features added to front.
Note:
Exceptions: none
Date:
7/26/89, DSJ, Created.

Definition at line 337 of file mfx.cpp.

References DegenerateOutline, ExtractMicroFeature(), NewFeature(), NextExtremity(), NULL, and push().

Referenced by BlobMicroFeatures().

00338                                                                   {
00339   MFOUTLINE Current;
00340   MFOUTLINE Last;
00341   MFOUTLINE First;
00342   MICROFEATURE NewFeature;
00343 
00344   if (DegenerateOutline (Outline))
00345     return (MicroFeatures);
00346 
00347   First = NextExtremity (Outline);
00348   Last = First;
00349   do {
00350     Current = NextExtremity (Last);
00351     NewFeature = ExtractMicroFeature (Last, Current);
00352     if (NewFeature != NULL)
00353       MicroFeatures = push (MicroFeatures, NewFeature);
00354     Last = Current;
00355   }
00356   while (Last != First);
00357 
00358   return (MicroFeatures);
00359 }                                /* ConvertToMicroFeatures */

MICROFEATURE ExtractMicroFeature ( MFOUTLINE  Start,
MFOUTLINE  End 
)

Computes the feature parameters which describe the micro-feature that starts and Start and ends at End.

Parameters:
Start Starting point of micro-feature
End Ending point of micro-feature
Returns:
New micro-feature or NULL if the feature was rejected.
A new micro-feature is allocated, filled with the feature parameters, and returned. The routine assumes that Start and End are not the same point. If they are the same point, NULL is returned, a warning message is printed, and the current outline is dumped to stdout.
Note:
Exceptions: none
Date:
7/26/89, DSJ, Created. 11/17/89, DSJ, Added handling for Start and End same point.

Definition at line 380 of file mfx.cpp.

References AverageOf, CenterX, CenterY, ComputeBulges(), DistanceBetween, LengthOf, NewFeature(), NewMicroFeature(), NormalizedAngleFrom(), OrientationOf, PointAt, PositionOf, XPositionOf, and YPositionOf.

Referenced by ConvertToMicroFeatures().

00380                                                                  { 
00381   MICROFEATURE NewFeature;
00382   MFEDGEPT *P1, *P2;
00383 
00384   P1 = PointAt (Start);
00385   P2 = PointAt (End);
00386 
00387   NewFeature = NewMicroFeature ();
00388   CenterX (NewFeature) = AverageOf (XPositionOf (P1), XPositionOf (P2));
00389   CenterY (NewFeature) = AverageOf (YPositionOf (P1), YPositionOf (P2));
00390   LengthOf (NewFeature) = DistanceBetween (PositionOf (P1), PositionOf (P2));
00391   OrientationOf (NewFeature) =
00392     NormalizedAngleFrom (&(PositionOf (P1)), &(PositionOf (P2)), 1.0);
00393   ComputeBulges(Start, End, NewFeature);
00394   return (NewFeature);
00395 }                                /* ExtractMicroFeature */

void InitMicroFxVars (  ) 

Initialize the micro-feature extractor variables (knobs) that can be tuned without recompiling.

Parameters:
none 
Note:
Globals:
  • MinSlope Slope below which lines are called horizontal
  • MaxSlope Slope above which lines are called vertical
  • NoiseSegmentLength Length below which outline segments are treated as noise
  • MaxFeatureLength Length above which a feature will be split into 2 equal pieces
  • ExtremityMode Controls how extremities are defined
  • XHeightAdjust Allows xheight of line to be adjusted
Returns:
none
Note:
Exceptions: none
Date:
Mon May 14 11:24:40 1990, DSJ, Created.

Definition at line 126 of file mfx.cpp.

References dummy, float_variable, MAX_SLOPE, MaxSlope, MIN_SLOPE, MinSlope, NOISE_SEGMENT_LENGTH, and NoiseSegmentLength.

Referenced by InitMicroFXVars().

00126                        { 
00127   VALUE dummy;
00128 
00129   float_variable (MinSlope, "MinSlope", MIN_SLOPE);
00130   float_variable (MaxSlope, "MaxSlope", MAX_SLOPE);
00131   float_variable (NoiseSegmentLength, "NoiseSegmentLength",
00132     NOISE_SEGMENT_LENGTH);
00133 }                                /* InitMicroFxVars */

void SmearBulges ( MICROFEATURES  MicroFeatures,
FLOAT32  XScale,
FLOAT32  YScale 
)

Add a random amount to each bulge parameter of each feature.

Parameters:
MicroFeatures Features to be smeared
XScale Number of normalized units per pixel in x dir
YScale Number of normalized units per pixel in y dir
Returns:
none
The amount added is between -0.5 pixels and 0.5 pixels. This is done to prevent the prototypes generated in training from being unrealistically tight.
Note:
Exceptions: none
Date:
Thu Jun 28 18:03:38 1990, DSJ, Created.

Definition at line 413 of file mfx.cpp.

References BULGENORMALIZER, FirstBulgeOf, iterate, LengthOf, NextFeatureOf, OrientationOf, PI, SecondBulgeOf, and UniformRandomNumber().

Referenced by BlobMicroFeatures().

00413                                                                               { 
00414   MICROFEATURE MicroFeature;
00415   FLOAT32 MinSmear;
00416   FLOAT32 MaxSmear;
00417   FLOAT32 Cos, Sin;
00418   FLOAT32 Scale;
00419 
00420   iterate(MicroFeatures) {
00421     MicroFeature = NextFeatureOf (MicroFeatures);
00422 
00423     Cos = fabs (cos (2.0 * PI * OrientationOf (MicroFeature)));
00424     Sin = fabs (sin (2.0 * PI * OrientationOf (MicroFeature)));
00425     Scale = YScale * Cos + XScale * Sin;
00426 
00427     MinSmear = -0.5 * Scale / (BULGENORMALIZER * LengthOf (MicroFeature));
00428     MaxSmear = 0.5 * Scale / (BULGENORMALIZER * LengthOf (MicroFeature));
00429 
00430     FirstBulgeOf (MicroFeature) += UniformRandomNumber (MinSmear, MaxSmear);
00431     SecondBulgeOf (MicroFeature) += UniformRandomNumber (MinSmear, MaxSmear);
00432   }
00433 }                                /* SmearBulges */


Variable Documentation

FLOAT32 MaxSlope [static]

Definition at line 101 of file mfx.cpp.

Referenced by BlobMicroFeatures(), and InitMicroFxVars().

FLOAT32 MinSlope [static]

Definition at line 100 of file mfx.cpp.

Referenced by BlobMicroFeatures(), and InitMicroFxVars().

FLOAT32 NoiseSegmentLength [static]

Definition at line 102 of file mfx.cpp.

Referenced by BlobMicroFeatures(), and InitMicroFxVars().


Generated on Wed Feb 28 19:49:20 2007 for Tesseract by  doxygen 1.5.1