tesseract  4.0.0-1-g2a2b
tesseract::BaselineBlock Class Reference

#include <baselinedetect.h>

Public Member Functions

 BaselineBlock (int debug_level, bool non_text, TO_BLOCK *block)
 
TO_BLOCKblock () const
 
double skew_angle () const
 
bool FitBaselinesAndFindSkew (bool use_box_bottoms)
 
void ParallelizeBaselines (double default_block_skew)
 
void SetupBlockParameters () const
 
void PrepareForSplineFitting (ICOORD page_tr, bool remove_noise)
 
void FitBaselineSplines (bool enable_splines, bool show_final_rows, Textord *textord)
 
void DrawFinalRows (const ICOORD &page_tr)
 
void DrawPixSpline (Pix *pix_in)
 

Static Public Member Functions

static double SpacingModelError (double perp_disp, double line_spacing, double line_offset)
 

Detailed Description

Definition at line 129 of file baselinedetect.h.

Constructor & Destructor Documentation

◆ BaselineBlock()

tesseract::BaselineBlock::BaselineBlock ( int  debug_level,
bool  non_text,
TO_BLOCK block 
)

Definition at line 411 of file baselinedetect.cpp.

412  : block_(block), debug_level_(debug_level), non_text_block_(non_text),
413  good_skew_angle_(false), skew_angle_(0.0),
414  line_spacing_(block->line_spacing), line_offset_(0.0), model_error_(0.0) {
415  TO_ROW_IT row_it(block_->get_rows());
416  for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
417  // Sort the blobs on the rows.
418  row_it.data()->blob_list()->sort(blob_x_order);
419  rows_.push_back(new BaselineRow(block->line_spacing, row_it.data()));
420  }
421 }
TO_ROW_LIST * get_rows()
Definition: blobbox.h:717
int blob_x_order(const void *item1, const void *item2)
Definition: makerow.cpp:2575
float line_spacing
Definition: blobbox.h:792
TO_BLOCK * block() const

Member Function Documentation

◆ block()

TO_BLOCK* tesseract::BaselineBlock::block ( ) const
inline

Definition at line 133 of file baselinedetect.h.

133  {
134  return block_;
135  }

◆ DrawFinalRows()

void tesseract::BaselineBlock::DrawFinalRows ( const ICOORD page_tr)

Definition at line 580 of file baselinedetect.cpp.

580  {
581 #ifndef GRAPHICS_DISABLED
582  if (non_text_block_) return;
583  double gradient = tan(skew_angle_);
584  FCOORD rotation(1.0f, 0.0f);
585  int left_edge = block_->block->pdblk.bounding_box().left();
586  ScrollView* win = create_to_win(page_tr);
588  TO_ROW_IT row_it = block_->get_rows();
589  for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
590  plot_parallel_row(row_it.data(), gradient, left_edge, colour, rotation);
591  colour = static_cast<ScrollView::Color>(colour + 1);
592  if (colour > ScrollView::MAGENTA)
593  colour = ScrollView::RED;
594  }
596  // Show discarded blobs.
597  plot_blob_list(win, &block_->underlines,
599  if (block_->blobs.length() > 0)
600  tprintf("%d blobs discarded as noise\n", block_->blobs.length());
601  draw_meanlines(block_, gradient, left_edge, ScrollView::WHITE, rotation);
602 #endif
603 }
void plot_blob_list(ScrollView *win, BLOBNBOX_LIST *list, ScrollView::Color body_colour, ScrollView::Color child_colour)
Definition: blobbox.cpp:1087
void draw_meanlines(TO_BLOCK *block, float gradient, int32_t left, ScrollView::Color colour, FCOORD rotation)
Definition: drawtord.cpp:209
TO_ROW_LIST * get_rows()
Definition: blobbox.h:717
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
void plot_parallel_row(TO_ROW *row, float gradient, int32_t left, ScrollView::Color colour, FCOORD rotation)
Definition: drawtord.cpp:124
BLOCK * block
Definition: blobbox.h:790
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:60
Definition: points.h:189
BLOBNBOX_LIST blobs
Definition: blobbox.h:785
ScrollView * create_to_win(ICOORD page_tr)
Definition: drawtord.cpp:46
BLOBNBOX_LIST underlines
Definition: blobbox.h:786
PDBLK pdblk
Definition: ocrblock.h:192

◆ DrawPixSpline()

void tesseract::BaselineBlock::DrawPixSpline ( Pix *  pix_in)

