tesseract  4.0.0-1-g2a2b
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  **
6  ** (c) Copyright Hewlett-Packard Company, 1988.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *****************************************************************************/
17 /*----------------------------------------------------------------------------
18  Include Files and Type Defines
19 ----------------------------------------------------------------------------*/
20 #include "mfx.h"
21 #include "mfdefs.h"
22 #include "mfoutline.h"
23 #include "clusttool.h" //NEEDED
24 #include "intfx.h"
25 #include "normalis.h"
26 #include "params.h"
27 
28 #include <cmath>
29 
30 /*----------------------------------------------------------------------------
31  Variables
32 ----------------------------------------------------------------------------*/
33 
34 /* old numbers corresponded to 10.0 degrees and 80.0 degrees */
35 double_VAR(classify_min_slope, 0.414213562,
36  "Slope below which lines are called horizontal");
37 double_VAR(classify_max_slope, 2.414213562,
38  "Slope above which lines are called vertical");
39 
40 /*----------------------------------------------------------------------------
41  Macros
42 ----------------------------------------------------------------------------*/
43 /* miscellaneous macros */
44 #define NormalizeAngle(A) ((((A) < 0) ? ((A) + 2 * M_PI) : (A)) / (2 * M_PI))
45 
46 /*----------------------------------------------------------------------------
47  Private Function Prototypes
48 -----------------------------------------------------------------------------*/
49 float ComputeOrientation(MFEDGEPT *Start, MFEDGEPT *End);
50 
52  MICROFEATURES MicroFeatures);
53 
55 
56 /*----------------------------------------------------------------------------
57  Public Code
58 ----------------------------------------------------------------------------*/
59 
69 MICROFEATURES BlobMicroFeatures(TBLOB* Blob, const DENORM& cn_denorm) {
70  MICROFEATURES MicroFeatures = NIL_LIST;
71  LIST Outlines;
72  LIST RemainingOutlines;
73  MFOUTLINE Outline;
74 
75  if (Blob != nullptr) {
76  Outlines = ConvertBlob(Blob);
77 
78  RemainingOutlines = Outlines;
79  iterate(RemainingOutlines) {
80  Outline = (MFOUTLINE) first_node (RemainingOutlines);
81  CharNormalizeOutline(Outline, cn_denorm);
82  }
83 
84  RemainingOutlines = Outlines;
85  iterate(RemainingOutlines) {
86  Outline = (MFOUTLINE) first_node(RemainingOutlines);
88  MarkDirectionChanges(Outline);
89  MicroFeatures = ConvertToMicroFeatures(Outline, MicroFeatures);
90  }
91  FreeOutlines(Outlines);
92  }
93  return MicroFeatures;
94 } /* BlobMicroFeatures */
95 
96 /*---------------------------------------------------------------------------
97  Private Code
98 ---------------------------------------------------------------------------*/
99 
114 float ComputeOrientation(MFEDGEPT *Start, MFEDGEPT *End) {
115  float Orientation = NormalizeAngle(AngleFrom(Start->Point, End->Point));
116 
117  /* ensure that round-off errors do not put circular param out of range */
118  if ((Orientation < 0) || (Orientation >= 1))
119  Orientation = 0;
120  return (Orientation);
121 } /* ComputeOrientation */
122 
131  MICROFEATURES MicroFeatures) {
132  MFOUTLINE Current;
133  MFOUTLINE Last;
134  MFOUTLINE First;
136 
137  if (DegenerateOutline (Outline))
138  return (MicroFeatures);
139 
140  First = NextExtremity (Outline);
141  Last = First;
142  do {
143  Current = NextExtremity (Last);
144  if (!PointAt(Current)->Hidden) {
145  NewFeature = ExtractMicroFeature (Last, Current);
146  if (NewFeature != nullptr)
147  MicroFeatures = push (MicroFeatures, NewFeature);
148  }
149  Last = Current;
150  }
151  while (Last != First);
152 
153  return (MicroFeatures);
154 } /* ConvertToMicroFeatures */
155 
171  MFEDGEPT *P1, *P2;
172 
173  P1 = PointAt(Start);
174  P2 = PointAt(End);
175 
177  NewFeature[XPOSITION] = AverageOf(P1->Point.x, P2->Point.x);
178  NewFeature[YPOSITION] = AverageOf(P1->Point.y, P2->Point.y);
181  NewFeature[FIRSTBULGE] = 0.0f; // deprecated
182  NewFeature[SECONDBULGE] = 0.0f; // deprecated
183 
184  return NewFeature;
185 } /* ExtractMicroFeature */
#define SECONDBULGE
Definition: mfdefs.h:41
#define MFLENGTH
Definition: mfdefs.h:38
MFOUTLINE NextExtremity(MFOUTLINE EdgePoint)
Definition: mfoutline.cpp:227
void MarkDirectionChanges(MFOUTLINE Outline)
Definition: mfoutline.cpp:190
#define NormalizeAngle(A)
Definition: mfx.cpp:44
MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatures)
Definition: mfx.cpp:130
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:172
#define DegenerateOutline(O)
Definition: mfoutline.h:67
#define double_VAR(name, val, comment)
Definition: params.h:285
float DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:28
void FindDirectionChanges(MFOUTLINE Outline, float MinSlope, float MaxSlope)
Definition: mfoutline.cpp:118
LIST push(LIST list, void *element)
Definition: oldlist.cpp:283
MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End)
Definition: mfx.cpp:169
float NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, float FullScale)
Definition: fpoint.cpp:44
double classify_min_slope
Definition: mfx.cpp:36
#define PointAt(O)
Definition: mfoutline.h:68
#define FIRSTBULGE
Definition: mfdefs.h:40
#define AngleFrom(A, B)
Definition: fpoint.h:42
MICROFEATURES BlobMicroFeatures(TBLOB *Blob, const DENORM &cn_denorm)
Definition: mfx.cpp:69
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:39
float y
Definition: fpoint.h:31
float ComputeOrientation(MFEDGEPT *Start, MFEDGEPT *End)
Definition: mfx.cpp:114
double classify_max_slope
Definition: mfx.cpp:38
#define first_node(l)
Definition: oldlist.h:141
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:81
float * MICROFEATURE
Definition: mfdefs.h:33
#define NIL_LIST
Definition: oldlist.h:127
#define AverageOf(A, B)
Definition: mfoutline.h:61
void CharNormalizeOutline(MFOUTLINE Outline, const DENORM &cn_denorm)
Definition: mfoutline.cpp:339
FPOINT Point
Definition: mfoutline.h:41
#define iterate(l)
Definition: oldlist.h:161
#define ORIENTATION
Definition: mfdefs.h:39
MICROFEATURE NewMicroFeature()
Definition: mfdefs.cpp:34
Definition: blobs.h:268
float x
Definition: fpoint.h:31
#define YPOSITION
Definition: mfdefs.h:37
#define XPOSITION
Definition: mfdefs.h:36
LIST MFOUTLINE
Definition: mfoutline.h:34