tesseract  4.00.00dev
mfx.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: mfx.c
3  ** Purpose: Micro feature extraction routines
4  ** Author: Dan Johnson
5  ** History: 7/21/89, DSJ, Created.
6  **
7  ** (c) Copyright Hewlett-Packard Company, 1988.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  ******************************************************************************/
18 /*----------------------------------------------------------------------------
19  Include Files and Type Defines
20 ----------------------------------------------------------------------------*/
21 #include "mfdefs.h"
22 #include "mfoutline.h"
23 #include "clusttool.h" //NEEDED
24 #include "const.h"
25 #include "intfx.h"
26 #include "normalis.h"
27 #include "params.h"
28 
29 #include <math.h>
30 
31 /*----------------------------------------------------------------------------
32  Variables
33 ----------------------------------------------------------------------------*/
34 
35 /* old numbers corresponded to 10.0 degrees and 80.0 degrees */
36 double_VAR(classify_min_slope, 0.414213562,
37  "Slope below which lines are called horizontal");
38 double_VAR(classify_max_slope, 2.414213562,
39  "Slope above which lines are called vertical");
40 
41 /*----------------------------------------------------------------------------
42  Macros
43 ----------------------------------------------------------------------------*/
44 /* miscellaneous macros */
45 #define NormalizeAngle(A) ( (((A)<0)?((A)+2*PI):(A)) / (2*PI) )
46 
47 /*----------------------------------------------------------------------------
48  Private Function Prototypes
49 -----------------------------------------------------------------------------*/
51 
53  MICROFEATURES MicroFeatures);
54 
56 
57 /*----------------------------------------------------------------------------
58  Public Code
59 ----------------------------------------------------------------------------*/
60 
72 MICROFEATURES BlobMicroFeatures(TBLOB* Blob, const DENORM& cn_denorm) {
73  MICROFEATURES MicroFeatures = NIL_LIST;
74  LIST Outlines;
75  LIST RemainingOutlines;
76  MFOUTLINE Outline;
77 
78  if (Blob != NULL) {
79  Outlines = ConvertBlob(Blob);
80 
81  RemainingOutlines = Outlines;
82  iterate(RemainingOutlines) {
83  Outline = (MFOUTLINE) first_node (RemainingOutlines);
84  CharNormalizeOutline(Outline, cn_denorm);
85  }
86 
87  RemainingOutlines = Outlines;
88  iterate(RemainingOutlines) {
89  Outline = (MFOUTLINE) first_node(RemainingOutlines);
91  MarkDirectionChanges(Outline);
92  MicroFeatures = ConvertToMicroFeatures(Outline, MicroFeatures);
93  }
94  FreeOutlines(Outlines);
95  }
96  return MicroFeatures;
97 } /* BlobMicroFeatures */
98 
99 
100 /*---------------------------------------------------------------------------
101  Private Code
102 ---------------------------------------------------------------------------*/
103 
122 
123  Orientation = NormalizeAngle (AngleFrom (Start->Point, End->Point));
124 
125  /* ensure that round-off errors do not put circular param out of range */
126  if ((Orientation < 0) || (Orientation >= 1))
127  Orientation = 0;
128  return (Orientation);
129 } /* ComputeOrientation */
130 
141  MICROFEATURES MicroFeatures) {
142  MFOUTLINE Current;
143  MFOUTLINE Last;
144  MFOUTLINE First;
146 
147  if (DegenerateOutline (Outline))
148  return (MicroFeatures);
149 
150  First = NextExtremity (Outline);
151  Last = First;
152  do {
153  Current = NextExtremity (Last);
154  if (!PointAt(Current)->Hidden) {
155  NewFeature = ExtractMicroFeature (Last, Current);
156  if (NewFeature != NULL)
157  MicroFeatures = push (MicroFeatures, NewFeature);
158  }
159  Last = Current;
160  }
161  while (Last != First);
162 
163  return (MicroFeatures);
164 } /* ConvertToMicroFeatures */
165 
185  MFEDGEPT *P1, *P2;
186 
187  P1 = PointAt(Start);
188  P2 = PointAt(End);
189 
190  NewFeature = NewMicroFeature ();
191  NewFeature[XPOSITION] = AverageOf(P1->Point.x, P2->Point.x);
192  NewFeature[YPOSITION] = AverageOf(P1->Point.y, P2->Point.y);
193  NewFeature[MFLENGTH] = DistanceBetween(P1->Point, P2->Point);
194  NewFeature[ORIENTATION] = NormalizedAngleFrom(&P1->Point, &P2->Point, 1.0);
195  NewFeature[FIRSTBULGE] = 0.0f; // deprecated
196  NewFeature[SECONDBULGE] = 0.0f; // deprecated
197 
198  return NewFeature;
199 } /* ExtractMicroFeature */
#define XPOSITION
Definition: mfdefs.h:36
FLOAT32 ComputeOrientation(MFEDGEPT *Start, MFEDGEPT *End)
Definition: mfx.cpp:120
#define SECONDBULGE
Definition: mfdefs.h:41
void FindDirectionChanges(MFOUTLINE Outline, FLOAT32 MinSlope, FLOAT32 MaxSlope)
Definition: mfoutline.cpp:121
double classify_min_slope
Definition: mfx.cpp:37
void MarkDirectionChanges(MFOUTLINE Outline)
Definition: mfoutline.cpp:199
#define AverageOf(A, B)
Definition: mfoutline.h:60
void CharNormalizeOutline(MFOUTLINE Outline, const DENORM &cn_denorm)
Definition: mfoutline.cpp:359
MICROFEATURE NewMicroFeature()
Definition: mfdefs.cpp:35
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:40
#define NIL_LIST
Definition: oldlist.h:126
MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End)
Definition: mfx.cpp:183
#define PointAt(O)
Definition: mfoutline.h:67
#define NormalizeAngle(A)
Definition: mfx.cpp:45
FLOAT32 NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, FLOAT32 FullScale)
Definition: fpoint.cpp:48
#define double_VAR(name, val, comment)
Definition: params.h:285
LIST MFOUTLINE
Definition: mfoutline.h:33
#define AngleFrom(A, B)
Definition: fpoint.h:42
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:88
double classify_max_slope
Definition: mfx.cpp:39
#define first_node(l)
Definition: oldlist.h:139
#define DegenerateOutline(O)
Definition: mfoutline.h:66
FLOAT32 y
Definition: fpoint.h:31
#define MFLENGTH
Definition: mfdefs.h:38
float FLOAT32
Definition: host.h:42
Definition: blobs.h:261
FPOINT Point
Definition: mfoutline.h:40
#define ORIENTATION
Definition: mfdefs.h:39
FLOAT32 x
Definition: fpoint.h:31
MICROFEATURES BlobMicroFeatures(TBLOB *Blob, const DENORM &cn_denorm)
Definition: mfx.cpp:72
#define FIRSTBULGE
Definition: mfdefs.h:40
MFOUTLINE NextExtremity(MFOUTLINE EdgePoint)
Definition: mfoutline.cpp:239
FLOAT32 * MICROFEATURE
Definition: mfdefs.h:33
#define iterate(l)
Definition: oldlist.h:159
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:179
#define YPOSITION
Definition: mfdefs.h:37
LIST push(LIST list, void *element)
Definition: oldlist.cpp:317
MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatures)
Definition: mfx.cpp:140
FLOAT32 DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:30