Tesseract  3.02
tesseract-ocr/training/mergenf.h File Reference
#include "protos.h"
#include "cluster.h"
#include "ocrfeatures.h"
#include "callcpp.h"
#include "picofeat.h"

Go to the source code of this file.

Classes

struct  FRECT

Defines

#define WORST_MATCH_ALLOWED   (0.9)
#define WORST_EVIDENCE   (1.0)
#define MAX_LENGTH_MISMATCH   (2.0 * GetPicoFeatureLength ())
#define PROTO_SUFFIX   ".mf.p"
#define CONFIG_SUFFIX   ".cl"
#define NO_PROTO   (-1)
#define XPOSITION   0
#define YPOSITION   1
#define MFLENGTH   2
#define ORIENTATION   3
#define CenterX(M)   ( (M)[XPOSITION] )
#define CenterY(M)   ( (M)[YPOSITION] )
#define LengthOf(M)   ( (M)[MFLENGTH] )
#define OrientationOf(M)   ( (M)[ORIENTATION] )

Functions

FLOAT32 CompareProtos (PROTO p1, PROTO p2)
void ComputeMergedProto (PROTO p1, PROTO p2, FLOAT32 w1, FLOAT32 w2, PROTO MergedProto)
int FindClosestExistingProto (CLASS_TYPE Class, int NumMerged[], PROTOTYPE *Prototype)
void MakeNewFromOld (PROTO New, PROTOTYPE *Old)
FLOAT32 SubfeatureEvidence (FEATURE Feature, PROTO Proto)
double EvidenceOf (register double Similarity)
BOOL8 DummyFastMatch (FEATURE Feature, PROTO Proto)
void ComputePaddedBoundingBox (PROTO Proto, FLOAT32 TangentPad, FLOAT32 OrthogonalPad, FRECT *BoundingBox)
BOOL8 PointInside (FRECT *Rectangle, FLOAT32 X, FLOAT32 Y)

Define Documentation

#define CenterX (   M)    ( (M)[XPOSITION] )

---------------------------------------------------------------------------- Public Macros ----------------------------------------------------------------------------

Definition at line 49 of file mergenf.h.

#define CenterY (   M)    ( (M)[YPOSITION] )

Definition at line 50 of file mergenf.h.

#define CONFIG_SUFFIX   ".cl"

Definition at line 34 of file mergenf.h.

#define LengthOf (   M)    ( (M)[MFLENGTH] )

Definition at line 51 of file mergenf.h.

#define MAX_LENGTH_MISMATCH   (2.0 * GetPicoFeatureLength ())

Definition at line 30 of file mergenf.h.

#define MFLENGTH   2

Definition at line 38 of file mergenf.h.

#define NO_PROTO   (-1)

Definition at line 35 of file mergenf.h.

#define ORIENTATION   3

Definition at line 39 of file mergenf.h.

#define OrientationOf (   M)    ( (M)[ORIENTATION] )

Definition at line 52 of file mergenf.h.

#define PROTO_SUFFIX   ".mf.p"

Definition at line 33 of file mergenf.h.

#define WORST_EVIDENCE   (1.0)

Definition at line 29 of file mergenf.h.

#define WORST_MATCH_ALLOWED   (0.9)

---------------------------------------------------------------------------- Include Files and Type Defines ----------------------------------------------------------------------------

Definition at line 28 of file mergenf.h.

#define XPOSITION   0

Definition at line 36 of file mergenf.h.

#define YPOSITION   1

Definition at line 37 of file mergenf.h.


Function Documentation

FLOAT32 CompareProtos ( PROTO  p1,
PROTO  p2 
)

---------------------------------------------------------------------------- Public Function Prototypes ----------------------------------------------------------------------------

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.

Parameters:
p1,p2protos to be compared

Globals: none

Returns:
Worst possible result when matching p1 to p2.
Note:
Exceptions: none
History: Mon Nov 26 08:27:53 1990, DSJ, Created.

