All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::TextlineProjection Class Reference

#include <textlineprojection.h>

Public Member Functions

 TextlineProjection (int resolution)
 
 ~TextlineProjection ()
 
void ConstructProjection (TO_BLOCK *input_block, const FCOORD &rotation, Pix *nontext_map)
 
void PlotGradedBlobs (BLOBNBOX_LIST *blobs, ScrollView *win)
 
void MoveNonTextlineBlobs (BLOBNBOX_LIST *blobs, BLOBNBOX_LIST *small_blobs) const
 
void DisplayProjection () const
 
int DistanceOfBoxFromPartition (const TBOX &box, const ColPartition &part, const DENORM *denorm, bool debug) const
 
int DistanceOfBoxFromBox (const TBOX &from_box, const TBOX &to_box, bool horizontal_textline, const DENORM *denorm, bool debug) const
 
int VerticalDistance (bool debug, int x, int y1, int y2) const
 
int HorizontalDistance (bool debug, int x1, int x2, int y) const
 
bool BoxOutOfHTextline (const TBOX &box, const DENORM *denorm, bool debug) const
 
int EvaluateColPartition (const ColPartition &part, const DENORM *denorm, bool debug) const
 
int EvaluateBox (const TBOX &box, const DENORM *denorm, bool debug) const
 

Detailed Description

Definition at line 33 of file textlineprojection.h.

Constructor & Destructor Documentation

tesseract::TextlineProjection::TextlineProjection ( int  resolution)
explicit

Definition at line 45 of file textlineprojection.cpp.

46  : x_origin_(0), y_origin_(0), pix_(NULL) {
47  // The projection map should be about 100 ppi, whatever the input.
48  scale_factor_ = IntCastRounded(resolution / 100.0);
49  if (scale_factor_ < 1) scale_factor_ = 1;
50 }
int IntCastRounded(double x)
Definition: helpers.h:172
#define NULL
Definition: host.h:144
tesseract::TextlineProjection::~TextlineProjection ( )

Definition at line 51 of file textlineprojection.cpp.

51  {
52  pixDestroy(&pix_);
53 }

Member Function Documentation

bool tesseract::TextlineProjection::BoxOutOfHTextline ( const TBOX box,
const DENORM denorm,
bool  debug 
) const

Definition at line 342 of file textlineprojection.cpp.

344  {
345  int grad1 = 0;
346  int grad2 = 0;
347  EvaluateBoxInternal(box, denorm, debug, &grad1, &grad2, NULL, NULL);
348  int worst_result = MIN(grad1, grad2);
349  int total_result = grad1 + grad2;
350  if (total_result >= 6) return false; // Strongly in textline.
351  // Medium strength: if either gradient is negative, it is likely outside
352  // the body of the textline.
353  if (worst_result < 0)
354  return true;
355  return false;
356 }
#define MIN(x, y)
Definition: ndminx.h:28
#define NULL
Definition: host.h:144
void tesseract::TextlineProjection::ConstructProjection ( TO_BLOCK input_block,
const FCOORD rotation,
Pix *  nontext_map 
)

Definition at line 62 of file textlineprojection.cpp.

64  {
65  pixDestroy(&pix_);
66  TBOX image_box(0, 0, pixGetWidth(nontext_map), pixGetHeight(nontext_map));
67  x_origin_ = 0;
68  y_origin_ = image_box.height();
69  int width = (image_box.width() + scale_factor_ - 1) / scale_factor_;
70  int height = (image_box.height() + scale_factor_ - 1) / scale_factor_;
71 
72  pix_ = pixCreate(width, height, 8);
73  ProjectBlobs(&input_block->blobs, rotation, image_box, nontext_map);
74  ProjectBlobs(&input_block->large_blobs, rotation, image_box, nontext_map);
75  Pix* final_pix = pixBlockconv(pix_, 1, 1);
76 // Pix* final_pix = pixBlockconv(pix_, 2, 2);
77  pixDestroy(&pix_);
78  pix_ = final_pix;
79 }
Definition: rect.h:30
BLOBNBOX_LIST large_blobs
Definition: blobbox.h:772
BLOBNBOX_LIST blobs
Definition: blobbox.h:768
void tesseract::TextlineProjection::DisplayProjection ( ) const

Definition at line 121 of file textlineprojection.cpp.