Definition at line 605 of file baselinedetect.cpp.

605  {
606  if (non_text_block_) return;
607  TO_ROW_IT row_it = block_->get_rows();
608  for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
609  row_it.data()->baseline.plot(pix_in);
610  }
611 }
TO_ROW_LIST * get_rows()
Definition: blobbox.h:717

◆ FitBaselinesAndFindSkew()

bool tesseract::BaselineBlock::FitBaselinesAndFindSkew ( bool  use_box_bottoms)

Definition at line 437 of file baselinedetect.cpp.

437  {
438  if (non_text_block_) return false;
439  GenericVector<double> angles;
440  for (int r = 0; r < rows_.size(); ++r) {
441  BaselineRow* row = rows_[r];
442  if (row->FitBaseline(use_box_bottoms)) {
443  double angle = row->BaselineAngle();
444  angles.push_back(angle);
445  }
446  if (debug_level_ > 1)
447  row->Print();
448  }
449 
450  if (!angles.empty()) {
451  skew_angle_ = MedianOfCircularValues(M_PI, &angles);
452  good_skew_angle_ = true;
453  } else {
454  skew_angle_ = 0.0f;
455  good_skew_angle_ = false;
456  }
457  if (debug_level_ > 0) {
458  tprintf("Initial block skew angle = %g, good = %d\n",
459  skew_angle_, good_skew_angle_);
460  }
461  return good_skew_angle_;
462 }
bool empty() const
Definition: genericvector.h:90
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
int push_back(T object)
T MedianOfCircularValues(T modulus, GenericVector< T > *v)
Definition: linlsq.h:113

◆ FitBaselineSplines()

void tesseract::BaselineBlock::FitBaselineSplines ( bool  enable_splines,
bool  show_final_rows,
Textord textord 
)

Definition at line 551 of file baselinedetect.cpp.

553  {
554  double gradient = tan(skew_angle_);
555  FCOORD rotation(1.0f, 0.0f);
556 
557  if (enable_splines) {
558  textord->make_spline_rows(block_, gradient, show_final_rows);
559  } else {
560  // Make a fake spline from the existing line.
561  TBOX block_box= block_->block->pdblk.bounding_box();
562  TO_ROW_IT row_it = block_->get_rows();
563  for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
564  TO_ROW* row = row_it.data();
565  int32_t xstarts[2] = { block_box.left(), block_box.right() };
566  double coeffs[3] = { 0.0, row->line_m(), row->line_c() };
567  row->baseline = QSPLINE(1, xstarts, coeffs);
568  textord->compute_row_xheight(row, block_->block->classify_rotation(),
569  row->line_m(), block_->line_size);
570  }
571  }
572  textord->compute_block_xheight(block_, gradient);
573  block_->block->set_xheight(block_->xheight);
574  if (textord_restore_underlines) // fix underlines
575  restore_underlined_blobs(block_);
576 }
QSPLINE baseline
Definition: blobbox.h:683
float line_m() const
Definition: blobbox.h:583
Definition: rect.h:34
void restore_underlined_blobs(TO_BLOCK *block)
Definition: underlin.cpp:34
TO_ROW_LIST * get_rows()
Definition: blobbox.h:717
int16_t left() const
Definition: rect.h:72
float line_c() const
Definition: blobbox.h:586
FCOORD classify_rotation() const
Definition: ocrblock.h:142
float xheight
Definition: blobbox.h:801
EXTERN bool textord_restore_underlines
Definition: underlin.cpp:26
BLOCK * block
Definition: blobbox.h:790
void set_xheight(int32_t height)
set char size
Definition: ocrblock.h:70
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:60
Definition: points.h:189
int16_t right() const
Definition: rect.h:79
PDBLK pdblk
Definition: ocrblock.h:192
float line_size
Definition: blobbox.h:798

◆ ParallelizeBaselines()

void tesseract::BaselineBlock::ParallelizeBaselines ( double  default_block_skew)

Definition at line 466 of file baselinedetect.cpp.

