tesseract  4.00.00dev
tesseract::DetLineFit Class Reference

#include <detlinefit.h>

Public Member Functions

 DetLineFit ()
 
 ~DetLineFit ()
 
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 39 of file detlinefit.cpp.

39  : square_length_(0.0) {
40 }

◆ ~DetLineFit()

tesseract::DetLineFit::~DetLineFit ( )

Definition at line 42 of file detlinefit.cpp.

42  {
43 }

Member Function Documentation

◆ Add() [1/2]

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

Definition at line 52 of file detlinefit.cpp.

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

◆ Add() [2/2]

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

Definition at line 59 of file detlinefit.cpp.

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

◆ Clear()

void tesseract::DetLineFit::Clear ( )

Definition at line 46 of file detlinefit.cpp.

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

◆ ConstrainedFit() [1/2]

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

Definition at line 131 of file detlinefit.cpp.

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

◆ ConstrainedFit() [2/2]

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

Definition at line 186 of file detlinefit.cpp.

186  {
187  // Do something sensible with no points.
188  if (pts_.empty()) {
189  *c = 0.0f;
190  return 0.0;
191  }
192  double cos = 1.0 / sqrt(1.0 + m * m);
193  FCOORD direction(cos, m * cos);
194  ICOORD line_pt;
195  double error = ConstrainedFit(direction, -MAX_FLOAT32, MAX_FLOAT32, false,
196  &line_pt);
197  *c = line_pt.y() - line_pt.x() * m;
198  return error;
199 }
Definition: points.h:189
inT16 x() const
access function
Definition: points.h:52
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
bool empty() const
Definition: genericvector.h:90
inT16 y() const
access_function
Definition: points.h:56
#define MAX_FLOAT32
Definition: host.h:66
double ConstrainedFit(const FCOORD &direction, double min_dist, double max_dist, bool debug, ICOORD *line_pt)
Definition: detlinefit.cpp:131
integer coordinate
Definition: points.h:30

◆ 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 66 of file detlinefit.cpp.

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

◆ Fit() [3/3]

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

Definition at line 170 of file detlinefit.cpp.

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

◆ SufficientPointsForIndependentFit()

bool tesseract::DetLineFit::SufficientPointsForIndependentFit ( ) const

Definition at line 163 of file detlinefit.cpp.

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

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