All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
 

Macros

#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)
 

Macro Definition 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.

67  {
68  FEATURE Feature;
69  FLOAT32 WorstEvidence = WORST_EVIDENCE;
70  FLOAT32 Evidence;
71  FLOAT32 Angle, Length;
72 
73  /* if p1 and p2 are not close in length, don't let them match */
74  Length = fabs (p1->Length - p2->Length);
75  if (Length > MAX_LENGTH_MISMATCH)
76  return (0.0);
77 
78  /* create a dummy pico-feature to be used for comparisons */
79  Feature = NewFeature (&PicoFeatDesc);
80  Feature->Params[PicoFeatDir] = p1->Angle;
81 
82  /* convert angle to radians */
83  Angle = p1->Angle * 2.0 * PI;
84 
85  /* find distance from center of p1 to 1/2 picofeat from end */
86  Length = p1->Length / 2.0 - GetPicoFeatureLength () / 2.0;
87  if (Length < 0) Length = 0;
88 
89  /* set the dummy pico-feature at one end of p1 and match it to p2 */
90  Feature->Params[PicoFeatX] = p1->X + cos (Angle) * Length;
91  Feature->Params[PicoFeatY] = p1->Y + sin (Angle) * Length;
92  if (DummyFastMatch (Feature, p2)) {
93  Evidence = SubfeatureEvidence (Feature, p2);
94  if (Evidence < WorstEvidence)
95  WorstEvidence = Evidence;
96  } else {
97  FreeFeature(Feature);
98  return 0.0;
99  }
100 
101  /* set the dummy pico-feature at the other end of p1 and match it to p2 */
102  Feature->Params[PicoFeatX] = p1->X - cos (Angle) * Length;
103  Feature->Params[PicoFeatY] = p1->Y - sin (Angle) * Length;
104  if (DummyFastMatch (Feature, p2)) {
105  Evidence = SubfeatureEvidence (Feature, p2);
106  if (Evidence < WorstEvidence)
107  WorstEvidence = Evidence;
108  } else {
109  FreeFeature(Feature);
110  return 0.0;
111  }
112 
113  FreeFeature (Feature);
114  return (WorstEvidence);
115 
116 } /* CompareProtos */
#define MAX_LENGTH_MISMATCH
Definition: mergenf.h:30
float FLOAT32
Definition: host.h:111
FLOAT32 SubfeatureEvidence(FEATURE Feature, PROTO Proto)
Definition: mergenf.cpp:223
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:96
const FEATURE_DESC_STRUCT PicoFeatDesc
FLOAT32 X
Definition: protos.h:47
FLOAT32 Angle
Definition: protos.h:49
#define GetPicoFeatureLength()
Definition: picofeat.h:59
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
FLOAT32 Length
Definition: protos.h:50
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:60
#define PI
Definition: const.h:19
#define WORST_EVIDENCE
Definition: mergenf.h:29
BOOL8 DummyFastMatch(FEATURE Feature, PROTO Proto)
Definition: mergenf.cpp:276
FLOAT32 Y
Definition: protos.h:48
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.

137  {
138  FLOAT32 TotalWeight;
139 
140  TotalWeight = w1 + w2;
141  w1 /= TotalWeight;
142  w2 /= TotalWeight;
143 
144  MergedProto->X = p1->X * w1 + p2->X * w2;
145  MergedProto->Y = p1->Y * w1 + p2->Y * w2;
146  MergedProto->Length = p1->Length * w1 + p2->Length * w2;
147  MergedProto->Angle = p1->Angle * w1 + p2->Angle * w2;
148  FillABC(MergedProto);
149 } /* ComputeMergedProto */
float FLOAT32
Definition: host.h:111
FLOAT32 X
Definition: protos.h:47
FLOAT32 Angle
Definition: protos.h:49
void FillABC(PROTO Proto)
Definition: protos.cpp:198
FLOAT32 Length
Definition: protos.h:50
FLOAT32 Y
Definition: protos.h:48
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.

319  {
320  FLOAT32 Pad, Length, Angle;
321  FLOAT32 CosOfAngle, SinOfAngle;
322 
323  Length = Proto->Length / 2.0 + TangentPad;
324  Angle = Proto->Angle * 2.0 * PI;
325  CosOfAngle = fabs(cos(Angle));
326  SinOfAngle = fabs(sin(Angle));
327 
328  Pad = MAX (CosOfAngle * Length, SinOfAngle * OrthogonalPad);
329  BoundingBox->MinX = Proto->X - Pad;
330  BoundingBox->MaxX = Proto->X + Pad;
331 
332  Pad = MAX(SinOfAngle * Length, CosOfAngle * OrthogonalPad);
333  BoundingBox->MinY = Proto->Y - Pad;
334  BoundingBox->MaxY = Proto->Y + Pad;
335 
336 } /* ComputePaddedBoundingBox */
FLOAT32 MaxY
Definition: mergenf.h:43
float FLOAT32
Definition: host.h:111
#define MAX(x, y)
Definition: ndminx.h:24
FLOAT32 MinY
Definition: mergenf.h:43
FLOAT32 X
Definition: protos.h:47
FLOAT32 Angle
Definition: protos.h:49
FLOAT32 MaxX
Definition: mergenf.h:43
FLOAT32 MinX
Definition: mergenf.h:43
FLOAT32 Length
Definition: protos.h:50
#define PI
Definition: const.h:19
FLOAT32 Y
Definition: protos.h:48
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.

