training/mergenf.cpp File Reference

#include "mergenf.h"
#include "general.h"
#include "efio.h"
#include "clusttool.h"
#include "cluster.h"
#include "oldlist.h"
#include "protos.h"
#include "minmax.h"
#include "ocrfeatures.h"
#include "debug.h"
#include "const.h"
#include "featdefs.h"
#include "intproto.h"
#include <stdio.h>
#include <string.h>
#include <math.h>

Go to the source code of this file.

Functions


Function Documentation

FLOAT32 CompareProtos ( PROTO  p1,
PROTO  p2 
)

Compare two prototypes and compute worst evidence rating.

Parameters:
p1 proto to compare
p2 proto to compare
Returns:
Worst evidence from comparing ALL pico-features in p1 to those in p2
Compare protos p1 and p2 and return an estimate of the worst evidence rating that will result for any part of p1 that is compared to p2.

In other words, if p1 were broken into pico-features and each pico-feature was matched to p2, what is the worst evidence rating that will be achieved for any pico-feature.

Note:
If length of all features of p1 differs significantly from that of p2, returns 0.0
Date:
Mon Nov 26 08:27:53 1990, DSJ, Created.

Definition at line 94 of file mergenf.cpp.

References DummyFastMatch(), FreeFeature(), GetPicoFeatureLength, MAX_LENGTH_MISMATCH, NewFeature(), ParamOf, PI, PicoFeatDesc, PicoFeatDir, PicoFeatX, PicoFeatY, ProtoAngle, ProtoLength, ProtoX, ProtoY, SubfeatureEvidence(), and WORST_EVIDENCE.

Referenced by FindClosestExistingProto().

00097 {
00098    FEATURE  Feature;
00099    FLOAT32  WorstEvidence = WORST_EVIDENCE;
00100    FLOAT32  Evidence;
00101    FLOAT32  Angle, Length;
00102 
00103    /* if p1 and p2 are not close in length, don't let them match */
00104    Length = fabs (ProtoLength (p1) - ProtoLength (p2));
00105    if (Length > MAX_LENGTH_MISMATCH)
00106       return (0.0);
00107 
00108    /* create a dummy pico-feature to be used for comparisons */
00109    Feature = NewFeature (&PicoFeatDesc);
00110    ParamOf (Feature, PicoFeatDir) = ProtoAngle (p1);
00111 
00112    /* convert angle to radians */
00113    Angle = ProtoAngle (p1) * 2.0 * PI;
00114 
00115    /* find distance from center of p1 to 1/2 picofeat from end */
00116    Length = ProtoLength (p1) / 2.0 - GetPicoFeatureLength () / 2.0;
00117    if (Length < 0) Length = 0;
00118 
00119    /* set the dummy pico-feature at one end of p1 and match it to p2 */
00120    ParamOf (Feature, PicoFeatX) = ProtoX (p1) + cos (Angle) * Length;
00121    ParamOf (Feature, PicoFeatY) = ProtoY (p1) + sin (Angle) * Length;
00122    if (DummyFastMatch (Feature, p2))
00123     {
00124       Evidence = SubfeatureEvidence (Feature, p2);
00125       if (Evidence < WorstEvidence)
00126          WorstEvidence = Evidence;
00127     }
00128    else
00129     {
00130       FreeFeature (Feature);
00131       return (0.0);
00132     }
00133 
00134    /* set the dummy pico-feature at the other end of p1 and match it to p2 */
00135    ParamOf (Feature, PicoFeatX) = ProtoX (p1) - cos (Angle) * Length;
00136    ParamOf (Feature, PicoFeatY) = ProtoY (p1) - sin (Angle) * Length;
00137    if (DummyFastMatch (Feature, p2))
00138     {
00139       Evidence = SubfeatureEvidence (Feature, p2);
00140       if (Evidence < WorstEvidence)
00141          WorstEvidence = Evidence;
00142     }
00143    else
00144     {
00145       FreeFeature (Feature);
00146       return (0.0);
00147     }
00148 
00149    FreeFeature (Feature);
00150    return (WorstEvidence);
00151 
00152 }// CompareProtos

