tesseract  5.0.0
tesseract::DENORM Class Reference

#include <normalis.h>

Public Member Functions

 DENORM ()
 
 DENORM (const DENORM &)
 
DENORMoperator= (const DENORM &)
 
 ~DENORM ()
 
void SetupNormalization (const BLOCK *block, const FCOORD *rotation, const DENORM *predecessor, float x_origin, float y_origin, float x_scale, float y_scale, float final_xshift, float final_yshift)
 
void SetupNonLinear (const DENORM *predecessor, const TBOX &box, float target_width, float target_height, float final_xshift, float final_yshift, const std::vector< std::vector< int >> &x_coords, const std::vector< std::vector< int >> &y_coords)
 
void LocalNormTransform (const TPOINT &pt, TPOINT *transformed) const
 
void LocalNormTransform (const FCOORD &pt, FCOORD *transformed) const
 
void NormTransform (const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
 
void NormTransform (const DENORM *first_norm, const FCOORD &pt, FCOORD *transformed) const
 
void LocalDenormTransform (const TPOINT &pt, TPOINT *original) const
 
void LocalDenormTransform (const FCOORD &pt, FCOORD *original) const
 
void DenormTransform (const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
 
void DenormTransform (const DENORM *last_denorm, const FCOORD &pt, FCOORD *original) const
 
void LocalNormBlob (TBLOB *blob) const
 
void XHeightRange (int unichar_id, const UNICHARSET &unicharset, const TBOX &bbox, float *min_xht, float *max_xht, float *yshift) const
 
void Print () const
 
Image pix () const
 
void set_pix (Image pix)
 
bool inverse () const
 
void set_inverse (bool value)
 
const DENORMRootDenorm () const
 
const DENORMpredecessor () const
 
float x_scale () const
 
float y_scale () const
 
const BLOCKblock () const
 
void set_block (const BLOCK *block)
 

Detailed Description

Definition at line 52 of file normalis.h.

Constructor & Destructor Documentation

◆ DENORM() [1/2]

tesseract::DENORM::DENORM ( )

Definition at line 39 of file normalis.cpp.

39  {
40  Init();
41 }

◆ DENORM() [2/2]

tesseract::DENORM::DENORM ( const DENORM src)

Definition at line 43 of file normalis.cpp.

43  {
44  rotation_ = nullptr;
45  *this = src;
46 }

◆ ~DENORM()

tesseract::DENORM::~DENORM ( )

Definition at line 68 of file normalis.cpp.

68  {
69  Clear();
70 }

Member Function Documentation

◆ block()

const BLOCK* tesseract::DENORM::block ( ) const
inline

Definition at line 265 of file normalis.h.

265  {
266  return block_;
267  }

◆ DenormTransform() [1/2]

void tesseract::DENORM::DenormTransform ( const DENORM last_denorm,
const FCOORD pt,
FCOORD original 
) const

Definition at line 406 of file normalis.cpp.

406  {
407  LocalDenormTransform(pt, original);
408  if (last_denorm != this) {
409  if (predecessor_ != nullptr) {
410  predecessor_->DenormTransform(last_denorm, *original, original);
411  } else if (block_ != nullptr) {
412  original->rotate(block_->re_rotation());
413  }
414  }
415 }
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:399
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:360
FCOORD re_rotation() const
Definition: ocrblock.h:129

◆ DenormTransform() [2/2]

void tesseract::DENORM::DenormTransform ( const DENORM last_denorm,
const TPOINT pt,
TPOINT original 
) const

Definition at line 399 of file normalis.cpp.

399  {
400  FCOORD src_pt(pt.x, pt.y);
401  FCOORD float_result;
402  DenormTransform(last_denorm, src_pt, &float_result);
403  original->x = IntCastRounded(float_result.x());
404  original->y = IntCastRounded(float_result.y());
405 }
int IntCastRounded(double x)
Definition: helpers.h:175

◆ inverse()

bool tesseract::DENORM::inverse ( ) const
inline

Definition at line 243 of file normalis.h.

243  {
244  return inverse_;
245  }

◆ LocalDenormTransform() [1/2]

void tesseract::DENORM::LocalDenormTransform ( const FCOORD pt,
FCOORD original 
) const

Definition at line 368 of file normalis.cpp.

368  {
369  FCOORD rotated(pt.x() - final_xshift_, pt.y() - final_yshift_);
370  if (x_map_ != nullptr && y_map_ != nullptr) {
371  auto pos = std::upper_bound(x_map_->begin(), x_map_->end(), rotated.x());
372  if (pos > x_map_->begin()) {
373  --pos;
374  }
375  auto x = pos - x_map_->begin();
376  original->set_x(x + x_origin_);
377  pos = std::upper_bound(y_map_->begin(), y_map_->end(), rotated.y());
378  if (pos > y_map_->begin()) {
379  --pos;
380  }
381  auto y = pos - y_map_->begin();
382  original->set_y(y + y_origin_);
383  } else {
384  if (rotation_ != nullptr) {
385  FCOORD inverse_rotation(rotation_->x(), -rotation_->y());
386  rotated.rotate(inverse_rotation);
387  }
388  original->set_x(rotated.x() / x_scale_ + x_origin_);
389  float y_scale = y_scale_;
390  original->set_y(rotated.y() / y_scale + y_origin_);
391  }
392 }
float y_scale() const
Definition: normalis.h:262
float y() const
Definition: points.h:209
float x() const
Definition: points.h:206

◆ LocalDenormTransform() [2/2]

void tesseract::DENORM::LocalDenormTransform ( const TPOINT pt,
TPOINT original 
) const

Definition at line 360 of file normalis.cpp.

360  {
361  FCOORD src_pt(pt.x, pt.y);
362  FCOORD float_result;
363  LocalDenormTransform(src_pt, &float_result);
364  original->x = IntCastRounded(float_result.x());
365  original->y = IntCastRounded(float_result.y());
366 }

◆ LocalNormBlob()

void tesseract::DENORM::LocalNormBlob ( TBLOB blob) const

Definition at line 419 of file normalis.cpp.

419  {
420  ICOORD translation(-IntCastRounded(x_origin_), -IntCastRounded(y_origin_));
421  blob->Move(translation);
422  if (y_scale_ != 1.0f) {
423  blob->Scale(y_scale_);
424  }
425  if (rotation_ != nullptr) {
426  blob->Rotate(*rotation_);
427  }
428  translation.set_x(IntCastRounded(final_xshift_));
429  translation.set_y(IntCastRounded(final_yshift_));
430  blob->Move(translation);
431 }

◆ LocalNormTransform() [1/2]

void tesseract::DENORM::LocalNormTransform ( const FCOORD pt,
FCOORD transformed 
) const

Definition at line 315 of file normalis.cpp.

315  {
316  FCOORD translated(pt.x() - x_origin_, pt.y() - y_origin_);
317  if (x_map_ != nullptr && y_map_ != nullptr) {
318  int x = ClipToRange(IntCastRounded(translated.x()), 0, static_cast<int>(x_map_->size() - 1));
319  translated.set_x((*x_map_)[x]);
320  int y = ClipToRange(IntCastRounded(translated.y()), 0, static_cast<int>(y_map_->size() - 1));
321  translated.set_y((*y_map_)[y]);
322  } else {
323  translated.set_x(translated.x() * x_scale_);
324  translated.set_y(translated.y() * y_scale_);
325  if (rotation_ != nullptr) {
326  translated.rotate(*rotation_);
327  }
328  }
329  transformed->set_x(translated.x() + final_xshift_);
330  transformed->set_y(translated.y() + final_yshift_);
331 }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:110

◆ LocalNormTransform() [2/2]

void tesseract::DENORM::LocalNormTransform ( const TPOINT pt,
TPOINT transformed 
) const

Definition at line 308 of file normalis.cpp.

308  {
309  FCOORD src_pt(pt.x, pt.y);
310  FCOORD float_result;
311  LocalNormTransform(src_pt, &float_result);
312  transformed->x = IntCastRounded(float_result.x());
313  transformed->y = IntCastRounded(float_result.y());
314 }
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:308

◆ NormTransform() [1/2]

void tesseract::DENORM::NormTransform ( const DENORM first_norm,
const FCOORD pt,
FCOORD transformed 
) const

Definition at line 345 of file normalis.cpp.

345  {
346  FCOORD src_pt(pt);
347  if (first_norm != this) {
348  if (predecessor_ != nullptr) {
349  predecessor_->NormTransform(first_norm, pt, &src_pt);
350  } else if (block_ != nullptr) {
351  FCOORD fwd_rotation(block_->re_rotation().x(), -block_->re_rotation().y());
352  src_pt.rotate(fwd_rotation);
353  }
354  }
355  LocalNormTransform(src_pt, transformed);
356 }
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:338

◆ NormTransform() [2/2]

void tesseract::DENORM::NormTransform ( const DENORM first_norm,
const TPOINT pt,
TPOINT transformed 
) const

Definition at line 338 of file normalis.cpp.

338  {
339  FCOORD src_pt(pt.x, pt.y);
340  FCOORD float_result;
341  NormTransform(first_norm, src_pt, &float_result);
342  transformed->x = IntCastRounded(float_result.x());
343  transformed->y = IntCastRounded(float_result.y());
344 }

◆ operator=()

DENORM & tesseract::DENORM::operator= ( const DENORM src)

Definition at line 48 of file normalis.cpp.

48  {
49  Clear();
50  inverse_ = src.inverse_;
51  predecessor_ = src.predecessor_;
52  pix_ = src.pix_;
53  block_ = src.block_;
54  if (src.rotation_ == nullptr) {
55  rotation_ = nullptr;
56  } else {
57  rotation_ = new FCOORD(*src.rotation_);
58  }
59  x_origin_ = src.x_origin_;
60  y_origin_ = src.y_origin_;
61  x_scale_ = src.x_scale_;
62  y_scale_ = src.y_scale_;
63  final_xshift_ = src.final_xshift_;
64  final_yshift_ = src.final_yshift_;
65  return *this;
66 }
Pix * pix_
Definition: image.h:27

◆ pix()

Image tesseract::DENORM::pix ( ) const
inline

Definition at line 237 of file normalis.h.

237  {
238  return pix_;
239  }

◆ predecessor()

const DENORM* tesseract::DENORM::predecessor ( ) const
inline

Definition at line 255 of file normalis.h.

255  {
256  return predecessor_;
257  }

◆ Print()

void tesseract::DENORM::Print ( ) const

Definition at line 513 of file normalis.cpp.

513  {
514  if (pix_ != nullptr) {
515  tprintf("Pix dimensions %d x %d x %d\n", pixGetWidth(pix_), pixGetHeight(pix_),
516  pixGetDepth(pix_));
517  }
518  if (inverse_) {
519  tprintf("Inverse\n");
520  }
521  if (block_ && block_->re_rotation().x() != 1.0f) {
522  tprintf("Block rotation %g, %g\n", block_->re_rotation().x(), block_->re_rotation().y());
523  }
524  tprintf("Input Origin = (%g, %g)\n", x_origin_, y_origin_);
525  if (x_map_ != nullptr && y_map_ != nullptr) {
526  tprintf("x map:\n");
527  for (auto x : *x_map_) {
528  tprintf("%g ", x);
529  }
530  tprintf("\ny map:\n");
531  for (auto y : *y_map_) {
532  tprintf("%g ", y);
533  }
534  tprintf("\n");
535  } else {
536  tprintf("Scale = (%g, %g)\n", x_scale_, y_scale_);
537  if (rotation_ != nullptr) {
538  tprintf("Rotation = (%g, %g)\n", rotation_->x(), rotation_->y());
539  }
540  }
541  tprintf("Final Origin = (%g, %g)\n", final_xshift_, final_xshift_);
542  if (predecessor_ != nullptr) {
543  tprintf("Predecessor:\n");
544  predecessor_->Print();
545  }
546 }
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void Print() const
Definition: normalis.cpp:513

◆ RootDenorm()

const DENORM* tesseract::DENORM::RootDenorm ( ) const
inline

Definition at line 249 of file normalis.h.

249  {
250  if (predecessor_ != nullptr) {
251  return predecessor_->RootDenorm();
252  }
253  return this;
254  }
const DENORM * RootDenorm() const
Definition: normalis.h:249

◆ set_block()

void tesseract::DENORM::set_block ( const BLOCK block)
inline

Definition at line 268 of file normalis.h.

268  {
269  block_ = block;
270  }
const BLOCK * block() const
Definition: normalis.h:265

◆ set_inverse()

void tesseract::DENORM::set_inverse ( bool  value)
inline

Definition at line 246 of file normalis.h.

246  {
247  inverse_ = value;
248  }

◆ set_pix()

void tesseract::DENORM::set_pix ( Image  pix)
inline

Definition at line 240 of file normalis.h.

240  {
241  pix_ = pix;
242  }
Image pix() const
Definition: normalis.h:237

◆ SetupNonLinear()

void tesseract::DENORM::SetupNonLinear ( const DENORM predecessor,
const TBOX box,
float  target_width,
float  target_height,
float  final_xshift,
float  final_yshift,
const std::vector< std::vector< int >> &  x_coords,
const std::vector< std::vector< int >> &  y_coords 
)

Definition at line 271 of file normalis.cpp.

274  {
275  Clear();
276  predecessor_ = predecessor;
277  // x_map_ and y_map_ store a mapping from input x and y coordinate to output
278  // x and y coordinate, based on scaling to the supplied target_width and
279  // target_height.
280  x_map_ = new std::vector<float>;
281  y_map_ = new std::vector<float>;
282  // Set a 2-d image array to the run lengths at each pixel.
283  int width = box.width();
284  int height = box.height();
285  GENERIC_2D_ARRAY<int> minruns(width, height, 0);
286  ComputeRunlengthImage(box, x_coords, y_coords, &minruns);
287  // Edge density is the sum of the inverses of the run lengths. Compute
288  // edge density projection profiles.
289  ComputeEdgeDensityProfiles(box, minruns, *x_map_, *y_map_);
290  // Convert the edge density profiles to the coordinates by multiplying by
291  // the desired size and accumulating.
292  (*x_map_)[width] = target_width;
293  for (int x = width - 1; x >= 0; --x) {
294  (*x_map_)[x] = (*x_map_)[x + 1] - (*x_map_)[x] * target_width;
295  }
296  (*y_map_)[height] = target_height;
297  for (int y = height - 1; y >= 0; --y) {
298  (*y_map_)[y] = (*y_map_)[y + 1] - (*y_map_)[y] * target_height;
299  }
300  x_origin_ = box.left();
301  y_origin_ = box.bottom();
302  final_xshift_ = final_xshift;
303  final_yshift_ = final_yshift;
304 }
const DENORM * predecessor() const
Definition: normalis.h:255

◆ SetupNormalization()

void tesseract::DENORM::SetupNormalization ( const BLOCK block,
const FCOORD rotation,
const DENORM predecessor,
float  x_origin,
float  y_origin,
float  x_scale,
float  y_scale,
float  final_xshift,
float  final_yshift 
)

Definition at line 97 of file normalis.cpp.

100  {
101  Clear();
102  block_ = block;
103  if (rotation == nullptr) {
104  rotation_ = nullptr;
105  } else {
106  rotation_ = new FCOORD(*rotation);
107  }
108  predecessor_ = predecessor;
109  x_origin_ = x_origin;
110  y_origin_ = y_origin;
111  x_scale_ = x_scale;
112  y_scale_ = y_scale;
113  final_xshift_ = final_xshift;
114  final_yshift_ = final_yshift;
115 }
float x_scale() const
Definition: normalis.h:259

◆ x_scale()

float tesseract::DENORM::x_scale ( ) const
inline

Definition at line 259 of file normalis.h.

259  {
260  return x_scale_;
261  }

◆ XHeightRange()

void tesseract::DENORM::XHeightRange ( int  unichar_id,
const UNICHARSET unicharset,
const TBOX bbox,
float *  min_xht,
float *  max_xht,
float *  yshift 
) const

Definition at line 437 of file normalis.cpp.

438  {
439  // Default return -- accept anything.
440  *yshift = 0.0f;
441  *min_xht = 0.0f;
442  *max_xht = FLT_MAX;
443 
444  if (!unicharset.top_bottom_useful()) {
445  return;
446  }
447 
448  // Clip the top and bottom to the limit of normalized feature space.
449  int top = ClipToRange<int>(bbox.top(), 0, kBlnCellHeight - 1);
450  int bottom = ClipToRange<int>(bbox.bottom(), 0, kBlnCellHeight - 1);
451  // A tolerance of yscale corresponds to 1 pixel in the image.
452  double tolerance = y_scale();
453  // If the script doesn't have upper and lower-case characters, widen the
454  // tolerance to allow sloppy baseline/x-height estimates.
455  if (!unicharset.script_has_upper_lower()) {
456  tolerance = y_scale() * kSloppyTolerance;
457  }
458 
459  int min_bottom, max_bottom, min_top, max_top;
460  unicharset.get_top_bottom(unichar_id, &min_bottom, &max_bottom, &min_top, &max_top);
461 
462  // Calculate the scale factor we'll use to get to image y-pixels
463  double midx = (bbox.left() + bbox.right()) / 2.0;
464  double ydiff = (bbox.top() - bbox.bottom()) + 2.0;
465  FCOORD mid_bot(midx, bbox.bottom()), tmid_bot;
466  FCOORD mid_high(midx, bbox.bottom() + ydiff), tmid_high;
467  DenormTransform(nullptr, mid_bot, &tmid_bot);
468  DenormTransform(nullptr, mid_high, &tmid_high);
469 
470  // bln_y_measure * yscale = image_y_measure
471  double yscale = tmid_high.pt_to_pt_dist(tmid_bot) / ydiff;
472 
473  // Calculate y-shift
474  int bln_yshift = 0, bottom_shift = 0, top_shift = 0;
475  if (bottom < min_bottom - tolerance) {
476  bottom_shift = bottom - min_bottom;
477  } else if (bottom > max_bottom + tolerance) {
478  bottom_shift = bottom - max_bottom;
479  }
480  if (top < min_top - tolerance) {
481  top_shift = top - min_top;
482  } else if (top > max_top + tolerance) {
483  top_shift = top - max_top;
484  }
485  if ((top_shift >= 0 && bottom_shift > 0) || (top_shift < 0 && bottom_shift < 0)) {
486  bln_yshift = (top_shift + bottom_shift) / 2;
487  }
488  *yshift = bln_yshift * yscale;
489 
490  // To help very high cap/xheight ratio fonts accept the correct x-height,
491  // and to allow the large caps in small caps to accept the xheight of the
492  // small caps, add kBlnBaselineOffset to chars with a maximum max, and have
493  // a top already at a significantly high position.
494  if (max_top == kBlnCellHeight - 1 && top > kBlnCellHeight - kBlnBaselineOffset / 2) {
495  max_top += kBlnBaselineOffset;
496  }
497  top -= bln_yshift;
498  int height = top - kBlnBaselineOffset;
499  double min_height = min_top - kBlnBaselineOffset - tolerance;
500  double max_height = max_top - kBlnBaselineOffset + tolerance;
501 
502  // We shouldn't try calculations if the characters are very short (for example
503  // for punctuation).
504  if (min_height > kBlnXHeight / 8 && height > 0) {
505  float result = height * kBlnXHeight * yscale / min_height;
506  *max_xht = result + kFinalPixelTolerance;
507  result = height * kBlnXHeight * yscale / max_height;
508  *min_xht = result - kFinalPixelTolerance;
509  }
510 }
const int kSloppyTolerance
Definition: normalis.cpp:35
const int kBlnXHeight
Definition: normalis.h:33
const float kFinalPixelTolerance
Definition: normalis.cpp:37
const int kBlnCellHeight
Definition: normalis.h:32
const int kBlnBaselineOffset
Definition: normalis.h:34

◆ y_scale()

float tesseract::DENORM::y_scale ( ) const
inline

Definition at line 262 of file normalis.h.

262  {
263  return y_scale_;
264  }

The documentation for this class was generated from the following files: