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

#include <baselinedetect.h>

Public Member Functions

 BaselineRow (double line_size, TO_ROW *to_row)
 
const TBOXbounding_box () const
 
void SetupOldLineParameters (TO_ROW *row) const
 
void Print () const
 
double BaselineAngle () const
 
double SpaceBetween (const BaselineRow &other) const
 
double PerpDisp (const FCOORD &direction) const
 
double StraightYAtX (double x) const
 
bool FitBaseline (bool use_box_bottoms)
 
void AdjustBaselineToParallel (int debug, const FCOORD &direction)
 
double AdjustBaselineToGrid (int debug, const FCOORD &direction, double line_spacing, double line_offset)
 

Detailed Description

Definition at line 40 of file baselinedetect.h.

Constructor & Destructor Documentation

◆ BaselineRow()

tesseract::BaselineRow::BaselineRow ( double  line_size,
TO_ROW to_row 
)

Definition at line 67 of file baselinedetect.cpp.

68  : blobs_(to_row->blob_list()),
69  baseline_pt1_(0.0f, 0.0f), baseline_pt2_(0.0f, 0.0f),
70  baseline_error_(0.0), good_baseline_(false) {
71  ComputeBoundingBox();
72  // Compute a scale factor for rounding to ints.
73  disp_quant_factor_ = kOffsetQuantizationFactor * line_spacing;
74  fit_halfrange_ = kFitHalfrangeFactor * line_spacing;
75  max_baseline_error_ = kMaxBaselineError * line_spacing;
76 }
const double kMaxBaselineError
const double kFitHalfrangeFactor
const double kOffsetQuantizationFactor
BLOBNBOX_LIST * blob_list()
Definition: blobbox.h:612

Member Function Documentation

◆ AdjustBaselineToGrid()

double tesseract::BaselineRow::AdjustBaselineToGrid ( int  debug,
const FCOORD direction,
double  line_spacing,
double  line_offset 
)

Definition at line 231 of file baselinedetect.cpp.

234  {
235  if (blobs_->empty()) {
236  if (debug > 1) {
237  tprintf("Row empty at:");
238  bounding_box_.print();
239  }
240  return line_offset;
241  }
242  // Find the displacement_modes_ entry nearest to the grid.
243  double best_error = 0.0;
244  int best_index = -1;
245  for (int i = 0; i < displacement_modes_.size(); ++i) {
246  double blob_y = displacement_modes_[i];
247  double error = BaselineBlock::SpacingModelError(blob_y, line_spacing,
248  line_offset);
249  if (debug > 1) {
250  tprintf("Mode at %g has error %g from model \n", blob_y, error);
251  }
252  if (best_index < 0 || error < best_error) {
253  best_error = error;
254  best_index = i;
255  }
256  }
257  // We will move the baseline only if the chosen mode is close enough to the
258  // model.
259  double model_margin = max_baseline_error_ - best_error;
260  if (best_index >= 0 && model_margin > 0.0) {
261  // But if the current baseline is already close to the mode there is no
262  // point, and only the potential to damage accuracy by changing its angle.
263  double perp_disp = PerpDisp(direction);
264  double shift = displacement_modes_[best_index] - perp_disp;
265  if (fabs(shift) > max_baseline_error_) {
266  if (debug > 1) {
267  tprintf("Attempting linespacing model fit with mode %g to row at:",
268  displacement_modes_[best_index]);
269  bounding_box_.print();
270  }
271  FitConstrainedIfBetter(debug, direction, model_margin,
272  displacement_modes_[best_index]);
273  } else if (debug > 1) {
274  tprintf("Linespacing model only moves current line by %g for row at:",
275  shift);
276  bounding_box_.print();
277  }
278  } else if (debug > 1) {
279  tprintf("Linespacing model not close enough to any mode for row at:");
280  bounding_box_.print();
281  }
282  return fmod(PerpDisp(direction), line_spacing);
283 }
int size() const
Definition: genericvector.h:71
void print() const
Definition: rect.h:278
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
double PerpDisp(const FCOORD &direction) const
static double SpacingModelError(double perp_disp, double line_spacing, double line_offset)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ AdjustBaselineToParallel()

void tesseract::BaselineRow::AdjustBaselineToParallel ( int  debug,
const FCOORD direction 
)

Definition at line 216 of file baselinedetect.cpp.

217  {
218  SetupBlobDisplacements(direction);
219  if (displacement_modes_.empty())
220  return;
221 #ifdef kDebugYCoord
222  if (bounding_box_.bottom() < kDebugYCoord &&
223  bounding_box_.top() > kDebugYCoord && debug < 3)
224  debug = 3;
225 #endif
226  FitConstrainedIfBetter(debug, direction, 0.0, displacement_modes_[0]);
227 }
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
int16_t top() const
Definition: rect.h:58
bool empty() const
Definition: genericvector.h:90
int16_t bottom() const
Definition: rect.h:65

◆ BaselineAngle()

double tesseract::BaselineRow::BaselineAngle ( ) const

Definition at line 100 of file baselinedetect.cpp.

100  {
101  FCOORD baseline_dir(baseline_pt2_ - baseline_pt1_);
102  double angle = baseline_dir.angle();
103  // Baseline directions are only unique in a range of pi so constrain to
104  // [-pi/2, pi/2].
105  return fmod(angle + M_PI * 1.5, M_PI) - M_PI * 0.5;
106 }
Definition: points.h:189

◆ bounding_box()

const TBOX& tesseract::BaselineRow::bounding_box ( ) const
inline

Definition at line 44 of file baselinedetect.h.

44  {
45  return bounding_box_;
46  }

◆ FitBaseline()

bool tesseract::BaselineRow::FitBaseline ( bool  use_box_bottoms)

Definition at line 144 of file baselinedetect.cpp.

144  {
145  // Deterministic fitting is used wherever possible.
146  fitter_.Clear();
147  // Linear least squares is a backup if the DetLineFit produces a bad line.
148  LLSQ llsq;
149  BLOBNBOX_IT blob_it(blobs_);
150 
151  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
152  BLOBNBOX* blob = blob_it.data();
153  if (!use_box_bottoms) blob->EstimateBaselinePosition();
154  const TBOX& box = blob->bounding_box();
155  int x_middle = (box.left() + box.right()) / 2;
156 #ifdef kDebugYCoord
157  if (box.bottom() < kDebugYCoord && box.top() > kDebugYCoord) {
158  tprintf("Box bottom = %d, baseline pos=%d for box at:",
159  box.bottom(), blob->baseline_position());
160  box.print();
161  }
162 #endif
163  fitter_.Add(ICOORD(x_middle, blob->baseline_position()), box.width() / 2);
164  llsq.add(x_middle, blob->baseline_position());
165  }
166  // Fit the line.
167  ICOORD pt1, pt2;
168  baseline_error_ = fitter_.Fit(&pt1, &pt2);
169  baseline_pt1_ = pt1;
170  baseline_pt2_ = pt2;
171  if (baseline_error_ > max_baseline_error_ &&
173  // The fit was bad but there were plenty of points, so try skipping
174  // the first and last few, and use the new line if it dramatically improves
175  // the error of fit.
176  double error = fitter_.Fit(kNumSkipPoints, kNumSkipPoints, &pt1, &pt2);
177  if (error < baseline_error_ / 2.0) {
178  baseline_error_ = error;
179  baseline_pt1_ = pt1;
180  baseline_pt2_ = pt2;
181  }
182  }
183  int debug = 0;
184 #ifdef kDebugYCoord
185  Print();
186  debug = bounding_box_.bottom() < kDebugYCoord &&
187  bounding_box_.top() > kDebugYCoord
188  ? 3 : 2;
189 #endif
190  // Now we obtained a direction from that fit, see if we can improve the
191  // fit using the same direction and some other start point.
192  FCOORD direction(pt2 - pt1);
193  double target_offset = direction * pt1;
194  good_baseline_ = false;
195  FitConstrainedIfBetter(debug, direction, 0.0, target_offset);
196  // Wild lines can be produced because DetLineFit allows vertical lines, but
197  // vertical text has been rotated so angles over pi/4 should be disallowed.
198  // Near vertical lines can still be produced by vertically aligned components
199  // on very short lines.
200  double angle = BaselineAngle();
201  if (fabs(angle) > M_PI * 0.25) {
202  // Use the llsq fit as a backup.
203  baseline_pt1_ = llsq.mean_point();
204  baseline_pt2_ = baseline_pt1_ + FCOORD(1.0f, llsq.m());
205  // TODO(rays) get rid of this when m and c are no longer used.
206  double m = llsq.m();
207  double c = llsq.c(m);
208  baseline_error_ = llsq.rms(m, c);
209  good_baseline_ = false;
210  }
211  return good_baseline_;
212 }
double c(double m) const
Definition: linlsq.cpp:116
void print() const
Definition: rect.h:278
Definition: rect.h:34
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
double BaselineAngle() const
int16_t width() const
Definition: rect.h:115
void add(double x, double y)
Definition: linlsq.cpp:48
bool SufficientPointsForIndependentFit() const
Definition: detlinefit.cpp:162
FCOORD mean_point() const
Definition: linlsq.cpp:166
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
void Add(const ICOORD &pt)
Definition: detlinefit.cpp:51
integer coordinate
Definition: points.h:32
void EstimateBaselinePosition()
Definition: blobbox.cpp:358
double m() const
Definition: linlsq.cpp:100
Definition: linlsq.h:28
double Fit(ICOORD *pt1, ICOORD *pt2)
Definition: detlinefit.h:75
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
double rms(double m, double c) const
Definition: linlsq.cpp:130
Definition: points.h:189
const TBOX & bounding_box() const
Definition: blobbox.h:231
int16_t right() const
Definition: rect.h:79
int baseline_position() const
Definition: blobbox.h:390
int16_t bottom() const
Definition: rect.h:65
const int kNumSkipPoints