void ComputeMergedProto ( PROTO  p1,
PROTO  p2,
FLOAT32  w1,
FLOAT32  w2,
PROTO  MergedProto 
)

Computes a proto which is the weighted average of protos p1 and p2.

Parameters:
p1 proto to merge
p2 proto to merge
w1 Weight of p1
w2 Weight of p2
MergedProto Place to put resulting merged proto
Returns:
MergedProto is modified
Date:
Mon Nov 26 08:15:08 1990, DSJ, Created.

Definition at line 167 of file mergenf.cpp.

References FillABC(), ProtoAngle, ProtoLength, ProtoX, and ProtoY.

Referenced by FindClosestExistingProto().

00173 {
00174    FLOAT32  TotalWeight;
00175 
00176    TotalWeight = w1 + w2;
00177    w1 /= TotalWeight;
00178    w2 /= TotalWeight;
00179 
00180    ProtoX      (MergedProto) = ProtoX      (p1) * w1 + ProtoX      (p2) * w2;
00181    ProtoY      (MergedProto) = ProtoY      (p1) * w1 + ProtoY      (p2) * w2;
00182    ProtoLength (MergedProto) = ProtoLength (p1) * w1 + ProtoLength (p2) * w2;
00183    ProtoAngle  (MergedProto) = ProtoAngle  (p1) * w1 + ProtoAngle  (p2) * w2;
00184    FillABC     (MergedProto);
00185 
00186 }// ComputeMergedProto

void ComputePaddedBoundingBox ( PROTO  Proto,
FLOAT32  TangentPad,
FLOAT32  OrthogonalPad,
FRECT BoundingBox 
)

Computes padded bounding box for Proto.

Parameters:
Proto Proto to compute bounding box for
TangentPad Amount of pad to add in direction of segment
OrthogonalPad Amount of pad to add orthogonal to segment
BoundingBox Place to put results
Returns:
BoundingBox is modified
Computes a bounding box that encloses the specified proto along with some padding. The amount of padding is specified as separate distances in the tangential and orthogonal directions.

Date:
Wed Nov 14 14:55:30 1990, DSJ, Created.

Definition at line 407 of file mergenf.cpp.

References MAX, FRECT::MaxX, FRECT::MaxY, FRECT::MinX, FRECT::MinY, PI, ProtoAngle, ProtoLength, ProtoX, and ProtoY.

Referenced by DummyFastMatch().

00412 {
00413    FLOAT32  Pad, Length, Angle;
00414    FLOAT32  CosOfAngle, SinOfAngle;
00415 
00416    Length     = ProtoLength (Proto) / 2.0 + TangentPad;
00417    Angle      = ProtoAngle (Proto) * 2.0 * PI;
00418    CosOfAngle = fabs (cos (Angle));
00419    SinOfAngle = fabs (sin (Angle));
00420 
00421    Pad = MAX (CosOfAngle * Length, SinOfAngle * OrthogonalPad);
00422    BoundingBox->MinX = ProtoX (Proto) - Pad;
00423    BoundingBox->MaxX = ProtoX (Proto) + Pad;
00424 
00425    Pad = MAX (SinOfAngle * Length, CosOfAngle * OrthogonalPad);
00426    BoundingBox->MinY = ProtoY (Proto) - Pad;
00427    BoundingBox->MaxY = ProtoY (Proto) + Pad;
00428 
00429 }// ComputePaddedBoundingBox

BOOL8 DummyFastMatch ( FEATURE  Feature,
PROTO  Proto 
)

Check if Feature would be matched by a fast match table built from Proto.

Parameters:
Feature Feature to be "fast matched" to prototype
Proto Prototype being "fast matched" against
Note:
Globals:
  • TangentBBoxPad = Bounding box pad TANGENT to proto
  • OrthogonalBBoxPad = Bounding box pad ORTHOGONAL to proto
Returns:
TRUE if feature could match Proto.
Date:
Wed Nov 14 17:19:58 1990, DSJ, Created.

Definition at line 363 of file mergenf.cpp.

References ComputePaddedBoundingBox(), FALSE, GetPicoFeatureLength, ParamOf, PicoFeatDir, PicoFeatX, PicoFeatY, PointInside(), and ProtoAngle.

