tesseract  4.00.00dev
normalis.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: normalis.h (Formerly denorm.h)
3  * Description: Code for the DENORM class.
4  * Author: Ray Smith
5  * Created: Thu Apr 23 09:22:43 BST 1992
6  *
7  * (C) Copyright 1992, Hewlett-Packard Ltd.
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 
20 #ifndef NORMALIS_H
21 #define NORMALIS_H
22 
23 #include <stdio.h>
24 #include "genericvector.h"
25 #include "host.h"
26 
27 const int kBlnCellHeight = 256; // Full-height for baseline normalization.
28 const int kBlnXHeight = 128; // x-height for baseline normalization.
29 const int kBlnBaselineOffset = 64; // offset for baseline normalization.
30 
31 struct Pix;
32 class ROW; // Forward decl
33 class BLOCK;
34 class FCOORD;
35 struct TBLOB;
36 class TBOX;
37 struct TPOINT;
38 class UNICHARSET;
39 
40 namespace tesseract {
41 
42 // Possible normalization methods. Use NEGATIVE values as these also
43 // double up as markers for the last sub-classifier.
45  NM_BASELINE = -3, // The original BL normalization mode.
46  NM_CHAR_ISOTROPIC = -2, // Character normalization but isotropic.
47  NM_CHAR_ANISOTROPIC = -1 // The original CN normalization mode.
48 };
49 
50 } // namespace tesseract.
51 
52 class DENORM {
53  public:
54  DENORM();
55 
56  // Copying a DENORM is allowed.
57  DENORM(const DENORM &);
58  DENORM& operator=(const DENORM&);
59  ~DENORM();
60 
61  // Setup the normalization transformation parameters.
62  // The normalizations applied to a blob are as follows:
63  // 1. An optional block layout rotation that was applied during layout
64  // analysis to make the textlines horizontal.
65  // 2. A normalization transformation (LocalNormTransform):
66  // Subtract the "origin"
67  // Apply an x,y scaling.
68  // Apply an optional rotation.
69  // Add back a final translation.
70  // The origin is in the block-rotated space, and is usually something like
71  // the x-middle of the word at the baseline.
72  // 3. Zero or more further normalization transformations that are applied
73  // in sequence, with a similar pattern to the first normalization transform.
74  //
75  // A DENORM holds the parameters of a single normalization, and can execute
76  // both the LocalNormTransform (a forwards normalization), and the
77  // LocalDenormTransform which is an inverse transform or de-normalization.
78  // A DENORM may point to a predecessor DENORM, which is actually the earlier
79  // normalization, so the full normalization sequence involves executing all
80  // predecessors first and then the transform in "this".
81  // Let x be image co-ordinates and that we have normalization classes A, B, C
82  // where we first apply A then B then C to get normalized x':
83  // x' = CBAx
84  // Then the backwards (to original coordinates) would be:
85  // x = A^-1 B^-1 C^-1 x'
86  // and A = B->predecessor_ and B = C->predecessor_
87  // NormTransform executes all predecessors recursively, and then this.
88  // NormTransform would be used to transform an image-based feature to
89  // normalized space for use in a classifier
90  // DenormTransform inverts this and then all predecessors. It can be
91  // used to get back to the original image coordinates from normalized space.
92  // The LocalNormTransform member executes just the transformation
93  // in "this" without the layout rotation or any predecessors. It would be
94  // used to run each successive normalization, eg the word normalization,
95  // and later the character normalization.
96 
97  // Arguments:
98  // block: if not NULL, then this is the first transformation, and
99  // block->re_rotation() needs to be used after the Denorm
100  // transformation to get back to the image coords.
101  // rotation: if not NULL, apply this rotation after translation to the
102  // origin and scaling. (Usually a classify rotation.)
103  // predecessor: if not NULL, then predecessor has been applied to the
104  // input space and needs to be undone to complete the inverse.
105  // The above pointers are not owned by this DENORM and are assumed to live
106  // longer than this denorm, except rotation, which is deep copied on input.
107  //
108  // x_origin: The x origin which will be mapped to final_xshift in the result.
109  // y_origin: The y origin which will be mapped to final_yshift in the result.
110  // Added to result of row->baseline(x) if not NULL.
111  //
112  // x_scale: scale factor for the x-coordinate.
113  // y_scale: scale factor for the y-coordinate. Ignored if segs is given.
114  // Note that these scale factors apply to the same x and y system as the
115  // x-origin and y-origin apply, ie after any block rotation, but before
116  // the rotation argument is applied.
117  //
118  // final_xshift: The x component of the final translation.
119  // final_yshift: The y component of the final translation.
120  //
121  // In theory, any of the commonly used normalizations can be setup here:
122  // * Traditional baseline normalization on a word:
123  // SetupNormalization(block, NULL, NULL,
124  // box.x_middle(), baseline,
125  // kBlnXHeight / x_height, kBlnXHeight / x_height,
126  // 0, kBlnBaselineOffset);
127  // * "Numeric mode" baseline normalization on a word, in which the blobs
128  // are positioned with the bottom as the baseline is achieved by making
129  // a separate DENORM for each blob.
130  // SetupNormalization(block, NULL, NULL,
131  // box.x_middle(), box.bottom(),
132  // kBlnXHeight / x_height, kBlnXHeight / x_height,
133  // 0, kBlnBaselineOffset);
134  // * Anisotropic character normalization used by IntFx.
135  // SetupNormalization(NULL, NULL, denorm,
136  // centroid_x, centroid_y,
137  // 51.2 / ry, 51.2 / rx, 128, 128);
138  // * Normalize blob height to x-height (current OSD):
139  // SetupNormalization(NULL, &rotation, NULL,
140  // box.rotational_x_middle(rotation),
141  // box.rotational_y_middle(rotation),
142  // kBlnXHeight / box.rotational_height(rotation),
143  // kBlnXHeight / box.rotational_height(rotation),
144  // 0, kBlnBaselineOffset);
145  // * Secondary normalization for classification rotation (current):
146  // FCOORD rotation = block->classify_rotation();
147  // float target_height = kBlnXHeight / CCStruct::kXHeightCapRatio;
148  // SetupNormalization(NULL, &rotation, denorm,
149  // box.rotational_x_middle(rotation),
150  // box.rotational_y_middle(rotation),
151  // target_height / box.rotational_height(rotation),
152  // target_height / box.rotational_height(rotation),
153  // 0, kBlnBaselineOffset);
154  // * Proposed new normalizations for CJK: Between them there is then
155  // no need for further normalization at all, and the character fills the cell.
156  // ** Replacement for baseline normalization on a word:
157  // Scales height and width independently so that modal height and pitch
158  // fill the cell respectively.
159  // float cap_height = x_height / CCStruct::kXHeightCapRatio;
160  // SetupNormalization(block, NULL, NULL,
161  // box.x_middle(), cap_height / 2.0f,
162  // kBlnCellHeight / fixed_pitch,
163  // kBlnCellHeight / cap_height,
164  // 0, 0);
165  // ** Secondary normalization for classification (with rotation) (proposed):
166  // Requires a simple translation to the center of the appropriate character
167  // cell, no further scaling and a simple rotation (or nothing) about the
168  // cell center.
169  // FCOORD rotation = block->classify_rotation();
170  // SetupNormalization(NULL, &rotation, denorm,
171  // fixed_pitch_cell_center,
172  // 0.0f,
173  // 1.0f,
174  // 1.0f,
175  // 0, 0);
176  void SetupNormalization(const BLOCK* block,
177  const FCOORD* rotation,
178  const DENORM* predecessor,
179  float x_origin, float y_origin,
180  float x_scale, float y_scale,
181  float final_xshift, float final_yshift);
182 
183  // Sets up the DENORM to execute a non-linear transformation based on
184  // preserving an even distribution of stroke edges. The transformation
185  // operates only within the given box, scaling input coords within the box
186  // non-linearly to a box of target_width by target_height, with all other
187  // coords being clipped to the box edge. As with SetupNormalization above,
188  // final_xshift and final_yshift are applied after scaling, and the bottom-
189  // left of box is used as a pre-scaling origin.
190  // x_coords is a collection of the x-coords of vertical edges for each
191  // y-coord starting at box.bottom().
192  // y_coords is a collection of the y-coords of horizontal edges for each
193  // x-coord starting at box.left().
194  // Eg x_coords[0] is a collection of the x-coords of edges at y=bottom.
195  // Eg x_coords[1] is a collection of the x-coords of edges at y=bottom + 1.
196  // The second-level vectors must all be sorted in ascending order.
197  void SetupNonLinear(const DENORM* predecessor, const TBOX& box,
198  float target_width, float target_height,
199  float final_xshift, float final_yshift,
200  const GenericVector<GenericVector<int> >& x_coords,
201  const GenericVector<GenericVector<int> >& y_coords);
202 
203  // Transforms the given coords one step forward to normalized space, without
204  // using any block rotation or predecessor.
205  void LocalNormTransform(const TPOINT& pt, TPOINT* transformed) const;
206  void LocalNormTransform(const FCOORD& pt, FCOORD* transformed) const;
207  // Transforms the given coords forward to normalized space using the
208  // full transformation sequence defined by the block rotation, the
209  // predecessors, deepest first, and finally this. If first_norm is not NULL,
210  // then the first and deepest transformation used is first_norm, ending
211  // with this, and the block rotation will not be applied.
212  void NormTransform(const DENORM* first_norm, const TPOINT& pt,
213  TPOINT* transformed) const;
214  void NormTransform(const DENORM* first_norm, const FCOORD& pt,
215  FCOORD* transformed) const;
216  // Transforms the given coords one step back to source space, without
217  // using to any block rotation or predecessor.
218  void LocalDenormTransform(const TPOINT& pt, TPOINT* original) const;
219  void LocalDenormTransform(const FCOORD& pt, FCOORD* original) const;
220  // Transforms the given coords all the way back to source image space using
221  // the full transformation sequence defined by this and its predecessors
222  // recursively, shallowest first, and finally any block re_rotation.
223  // If last_denorm is not NULL, then the last transformation used will
224  // be last_denorm, and the block re_rotation will never be executed.
225  void DenormTransform(const DENORM* last_denorm, const TPOINT& pt,
226  TPOINT* original) const;
227  void DenormTransform(const DENORM* last_denorm, const FCOORD& pt,
228  FCOORD* original) const;
229 
230  // Normalize a blob using blob transformations. Less accurate, but
231  // more accurately copies the old way.
232  void LocalNormBlob(TBLOB* blob) const;
233 
234  // Fills in the x-height range accepted by the given unichar_id in blob
235  // coordinates, given its bounding box in the usual baseline-normalized
236  // coordinates, with some initial crude x-height estimate (such as word
237  // size) and this denoting the transformation that was used.
238  // Also returns the amount the character must have shifted up or down.
239  void XHeightRange(int unichar_id, const UNICHARSET& unicharset,
240  const TBOX& bbox,
241  float* min_xht,
242  float* max_xht,
243  float* yshift) const;
244 
245  // Prints the content of the DENORM for debug purposes.
246  void Print() const;
247 
248  Pix* pix() const {
249  return pix_;
250  }
251  void set_pix(Pix* pix) {
252  pix_ = pix;
253  }
254  bool inverse() const {
255  return inverse_;
256  }
257  void set_inverse(bool value) {
258  inverse_ = value;
259  }
260  const DENORM* RootDenorm() const {
261  if (predecessor_ != NULL)
262  return predecessor_->RootDenorm();
263  return this;
264  }
265  const DENORM* predecessor() const {
266  return predecessor_;
267  }
268  // Accessors - perhaps should not be needed.
269  float x_scale() const {
270  return x_scale_;
271  }
272  float y_scale() const {
273  return y_scale_;
274  }
275  const BLOCK* block() const {
276  return block_;
277  }
278  void set_block(const BLOCK* block) {
279  block_ = block;
280  }
281 
282  private:
283  // Free allocated memory and clear pointers.
284  void Clear();
285  // Setup default values.
286  void Init();
287 
288  // Best available image.
289  Pix* pix_;
290  // True if the source image is white-on-black.
291  bool inverse_;
292  // Block the word came from. If not null, block->re_rotation() takes the
293  // "untransformed" coordinates even further back to the original image.
294  // Used only on the first DENORM in a chain.
295  const BLOCK* block_;
296  // Rotation to apply between translation to the origin and scaling.
297  const FCOORD* rotation_;
298  // Previous transformation in a chain.
299  const DENORM* predecessor_;
300  // Non-linear transformation maps directly from each integer offset from the
301  // origin to the corresponding x-coord. Owned by the DENORM.
302  GenericVector<float>* x_map_;
303  // Non-linear transformation maps directly from each integer offset from the
304  // origin to the corresponding y-coord. Owned by the DENORM.
305  GenericVector<float>* y_map_;
306  // x-coordinate to be mapped to final_xshift_ in the result.
307  float x_origin_;
308  // y-coordinate to be mapped to final_yshift_ in the result.
309  float y_origin_;
310  // Scale factors for x and y coords. Applied to pre-rotation system.
311  float x_scale_;
312  float y_scale_;
313  // Destination coords of the x_origin_ and y_origin_.
314  float final_xshift_;
315  float final_yshift_;
316 };
317 #endif
Pix * pix() const
Definition: normalis.h:248
const DENORM * RootDenorm() const
Definition: normalis.h:260
Definition: points.h:189
const DENORM * predecessor() const
Definition: normalis.h:265
void set_inverse(bool value)
Definition: normalis.h:257
NormalizationMode
Definition: normalis.h:44
const int kBlnXHeight
Definition: normalis.h:28
const int kBlnBaselineOffset
Definition: normalis.h:29
void set_block(const BLOCK *block)
Definition: normalis.h:278
float y_scale() const
Definition: normalis.h:272
const int kBlnCellHeight
Definition: normalis.h:27
bool inverse() const
Definition: normalis.h:254
const BLOCK * block() const
Definition: normalis.h:275
float x_scale() const
Definition: normalis.h:269
Definition: rect.h:30
Definition: blobs.h:261
Definition: blobs.h:50
Definition: ocrrow.h:32
Definition: ocrblock.h:30
void set_pix(Pix *pix)
Definition: normalis.h:251