tesseract  4.00.00dev
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

◆ CenterX

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

Public Macros

Definition at line 53 of file mergenf.h.

◆ CenterY

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

Definition at line 54 of file mergenf.h.

◆ CONFIG_SUFFIX

#define CONFIG_SUFFIX   ".cl"

Definition at line 38 of file mergenf.h.

◆ LengthOf

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

Definition at line 55 of file mergenf.h.

◆ MAX_LENGTH_MISMATCH

#define MAX_LENGTH_MISMATCH   (2.0 * GetPicoFeatureLength ())

Definition at line 34 of file mergenf.h.

◆ MFLENGTH

#define MFLENGTH   2

Definition at line 42 of file mergenf.h.

◆ NO_PROTO

#define NO_PROTO   (-1)

Definition at line 39 of file mergenf.h.

◆ ORIENTATION

#define ORIENTATION   3

Definition at line 43 of file mergenf.h.

◆ OrientationOf

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

Definition at line 56 of file mergenf.h.

◆ PROTO_SUFFIX

#define PROTO_SUFFIX   ".mf.p"

Definition at line 37 of file mergenf.h.

◆ WORST_EVIDENCE

#define WORST_EVIDENCE   (1.0)

Definition at line 33 of file mergenf.h.

◆ WORST_MATCH_ALLOWED

#define WORST_MATCH_ALLOWED   (0.9)

Include Files and Type Defines

Definition at line 32 of file mergenf.h.

◆ XPOSITION

#define XPOSITION   0

Definition at line 40 of file mergenf.h.

◆ YPOSITION

#define YPOSITION   1

Definition at line 41 of file mergenf.h.

Function Documentation

◆ CompareProtos()

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 66 of file mergenf.cpp.

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

◆ ComputeMergedProto()

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 132 of file mergenf.cpp.

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

◆ ComputePaddedBoundingBox()

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 317 of file mergenf.cpp.

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

◆ DummyFastMatch()

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 275 of file mergenf.cpp.

278 {
279  FRECT BoundingBox;
280  FLOAT32 MaxAngleError;
281  FLOAT32 AngleError;
282 
283  MaxAngleError = training_angle_pad / 360.0;
284  AngleError = fabs (Proto->Angle - Feature->Params[PicoFeatDir]);
285  if (AngleError > 0.5)
286  AngleError = 1.0 - AngleError;
287 
288  if (AngleError > MaxAngleError)
289  return (FALSE);
290 
294  &BoundingBox);
295 
296  return PointInside(&BoundingBox, Feature->Params[PicoFeatX],
297  Feature->Params[PicoFeatY]);
298 } /* DummyFastMatch */
double training_tangent_bbox_pad
Definition: mergenf.cpp:44
BOOL8 PointInside(FRECT *Rectangle, FLOAT32 X, FLOAT32 Y)
Definition: mergenf.cpp:346
double training_orthogonal_bbox_pad
Definition: mergenf.cpp:46
#define FALSE
Definition: capi.h:46
void ComputePaddedBoundingBox(PROTO Proto, FLOAT32 TangentPad, FLOAT32 OrthogonalPad, FRECT *BoundingBox)
Definition: mergenf.cpp:317
float FLOAT32
Definition: host.h:42
Definition: mergenf.h:45
FLOAT32 Angle
Definition: protos.h:49
double training_angle_pad
Definition: mergenf.cpp:48
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
#define GetPicoFeatureLength()
Definition: picofeat.h:59

◆ EvidenceOf()

double EvidenceOf ( register double  Similarity)

◆ FindClosestExistingProto()

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

This routine searches through 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 166 of file mergenf.cpp.

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

◆ MakeNewFromOld()

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 207 of file mergenf.cpp.

207  {
208  New->X = CenterX(Old->Mean);
209  New->Y = CenterY(Old->Mean);
210  New->Length = LengthOf(Old->Mean);
211  New->Angle = OrientationOf(Old->Mean);
212  FillABC(New);
213 } /* MakeNewFromOld */
#define LengthOf(M)
Definition: mergenf.h:55
#define OrientationOf(M)
Definition: mergenf.h:56
void FillABC(PROTO Proto)
Definition: protos.cpp:197
FLOAT32 Length
Definition: protos.h:50
#define CenterY(M)
Definition: mergenf.h:54
FLOAT32 * Mean
Definition: cluster.h:78
FLOAT32 Y
Definition: protos.h:48
FLOAT32 X
Definition: protos.h:47
FLOAT32 Angle
Definition: protos.h:49
#define CenterX(M)
Definition: mergenf.h:53

◆ PointInside()

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 346 of file mergenf.cpp.

346  {
347  if (X < Rectangle->MinX) return (FALSE);
348  if (X > Rectangle->MaxX) return (FALSE);
349  if (Y < Rectangle->MinY) return (FALSE);
350  if (Y > Rectangle->MaxY) return (FALSE);
351  return (TRUE);
352 
353 } /* PointInside */
#define TRUE
Definition: capi.h:45
FLOAT32 MaxX
Definition: mergenf.h:47
FLOAT32 MaxY
Definition: mergenf.h:47
#define FALSE
Definition: capi.h:46

◆ SubfeatureEvidence()

FLOAT32 SubfeatureEvidence ( FEATURE  Feature,
PROTO  Proto 
)

Definition at line 222 of file mergenf.cpp.

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