Referenced by CompareProtos().

00366 {
00367    FRECT    BoundingBox;
00368    FLOAT32  MaxAngleError;
00369    FLOAT32  AngleError;
00370 
00371    MaxAngleError = AnglePad / 360.0;
00372    AngleError = fabs (ProtoAngle (Proto) - ParamOf (Feature, PicoFeatDir));
00373    if (AngleError > 0.5)
00374       AngleError = 1.0 - AngleError;
00375 
00376    if (AngleError > MaxAngleError)
00377       return (FALSE);
00378 
00379    ComputePaddedBoundingBox (Proto,
00380       TangentBBoxPad * GetPicoFeatureLength (),
00381       OrthogonalBBoxPad * GetPicoFeatureLength (),
00382       &BoundingBox);
00383 
00384    return (PointInside (&BoundingBox,
00385       ParamOf (Feature, PicoFeatX),
00386       ParamOf (Feature, PicoFeatY)));
00387 
00388 }// DummyFastMatch

FLOAT32 EvidenceOf ( register FLOAT32  Similarity  ) 

Computes evidence Computes new type of evidence number corresponding to this distance value, no longer based on the chi-squared approximation. The new equation that represents the transform is:.

Parameters:
Similarity Distance value (=Distance^2 + Dangle^2, see SubfeatureEvidence())
Note:
Globals:
  • SimilarityMidpoint = Constant "midpoint", preset to 0.0075
  • SimilarityCurl = Constant "curl", preset to 2.0
Returns:
Evidence of (what ???)
    1 / (1 + (sim / midpoint) ^ curl)

Note:
Comparison of feature to prototype, uses coefficients A, B, and C mentioned in Patent
Todo:
This needs a higher-level explanation of what it does and how

Definition at line 320 of file mergenf.cpp.

Referenced by SubfeatureEvidence().

00322 {
00323 
00324    Similarity /= SimilarityMidpoint;
00325 
00326    if (SimilarityCurl == 3)
00327       Similarity = Similarity * Similarity * Similarity;
00328    else if (SimilarityCurl == 2)
00329       Similarity = Similarity * Similarity;
00330    else
00331       Similarity = pow (Similarity, SimilarityCurl);
00332 
00333    return (1.0 / (1.0 + Similarity));
00334 }// EvidenceOf

int FindClosestExistingProto ( CLASS_TYPE  Class,
int  NumMerged[],
PROTOTYPE Prototype 
)

Find Id of prototype closest matching Prototype in Class.

Parameters:
Class Class(font) to search for matching old proto in
NumMerged Number of protos merged into each proto of Class
Prototype New proto to find match for
Returns:
Id of closest proto in Class or NO_PROTO
Searches thru all of the prototypes in Class and returns the id of the proto which would provide the best approximation of Prototype.
Date:
Sat Nov 24 11:42:58 1990, DSJ, Created.

Definition at line 202 of file mergenf.cpp.

References CompareProtos(), ComputeMergedProto(), MakeNewFromOld(), MIN, NO_PROTO, NumProtosIn, ProtoIn, and WORST_MATCH_ALLOWED.

00206 {
00207    PROTO_STRUCT   NewProto;
00208    PROTO_STRUCT   MergedProto;
00209    int      Pid;
00210    PROTO    Proto;
00211    int      BestProto;
00212    FLOAT32  BestMatch;
00213    FLOAT32  Match, OldMatch, NewMatch;
00214 
00215    MakeNewFromOld (&NewProto, Prototype);
00216 
00217    BestProto = NO_PROTO;
00218    BestMatch = WORST_MATCH_ALLOWED;
00219    for (Pid = 0; Pid < NumProtosIn (Class); Pid++)
00220    {
00221       Proto  = ProtoIn (Class, Pid);
00222       ComputeMergedProto (Proto, &NewProto,
00223          (FLOAT32) NumMerged[Pid], 1.0, &MergedProto);
00224       OldMatch = CompareProtos (Proto, &MergedProto);
00225       NewMatch = CompareProtos (&NewProto, &MergedProto);
00226       Match = MIN (OldMatch, NewMatch);
00227       if (Match > BestMatch)
00228       {
00229          BestProto = Pid;
00230          BestMatch = Match;
00231       }
00232    }//for
00233    return (BestProto);
00234 
00235 }// FindClosestExistingProto