◆ PerpDisp()

double tesseract::BaselineRow::PerpDisp ( const FCOORD direction) const

Definition at line 123 of file baselinedetect.cpp.

123  {
124  float middle_x = (bounding_box_.left() + bounding_box_.right()) / 2.0f;
125  FCOORD middle_pos(middle_x, StraightYAtX(middle_x));
126  return direction * middle_pos / direction.length();
127 }
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
int16_t left() const
Definition: rect.h:72
Definition: points.h:189
int16_t right() const
Definition: rect.h:79
double StraightYAtX(double x) const

◆ Print()

void tesseract::BaselineRow::Print ( ) const

Definition at line 89 of file baselinedetect.cpp.

89  {
90  tprintf("Baseline (%g,%g)->(%g,%g), angle=%g, intercept=%g\n",
91  baseline_pt1_.x(), baseline_pt1_.y(),
92  baseline_pt2_.x(), baseline_pt2_.y(),
93  BaselineAngle(), StraightYAtX(0.0));
94  tprintf("Quant factor=%g, error=%g, good=%d, box:",
95  disp_quant_factor_, baseline_error_, good_baseline_);
96  bounding_box_.print();
97 }
void print() const
Definition: rect.h:278
double BaselineAngle() const
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
float x() const
Definition: points.h:208
double StraightYAtX(double x) const
float y() const
Definition: points.h:211

◆ SetupOldLineParameters()

void tesseract::BaselineRow::SetupOldLineParameters ( TO_ROW row) const

Definition at line 79 of file baselinedetect.cpp.

79  {
80  // TODO(rays) get rid of this when m and c are no longer used.
81  double gradient = tan(BaselineAngle());
82  // para_c is the actual intercept of the baseline on the y-axis.
83  float para_c = StraightYAtX(0.0);
84  row->set_line(gradient, para_c, baseline_error_);
85  row->set_parallel_line(gradient, para_c, baseline_error_);
86 }
double BaselineAngle() const
void set_line(float new_m, float new_c, float new_error)
Definition: blobbox.h:616
void set_parallel_line(float gradient, float new_c, float new_error)
Definition: blobbox.h:624
double StraightYAtX(double x) const

◆ SpaceBetween()

double tesseract::BaselineRow::SpaceBetween ( const BaselineRow other) const

Definition at line 110 of file baselinedetect.cpp.

110  {
111  // Find the x-centre of overlap of the lines.
112  float x = (std::max(bounding_box_.left(), other.bounding_box_.left()) +
113  std::min(bounding_box_.right(), other.bounding_box_.right())) / 2.0f;
114  // Find the vertical centre between them.
115  float y = (StraightYAtX(x) + other.StraightYAtX(x)) / 2.0f;
116  // Find the perpendicular distance of (x,y) from each line.
117  FCOORD pt(x, y);
118  return PerpDistanceFromBaseline(pt) + other.PerpDistanceFromBaseline(pt);
119 }
int16_t left() const
Definition: rect.h:72
Definition: points.h:189
int16_t right() const
Definition: rect.h:79
double StraightYAtX(double x) const

◆ StraightYAtX()

double tesseract::BaselineRow::StraightYAtX ( double  x) const

Definition at line 131 of file baselinedetect.cpp.

131  {
132  double denominator = baseline_pt2_.x() - baseline_pt1_.x();
133  if (denominator == 0.0)
134  return (baseline_pt1_.y() + baseline_pt2_.y()) / 2.0;
135  return baseline_pt1_.y() +
136  (x - baseline_pt1_.x()) * (baseline_pt2_.y() - baseline_pt1_.y()) /
137  denominator;
138 }
float x() const
Definition: points.h:208
float y() const
Definition: points.h:211

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