tesseract  5.0.0
tesseract::DetLineFit Class Reference

#include <detlinefit.h>

Public Member Functions

 DetLineFit ()
 
 ~DetLineFit ()=default
 
void Clear ()
 
void Add (const ICOORD &pt)
 
void Add (const ICOORD &pt, int halfwidth)
 
double Fit (ICOORD *pt1, ICOORD *pt2)
 
double Fit (int skip_first, int skip_last, ICOORD *pt1, ICOORD *pt2)
 
double ConstrainedFit (const FCOORD &direction, double min_dist, double max_dist, bool debug, ICOORD *line_pt)
 
bool SufficientPointsForIndependentFit () const
 
double Fit (float *m, float *c)
 
double ConstrainedFit (double m, float *c)
 

Detailed Description

Definition at line 54 of file detlinefit.h.

Constructor & Destructor Documentation

◆ DetLineFit()

tesseract::DetLineFit::DetLineFit ( )

Definition at line 41 of file detlinefit.cpp.

41 : square_length_(0.0) {}

◆ ~DetLineFit()

tesseract::DetLineFit::~DetLineFit ( )
default

Member Function Documentation

◆ Add() [1/2]

void tesseract::DetLineFit::Add ( const ICOORD pt)

Definition at line 50 of file detlinefit.cpp.

50  {
51  pts_.emplace_back(pt, 0);
52 }

◆ Add() [2/2]

void tesseract::DetLineFit::Add ( const ICOORD pt,
int  halfwidth 
)

Definition at line 57 of file detlinefit.cpp.

57  {
58  pts_.emplace_back(pt, halfwidth);
59 }

◆ Clear()

void tesseract::DetLineFit::Clear ( )

Definition at line 44 of file detlinefit.cpp.

44  {
45  pts_.clear();
46  distances_.clear();
47 }

◆ ConstrainedFit() [1/2]

double tesseract::DetLineFit::ConstrainedFit ( const FCOORD direction,
double  min_dist,
double  max_dist,
bool  debug,
ICOORD line_pt 
)

Definition at line 133 of file detlinefit.cpp.

134  {
135  ComputeConstrainedDistances(direction, min_dist, max_dist);
136  // Do something sensible with no points or computed distances.
137  if (pts_.empty() || distances_.empty()) {
138  line_pt->set_x(0);
139  line_pt->set_y(0);
140  return 0.0;
141  }
142  auto median_index = distances_.size() / 2;
143  std::nth_element(distances_.begin(), distances_.begin() + median_index, distances_.end());
144  *line_pt = distances_[median_index].data();
145  if (debug) {
146  tprintf("Constrained fit to dir %g, %g = %d, %d :%zu distances:\n", direction.x(), direction.y(),
147  line_pt->x(), line_pt->y(), distances_.size());
148  for (unsigned i = 0; i < distances_.size(); ++i) {
149  tprintf("%d: %d, %d -> %g\n", i, distances_[i].data().x(), distances_[i].data().y(),
150  distances_[i].key());
151  }
152  tprintf("Result = %zu\n", median_index);
153  }
154  // Center distances on the fitted point.
155  double dist_origin = direction * *line_pt;
156  for (auto &distance : distances_) {
157  distance.key() -= dist_origin;
158  }
159  return sqrt(EvaluateLineFit());
160 }
UnicodeText::const_iterator::difference_type distance(const UnicodeText::const_iterator &first, const UnicodeText::const_iterator &last)
Definition: unicodetext.cc:44
void tprintf(const char *format,...)
Definition: tprintf.cpp:41

◆ ConstrainedFit() [2/2]

double tesseract::DetLineFit::ConstrainedFit ( double  m,
float *  c 
)

Definition at line 187 of file detlinefit.cpp.

187  {
188  // Do something sensible with no points.
189  if (pts_.empty()) {
190  *c = 0.0f;
191  return 0.0;
192  }
193  double cos = 1.0 / sqrt(1.0 + m * m);
194  FCOORD direction(cos, m * cos);
195  ICOORD line_pt;
196  double error = ConstrainedFit(direction, -FLT_MAX, FLT_MAX, false, &line_pt);
197  *c = line_pt.y() - line_pt.x() * m;
198  return error;
199 }
double ConstrainedFit(const FCOORD &direction, double min_dist, double max_dist, bool debug, ICOORD *line_pt)
Definition: detlinefit.cpp:133