void InitFastTrainerVars (  ) 

Initialize all of control-variables for fast trainer.

Note:
Mon Nov 12 13:27:35 1990, DSJ, Created.

Done once in fasttrain

Definition at line 342 of file mergenf.cpp.

00343 {
00344    MakeTangentBBoxPad ();
00345    MakeOrthogonalBBoxPad ();
00346    MakeAnglePad ();
00347 
00348 }// InitFastTrainerVars

void InitSubfeatureVars (  ) 

Create and set up all menus and variables needed for this file.

Note:
Done once in subfeat

Definition at line 265 of file mergenf.cpp.

00266 {
00267    MakeAngleMatchScale     ();
00268    MakeSimilarityCurl      ();
00269    MakeSimilarityMidpoint  ();
00270 }

void MakeNewFromOld ( PROTO  New,
PROTOTYPE Old 
)

Fill in fields of New proto based on those of Old proto.

Parameters:
New New proto to be filled in
Old Old proto to be converted
Returns:
New is modified
Date:
Mon Nov 26 09:45:39 1990, DSJ, Created.

Definition at line 247 of file mergenf.cpp.

References CenterX, CenterY, FillABC(), LengthOf, proto::Mean, OrientationOf, ProtoAngle, ProtoLength, ProtoX, and ProtoY.

Referenced by FindClosestExistingProto().

00250 {
00251    ProtoX      (New) = CenterX       (Old->Mean);
00252    ProtoY      (New) = CenterY       (Old->Mean);
00253    ProtoLength (New) = LengthOf      (Old->Mean);
00254    ProtoAngle  (New) = OrientationOf (Old->Mean);
00255    FillABC     (New);
00256 
00257 }// MakeNewFromOld

BOOL8 PointInside ( FRECT Rectangle,
FLOAT32  X,
FLOAT32  Y 
)

Check if point (X,Y) is inside area of Rectangle.

Parameters:
Rectangle 
X 
Y 
Returns:
TRUE if point (X,Y) is inside Rectangle
Date:
Wed Nov 14 17:26:35 1990, DSJ, Created.

Definition at line 443 of file mergenf.cpp.

References FALSE, FRECT::MaxX, FRECT::MaxY, and TRUE.

Referenced by DummyFastMatch().

00447 {
00448    if (X < Rectangle->MinX) return (FALSE);
00449    if (X > Rectangle->MaxX) return (FALSE);
00450    if (Y < Rectangle->MinY) return (FALSE);
00451    if (Y > Rectangle->MaxY) return (FALSE);
00452    return (TRUE);
00453 
00454 }// PointInside

FLOAT32 SubfeatureEvidence ( FEATURE  Feature,
PROTO  Proto 
)

Compare a feature to a prototype.

Parameters:
Feature Feature to compare to prototype
Proto Prototype to compare to feature
Returns:
Evidence of comparison
Note:
Uses coefficients A, B, and C mentioned in Patent

Definition at line 281 of file mergenf.cpp.

References CoefficientA, CoefficientB, CoefficientC, Distance, EvidenceOf(), ParamOf, PicoFeatDir, PicoFeatX, PicoFeatY, and ProtoAngle.

Referenced by CompareProtos().

00284 {
00285    float       Distance;
00286    float       Dangle;
00287 
00288    Dangle   = ProtoAngle (Proto) - ParamOf(Feature, PicoFeatDir);
00289    if (Dangle < -0.5) Dangle += 1.0;
00290    if (Dangle >  0.5) Dangle -= 1.0;
00291    Dangle   *= AngleMatchScale;
00292 
00293    Distance = CoefficientA (Proto) * ParamOf(Feature, PicoFeatX) +
00294       CoefficientB (Proto) * ParamOf(Feature, PicoFeatY) +
00295       CoefficientC (Proto);
00296 
00297    return (EvidenceOf (Distance * Distance + Dangle * Dangle));
00298 }// SubfeatureEvidence


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