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

#include <strokewidth.h>

Inheritance diagram for tesseract::StrokeWidth:
tesseract::BlobGrid tesseract::BBGrid< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT > tesseract::GridBase

Public Member Functions

 StrokeWidth (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~StrokeWidth ()
 
void SetNeighboursOnMediumBlobs (TO_BLOCK *block)
 
void FindTextlineDirectionAndFixBrokenCJK (PageSegMode pageseg_mode, bool cjk_merge, TO_BLOCK *input_block)
 
bool TestVerticalTextDirection (double find_vertical_text_ratio, TO_BLOCK *block, BLOBNBOX_CLIST *osd_blobs)
 
void CorrectForRotation (const FCOORD &rerotation, ColPartitionGrid *part_grid)
 
void FindLeaderPartitions (TO_BLOCK *block, ColPartitionGrid *part_grid)
 
void RemoveLineResidue (ColPartition_LIST *big_part_list)
 
void GradeBlobsIntoPartitions (PageSegMode pageseg_mode, const FCOORD &rerotation, TO_BLOCK *block, Pix *nontext_pix, const DENORM *denorm, bool cjk_script, TextlineProjection *projection, BLOBNBOX_LIST *diacritic_blobs, ColPartitionGrid *part_grid, ColPartition_LIST *big_parts)
 
virtual void HandleClick (int x, int y)
 
- Public Member Functions inherited from tesseract::BlobGrid
 BlobGrid (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~BlobGrid ()
 
void InsertBlobList (BLOBNBOX_LIST *blobs)
 
- Public Member Functions inherited from tesseract::BBGrid< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT >
 BBGrid ()
 
 BBGrid (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~BBGrid ()
 
void Init (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
void Clear ()
 
void ClearGridData (void(*free_method)(BLOBNBOX *))
 
void InsertBBox (bool h_spread, bool v_spread, BLOBNBOX *bbox)
 
void InsertPixPtBBox (int left, int bottom, Pix *pix, BLOBNBOX *bbox)
 
void RemoveBBox (BLOBNBOX *bbox)
 
bool RectangleEmpty (const TBOX &rect)
 
IntGridCountCellElements ()
 
ScrollViewMakeWindow (int x, int y, const char *window_name)
 
void DisplayBoxes (ScrollView *window)
 
void AssertNoDuplicates ()
 
- Public Member Functions inherited from tesseract::GridBase
 GridBase ()
 
 GridBase (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~GridBase ()
 
void Init (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
int gridsize () const
 
int gridwidth () const
 
int gridheight () const
 
const ICOORDbleft () const
 
const ICOORDtright () const
 
void GridCoords (int x, int y, int *grid_x, int *grid_y) const
 
void ClipGridCoords (int *x, int *y) const
 

Additional Inherited Members

- Protected Attributes inherited from tesseract::BBGrid< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT >
BLOBNBOX_CLIST * grid_
 
- Protected Attributes inherited from tesseract::GridBase
int gridsize_
 
int gridwidth_
 
int gridheight_
 
int gridbuckets_
 
ICOORD bleft_
 
ICOORD tright_
 

Detailed Description

The StrokeWidth class holds all the normal and large blobs. It is used to find good large blobs and move them to the normal blobs by virtue of having a reasonable strokewidth compatible neighbour.

Definition at line 57 of file strokewidth.h.

Constructor & Destructor Documentation

tesseract::StrokeWidth::StrokeWidth ( int  gridsize,
const ICOORD bleft,
const ICOORD tright 
)

Definition at line 120 of file strokewidth.cpp.

122  : BlobGrid(gridsize, bleft, tright), nontext_map_(NULL), projection_(NULL),
123  denorm_(NULL), grid_box_(bleft, tright), rerotation_(1.0f, 0.0f) {
124  leaders_win_ = NULL;
125  widths_win_ = NULL;
126  initial_widths_win_ = NULL;
127  chains_win_ = NULL;
128  diacritics_win_ = NULL;
129  textlines_win_ = NULL;
130  smoothed_win_ = NULL;
131 }
int gridsize() const
Definition: bbgrid.h:63
#define NULL
Definition: host.h:144
BlobGrid(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: blobgrid.cpp:24
tesseract::StrokeWidth::~StrokeWidth ( )
virtual

Definition at line 133 of file strokewidth.cpp.

133  {
134  if (widths_win_ != NULL) {
135  #ifndef GRAPHICS_DISABLED
136  delete widths_win_->AwaitEvent(SVET_DESTROY);
137  #endif // GRAPHICS_DISABLED
139  exit(0);
140  delete widths_win_;
141  }
142  delete leaders_win_;
143  delete initial_widths_win_;
144  delete chains_win_;
145  delete textlines_win_;
146  delete smoothed_win_;
147  delete diacritics_win_;
148 }
bool textord_tabfind_only_strokewidths
Definition: strokewidth.cpp:45
SVEvent * AwaitEvent(SVEventType type)
Definition: scrollview.cpp:449
#define NULL
Definition: host.h:144

Member Function Documentation

void tesseract::StrokeWidth::CorrectForRotation ( const FCOORD rerotation,
ColPartitionGrid part_grid 
)

Definition at line 261 of file strokewidth.cpp.

262  {
263  Init(part_grid->gridsize(), part_grid->bleft(), part_grid->tright());
264  grid_box_ = TBOX(bleft(), tright());
265  rerotation_.set_x(rotation.x());
266  rerotation_.set_y(-rotation.y());
267 }
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
void set_x(float xin)
rewrite function
Definition: points.h:216
void set_y(float yin)
rewrite function
Definition: points.h:220
const ICOORD & bleft() const
Definition: bbgrid.h:72
Definition: rect.h:30
const ICOORD & tright() const
Definition: bbgrid.h:75
void tesseract::StrokeWidth::FindLeaderPartitions ( TO_BLOCK block,
ColPartitionGrid part_grid 
)

Definition at line 270 of file strokewidth.cpp.

271  {
272  Clear();
273  // Find and isolate leaders in the noise list.
274  ColPartition_LIST leader_parts;
275  FindLeadersAndMarkNoise(block, &leader_parts);
276  // Setup the strokewidth grid with the block's remaining (non-noise) blobs.
277  InsertBlobList(&block->blobs);
278  // Mark blobs that have leader neighbours.
279  for (ColPartition_IT it(&leader_parts); !it.empty(); it.forward()) {
280  ColPartition* part = it.extract();
281  part->ClaimBoxes();
282  MarkLeaderNeighbours(part, LR_LEFT);
283  MarkLeaderNeighbours(part, LR_RIGHT);
284  part_grid->InsertBBox(true, true, part);
285  }
286 }
void InsertBlobList(BLOBNBOX_LIST *blobs)
Definition: blobgrid.cpp:34
BLOBNBOX_LIST blobs
Definition: blobbox.h:768
void tesseract::StrokeWidth::FindTextlineDirectionAndFixBrokenCJK ( PageSegMode  pageseg_mode,
bool  cjk_merge,
TO_BLOCK input_block 
)

Definition at line 167 of file strokewidth.cpp.

169  {
170  // Setup the grid with the remaining (non-noise) blobs.
171  InsertBlobs(input_block);
172  // Repair broken CJK characters if needed.
173  while (cjk_merge && FixBrokenCJK(input_block));
174  // Grade blobs by inspection of neighbours.
175  FindTextlineFlowDirection(pageseg_mode, false);
176  // Clear the grid ready for rotation or leader finding.
177  Clear();
178 }
void tesseract::StrokeWidth::GradeBlobsIntoPartitions ( PageSegMode  pageseg_mode,
const FCOORD rerotation,
TO_BLOCK block,
Pix *  nontext_pix,
const DENORM denorm,
bool  cjk_script,
TextlineProjection projection,
BLOBNBOX_LIST *  diacritic_blobs,
ColPartitionGrid part_grid,
ColPartition_LIST *  big_parts 
)

Definition at line 354 of file strokewidth.cpp.

358  {
359  nontext_map_ = nontext_pix;
360  projection_ = projection;
361  denorm_ = denorm;
362  // Clear and re Insert to take advantage of the tab stops in the blobs.
363  Clear();
364  // Setup the strokewidth grid with the remaining non-noise, non-leader blobs.
365  InsertBlobs(block);
366 
367  // Run FixBrokenCJK() again if the page is CJK.
368  if (cjk_script) {
369  FixBrokenCJK(block);
370  }
371  FindTextlineFlowDirection(pageseg_mode, false);
372  projection_->ConstructProjection(block, rerotation, nontext_map_);
374  ScrollView* line_blobs_win = MakeWindow(0, 0, "Initial textline Blobs");
375  projection_->PlotGradedBlobs(&block->blobs, line_blobs_win);
376  projection_->PlotGradedBlobs(&block->small_blobs, line_blobs_win);
377  }
378  projection_->MoveNonTextlineBlobs(&block->blobs, &block->noise_blobs);
379  projection_->MoveNonTextlineBlobs(&block->small_blobs, &block->noise_blobs);
380  // Clear and re Insert to take advantage of the removed diacritics.
381  Clear();
382  InsertBlobs(block);
383  FCOORD skew;
384  FindTextlineFlowDirection(pageseg_mode, true);
386  FindInitialPartitions(pageseg_mode, rerotation, true, block,
387  diacritic_blobs, part_grid, big_parts, &skew);
388  if (r == PFR_NOISE) {
389  tprintf("Detected %d diacritics\n", diacritic_blobs->length());
390  // Noise was found, and removed.
391  Clear();
392  InsertBlobs(block);
393  FindTextlineFlowDirection(pageseg_mode, true);
394  r = FindInitialPartitions(pageseg_mode, rerotation, false, block,
395  diacritic_blobs, part_grid, big_parts, &skew);
396  }
397  nontext_map_ = NULL;
398  projection_ = NULL;
399  denorm_ = NULL;
400 }
void MoveNonTextlineBlobs(BLOBNBOX_LIST *blobs, BLOBNBOX_LIST *small_blobs) const
#define tprintf(...)
Definition: tprintf.h:31
void ConstructProjection(TO_BLOCK *input_block, const FCOORD &rotation, Pix *nontext_map)
BLOBNBOX_LIST small_blobs
Definition: blobbox.h:771
BLOBNBOX_LIST noise_blobs
Definition: blobbox.h:770
int textord_tabfind_show_strokewidths
Definition: strokewidth.cpp:44
PartitionFindResult
Definition: strokewidth.h:46
ScrollView * MakeWindow(int x, int y, const char *window_name)
#define NULL
Definition: host.h:144
void PlotGradedBlobs(BLOBNBOX_LIST *blobs, ScrollView *win)
Definition: points.h:189
BLOBNBOX_LIST blobs
Definition: blobbox.h:768
void tesseract::StrokeWidth::HandleClick ( int  x,
int  y 
)
virtual

Handles a click event in a display window.

Reimplemented from tesseract::BBGrid< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT >.

Definition at line 411 of file strokewidth.cpp.

411  {
413  // Run a radial search for blobs that overlap.
414  BlobGridSearch radsearch(this);
415  radsearch.StartRadSearch(x, y, 1);
416  BLOBNBOX* neighbour;
417  FCOORD click(static_cast<float>(x), static_cast<float>(y));
418  while ((neighbour = radsearch.NextRadSearch()) != NULL) {
419  TBOX nbox = neighbour->bounding_box();
420  if (nbox.contains(click) && neighbour->cblob() != NULL) {
421  PrintBoxWidths(neighbour);
422  if (neighbour->neighbour(BND_LEFT) != NULL)
423  PrintBoxWidths(neighbour->neighbour(BND_LEFT));
424  if (neighbour->neighbour(BND_RIGHT) != NULL)
425  PrintBoxWidths(neighbour->neighbour(BND_RIGHT));
426  if (neighbour->neighbour(BND_ABOVE) != NULL)
427  PrintBoxWidths(neighbour->neighbour(BND_ABOVE));
428  if (neighbour->neighbour(BND_BELOW) != NULL)
429  PrintBoxWidths(neighbour->neighbour(BND_BELOW));
430  int gaps[BND_COUNT];
431  neighbour->NeighbourGaps(gaps);
432  tprintf("Left gap=%d, right=%d, above=%d, below=%d, horz=%d, vert=%d\n"
433  "Good= %d %d %d %d\n",
434  gaps[BND_LEFT], gaps[BND_RIGHT],
435  gaps[BND_ABOVE], gaps[BND_BELOW],
436  neighbour->horz_possible(),
437  neighbour->vert_possible(),
438  neighbour->good_stroke_neighbour(BND_LEFT),
439  neighbour->good_stroke_neighbour(BND_RIGHT),
440  neighbour->good_stroke_neighbour(BND_ABOVE),
441  neighbour->good_stroke_neighbour(BND_BELOW));
442  break;
443  }
444  }
445 }
GridSearch< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT > BlobGridSearch
Definition: blobgrid.h:31
#define tprintf(...)
Definition: tprintf.h:31
bool horz_possible() const
Definition: blobbox.h:292
void NeighbourGaps(int gaps[BND_COUNT]) const
Definition: blobbox.cpp:174
virtual void HandleClick(int x, int y)
Definition: bbgrid.h:658
C_BLOB * cblob() const
Definition: blobbox.h:253
bool good_stroke_neighbour(BlobNeighbourDir n) const
Definition: blobbox.h:358
bool vert_possible() const
Definition: blobbox.h:286
BLOBNBOX * neighbour(BlobNeighbourDir n) const
Definition: blobbox.h:355
Definition: rect.h:30
bool contains(const FCOORD pt) const
Definition: rect.h:323
#define NULL
Definition: host.h:144
const TBOX & bounding_box() const
Definition: blobbox.h:215
Definition: points.h:189
void tesseract::StrokeWidth::RemoveLineResidue ( ColPartition_LIST *  big_part_list)

Definition at line 290 of file strokewidth.cpp.

290  {
291  BlobGridSearch gsearch(this);
292  BLOBNBOX* bbox;
293  // For every vertical line-like bbox in the grid, search its neighbours
294  // to find the tallest, and if the original box is taller by sufficient
295  // margin, then call it line residue and delete it.
296  gsearch.StartFullSearch();
297  while ((bbox = gsearch.NextFullSearch()) != NULL) {
298  TBOX box = bbox->bounding_box();
299  if (box.height() < box.width() * kLineResidueAspectRatio)
300  continue;
301  // Set up a rectangle search around the blob to find the size of its
302  // neighbours.
303  int padding = box.height() * kLineResiduePadRatio;
304  TBOX search_box = box;
305  search_box.pad(padding, padding);
306  bool debug = AlignedBlob::WithinTestRegion(2, box.left(),
307  box.bottom());
308  // Find the largest object in the search box not equal to bbox.
309  BlobGridSearch rsearch(this);
310  int max_size = 0;
311  BLOBNBOX* n;
312  rsearch.StartRectSearch(search_box);
313  while ((n = rsearch.NextRectSearch()) != NULL) {
314  if (n == bbox) continue;
315  TBOX nbox = n->bounding_box();
316  if (nbox.height() > max_size) {
317  max_size = nbox.height();
318  }
319  }
320  if (debug) {
321  tprintf("Max neighbour size=%d for candidate line box at:", max_size);
322  box.print();
323  }
324  if (max_size * kLineResidueSizeRatio < box.height()) {
325  #ifndef GRAPHICS_DISABLED
326  if (leaders_win_ != NULL) {
327  // We are debugging, so display deleted in pink blobs in the same
328  // window that we use to display leader detection.
329  leaders_win_->Pen(ScrollView::PINK);
330  leaders_win_->Rectangle(box.left(), box.bottom(),
331  box.right(), box.top());
332  }
333  #endif // GRAPHICS_DISABLED
334  ColPartition::MakeBigPartition(bbox, big_part_list);
335  }
336  }
337 }
GridSearch< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT > BlobGridSearch
Definition: blobgrid.h:31
void Pen(Color color)
Definition: scrollview.cpp:726
static bool WithinTestRegion(int detail_level, int x, int y)
const double kLineResidueSizeRatio
#define tprintf(...)
Definition: tprintf.h:31
void print() const
Definition: rect.h:270
inT16 right() const
Definition: rect.h:75
const int kLineResiduePadRatio
void pad(int xpad, int ypad)
Definition: rect.h:127
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
inT16 height() const
Definition: rect.h:104
inT16 width() const
Definition: rect.h:111
Definition: rect.h:30
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:606
static ColPartition * MakeBigPartition(BLOBNBOX *box, ColPartition_LIST *big_part_list)
#define NULL
Definition: host.h:144
const TBOX & bounding_box() const
Definition: blobbox.h:215
const double kLineResidueAspectRatio
inT16 top() const
Definition: rect.h:54
void tesseract::StrokeWidth::SetNeighboursOnMediumBlobs ( TO_BLOCK block)

Definition at line 153 of file strokewidth.cpp.

153  {
154  // Run a preliminary strokewidth neighbour detection on the medium blobs.
155  InsertBlobList(&block->blobs);
156  BLOBNBOX_IT blob_it(&block->blobs);
157  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
158  SetNeighbours(false, false, blob_it.data());
159  }
160  Clear();
161 }
void InsertBlobList(BLOBNBOX_LIST *blobs)
Definition: blobgrid.cpp:34
BLOBNBOX_LIST blobs
Definition: blobbox.h:768
bool tesseract::StrokeWidth::TestVerticalTextDirection ( double  find_vertical_text_ratio,
TO_BLOCK block,
BLOBNBOX_CLIST *  osd_blobs 
)

Definition at line 219 of file strokewidth.cpp.

221  {
222  int vertical_boxes = 0;
223  int horizontal_boxes = 0;
224  // Count vertical normal and large blobs.
225  BLOBNBOX_CLIST vertical_blobs;
226  BLOBNBOX_CLIST horizontal_blobs;
227  BLOBNBOX_CLIST nondescript_blobs;
228  CollectHorizVertBlobs(&block->blobs, &vertical_boxes, &horizontal_boxes,
229  &vertical_blobs, &horizontal_blobs, &nondescript_blobs);
230  CollectHorizVertBlobs(&block->large_blobs, &vertical_boxes, &horizontal_boxes,
231  &vertical_blobs, &horizontal_blobs, &nondescript_blobs);
233  tprintf("TextDir hbox=%d vs vbox=%d, %dH, %dV, %dN osd blobs\n",
234  horizontal_boxes, vertical_boxes,
235  horizontal_blobs.length(), vertical_blobs.length(),
236  nondescript_blobs.length());
237  if (osd_blobs != NULL && vertical_boxes == 0 && horizontal_boxes == 0) {
238  // Only nondescript blobs available, so return those.
239  BLOBNBOX_C_IT osd_it(osd_blobs);
240  osd_it.add_list_after(&nondescript_blobs);
241  return false;
242  }
243  int min_vert_boxes = static_cast<int>((vertical_boxes + horizontal_boxes) *
244  find_vertical_text_ratio);
245  if (vertical_boxes >= min_vert_boxes) {
246  if (osd_blobs != NULL) {
247  BLOBNBOX_C_IT osd_it(osd_blobs);
248  osd_it.add_list_after(&vertical_blobs);
249  }
250  return true;
251  } else {
252  if (osd_blobs != NULL) {
253  BLOBNBOX_C_IT osd_it(osd_blobs);
254  osd_it.add_list_after(&horizontal_blobs);
255  }
256  return false;
257  }
258 }
#define tprintf(...)
Definition: tprintf.h:31
int textord_debug_tabfind
Definition: alignedblob.cpp:27
#define NULL
Definition: host.h:144
BLOBNBOX_LIST large_blobs
Definition: blobbox.h:772
BLOBNBOX_LIST blobs
Definition: blobbox.h:768

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