121  {
122  int width = pixGetWidth(pix_);
123  int height = pixGetHeight(pix_);
124  Pix* pixc = pixCreate(width, height, 32);
125  int src_wpl = pixGetWpl(pix_);
126  int col_wpl = pixGetWpl(pixc);
127  uinT32* src_data = pixGetData(pix_);
128  uinT32* col_data = pixGetData(pixc);
129  for (int y = 0; y < height; ++y, src_data += src_wpl, col_data += col_wpl) {
130  for (int x = 0; x < width; ++x) {
131  int pixel = GET_DATA_BYTE(src_data, x);
132  l_uint32 result;
133  if (pixel <= 17)
134  composeRGBPixel(0, 0, pixel * 15, &result);
135  else if (pixel <= 145)
136  composeRGBPixel(0, (pixel - 17) * 2, 255, &result);
137  else
138  composeRGBPixel((pixel - 145) * 2, 255, 255, &result);
139  col_data[x] = result;
140  }
141  }
142 #if 0
143  // TODO(rays) uncomment when scrollview can display non-binary images.
144  ScrollView* win = new ScrollView("Projection", 0, 0,
145  width, height, width, height);
146  win->Image(pixc, 0, 0);
147  win->Update();
148 #else
149  pixWrite("projection.png", pixc, IFF_PNG);
150 #endif
151  pixDestroy(&pixc);
152 }
static void Update()
Definition: scrollview.cpp:715
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:773
unsigned int uinT32
Definition: host.h:103
int tesseract::TextlineProjection::DistanceOfBoxFromBox ( const TBOX from_box,
const TBOX to_box,
bool  horizontal_textline,
const DENORM denorm,
bool  debug 
) const

Definition at line 198 of file textlineprojection.cpp.

202  {
203  // The parallel_gap is the horizontal gap between a horizontal textline and
204  // the box. Analogous for vertical.
205  int parallel_gap = 0;
206  // start_pt is the box end of the line to be modified for curved space.
207  TPOINT start_pt;
208  // end_pt is the partition end of the line to be modified for curved space.
209  TPOINT end_pt;
210  if (horizontal_textline) {
211  parallel_gap = from_box.x_gap(to_box) + from_box.width();
212  start_pt.x = (from_box.left() + from_box.right()) / 2;
213  end_pt.x = start_pt.x;
214  if (from_box.top() - to_box.top() >= to_box.bottom() - from_box.bottom()) {
215  start_pt.y = from_box.top();
216  end_pt.y = MIN(to_box.top(), start_pt.y);
217  } else {
218  start_pt.y = from_box.bottom();
219  end_pt.y = MAX(to_box.bottom(), start_pt.y);
220  }
221  } else {
222  parallel_gap = from_box.y_gap(to_box) + from_box.height();
223  if (from_box.right() - to_box.right() >= to_box.left() - from_box.left()) {
224  start_pt.x = from_box.right();
225  end_pt.x = MIN(to_box.right(), start_pt.x);
226  } else {
227  start_pt.x = from_box.left();
228  end_pt.x = MAX(to_box.left(), start_pt.x);
229  }
230  start_pt.y = (from_box.bottom() + from_box.top()) / 2;
231  end_pt.y = start_pt.y;
232  }
233  // The perpendicular gap is the max vertical distance gap out of:
234  // top of from_box to to_box top and bottom of from_box to to_box bottom.
235  // This value is then modified for curved projection space.
236  // Analogous for vertical.
237  int perpendicular_gap = 0;
238  // If start_pt == end_pt, then the from_box lies entirely within the to_box
239  // (in the perpendicular direction), so we don't need to calculate the
240  // perpendicular_gap.
241  if (start_pt.x != end_pt.x || start_pt.y != end_pt.y) {
242  if (denorm != NULL) {
243  // Denormalize the start and end.
244  denorm->DenormTransform(NULL, start_pt, &start_pt);
245  denorm->DenormTransform(NULL, end_pt, &end_pt);
246  }
247  if (abs(start_pt.y - end_pt.y) >= abs(start_pt.x - end_pt.x)) {
248  perpendicular_gap = VerticalDistance(debug, start_pt.x, start_pt.y,
249  end_pt.y);
250  } else {
251  perpendicular_gap = HorizontalDistance(debug, start_pt.x, end_pt.x,
252  start_pt.y);
253  }
254  }
255  // The parallel_gap weighs less than the perpendicular_gap.
256  return perpendicular_gap + parallel_gap / kParaPerpDistRatio;
257 }
#define MAX(x, y)
Definition: ndminx.h:24
const int kParaPerpDistRatio
#define MIN(x, y)
Definition: ndminx.h:28
int VerticalDistance(bool debug, int x, int y1, int y2) const
inT16 y
Definition: blobs.h:72
void DenormTransform(const DENORM *last_denorm, const TPOINT &pt, TPOINT *original) const
Definition: normalis.cpp:389
inT16 right() const
Definition: rect.h:75
inT16 left() const
Definition: rect.h:68
int y_gap(const TBOX &box) const
Definition: rect.h:225
Definition: blobs.h:50
inT16 x
Definition: blobs.h:71
inT16 bottom() const
Definition: rect.h:61
inT16 height() const
Definition: rect.h:104
inT16 width() const
Definition: rect.h:111
int x_gap(const TBOX &box) const
Definition: rect.h:217
int HorizontalDistance(bool debug, int x1, int x2, int y) const
#define NULL
Definition: host.h:144
inT16 top() const
Definition: rect.h:54
int tesseract::TextlineProjection::DistanceOfBoxFromPartition ( const TBOX box,
const ColPartition part,
const DENORM denorm,
bool  debug 
) const

