tesseract  5.0.0
outfeat.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: outfeat.c
3  ** Purpose: Definition of outline-features.
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 "outfeat.h"
19 
20 #include "classify.h"
21 #include "featdefs.h"
22 #include "mfoutline.h"
23 #include "ocrfeatures.h"
24 
25 #include <cstdio>
26 
27 namespace tesseract {
28 
29 /*----------------------------------------------------------------------------
30  Public Code
31 ----------------------------------------------------------------------------*/
32 
41  auto FeatureSet = new FEATURE_SET_STRUCT(MAX_OUTLINE_FEATURES);
42  if (Blob == nullptr) {
43  return (FeatureSet);
44  }
45 
46  auto Outlines = ConvertBlob(Blob);
47 
48  float XScale, YScale;
49  NormalizeOutlines(Outlines, &XScale, &YScale);
50  auto RemainingOutlines = Outlines;
51  iterate(RemainingOutlines) {
52  auto Outline = static_cast<MFOUTLINE>(RemainingOutlines->first_node());
53  ConvertToOutlineFeatures(Outline, FeatureSet);
54  }
55  if (classify_norm_method == baseline) {
56  NormalizeOutlineX(FeatureSet);
57  }
58  FreeOutlines(Outlines);
59  return (FeatureSet);
60 } /* ExtractOutlineFeatures */
61 
62 /*----------------------------------------------------------------------------
63  Private Code
64 ----------------------------------------------------------------------------*/
65 /*---------------------------------------------------------------------------*/
78 void AddOutlineFeatureToSet(FPOINT *Start, FPOINT *End, FEATURE_SET FeatureSet) {
79  auto Feature = new FEATURE_STRUCT(&OutlineFeatDesc);
80  Feature->Params[OutlineFeatDir] = NormalizedAngleFrom(Start, End, 1.0);
81  Feature->Params[OutlineFeatX] = AverageOf(Start->x, End->x);
82  Feature->Params[OutlineFeatY] = AverageOf(Start->y, End->y);
83  Feature->Params[OutlineFeatLength] = DistanceBetween(*Start, *End);
84  AddFeature(FeatureSet, Feature);
85 
86 } /* AddOutlineFeatureToSet */
87 
88 /*---------------------------------------------------------------------------*/
97 void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet) {
98  MFOUTLINE Next;
99  MFOUTLINE First;
100  FPOINT FeatureStart;
101  FPOINT FeatureEnd;
102 
103  if (DegenerateOutline(Outline)) {
104  return;
105  }
106 
107  First = Outline;
108  Next = First;
109  do {
110  FeatureStart = PointAt(Next)->Point;
111  Next = NextPointAfter(Next);
112 
113  /* note that an edge is hidden if the ending point of the edge is
114  marked as hidden. This situation happens because the order of
115  the outlines is reversed when they are converted from the old
116  format. In the old format, a hidden edge is marked by the
117  starting point for that edge. */
118  if (!PointAt(Next)->Hidden) {
119  FeatureEnd = PointAt(Next)->Point;
120  AddOutlineFeatureToSet(&FeatureStart, &FeatureEnd, FeatureSet);
121  }
122  } while (Next != First);
123 } /* ConvertToOutlineFeatures */
124 
125 /*---------------------------------------------------------------------------*/
134 void NormalizeOutlineX(FEATURE_SET FeatureSet) {
135  int i;
136  FEATURE Feature;
137  float Length;
138  float TotalX = 0.0;
139  float TotalWeight = 0.0;
140  float Origin;
141 
142  if (FeatureSet->NumFeatures <= 0) {
143  return;
144  }
145 
146  for (i = 0; i < FeatureSet->NumFeatures; i++) {
147  Feature = FeatureSet->Features[i];
148  Length = Feature->Params[OutlineFeatLength];
149  TotalX += Feature->Params[OutlineFeatX] * Length;
150  TotalWeight += Length;
151  }
152  Origin = TotalX / TotalWeight;
153 
154  for (i = 0; i < FeatureSet->NumFeatures; i++) {
155  Feature = FeatureSet->Features[i];
156  Feature->Params[OutlineFeatX] -= Origin;
157  }
158 } /* NormalizeOutlineX */
159 
160 } // namespace tesseract
float DistanceBetween(FPOINT A, FPOINT B)
Definition: fpoint.cpp:29
float NormalizedAngleFrom(FPOINT *Point1, FPOINT *Point2, float FullScale)
Definition: fpoint.cpp:44
#define AverageOf(A, B)
Definition: mfoutline.h:58
#define MAX_OUTLINE_FEATURES
Definition: outfeat.h:34
#define iterate(l)
Definition: oldlist.h:91
@ OutlineFeatLength
Definition: outfeat.h:30
@ OutlineFeatY
Definition: outfeat.h:29
@ OutlineFeatX
Definition: outfeat.h:28
@ OutlineFeatDir
Definition: outfeat.h:31
void FreeOutlines(LIST Outlines)
Definition: mfoutline.cpp:151
void AddOutlineFeatureToSet(FPOINT *Start, FPOINT *End, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:78
void NormalizeOutlineX(FEATURE_SET FeatureSet)
Definition: outfeat.cpp:134
void ConvertToOutlineFeatures(MFOUTLINE Outline, FEATURE_SET FeatureSet)
Definition: outfeat.cpp:97
@ baseline
Definition: mfoutline.h:53
LIST ConvertBlob(TBLOB *blob)
Definition: mfoutline.cpp:34
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:39
const FEATURE_DESC_STRUCT OutlineFeatDesc
void NormalizeOutlines(LIST Outlines, float *XScale, float *YScale)
Definition: mfoutline.cpp:249
FEATURE_SET ExtractOutlineFeatures(TBLOB *Blob)
Definition: outfeat.cpp:40
Definition: fpoint.h:29
float y
Definition: fpoint.h:30
float x
Definition: fpoint.h:30
std::vector< float > Params
Definition: ocrfeatures.h:66
std::vector< FEATURE_STRUCT * > Features
Definition: ocrfeatures.h:85