tesseract v5.3.3.20231005
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 x_map_ = nullptr;
46 y_map_ = nullptr;
47 *this = src;
48}

◆ ~DENORM()

tesseract::DENORM::~DENORM ( )

Definition at line 70 of file normalis.cpp.

70 {
71 Clear();
72}

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 408 of file normalis.cpp.

408 {
409 LocalDenormTransform(pt, original);
410 if (last_denorm != this) {
411 if (predecessor_ != nullptr) {
412 predecessor_->DenormTransform(last_denorm, *original, original);
413 } else if (block_ != nullptr) {
414 original->rotate(block_->re_rotation());
415 }
416 }
417}
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:401
void LocalDenormTransform(const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:362
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 401 of file normalis.cpp.

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

◆ 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 370 of file normalis.cpp.

370 {
371 FCOORD rotated(pt.x() - final_xshift_, pt.y() - final_yshift_);
372 if (x_map_ != nullptr && y_map_ != nullptr) {
373 auto pos = std::upper_bound(x_map_->begin(), x_map_->end(), rotated.x());
374 if (pos > x_map_->begin()) {
375 --pos;
376 }
377 auto x = pos - x_map_->begin();
378 original->set_x(x + x_origin_);
379 pos = std::upper_bound(y_map_->begin(), y_map_->end(), rotated.y());
380 if (pos > y_map_->begin()) {
381 --pos;
382 }
383 auto y = pos - y_map_->begin();
384 original->set_y(y + y_origin_);
385 } else {
386 if (rotation_ != nullptr) {
387 FCOORD inverse_rotation(rotation_->x(), -rotation_->y());
388 rotated.rotate(inverse_rotation);
389 }
390 original->set_x(rotated.x() / x_scale_ + x_origin_);
391 float y_scale = y_scale_;
392 original->set_y(rotated.y() / y_scale + y_origin_);
393 }
394}
const double y
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 362 of file normalis.cpp.

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

◆ LocalNormBlob()

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

Definition at line 421 of file normalis.cpp.

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

◆ LocalNormTransform() [1/2]

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

Definition at line 317 of file normalis.cpp.

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

◆ LocalNormTransform() [2/2]

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

Definition at line 310 of file normalis.cpp.

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

◆ NormTransform() [1/2]

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

Definition at line 347 of file normalis.cpp.

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

◆ NormTransform() [2/2]

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

Definition at line 340 of file normalis.cpp.

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

◆ operator=()

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

Definition at line 50 of file normalis.cpp.

50 {
51 Clear();
52 inverse_ = src.inverse_;
53 predecessor_ = src.predecessor_;
54 pix_ = src.pix_;
55 block_ = src.block_;
56 if (src.rotation_ == nullptr) {
57 rotation_ = nullptr;
58 } else {
59 rotation_ = new FCOORD(*src.rotation_);
60 }
61 x_origin_ = src.x_origin_;
62 y_origin_ = src.y_origin_;
63 x_scale_ = src.x_scale_;
64 y_scale_ = src.y_scale_;
65 final_xshift_ = src.final_xshift_;
66 final_yshift_ = src.final_yshift_;
67 return *this;
68}
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 515 of file normalis.cpp.

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

◆ 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 }
int value

◆ 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 273 of file normalis.cpp.

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

102 {
103 Clear();
104 block_ = block;
105 if (rotation == nullptr) {
106 rotation_ = nullptr;
107 } else {
108 rotation_ = new FCOORD(*rotation);
109 }
110 predecessor_ = predecessor;
111 x_origin_ = x_origin;
112 y_origin_ = y_origin;
113 x_scale_ = x_scale;
114 y_scale_ = y_scale;
115 final_xshift_ = final_xshift;
116 final_yshift_ = final_yshift;
117}
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 439 of file normalis.cpp.

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