Definition at line 158 of file textlineprojection.cpp.

161  {
162  // Compute a partition box that uses the median top/bottom of the blobs
163  // within and median left/right for vertical.
164  TBOX part_box = part.bounding_box();
165  if (part.IsHorizontalType()) {
166  part_box.set_top(part.median_top());
167  part_box.set_bottom(part.median_bottom());
168  } else {
169  part_box.set_left(part.median_left());
170  part_box.set_right(part.median_right());
171  }
172  // Now use DistanceOfBoxFromBox to make the actual calculation.
173  return DistanceOfBoxFromBox(box, part_box, part.IsHorizontalType(),
174  denorm, debug);
175 }
void set_right(int x)
Definition: rect.h:78
void set_left(int x)
Definition: rect.h:71
int DistanceOfBoxFromBox(const TBOX &from_box, const TBOX &to_box, bool horizontal_textline, const DENORM *denorm, bool debug) const
void set_bottom(int y)
Definition: rect.h:64
Definition: rect.h:30
void set_top(int y)
Definition: rect.h:57
int tesseract::TextlineProjection::EvaluateBox ( const TBOX box,
const DENORM denorm,
bool  debug 
) const

Definition at line 414 of file textlineprojection.cpp.

415  {
416  return EvaluateBoxInternal(box, denorm, debug, NULL, NULL, NULL, NULL);
417 }
#define NULL
Definition: host.h:144
int tesseract::TextlineProjection::EvaluateColPartition ( const ColPartition part,
const DENORM denorm,
bool  debug 
) const

Definition at line 363 of file textlineprojection.cpp.

365  {
366  if (part.IsSingleton())
367  return EvaluateBox(part.bounding_box(), denorm, debug);
368  // Test vertical orientation.
369  TBOX box = part.bounding_box();
370  // Use the partition median for left/right.
371  box.set_left(part.median_left());
372  box.set_right(part.median_right());
373  int vresult = EvaluateBox(box, denorm, debug);
374 
375  // Test horizontal orientation.
376  box = part.bounding_box();
377  // Use the partition median for top/bottom.
378  box.set_top(part.median_top());
379  box.set_bottom(part.median_bottom());
380  int hresult = EvaluateBox(box, denorm, debug);
381  if (debug) {
382  tprintf("Partition hresult=%d, vresult=%d from:", hresult, vresult);
383  part.bounding_box().print();
384  part.Print();
385  }
386  return hresult >= -vresult ? hresult : vresult;
387 }
#define tprintf(...)
Definition: tprintf.h:31
void set_right(int x)
Definition: rect.h:78
void set_left(int x)
Definition: rect.h:71
void set_bottom(int y)
Definition: rect.h:64
Definition: rect.h:30
void set_top(int y)
Definition: rect.h:57
int EvaluateBox(const TBOX &box, const DENORM *denorm, bool debug) const
int tesseract::TextlineProjection::HorizontalDistance ( bool  debug,
int  x1,
int  x2,
int  y 
) const

Definition at line 310 of file textlineprojection.cpp.

311  {
312  x1 = ImageXToProjectionX(x1);
313  x2 = ImageXToProjectionX(x2);
314  y = ImageYToProjectionY(y);
315  if (x1 == x2) return 0;
316  int wpl = pixGetWpl(pix_);
317  int step = x1 < x2 ? 1 : -1;
318  uinT32* data = pixGetData(pix_) + y * wpl;
319  int prev_pixel = GET_DATA_BYTE(data, x1);
320  int distance = 0;
321  int right_way_steps = 0;
322  for (int x = x1; x != x2; x += step) {
323  int pixel = GET_DATA_BYTE(data, x + step);
324  if (debug)
325  tprintf("At (%d,%d), pix = %d, prev=%d\n",
326  x + step, y, pixel, prev_pixel);
327  if (pixel < prev_pixel)
328  distance += kWrongWayPenalty;
329  else if (pixel > prev_pixel)
330  ++right_way_steps;
331  else
332  ++distance;
333  prev_pixel = pixel;
334  }
335  return distance * scale_factor_ +
336  right_way_steps * scale_factor_ / kWrongWayPenalty;
337 }
const int kWrongWayPenalty
#define tprintf(...)
Definition: tprintf.h:31
unsigned int uinT32
Definition: host.h:103
void tesseract::TextlineProjection::MoveNonTextlineBlobs ( BLOBNBOX_LIST *  blobs,
BLOBNBOX_LIST *  small_blobs 
) const