Definition at line 67 of file mergenf.cpp.

                                          {
  FEATURE Feature;
  FLOAT32 WorstEvidence = WORST_EVIDENCE;
  FLOAT32 Evidence;
  FLOAT32 Angle, Length;

  /* if p1 and p2 are not close in length, don't let them match */
  Length = fabs (p1->Length - p2->Length);
  if (Length > MAX_LENGTH_MISMATCH)
    return (0.0);

  /* create a dummy pico-feature to be used for comparisons */
  Feature = NewFeature (&PicoFeatDesc);
  Feature->Params[PicoFeatDir] = p1->Angle;

  /* convert angle to radians */
  Angle = p1->Angle * 2.0 * PI;

  /* find distance from center of p1 to 1/2 picofeat from end */
  Length = p1->Length / 2.0 - GetPicoFeatureLength () / 2.0;
  if (Length < 0) Length = 0;

  /* set the dummy pico-feature at one end of p1 and match it to p2 */
  Feature->Params[PicoFeatX] = p1->X + cos (Angle) * Length;
  Feature->Params[PicoFeatY] = p1->Y + sin (Angle) * Length;
  if (DummyFastMatch (Feature, p2)) {
    Evidence = SubfeatureEvidence (Feature, p2);
    if (Evidence < WorstEvidence)
      WorstEvidence = Evidence;
  } else {
    FreeFeature(Feature);
    return 0.0;
  }

  /* set the dummy pico-feature at the other end of p1 and match it to p2 */
  Feature->Params[PicoFeatX] = p1->X - cos (Angle) * Length;
  Feature->Params[PicoFeatY] = p1->Y - sin (Angle) * Length;
  if (DummyFastMatch (Feature, p2)) {
    Evidence = SubfeatureEvidence (Feature, p2);
    if (Evidence < WorstEvidence)
      WorstEvidence = Evidence;
  } else {
    FreeFeature(Feature);
    return 0.0;
  }

  FreeFeature (Feature);
  return (WorstEvidence);

} /* CompareProtos */
void ComputeMergedProto ( PROTO  p1,
PROTO  p2,
FLOAT32  w1,
FLOAT32  w2,
PROTO  MergedProto 
)

This routine computes a proto which is the weighted average of protos p1 and p2. The new proto is returned in MergedProto.

Parameters:
p1,p2protos to be merged
w1,w2weight of each proto
MergedProtoplace to put resulting merged proto

Globals: none

Returns:
none (results are returned in MergedProto)
Note:
Exceptions: none
History: Mon Nov 26 08:15:08 1990, DSJ, Created.

Definition at line 133 of file mergenf.cpp.

                                             {
  FLOAT32 TotalWeight;

  TotalWeight = w1 + w2;
  w1 /= TotalWeight;
  w2 /= TotalWeight;

  MergedProto->X = p1->X * w1 + p2->X * w2;
  MergedProto->Y = p1->Y * w1 + p2->Y * w2;
  MergedProto->Length = p1->Length * w1 + p2->Length * w2;
  MergedProto->Angle = p1->Angle * w1 + p2->Angle * w2;
  FillABC(MergedProto);
} /* ComputeMergedProto */
void ComputePaddedBoundingBox ( PROTO  Proto,
FLOAT32  TangentPad,
FLOAT32  OrthogonalPad,
FRECT BoundingBox 
)

This routine 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.

Parameters:
Protoproto to compute bounding box for
TangentPadamount of pad to add in direction of segment
OrthogonalPadamount of pad to add orthogonal to segment
[out]BoundingBoxplace to put results

Globals: none

Returns:
none (results are returned in BoundingBox)
Note:
Exceptions: none
History: Wed Nov 14 14:55:30 1990, DSJ, Created.

Definition at line 318 of file mergenf.cpp.

                                                                            {
  FLOAT32 Pad, Length, Angle;
  FLOAT32 CosOfAngle, SinOfAngle;

  Length     = Proto->Length / 2.0 + TangentPad;
  Angle      = Proto->Angle * 2.0 * PI;
  CosOfAngle = fabs(cos(Angle));
  SinOfAngle = fabs(sin(Angle));

  Pad = MAX (CosOfAngle * Length, SinOfAngle * OrthogonalPad);
  BoundingBox->MinX = Proto->X - Pad;
  BoundingBox->MaxX = Proto->X + Pad;

  Pad = MAX(SinOfAngle * Length, CosOfAngle * OrthogonalPad);
  BoundingBox->MinY = Proto->Y - Pad;
  BoundingBox->MaxY = Proto->Y + Pad;

} /* ComputePaddedBoundingBox */
BOOL8 DummyFastMatch ( FEATURE  Feature,
PROTO  Proto 
)

This routine returns TRUE if Feature would be matched by a fast match table built from Proto.

Parameters:
Featurefeature to be "fast matched" to proto
Protoproto being "fast matched" against

Globals:

  • training_tangent_bbox_pad bounding box pad tangent to proto
  • training_orthogonal_bbox_pad bounding box pad orthogonal to proto
Returns:
TRUE if feature could match Proto.
Note:
Exceptions: none
History: Wed Nov 14 17:19:58 1990, DSJ, Created.