◆ Fit() [1/3]

double tesseract::DetLineFit::Fit ( float *  m,
float *  c 
)

Definition at line 171 of file detlinefit.cpp.

171  {
172  ICOORD start, end;
173  double error = Fit(&start, &end);
174  if (end.x() != start.x()) {
175  *m = static_cast<float>(end.y() - start.y()) / (end.x() - start.x());
176  *c = start.y() - *m * start.x();
177  } else {
178  *m = 0.0f;
179  *c = 0.0f;
180  }
181  return error;
182 }
double Fit(ICOORD *pt1, ICOORD *pt2)
Definition: detlinefit.h:73

◆ Fit() [2/3]

double tesseract::DetLineFit::Fit ( ICOORD pt1,
ICOORD pt2 
)
inline

Definition at line 73 of file detlinefit.h.

73  {
74  return Fit(0, 0, pt1, pt2);
75  }

◆ Fit() [3/3]

double tesseract::DetLineFit::Fit ( int  skip_first,
int  skip_last,
ICOORD pt1,
ICOORD pt2 
)

Definition at line 64 of file detlinefit.cpp.

64  {
65  // Do something sensible with no points.
66  if (pts_.empty()) {
67  pt1->set_x(0);
68  pt1->set_y(0);
69  *pt2 = *pt1;
70  return 0.0;
71  }
72  // Count the points and find the first and last kNumEndPoints.
73  int pt_count = pts_.size();
74  ICOORD *starts[kNumEndPoints];
75  if (skip_first >= pt_count) {
76  skip_first = pt_count - 1;
77  }
78  int start_count = 0;
79  int end_i = std::min(skip_first + kNumEndPoints, pt_count);
80  for (int i = skip_first; i < end_i; ++i) {
81  starts[start_count++] = &pts_[i].pt;
82  }
83  ICOORD *ends[kNumEndPoints];
84  if (skip_last >= pt_count) {
85  skip_last = pt_count - 1;
86  }
87  int end_count = 0;
88  end_i = std::max(0, pt_count - kNumEndPoints - skip_last);
89  for (int i = pt_count - 1 - skip_last; i >= end_i; --i) {
90  ends[end_count++] = &pts_[i].pt;
91  }
92  // 1 or 2 points need special treatment.
93  if (pt_count <= 2) {
94  *pt1 = *starts[0];
95  if (pt_count > 1) {
96  *pt2 = *ends[0];
97  } else {
98  *pt2 = *pt1;
99  }
100  return 0.0;
101  }
102  // Although with between 2 and 2*kNumEndPoints-1 points, there will be
103  // overlap in the starts, ends sets, this is OK and taken care of by the
104  // if (*start != *end) test below, which also tests for equal input points.
105  double best_uq = -1.0;
106  // Iterate each pair of points and find the best fitting line.
107  for (int i = 0; i < start_count; ++i) {
108  ICOORD *start = starts[i];
109  for (int j = 0; j < end_count; ++j) {
110  ICOORD *end = ends[j];
111  if (*start != *end) {
112  ComputeDistances(*start, *end);
113  // Compute the upper quartile error from the line.
114  double dist = EvaluateLineFit();
115  if (dist < best_uq || best_uq < 0.0) {
116  best_uq = dist;
117  *pt1 = *start;
118  *pt2 = *end;
119  }
120  }
121  }
122  }
123  // Finally compute the square root to return the true distance.
124  return best_uq > 0.0 ? sqrt(best_uq) : best_uq;
125 }
const int kNumEndPoints
Definition: detlinefit.cpp:30

◆ SufficientPointsForIndependentFit()

bool tesseract::DetLineFit::SufficientPointsForIndependentFit ( ) const

Definition at line 164 of file detlinefit.cpp.

164  {
165  return distances_.size() >= kMinPointsForErrorCount;
166 }
const int kMinPointsForErrorCount
Definition: detlinefit.cpp:36

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