Definition at line 104 of file textlineprojection.cpp.

105  {
106  BLOBNBOX_IT it(blobs);
107  BLOBNBOX_IT small_it(small_blobs);
108  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
109  BLOBNBOX* blob = it.data();
110  const TBOX& box = blob->bounding_box();
111  bool debug = AlignedBlob::WithinTestRegion(2, box.left(),
112  box.bottom());
113  if (BoxOutOfHTextline(box, NULL, debug) && !blob->UniquelyVertical()) {
114  blob->ClearNeighbours();
115  small_it.add_to_end(it.extract());
116  }
117  }
118 }
static bool WithinTestRegion(int detail_level, int x, int y)
void ClearNeighbours()
Definition: blobbox.h:494
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
bool BoxOutOfHTextline(const TBOX &box, const DENORM *denorm, bool debug) const
Definition: rect.h:30
bool UniquelyVertical() const
Definition: blobbox.h:395
#define NULL
Definition: host.h:144
const TBOX & bounding_box() const
Definition: blobbox.h:215
void tesseract::TextlineProjection::PlotGradedBlobs ( BLOBNBOX_LIST *  blobs,
ScrollView win 
)

Definition at line 82 of file textlineprojection.cpp.

83  {
84 #ifndef GRAPHICS_DISABLED
85  BLOBNBOX_IT it(blobs);
86  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
87  BLOBNBOX* blob = it.data();
88  const TBOX& box = blob->bounding_box();
89  bool bad_box = BoxOutOfHTextline(box, NULL, false);
90  if (blob->UniquelyVertical())
91  win->Pen(ScrollView::YELLOW);
92  else
93  win->Pen(bad_box ? ScrollView::RED : ScrollView::BLUE);
94  win->Rectangle(box.left(), box.bottom(), box.right(), box.top());
95  }
96  win->Update();
97 #endif // GRAPHICS_DISABLED
98 }
void Pen(Color color)
Definition: scrollview.cpp:726
static void Update()
Definition: scrollview.cpp:715
inT16 right() const
Definition: rect.h:75
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
bool BoxOutOfHTextline(const TBOX &box, const DENORM *denorm, bool debug) const
Definition: rect.h:30
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:606
bool UniquelyVertical() const
Definition: blobbox.h:395
#define NULL
Definition: host.h:144
const TBOX & bounding_box() const
Definition: blobbox.h:215
inT16 top() const
Definition: rect.h:54
int tesseract::TextlineProjection::VerticalDistance ( bool  debug,
int  x,
int  y1,
int  y2 
) const

Definition at line 277 of file textlineprojection.cpp.

278  {
279  x = ImageXToProjectionX(x);
280  y1 = ImageYToProjectionY(y1);
281  y2 = ImageYToProjectionY(y2);
282  if (y1 == y2) return 0;
283  int wpl = pixGetWpl(pix_);
284  int step = y1 < y2 ? 1 : -1;
285  uinT32* data = pixGetData(pix_) + y1 * wpl;
286  wpl *= step;
287  int prev_pixel = GET_DATA_BYTE(data, x);
288  int distance = 0;
289  int right_way_steps = 0;
290  for (int y = y1; y != y2; y += step) {
291  data += wpl;
292  int pixel = GET_DATA_BYTE(data, x);
293  if (debug)
294  tprintf("At (%d,%d), pix = %d, prev=%d\n",
295  x, y + step, pixel, prev_pixel);
296  if (pixel < prev_pixel)
297  distance += kWrongWayPenalty;
298  else if (pixel > prev_pixel)
299  ++right_way_steps;
300  else
301  ++distance;
302  prev_pixel = pixel;
303  }
304  return distance * scale_factor_ +
305  right_way_steps * scale_factor_ / kWrongWayPenalty;
306 }
const int kWrongWayPenalty
#define tprintf(...)
Definition: tprintf.h:31
unsigned int uinT32
Definition: host.h:103

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