279 {
280  FRECT BoundingBox;
281  FLOAT32 MaxAngleError;
282  FLOAT32 AngleError;
283 
284  MaxAngleError = training_angle_pad / 360.0;
285  AngleError = fabs (Proto->Angle - Feature->Params[PicoFeatDir]);
286  if (AngleError > 0.5)
287  AngleError = 1.0 - AngleError;
288 
289  if (AngleError > MaxAngleError)
290  return (FALSE);
291 
295  &BoundingBox);
296 
297  return PointInside(&BoundingBox, Feature->Params[PicoFeatX],
298  Feature->Params[PicoFeatY]);
299 } /* DummyFastMatch */
float FLOAT32
Definition: host.h:111
double training_angle_pad
Definition: mergenf.cpp:49
double training_tangent_bbox_pad
Definition: mergenf.cpp:45
Definition: mergenf.h:41
FLOAT32 Angle
Definition: protos.h:49
#define GetPicoFeatureLength()
Definition: picofeat.h:59
BOOL8 PointInside(FRECT *Rectangle, FLOAT32 X, FLOAT32 Y)
Definition: mergenf.cpp:347
double training_orthogonal_bbox_pad
Definition: mergenf.cpp:47
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
#define FALSE
Definition: capi.h:29
void ComputePaddedBoundingBox(PROTO Proto, FLOAT32 TangentPad, FLOAT32 OrthogonalPad, FRECT *BoundingBox)
Definition: mergenf.cpp:318
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.

168  {
169  PROTO_STRUCT NewProto;
170  PROTO_STRUCT MergedProto;
171  int Pid;
172  PROTO Proto;
173  int BestProto;
174  FLOAT32 BestMatch;
175  FLOAT32 Match, OldMatch, NewMatch;
176 
177  MakeNewFromOld (&NewProto, Prototype);
178 
179  BestProto = NO_PROTO;
180  BestMatch = WORST_MATCH_ALLOWED;
181  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
182  Proto = ProtoIn(Class, Pid);
183  ComputeMergedProto(Proto, &NewProto,
184  (FLOAT32) NumMerged[Pid], 1.0, &MergedProto);
185  OldMatch = CompareProtos(Proto, &MergedProto);
186  NewMatch = CompareProtos(&NewProto, &MergedProto);
187  Match = MIN(OldMatch, NewMatch);
188  if (Match > BestMatch) {
189  BestProto = Pid;
190  BestMatch = Match;
191  }
192  }
193  return BestProto;
194 } /* FindClosestExistingProto */
void ComputeMergedProto(PROTO p1, PROTO p2, FLOAT32 w1, FLOAT32 w2, PROTO MergedProto)
Definition: mergenf.cpp:133
#define WORST_MATCH_ALLOWED
Definition: mergenf.h:28
float FLOAT32
Definition: host.h:111
#define ProtoIn(Class, Pid)
Definition: protos.h:123
#define MIN(x, y)
Definition: ndminx.h:28
inT16 NumProtos
Definition: protos.h:59
#define NO_PROTO
Definition: matchdefs.h:42
FLOAT32 CompareProtos(PROTO p1, PROTO p2)
Definition: mergenf.cpp:67
void MakeNewFromOld(PROTO New, PROTOTYPE *Old)
Definition: mergenf.cpp:208
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.

208  {
209  New->X = CenterX(Old->Mean);
210  New->Y = CenterY(Old->Mean);
211  New->Length = LengthOf(Old->Mean);
212  New->Angle = OrientationOf(Old->Mean);
213  FillABC(New);
214 } /* MakeNewFromOld */
#define CenterY(M)
Definition: mergenf.h:50
FLOAT32 * Mean
Definition: cluster.h:78
#define LengthOf(M)
Definition: mergenf.h:51
FLOAT32 X
Definition: protos.h:47
#define CenterX(M)
Definition: mergenf.h:49
FLOAT32 Angle
Definition: protos.h:49
void FillABC(PROTO Proto)
Definition: protos.cpp:198
FLOAT32 Length
Definition: protos.h:50
#define OrientationOf(M)
Definition: mergenf.h:52
FLOAT32 Y
Definition: protos.h:48
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.

347  {
348  if (X < Rectangle->MinX) return (FALSE);
349  if (X > Rectangle->MaxX) return (FALSE);
350  if (Y < Rectangle->MinY) return (FALSE);
351  if (Y > Rectangle->MaxY) return (FALSE);
352  return (TRUE);
353 
354 } /* PointInside */
FLOAT32 MaxY
Definition: mergenf.h:43
FLOAT32 MaxX
Definition: mergenf.h:43
#define FALSE
Definition: capi.h:29
#define TRUE
Definition: capi.h:28
FLOAT32 SubfeatureEvidence ( FEATURE  Feature,
PROTO  Proto 
)

Definition at line 223 of file mergenf.cpp.

223  {
224  float Distance;
225  float Dangle;
226 
227  Dangle = Proto->Angle - Feature->Params[PicoFeatDir];
228  if (Dangle < -0.5) Dangle += 1.0;
229  if (Dangle > 0.5) Dangle -= 1.0;
230  Dangle *= training_angle_match_scale;
231 
232  Distance = Proto->A * Feature->Params[PicoFeatX] +
233  Proto->B * Feature->Params[PicoFeatY] +
234  Proto->C;
235 
236  return (EvidenceOf (Distance * Distance + Dangle * Dangle));
237 }
FLOAT32 Angle
Definition: protos.h:49
FLOAT32 B
Definition: protos.h:45
double training_angle_match_scale
Definition: mergenf.cpp:38
double EvidenceOf(double Similarity)
Definition: mergenf.cpp:247
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
FLOAT32 C
Definition: protos.h:46
FLOAT32 A
Definition: protos.h:44