Definition at line 276 of file mergenf.cpp.

{
  FRECT   BoundingBox;
  FLOAT32 MaxAngleError;
  FLOAT32 AngleError;

  MaxAngleError = training_angle_pad / 360.0;
  AngleError = fabs (Proto->Angle - Feature->Params[PicoFeatDir]);
  if (AngleError > 0.5)
    AngleError = 1.0 - AngleError;

  if (AngleError > MaxAngleError)
    return (FALSE);

  ComputePaddedBoundingBox (Proto,
    training_tangent_bbox_pad * GetPicoFeatureLength (),
    training_orthogonal_bbox_pad * GetPicoFeatureLength (),
    &BoundingBox);

  return PointInside(&BoundingBox, Feature->Params[PicoFeatX],
                     Feature->Params[PicoFeatY]);
} /* DummyFastMatch */
double EvidenceOf ( register double  Similarity)
int FindClosestExistingProto ( CLASS_TYPE  Class,
int  NumMerged[],
PROTOTYPE Prototype 
)

This routine searches thru all of the prototypes in Class and returns the id of the proto which would provide the best approximation of Prototype. If no close approximation can be found, NO_PROTO is returned.

Parameters:
Classclass to search for matching old proto in
NumMerged# of protos merged into each proto of Class
Prototypenew proto to find match for

Globals: none

Returns:
Id of closest proto in Class or NO_PROTO.
Note:
Exceptions: none
History: Sat Nov 24 11:42:58 1990, DSJ, Created.

Definition at line 167 of file mergenf.cpp.

                                                    {
  PROTO_STRUCT  NewProto;
  PROTO_STRUCT  MergedProto;
  int   Pid;
  PROTO   Proto;
  int   BestProto;
  FLOAT32 BestMatch;
  FLOAT32 Match, OldMatch, NewMatch;

  MakeNewFromOld (&NewProto, Prototype);

  BestProto = NO_PROTO;
  BestMatch = WORST_MATCH_ALLOWED;
  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
    Proto  = ProtoIn(Class, Pid);
    ComputeMergedProto(Proto, &NewProto,
      (FLOAT32) NumMerged[Pid], 1.0, &MergedProto);
    OldMatch = CompareProtos(Proto, &MergedProto);
    NewMatch = CompareProtos(&NewProto, &MergedProto);
    Match = MIN(OldMatch, NewMatch);
    if (Match > BestMatch) {
      BestProto = Pid;
      BestMatch = Match;
    }
  }
  return BestProto;
} /* FindClosestExistingProto */
void MakeNewFromOld ( PROTO  New,
PROTOTYPE Old 
)

This fills in the fields of the New proto based on the fields of the Old proto.

Parameters:
Newnew proto to be filled in
Oldold proto to be converted

Globals: none

Exceptions: none History: Mon Nov 26 09:45:39 1990, DSJ, Created.

Definition at line 208 of file mergenf.cpp.

                                               {
  New->X = CenterX(Old->Mean);
  New->Y = CenterY(Old->Mean);
  New->Length = LengthOf(Old->Mean);
  New->Angle = OrientationOf(Old->Mean);
  FillABC(New);
} /* MakeNewFromOld */
BOOL8 PointInside ( FRECT Rectangle,
FLOAT32  X,
FLOAT32  Y 
)

Return TRUE if point (X,Y) is inside of Rectangle.

Globals: none

Returns:
TRUE if point (X,Y) is inside of Rectangle.
Note:
Exceptions: none
History: Wed Nov 14 17:26:35 1990, DSJ, Created.

Definition at line 347 of file mergenf.cpp.

                                                           {
  if (X < Rectangle->MinX) return (FALSE);
  if (X > Rectangle->MaxX) return (FALSE);
  if (Y < Rectangle->MinY) return (FALSE);
  if (Y > Rectangle->MaxY) return (FALSE);
  return (TRUE);

} /* PointInside */
FLOAT32 SubfeatureEvidence ( FEATURE  Feature,
PROTO  Proto 
)

Definition at line 223 of file mergenf.cpp.

                                                         {
  float       Distance;
  float       Dangle;

  Dangle   = Proto->Angle - Feature->Params[PicoFeatDir];
  if (Dangle < -0.5) Dangle += 1.0;
  if (Dangle >  0.5) Dangle -= 1.0;
  Dangle *= training_angle_match_scale;

  Distance = Proto->A * Feature->Params[PicoFeatX] +
    Proto->B * Feature->Params[PicoFeatY] +
    Proto->C;

  return (EvidenceOf (Distance * Distance + Dangle * Dangle));
}