Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tesseract::DetLineFit Class Reference

#include <detlinefit.h>

Public Member Functions

 DetLineFit ()
 
 ~DetLineFit ()
 
void Clear ()
 
void Add (const ICOORD &pt)
 
double Fit (ICOORD *pt1, ICOORD *pt2)
 
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

tesseract::DetLineFit::DetLineFit ( )

Definition at line 29 of file detlinefit.cpp.

29  {
30 }
tesseract::DetLineFit::~DetLineFit ( )

Definition at line 32 of file detlinefit.cpp.

32  {
33 }

Member Function Documentation

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

Definition at line 41 of file detlinefit.cpp.

41  {
42  ICOORDELT_IT it = &pt_list_;
43  ICOORDELT* new_pt = new ICOORDELT(pt);
44  it.add_to_end(new_pt);
45 }
void tesseract::DetLineFit::Clear ( )

Definition at line 36 of file detlinefit.cpp.

36  {
37  pt_list_.clear();
38 }
double tesseract::DetLineFit::ConstrainedFit ( double  m,
float *  c 
)

Definition at line 159 of file detlinefit.cpp.

159  {
160  ICOORDELT_IT it(&pt_list_);
161  // Do something sensible with no points.
162  if (pt_list_.empty()) {
163  *c = 0.0f;
164  return 0.0;
165  }
166  // Count the points and find the first and last kNumEndPoints.
167  // Put the ends in a single array to make their use easier later.
168  ICOORD* pts[kNumEndPoints * 2];
169  int pt_count = 0;
170  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
171  if (pt_count < kNumEndPoints) {
172  pts[pt_count] = it.data();
173  pts[kNumEndPoints + pt_count] = pts[pt_count];
174  } else {
175  for (int i = 1; i < kNumEndPoints; ++i)
176  pts[kNumEndPoints + i - 1] = pts[kNumEndPoints + i];
177  pts[kNumEndPoints * 2 - 1] = it.data();
178  }
179  ++pt_count;
180  }
181  while (pt_count < kNumEndPoints) {
182  pts[pt_count] = NULL;
183  pts[kNumEndPoints + pt_count++] = NULL;
184  }
185  int* distances = new int[pt_count];
186  double best_uq = -1.0;
187  // Iterate each pair of points and find the best fitting line.
188  for (int i = 0; i < kNumEndPoints * 2; ++i) {
189  ICOORD* start = pts[i];
190  if (start == NULL) continue;
191  ICOORD end = ComputeEndFromGradient(*start, m);
192  // Compute the upper quartile error from the line.
193  double dist = ComputeErrors(*start, end, distances);
194  if (dist < best_uq || best_uq < 0.0) {
195  best_uq = dist;
196  *c = start->y() - start->x() * m;
197  }
198  }
199  delete [] distances;
200  // Finally compute the square root to return the true distance.
201  return best_uq > 0.0 ? sqrt(best_uq) : best_uq;
202 }
inT16 x() const
access function
Definition: points.h:52
#define NULL
Definition: host.h:144
inT16 y() const
access_function
Definition: points.h:56
const int kNumEndPoints
Definition: detlinefit.cpp:27
integer coordinate
Definition: points.h:30
ICOORD ComputeEndFromGradient(const ICOORD &start, double m)
Definition: detlinefit.cpp:124
double tesseract::DetLineFit::Fit ( ICOORD pt1,
ICOORD pt2 
)

Definition at line 49 of file detlinefit.cpp.

49  {
50  ICOORDELT_IT it(&pt_list_);
51  // Do something sensible with no points.
52  if (pt_list_.empty()) {
53  pt1->set_x(0);
54  pt1->set_y(0);
55  *pt2 = *pt1;
56  return 0.0;
57  }
58  // Count the points and find the first and last kNumEndPoints.
59  ICOORD* starts[kNumEndPoints];
60  ICOORD* ends[kNumEndPoints];
61  int pt_count = 0;
62  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
63  if (pt_count < kNumEndPoints) {
64  starts[pt_count] = it.data();
65  ends[pt_count] = starts[pt_count];
66  } else {
67  for (int i = 1; i < kNumEndPoints; ++i)
68  ends[i - 1] = ends[i];
69  ends[kNumEndPoints - 1] = it.data();
70  }
71  ++pt_count;
72  }
73  // 1 or 2 points need special treatment.
74  if (pt_count <= 2) {
75  *pt1 = *starts[0];
76  if (pt_count > 1)
77  *pt2 = *starts[1];
78  else
79  *pt2 = *pt1;
80  return 0.0;
81  }
82  int end_count = MIN(pt_count, kNumEndPoints);
83  int* distances = new int[pt_count];
84  double best_uq = -1.0;
85  // Iterate each pair of points and find the best fitting line.
86  for (int i = 0; i < end_count; ++i) {
87  ICOORD* start = starts[i];
88  for (int j = 0; j < end_count; ++j) {
89  ICOORD* end = ends[j];
90  if (start != end) {
91  // Compute the upper quartile error from the line.
92  double dist = ComputeErrors(*start, *end, distances);
93  if (dist < best_uq || best_uq < 0.0) {
94  best_uq = dist;
95  *pt1 = *start;
96  *pt2 = *end;
97  }
98  }
99  }
100  }
101  delete [] distances;
102  // Finally compute the square root to return the true distance.
103  return best_uq > 0.0 ? sqrt(best_uq) : best_uq;
104 }
const int kNumEndPoints
Definition: detlinefit.cpp:27
void set_y(inT16 yin)
rewrite function
Definition: points.h:65
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
integer coordinate
Definition: points.h:30
#define MIN(x, y)
Definition: ndminx.h:28
double tesseract::DetLineFit::Fit ( float *  m,
float *  c 
)

Definition at line 109 of file detlinefit.cpp.

109  {
110  ICOORD start, end;
111  double error = Fit(&start, &end);
112  if (end.x() != start.x()) {
113  *m = static_cast<float>(end.y() - start.y()) / (end.x() - start.x());
114  *c = start.y() - *m * start.x();
115  } else {
116  *m = 0.0f;
117  *c = 0.0f;
118  }
119  return error;
120 }
inT16 x() const
access function
Definition: points.h:52
double Fit(ICOORD *pt1, ICOORD *pt2)
Definition: detlinefit.cpp:49
inT16 y() const
access_function
Definition: points.h:56
integer coordinate
Definition: points.h:30

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