466  {
467  if (non_text_block_) return;
468  if (!good_skew_angle_) skew_angle_ = default_block_skew;
469  if (debug_level_ > 0)
470  tprintf("Adjusting block to skew angle %g\n", skew_angle_);
471  FCOORD direction(cos(skew_angle_), sin(skew_angle_));
472  for (int r = 0; r < rows_.size(); ++r) {
473  BaselineRow* row = rows_[r];
474  row->AdjustBaselineToParallel(debug_level_, direction);
475  if (debug_level_ > 1)
476  row->Print();
477  }
478  if (rows_.size() < 3 || !ComputeLineSpacing())
479  return;
480  // Enforce the line spacing model on all lines that don't yet have a good
481  // baseline.
482  // Start by finding the row that is best fitted to the model.
483  int best_row = 0;
484  double best_error = SpacingModelError(rows_[0]->PerpDisp(direction),
485  line_spacing_, line_offset_);
486  for (int r = 1; r < rows_.size(); ++r) {
487  double error = SpacingModelError(rows_[r]->PerpDisp(direction),
488  line_spacing_, line_offset_);
489  if (error < best_error) {
490  best_error = error;
491  best_row = r;
492  }
493  }
494  // Starting at the best fitting row, work outwards, syncing the offset.
495  double offset = line_offset_;
496  for (int r = best_row + 1; r < rows_.size(); ++r) {
497  offset = rows_[r]->AdjustBaselineToGrid(debug_level_, direction,
498  line_spacing_, offset);
499  }
500  offset = line_offset_;
501  for (int r = best_row - 1; r >= 0; --r) {
502  offset = rows_[r]->AdjustBaselineToGrid(debug_level_, direction,
503  line_spacing_, offset);
504  }
505 }
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
static double SpacingModelError(double perp_disp, double line_spacing, double line_offset)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
Definition: points.h:189

◆ PrepareForSplineFitting()

void tesseract::BaselineBlock::PrepareForSplineFitting ( ICOORD  page_tr,
bool  remove_noise 
)

Definition at line 535 of file baselinedetect.cpp.

535  {
536  if (non_text_block_) return;
537  if (remove_noise) {
538  vigorous_noise_removal(block_);
539  }
540  FCOORD rotation(1.0f, 0.0f);
541  double gradient = tan(skew_angle_);
542  separate_underlines(block_, gradient, rotation, true);
543  pre_associate_blobs(page_tr, block_, rotation, true);
544 }
void pre_associate_blobs(ICOORD page_tr, TO_BLOCK *block, FCOORD rotation, bool testing_on)
Definition: makerow.cpp:1847
void separate_underlines(TO_BLOCK *block, float gradient, FCOORD rotation, bool testing_on)
Definition: makerow.cpp:1774
Definition: points.h:189
void vigorous_noise_removal(TO_BLOCK *block)
Definition: makerow.cpp:467

◆ SetupBlockParameters()

void tesseract::BaselineBlock::SetupBlockParameters ( ) const

Definition at line 508 of file baselinedetect.cpp.

508  {
509  if (line_spacing_ > 0.0) {
510  // Where was block_line_spacing set before?
511  float min_spacing = std::min(block_->line_spacing, static_cast<float>(line_spacing_));
512  if (min_spacing < block_->line_size)
513  block_->line_size = min_spacing;
514  block_->line_spacing = line_spacing_;
515  block_->baseline_offset = line_offset_;
516  block_->max_blob_size = line_spacing_ * kMaxBlobSizeMultiple;
517  }
518  // Setup the parameters on all the rows.
519  TO_ROW_IT row_it(block_->get_rows());
520  for (int r = 0; r < rows_.size(); ++r, row_it.forward()) {
521  BaselineRow* row = rows_[r];
522  TO_ROW* to_row = row_it.data();
523  row->SetupOldLineParameters(to_row);
524  }
525 }
TO_ROW_LIST * get_rows()
Definition: blobbox.h:717
float line_spacing
Definition: blobbox.h:792
float max_blob_size
Definition: blobbox.h:799
float baseline_offset
Definition: blobbox.h:800
const double kMaxBlobSizeMultiple
float line_size
Definition: blobbox.h:798

◆ skew_angle()

double tesseract::BaselineBlock::skew_angle ( ) const
inline

Definition at line 136 of file baselinedetect.h.

136  {
137  return skew_angle_;
138  }

◆ SpacingModelError()

double tesseract::BaselineBlock::SpacingModelError ( double  perp_disp,
double  line_spacing,
double  line_offset 
)
static

Definition at line 425 of file baselinedetect.cpp.

426  {
427  // Round to the nearest multiple of line_spacing + line offset.
428  int multiple = IntCastRounded((perp_disp - line_offset) / line_spacing);
429  double model_y = line_spacing * multiple + line_offset;
430  return fabs(perp_disp - model_y);
431 }
int IntCastRounded(double x)
Definition: helpers.h:168

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