All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
picofeat.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: picofeat.c
3  ** Purpose: Definition of pico-features.
4  ** Author: Dan Johnson
5  ** History: 9/4/90, 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 "picofeat.h"
22 
23 #include "classify.h"
24 #include "efio.h"
25 #include "featdefs.h"
26 #include "fpoint.h"
27 #include "mfoutline.h"
28 #include "ocrfeatures.h"
29 #include "params.h"
30 #include "trainingsample.h"
31 
32 #include <math.h>
33 #include <stdio.h>
34 
35 /*---------------------------------------------------------------------------
36  Variables
37 ----------------------------------------------------------------------------*/
38 
39 double_VAR(classify_pico_feature_length, 0.05, "Pico Feature Length");
40 
41 /*---------------------------------------------------------------------------
42  Private Function Prototypes
43 ----------------------------------------------------------------------------*/
45  FPOINT *End,
46  FEATURE_SET FeatureSet);
47 
48 void ConvertToPicoFeatures2(MFOUTLINE Outline, FEATURE_SET FeatureSet);
49 
50 void NormalizePicoX(FEATURE_SET FeatureSet);
51 
52 /*----------------------------------------------------------------------------
53  Public Code
54 ----------------------------------------------------------------------------*/
55 /*---------------------------------------------------------------------------*/
56 namespace tesseract {
68  LIST Outlines;
69  LIST RemainingOutlines;
70  MFOUTLINE Outline;
71  FEATURE_SET FeatureSet;
72  FLOAT32 XScale, YScale;
73 
74  FeatureSet = NewFeatureSet(MAX_PICO_FEATURES);
75  Outlines = ConvertBlob(Blob);
76  NormalizeOutlines(Outlines, &XScale, &YScale);
77  RemainingOutlines = Outlines;
78  iterate(RemainingOutlines) {
79  Outline = (MFOUTLINE) first_node (RemainingOutlines);
80  ConvertToPicoFeatures2(Outline, FeatureSet);
81  }
83  NormalizePicoX(FeatureSet);
84  FreeOutlines(Outlines);
85  return (FeatureSet);
86 
87 } /* ExtractPicoFeatures */
88 } // namespace tesseract
89 
90 /*----------------------------------------------------------------------------
91  Private Code
92 ----------------------------------------------------------------------------*/
93 /*---------------------------------------------------------------------------*/
110  FPOINT *End,
111  FEATURE_SET FeatureSet) {
112  FEATURE Feature;
113  FLOAT32 Angle;
114  FLOAT32 Length;
115  int NumFeatures;
116  FPOINT Center;
117  FPOINT Delta;
118  int i;
119 
120  Angle = NormalizedAngleFrom (Start, End, 1.0);
121  Length = DistanceBetween (*Start, *End);
122  NumFeatures = (int) floor (Length / classify_pico_feature_length + 0.5);
123  if (NumFeatures < 1)
124  NumFeatures = 1;
125 
126  /* compute vector for one pico feature */
127  Delta.x = XDelta (*Start, *End) / NumFeatures;
128  Delta.y = YDelta (*Start, *End) / NumFeatures;
129 
130  /* compute position of first pico feature */
131  Center.x = Start->x + Delta.x / 2.0;
132  Center.y = Start->y + Delta.y / 2.0;
133 
134  /* compute each pico feature in segment and add to feature set */
135  for (i = 0; i < NumFeatures; i++) {
136  Feature = NewFeature (&PicoFeatDesc);
137  Feature->Params[PicoFeatDir] = Angle;
138  Feature->Params[PicoFeatX] = Center.x;
139  Feature->Params[PicoFeatY] = Center.y;
140  AddFeature(FeatureSet, Feature);
141 
142  Center.x += Delta.x;
143  Center.y += Delta.y;
144  }
145 } /* ConvertSegmentToPicoFeat */
146 
147 
148 /*---------------------------------------------------------------------------*/
163 void ConvertToPicoFeatures2(MFOUTLINE Outline, FEATURE_SET FeatureSet) {
164  MFOUTLINE Next;
165  MFOUTLINE First;
166  MFOUTLINE Current;
167 
168  if (DegenerateOutline(Outline))
169  return;
170 
171  First = Outline;
172  Current = First;
173  Next = NextPointAfter(Current);
174  do {
175  /* note that an edge is hidden if the ending point of the edge is
176  marked as hidden. This situation happens because the order of
177  the outlines is reversed when they are converted from the old
178  format. In the old format, a hidden edge is marked by the
179  starting point for that edge. */
180  if (!(PointAt(Next)->Hidden))
181  ConvertSegmentToPicoFeat (&(PointAt(Current)->Point),
182  &(PointAt(Next)->Point), FeatureSet);
183 
184  Current = Next;
185  Next = NextPointAfter(Current);
186  }
187  while (Current != First);
188 
189 } /* ConvertToPicoFeatures2 */
190 
191 
192 /*---------------------------------------------------------------------------*/
204 void NormalizePicoX(FEATURE_SET FeatureSet) {
205  int i;
206  FEATURE Feature;
207  FLOAT32 Origin = 0.0;
208 
209  for (i = 0; i < FeatureSet->NumFeatures; i++) {
210  Feature = FeatureSet->Features[i];
211  Origin += Feature->Params[PicoFeatX];
212  }
213  Origin /= FeatureSet->NumFeatures;
214 
215  for (i = 0; i < FeatureSet->NumFeatures; i++) {
216  Feature = FeatureSet->Features[i];
217  Feature->Params[PicoFeatX] -= Origin;
218  }
219 } /* NormalizePicoX */
220 
221 namespace tesseract {
222 /*---------------------------------------------------------------------------*/
231  const TBLOB& blob, const INT_FX_RESULT_STRUCT& fx_info) {
232  INT_FX_RESULT_STRUCT local_fx_info(fx_info);
235  blob, false, &local_fx_info, &bl_features);
236  if (sample == NULL) return NULL;
237 
238  int num_features = sample->num_features();
239  const INT_FEATURE_STRUCT* features = sample->features();
240  FEATURE_SET feature_set = NewFeatureSet(num_features);
241  for (int f = 0; f < num_features; ++f) {
242  FEATURE feature = NewFeature(&IntFeatDesc);
243 
244  feature->Params[IntX] = features[f].X;
245  feature->Params[IntY] = features[f].Y;
246  feature->Params[IntDir] = features[f].Theta;
247  AddFeature(feature_set, feature);
248  }
249  delete sample;
250 
251  return feature_set;
252 } /* ExtractIntCNFeatures */
253 
254 /*---------------------------------------------------------------------------*/
263  const TBLOB& blob, const INT_FX_RESULT_STRUCT& fx_info) {
264  INT_FX_RESULT_STRUCT local_fx_info(fx_info);
267  blob, false, &local_fx_info, &bl_features);
268  if (sample == NULL) return NULL;
269 
270  FEATURE_SET feature_set = NewFeatureSet(1);
271  FEATURE feature = NewFeature(&IntFeatDesc);
272 
273  feature->Params[GeoBottom] = sample->geo_feature(GeoBottom);
274  feature->Params[GeoTop] = sample->geo_feature(GeoTop);
275  feature->Params[GeoWidth] = sample->geo_feature(GeoWidth);
276  AddFeature(feature_set, feature);
277  delete sample;
278 
279  return feature_set;
280 } /* ExtractIntGeoFeatures */
281 
282 } // namespace tesseract.
Definition: blobs.h:261
#define YDelta(A, B)
Definition: fpoint.h:40
float FLOAT32
Definition: host.h:111
#define double_VAR(name, val, comment)
Definition: params.h:286
const INT_FEATURE_STRUCT * features() const
void NormalizePicoX(FEATURE_SET FeatureSet)
Definition: picofeat.cpp:204
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:96
FEATURE_SET NewFeatureSet(int NumFeatures)
FLOAT32 NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, FLOAT32 FullScale)
Definition: fpoint.cpp:48
const FEATURE_DESC_STRUCT PicoFeatDesc
FEATURE Features[1]
Definition: ocrfeatures.h:72
FEATURE_SET ExtractPicoFeatures(TBLOB *Blob)
Definition: picofeat.cpp:67
Definition: picofeat.h:29
void ConvertToPicoFeatures2(MFOUTLINE Outline, FEATURE_SET FeatureSet)
Definition: picofeat.cpp:163
Definition: fpoint.h:29
#define MAX_PICO_FEATURES
Definition: picofeat.h:47
double classify_pico_feature_length
Definition: picofeat.cpp:39
FEATURE_SET ExtractIntGeoFeatures(const TBLOB &blob, const INT_FX_RESULT_STRUCT &fx_info)
Definition: picofeat.cpp:262
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:39
#define PointAt(O)
Definition: mfoutline.h:67
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:178
FLOAT32 DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:30
FLOAT32 y
Definition: fpoint.h:31
#define first_node(l)
Definition: oldlist.h:139
const FEATURE_DESC_STRUCT IntFeatDesc
#define iterate(l)
Definition: oldlist.h:159
#define DegenerateOutline(O)
Definition: mfoutline.h:66
TrainingSample * BlobToTrainingSample(const TBLOB &blob, bool nonlinear_norm, INT_FX_RESULT_STRUCT *fx_info, GenericVector< INT_FEATURE_STRUCT > *bl_features)
Definition: intfx.cpp:81
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:44
Definition: cluster.h:32
Definition: picofeat.h:30
#define NextPointAfter(E)
Definition: mfoutline.h:68
void ConvertSegmentToPicoFeat(FPOINT *Start, FPOINT *End, FEATURE_SET FeatureSet)
Definition: picofeat.cpp:109
#define NULL
Definition: host.h:144
LIST MFOUTLINE
Definition: mfoutline.h:33
FEATURE_SET ExtractIntCNFeatures(const TBLOB &blob, const INT_FX_RESULT_STRUCT &fx_info)
Definition: picofeat.cpp:230
FLOAT32 x
Definition: fpoint.h:31
void NormalizeOutlines(LIST Outlines, FLOAT32 *XScale, FLOAT32 *YScale)
Definition: mfoutline.cpp:300
int geo_feature(int index) const
#define XDelta(A, B)
Definition: fpoint.h:39