tesseract  4.0.0-1-g2a2b
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 56 of file detlinefit.h.

Constructor & Destructor Documentation

◆ DetLineFit()

tesseract::DetLineFit::DetLineFit ( )

Definition at line 41 of file detlinefit.cpp.

41  : square_length_(0.0) {
42 }

◆ ~DetLineFit()

tesseract::DetLineFit::~DetLineFit ( )
default

Member Function Documentation

◆ Add() [1/2]

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

Definition at line 51 of file detlinefit.cpp.

51  {
52  pts_.push_back(PointWidth(pt, 0));
53 }
int push_back(T object)

◆ Add() [2/2]

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

Definition at line 58 of file detlinefit.cpp.

58  {
59  pts_.push_back(PointWidth(pt, halfwidth));
60 }
int push_back(T object)

◆ Clear()

void tesseract::DetLineFit::Clear ( )

Definition at line 45 of file detlinefit.cpp.

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

◆ ConstrainedFit() [1/2]

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

Definition at line 130 of file detlinefit.cpp.

132  {
133  ComputeConstrainedDistances(direction, min_dist, max_dist);
134  // Do something sensible with no points or computed distances.
135  if (pts_.empty() || distances_.empty()) {
136  line_pt->set_x(0);
137  line_pt->set_y(0);
138  return 0.0;
139  }
140  int median_index = distances_.choose_nth_item(distances_.size() / 2);
141  *line_pt = distances_[median_index].data;
142  if (debug) {
143  tprintf("Constrained fit to dir %g, %g = %d, %d :%d distances:\n",
144  direction.x(), direction.y(),
145  line_pt->x(), line_pt->y(), distances_.size());
146  for (int i = 0; i < distances_.size(); ++i) {
147  tprintf("%d: %d, %d -> %g\n", i, distances_[i].data.x(),
148  distances_[i].data.y(), distances_[i].key);
149  }
150  tprintf("Result = %d\n", median_index);
151  }
152  // Center distances on the fitted point.
153  double dist_origin = direction * *line_pt;
154  for (int i = 0; i < distances_.size(); ++i) {
155  distances_[i].key -= dist_origin;
156  }
157  return sqrt(EvaluateLineFit());
158 }
void set_x(int16_t xin)
rewrite function
Definition: points.h:62
int16_t y() const
access_function
Definition: points.h:57
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
int16_t x() const
access function
Definition: points.h:53
bool empty() const
Definition: genericvector.h:90
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
void set_y(int16_t yin)
rewrite function
Definition: points.h:66

◆ ConstrainedFit() [2/2]

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

Definition at line 185 of file detlinefit.cpp.

185  {
186  // Do something sensible with no points.
187  if (pts_.empty()) {
188  *c = 0.0f;
189  return 0.0;
190  }
191  double cos = 1.0 / sqrt(1.0 + m * m);
192  FCOORD direction(cos, m * cos);
193  ICOORD line_pt;
194  double error = ConstrainedFit(direction, -FLT_MAX, FLT_MAX, false, &line_pt);
195  *c = line_pt.y() - line_pt.x() * m;
196  return error;
197 }
int16_t y() const
access_function
Definition: points.h:57
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53
bool empty() const
Definition: genericvector.h:90
Definition: points.h:189
double ConstrainedFit(const FCOORD &direction, double min_dist, double max_dist, bool debug, ICOORD *line_pt)
Definition: detlinefit.cpp:130

◆ Fit() [1/3]

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

Definition at line 75 of file detlinefit.h.

75  {
76  return Fit(0, 0, pt1, pt2);
77  }
double Fit(ICOORD *pt1, ICOORD *pt2)
Definition: detlinefit.h:75

◆ Fit() [2/3]

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

Definition at line 65 of file detlinefit.cpp.

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

◆ Fit() [3/3]

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

Definition at line 169 of file detlinefit.cpp.

169  {
170  ICOORD start, end;
171  double error = Fit(&start, &end);
172  if (end.x() != start.x()) {
173  *m = static_cast<float>(end.y() - start.y()) / (end.x() - start.x());
174  *c = start.y() - *m * start.x();
175  } else {
176  *m = 0.0f;
177  *c = 0.0f;
178  }
179  return error;
180 }
int16_t y() const
access_function
Definition: points.h:57
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53
double Fit(ICOORD *pt1, ICOORD *pt2)
Definition: detlinefit.h:75

◆ SufficientPointsForIndependentFit()

bool tesseract::DetLineFit::SufficientPointsForIndependentFit ( ) const

Definition at line 162 of file detlinefit.cpp.

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

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