"Fossies" - the Fresh Open Source Software Archive

Member "tesseract-ocr/doc/html/intfeaturespace_8cpp_source.html" (26 Oct 2012, 37488 Bytes) of package /linux/misc/old/tesseract-ocr-3.02.02-doc-html.tar.gz:


Caution: In this restricted "Fossies" environment the current HTML page may not be correctly presentated and may have some non-functional links. You can here alternatively try to browse the pure source code or just view or download the uninterpreted raw source code. If the rendering is insufficient you may try to find and view the page on the tesseract-ocr-3.02.02-doc-html.tar.gz project site itself.

Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
intfeaturespace.cpp
Go to the documentation of this file.
1 // Copyright 2010 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
4 // File: intfeaturespace.cpp
5 // Description: Indexed feature space based on INT_FEATURE_STRUCT.
6 // Created: Wed Mar 24 11:21:27 PDT 2010
7 //
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 //
19 
20 #include "intfeaturespace.h"
21 #include "intfx.h"
22 
23 namespace tesseract {
24 
26  : x_buckets_(0), y_buckets_(0), theta_buckets_(0) {
27 }
28 
29 void IntFeatureSpace::Init(uinT8 xbuckets, uinT8 ybuckets, uinT8 thetabuckets) {
30  x_buckets_ = xbuckets;
31  y_buckets_ = ybuckets;
32  theta_buckets_ = thetabuckets;
33 }
34 
35 // Serializes the feature space definition to the given file.
36 // Returns false on error.
37 bool IntFeatureSpace::Serialize(FILE* fp) const {
38  if (fwrite(&x_buckets_, sizeof(x_buckets_), 1, fp) != 1)
39  return false;
40  if (fwrite(&y_buckets_, sizeof(y_buckets_), 1, fp) != 1)
41  return false;
42  if (fwrite(&theta_buckets_, sizeof(theta_buckets_), 1, fp) != 1)
43  return false;
44  return true;
45 }
46 
47 // DeSerializes the feature space definition from the given file.
48 // If swap is true, the data is big/little-endian swapped.
49 // Returns false on error.
50 bool IntFeatureSpace::DeSerialize(bool swap, FILE* fp) {
51  if (fread(&x_buckets_, sizeof(x_buckets_), 1, fp) != 1)
52  return false;
53  if (fread(&y_buckets_, sizeof(y_buckets_), 1, fp) != 1)
54  return false;
55  if (fread(&theta_buckets_, sizeof(theta_buckets_), 1, fp) != 1)
56  return false;
57  return true;
58 }
59 
60 // Returns an INT_FEATURE_STRUCT corresponding to the given index.
61 // This is the inverse of the Index member.
64  index / theta_buckets_ % y_buckets_,
65  index % theta_buckets_);
66 }
67 
68 // Bulk calls to Index. Maps the given array of features to a vector of
69 // inT32 indices in the same order as the input.
71  int num_features,
72  GenericVector<int>* mapped_features) const {
73  mapped_features->truncate(0);
74  for (int f = 0; f < num_features; ++f)
75  mapped_features->push_back(Index(features[f]));
76 }
77 
78 // Bulk calls to Index. Maps the given array of features to a vector of
79 // sorted inT32 indices.
81  const INT_FEATURE_STRUCT* features, int num_features,
82  GenericVector<int>* sorted_features) const {
83  sorted_features->truncate(0);
84  for (int f = 0; f < num_features; ++f)
85  sorted_features->push_back(Index(features[f]));
86  sorted_features->sort();
87 }
88 
89 // Returns a feature space index for the given x,y position in a display
90 // window, or -1 if the feature is a miss.
91 int IntFeatureSpace::XYToFeatureIndex(int x, int y) const {
92  // Round the x,y position to a feature. Search for a valid theta.
93  INT_FEATURE_STRUCT feature = {static_cast<uinT8>(x), static_cast<uinT8>(y),
94  0, 0};
95  int index = -1;
96  for (int theta = 0; theta <= MAX_UINT8 && index < 0; ++theta) {
97  feature.Theta = theta;
98  index = Index(feature);
99  }
100  if (index < 0) {
101  tprintf("(%d,%d) does not exist in feature space!\n", x, y);
102  return -1;
103  }
104  feature = PositionFromIndex(index);
105  tprintf("Click at (%d, %d) ->(%d, %d), ->(%d, %d)\n",
106  x, y, feature.X, feature.Y, x - feature.X, y - feature.Y);
107  // Get the relative position of x,y from the rounded feature.
108  x -= feature.X;
109  y -= feature.Y;
110  if (x != 0 || y != 0) {
111  double angle = atan2(static_cast<double>(y), static_cast<double>(x)) + PI;
112  angle *= kIntFeatureExtent / (2.0 * PI);
113  feature.Theta = static_cast<uinT8>(angle + 0.5);
114  index = Index(feature);
115  if (index < 0) {
116  tprintf("Feature failed to map to a valid index:");
117  feature.print();
118  return -1;
119  }
120  feature = PositionFromIndex(index);
121  }
122  feature.print();
123  return index;
124 }
125 
126 // Returns an INT_FEATURE_STRUCT corresponding to the given bucket coords.
128  int y,
129  int theta) const {
130  INT_FEATURE_STRUCT pos = {
131  static_cast<uinT8>(ClipToRange(
133  0, MAX_UINT8)),
134  static_cast<uinT8>(ClipToRange(
136  0, MAX_UINT8)),
137  static_cast<uinT8>(ClipToRange(
139  0, MAX_UINT8))};
140  return pos;
141 }
142 
143 } // namespace tesseract.