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

#include <colpartitiongrid.h>

Inheritance diagram for tesseract::ColPartitionGrid:
tesseract::BBGrid< ColPartition, ColPartition_CLIST, ColPartition_C_IT > tesseract::GridBase

Public Member Functions

 ColPartitionGrid ()
 
 ColPartitionGrid (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~ColPartitionGrid ()
 
void HandleClick (int x, int y)
 
void Merges (TessResultCallback2< bool, ColPartition *, TBOX * > *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition * > *confirm_cb)
 
bool MergePart (TessResultCallback2< bool, ColPartition *, TBOX * > *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition * > *confirm_cb, ColPartition *part)
 
int ComputeTotalOverlap (ColPartitionGrid **overlap_grid)
 
void FindOverlappingPartitions (const TBOX &box, const ColPartition *not_this, ColPartition_CLIST *parts)
 
ColPartitionBestMergeCandidate (const ColPartition *part, ColPartition_CLIST *candidates, bool debug, TessResultCallback2< bool, const ColPartition *, const ColPartition * > *confirm_cb, int *overlap_increase)
 
void SplitOverlappingPartitions (ColPartition_LIST *big_parts)
 
bool GridSmoothNeighbours (BlobTextFlowType source_type, Pix *nontext_map, const TBOX &im_box, const FCOORD &rerotation)
 
void ComputePartitionColors (Pix *scaled_color, int scaled_factor, const FCOORD &rerotation)
 
void ReflectInYAxis ()
 
void Deskew (const FCOORD &deskew)
 
void ExtractPartitionsAsBlocks (BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
 
void SetTabStops (TabFind *tabgrid)
 
bool MakeColPartSets (PartSetVector *part_sets)
 
ColPartitionSetMakeSingleColumnSet (WidthCallback *cb)
 
void ClaimBoxes ()
 
void ReTypeBlobs (BLOBNBOX_LIST *im_blobs)
 
void RecomputeBounds (int gridsize, const ICOORD &bleft, const ICOORD &tright, const ICOORD &vertical)
 
void GridFindMargins (ColPartitionSet **best_columns)
 
void ListFindMargins (ColPartitionSet **best_columns, ColPartition_LIST *parts)
 
void DeleteParts ()
 
void DeleteUnknownParts (TO_BLOCK *block)
 
void DeleteNonLeaderParts ()
 
void FindFigureCaptions ()
 
void FindPartitionPartners ()
 
void FindPartitionPartners (bool upper, ColPartition *part)
 
void FindVPartitionPartners (bool to_the_left, ColPartition *part)
 
void RefinePartitionPartners (bool get_desperate)
 
- Public Member Functions inherited from tesseract::BBGrid< ColPartition, ColPartition_CLIST, ColPartition_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)(ColPartition *))
 
void InsertBBox (bool h_spread, bool v_spread, ColPartition *bbox)
 
void InsertPixPtBBox (int left, int bottom, Pix *pix, ColPartition *bbox)
 
void RemoveBBox (ColPartition *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< ColPartition, ColPartition_CLIST, ColPartition_C_IT >
ColPartition_CLIST * grid_
 
- Protected Attributes inherited from tesseract::GridBase
int gridsize_
 
int gridwidth_
 
int gridheight_
 
int gridbuckets_
 
ICOORD bleft_
 
ICOORD tright_
 

Detailed Description

Definition at line 33 of file colpartitiongrid.h.

Constructor & Destructor Documentation

tesseract::ColPartitionGrid::ColPartitionGrid ( )

Definition at line 75 of file colpartitiongrid.cpp.

75  {
76 }
tesseract::ColPartitionGrid::ColPartitionGrid ( int  gridsize,
const ICOORD bleft,
const ICOORD tright 
)

Definition at line 77 of file colpartitiongrid.cpp.

79  : BBGrid<ColPartition, ColPartition_CLIST, ColPartition_C_IT>(gridsize,
80  bleft, tright) {
81 }
int gridsize() const
Definition: bbgrid.h:63
const ICOORD & bleft() const
Definition: bbgrid.h:72
const ICOORD & tright() const
Definition: bbgrid.h:75
tesseract::ColPartitionGrid::~ColPartitionGrid ( )
virtual

Definition at line 83 of file colpartitiongrid.cpp.

83  {
84 }

Member Function Documentation

ColPartition * tesseract::ColPartitionGrid::BestMergeCandidate ( const ColPartition part,
ColPartition_CLIST *  candidates,
bool  debug,
TessResultCallback2< bool, const ColPartition *, const ColPartition * > *  confirm_cb,
int *  overlap_increase 
)

Definition at line 417 of file colpartitiongrid.cpp.

420  {
421  if (overlap_increase != NULL)
422  *overlap_increase = 0;
423  if (candidates->empty())
424  return NULL;
425  int ok_overlap =
426  static_cast<int>(kTinyEnoughTextlineOverlapFraction * gridsize() + 0.5);
427  // The best neighbour to merge with is the one that causes least
428  // total pairwise overlap among all the neighbours.
429  // If more than one offers the same total overlap, choose the one
430  // with the least total area.
431  const TBOX& part_box = part->bounding_box();
432  ColPartition_C_IT it(candidates);
433  ColPartition* best_candidate = NULL;
434  // Find the total combined box of all candidates and the original.
435  TBOX full_box(part_box);
436  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
437  ColPartition* candidate = it.data();
438  full_box += candidate->bounding_box();
439  }
440  // Keep valid neighbours in a list.
441  ColPartition_CLIST neighbours;
442  // Now run a rect search of the merged box for overlapping neighbours, as
443  // we need anything that might be overlapped by the merged box.
444  FindOverlappingPartitions(full_box, part, &neighbours);
445  if (debug) {
446  tprintf("Finding best merge candidate from %d, %d neighbours for box:",
447  candidates->length(), neighbours.length());
448  part_box.print();
449  }
450  // If the best increase in overlap is positive, then we also check the
451  // worst non-candidate overlap. This catches the case of multiple good
452  // candidates that overlap each other when merged. If the worst
453  // non-candidate overlap is better than the best overlap, then return
454  // the worst non-candidate overlap instead.
455  ColPartition_CLIST non_candidate_neighbours;
456  non_candidate_neighbours.set_subtract(SortByBoxLeft<ColPartition>, true,
457  &neighbours, candidates);
458  int worst_nc_increase = 0;
459  int best_increase = MAX_INT32;
460  int best_area = 0;
461  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
462  ColPartition* candidate = it.data();
463  if (confirm_cb != NULL && !confirm_cb->Run(part, candidate)) {
464  if (debug) {
465  tprintf("Candidate not confirmed:");
466  candidate->bounding_box().print();
467  }
468  continue;
469  }
470  int increase = IncreaseInOverlap(part, candidate, ok_overlap, &neighbours);
471  const TBOX& cand_box = candidate->bounding_box();
472  if (best_candidate == NULL || increase < best_increase) {
473  best_candidate = candidate;
474  best_increase = increase;
475  best_area = cand_box.bounding_union(part_box).area() - cand_box.area();
476  if (debug) {
477  tprintf("New best merge candidate has increase %d, area %d, over box:",
478  increase, best_area);
479  full_box.print();
480  candidate->Print();
481  }
482  } else if (increase == best_increase) {
483  int area = cand_box.bounding_union(part_box).area() - cand_box.area();
484  if (area < best_area) {
485  best_area = area;
486  best_candidate = candidate;
487  }
488  }
489  increase = IncreaseInOverlap(part, candidate, ok_overlap,
490  &non_candidate_neighbours);
491  if (increase > worst_nc_increase)
492  worst_nc_increase = increase;
493  }
494  if (best_increase > 0) {
495  // If the worst non-candidate increase is less than the best increase
496  // including the candidates, then all the candidates can merge together
497  // and the increase in outside overlap would be less, so use that result,
498  // but only if each candidate is either a good diacritic merge with part,
499  // or an ok merge candidate with all the others.
500  // See TestCompatibleCandidates for more explanation and a picture.
501  if (worst_nc_increase < best_increase &&
502  TestCompatibleCandidates(*part, debug, candidates)) {
503  best_increase = worst_nc_increase;
504  }
505  }
506  if (overlap_increase != NULL)
507  *overlap_increase = best_increase;
508  return best_candidate;
509 }
virtual R Run(A1, A2)=0
#define tprintf(...)
Definition: tprintf.h:31
void print() const
Definition: rect.h:270
void FindOverlappingPartitions(const TBOX &box, const ColPartition *not_this, ColPartition_CLIST *parts)
inT32 area() const
Definition: rect.h:118
int gridsize() const
Definition: bbgrid.h:63
const double kTinyEnoughTextlineOverlapFraction
TBOX bounding_union(const TBOX &box) const
Definition: rect.cpp:129
#define MAX_INT32
Definition: host.h:120
Definition: rect.h:30
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::ClaimBoxes ( )

Definition at line 914 of file colpartitiongrid.cpp.

914  {
915  // Iterate the ColPartitions in the grid.
916  ColPartitionGridSearch gsearch(this);
917  gsearch.StartFullSearch();
918  ColPartition* part;
919  while ((part = gsearch.NextFullSearch()) != NULL) {
920  part->ClaimBoxes();
921  }
922 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::ComputePartitionColors ( Pix *  scaled_color,
int  scaled_factor,
const FCOORD rerotation 
)

Definition at line 658 of file colpartitiongrid.cpp.

660  {
661  if (scaled_color == NULL)
662  return;
663  Pix* color_map1 = NULL;
664  Pix* color_map2 = NULL;
665  Pix* rms_map = NULL;
667  int width = pixGetWidth(scaled_color);
668  int height = pixGetHeight(scaled_color);
669  color_map1 = pixCreate(width, height, 32);
670  color_map2 = pixCreate(width, height, 32);
671  rms_map = pixCreate(width, height, 8);
672  }
673  // Iterate the ColPartitions in the grid.
674  ColPartitionGridSearch gsearch(this);
675  gsearch.StartFullSearch();
676  ColPartition* part;
677  while ((part = gsearch.NextFullSearch()) != NULL) {
678  TBOX part_box = part->bounding_box();
679  part_box.rotate_large(rerotation);
680  ImageFind::ComputeRectangleColors(part_box, scaled_color,
681  scaled_factor,
682  color_map1, color_map2, rms_map,
683  part->color1(), part->color2());
684  }
685  if (color_map1 != NULL) {
686  pixWrite("swcolorinput.png", scaled_color, IFF_PNG);
687  pixWrite("swcolor1.png", color_map1, IFF_PNG);
688  pixWrite("swcolor2.png", color_map2, IFF_PNG);
689  pixWrite("swrms.png", rms_map, IFF_PNG);
690  pixDestroy(&color_map1);
691  pixDestroy(&color_map2);
692  pixDestroy(&rms_map);
693  }
694 }
static void ComputeRectangleColors(const TBOX &rect, Pix *pix, int factor, Pix *color_map1, Pix *color_map2, Pix *rms_map, uinT8 *color1, uinT8 *color2)
Definition: imagefind.cpp:390
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
void rotate_large(const FCOORD &vec)
Definition: rect.cpp:72
bool textord_tabfind_show_color_fit
Definition: rect.h:30
#define NULL
Definition: host.h:144
int tesseract::ColPartitionGrid::ComputeTotalOverlap ( ColPartitionGrid **  overlap_grid)

Definition at line 330 of file colpartitiongrid.cpp.

330  {
331  int total_overlap = 0;
332  // Iterate the ColPartitions in the grid.
333  ColPartitionGridSearch gsearch(this);
334  gsearch.StartFullSearch();
335  ColPartition* part;
336  while ((part = gsearch.NextFullSearch()) != NULL) {
337  ColPartition_CLIST neighbors;
338  const TBOX& part_box = part->bounding_box();
339  FindOverlappingPartitions(part_box, part, &neighbors);
340  ColPartition_C_IT n_it(&neighbors);
341  bool any_part_overlap = false;
342  for (n_it.mark_cycle_pt(); !n_it.cycled_list(); n_it.forward()) {
343  const TBOX& n_box = n_it.data()->bounding_box();
344  int overlap = n_box.intersection(part_box).area();
345  if (overlap > 0 && overlap_grid != NULL) {
346  if (*overlap_grid == NULL) {
347  *overlap_grid = new ColPartitionGrid(gridsize(), bleft(), tright());
348  }
349  (*overlap_grid)->InsertBBox(true, true, n_it.data()->ShallowCopy());
350  if (!any_part_overlap) {
351  (*overlap_grid)->InsertBBox(true, true, part->ShallowCopy());
352  }
353  }
354  any_part_overlap = true;
355  total_overlap += overlap;
356  }
357  }
358  return total_overlap;
359 }
void FindOverlappingPartitions(const TBOX &box, const ColPartition *not_this, ColPartition_CLIST *parts)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
inT32 area() const
Definition: rect.h:118
int gridsize() const
Definition: bbgrid.h:63
const ICOORD & bleft() const
Definition: bbgrid.h:72
TBOX intersection(const TBOX &box) const
Definition: rect.cpp:87
Definition: rect.h:30
const ICOORD & tright() const
Definition: bbgrid.h:75
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::DeleteNonLeaderParts ( )

Definition at line 1093 of file colpartitiongrid.cpp.

1093  {
1094  ColPartitionGridSearch gsearch(this);
1095  gsearch.StartFullSearch();
1096  ColPartition* part;
1097  while ((part = gsearch.NextFullSearch()) != NULL) {
1098  if (part->flow() != BTFT_LEADER) {
1099  gsearch.RemoveBBox();
1100  if (part->ReleaseNonLeaderBoxes()) {
1101  InsertBBox(true, true, part);
1102  gsearch.RepositionIterator();
1103  } else {
1104  delete part;
1105  }
1106  }
1107  }
1108 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::DeleteParts ( )

Definition at line 1059 of file colpartitiongrid.cpp.

1059  {
1060  ColPartition_LIST dead_parts;
1061  ColPartition_IT dead_it(&dead_parts);
1062  ColPartitionGridSearch gsearch(this);
1063  gsearch.StartFullSearch();
1064  ColPartition* part;
1065  while ((part = gsearch.NextFullSearch()) != NULL) {
1066  part->DisownBoxes();
1067  dead_it.add_to_end(part); // Parts will be deleted on return.
1068  }
1069  Clear();
1070 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::DeleteUnknownParts ( TO_BLOCK block)

Definition at line 1074 of file colpartitiongrid.cpp.

1074  {
1075  ColPartitionGridSearch gsearch(this);
1076  gsearch.StartFullSearch();
1077  ColPartition* part;
1078  while ((part = gsearch.NextFullSearch()) != NULL) {
1079  if (part->blob_type() == BRT_UNKNOWN) {
1080  gsearch.RemoveBBox();
1081  // Once marked, the blobs will be swept up by DeleteUnownedNoise.
1082  part->set_flow(BTFT_NONTEXT);
1083  part->set_blob_type(BRT_NOISE);
1084  part->SetBlobTypes();
1085  part->DisownBoxes();
1086  delete part;
1087  }
1088  }
1089  block->DeleteUnownedNoise();
1090 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
void DeleteUnownedNoise()
Definition: blobbox.cpp:1031
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::Deskew ( const FCOORD deskew)

Definition at line 786 of file colpartitiongrid.cpp.

786  {
787  ColPartition_LIST parts;
788  ColPartition_IT part_it(&parts);
789  // Iterate the ColPartitions in the grid to extract them.
790  ColPartitionGridSearch gsearch(this);
791  gsearch.StartFullSearch();
792  ColPartition* part;
793  while ((part = gsearch.NextFullSearch()) != NULL) {
794  part_it.add_after_then_move(part);
795  }
796  // Rebuild the grid to the new size.
797  TBOX grid_box(bleft_, tright_);
798  grid_box.rotate_large(deskew);
799  Init(gridsize(), grid_box.botleft(), grid_box.topright());
800  // Reinitializing the grid with rotated coords also clears all the
801  // pointers, so parts will now own the ColPartitions. (Briefly).
802  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
803  part = part_it.extract();
804  part->ComputeLimits();
805  InsertBBox(true, true, part);
806  }
807 }
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
int gridsize() const
Definition: bbgrid.h:63
Definition: rect.h:30
#define NULL
Definition: host.h:144
ICOORD tright_
Definition: bbgrid.h:91
void tesseract::ColPartitionGrid::ExtractPartitionsAsBlocks ( BLOCK_LIST *  blocks,
TO_BLOCK_LIST *  to_blocks 
)

Definition at line 726 of file colpartitiongrid.cpp.

727  {
728  TO_BLOCK_IT to_block_it(to_blocks);
729  BLOCK_IT block_it(blocks);
730  // All partitions will be put on this list and deleted on return.
731  ColPartition_LIST parts;
732  ColPartition_IT part_it(&parts);
733  // Iterate the ColPartitions in the grid to extract them.
734  ColPartitionGridSearch gsearch(this);
735  gsearch.StartFullSearch();
736  ColPartition* part;
737  while ((part = gsearch.NextFullSearch()) != NULL) {
738  part_it.add_after_then_move(part);
739  // The partition has to be at least vaguely like text.
740  BlobRegionType blob_type = part->blob_type();
741  if (BLOBNBOX::IsTextType(blob_type) ||
742  (blob_type == BRT_UNKNOWN && part->boxes_count() > 1)) {
743  PolyBlockType type = blob_type == BRT_VERT_TEXT ? PT_VERTICAL_TEXT
744  : PT_FLOWING_TEXT;
745  // Get metrics from the row that will be used for the block.
746  TBOX box = part->bounding_box();
747  int median_width = part->median_width();
748  int median_height = part->median_size();
749  // Turn the partition into a TO_ROW.
750  TO_ROW* row = part->MakeToRow();
751  if (row == NULL) {
752  // This partition is dead.
753  part->DeleteBoxes();
754  continue;
755  }
756  BLOCK* block = new BLOCK("", true, 0, 0, box.left(), box.bottom(),
757  box.right(), box.top());
758  block->set_poly_block(new POLY_BLOCK(box, type));
759  TO_BLOCK* to_block = new TO_BLOCK(block);
760  TO_ROW_IT row_it(to_block->get_rows());
761  row_it.add_after_then_move(row);
762  // We haven't differentially rotated vertical and horizontal text at
763  // this point, so use width or height as appropriate.
764  if (blob_type == BRT_VERT_TEXT) {
765  to_block->line_size = static_cast<float>(median_width);
766  to_block->line_spacing = static_cast<float>(box.width());
767  to_block->max_blob_size = static_cast<float>(box.width() + 1);
768  } else {
769  to_block->line_size = static_cast<float>(median_height);
770  to_block->line_spacing = static_cast<float>(box.height());
771  to_block->max_blob_size = static_cast<float>(box.height() + 1);
772  }
773  block_it.add_to_end(block);
774  to_block_it.add_to_end(to_block);
775  } else {
776  // This partition is dead.
777  part->DeleteBoxes();
778  }
779  }
780  Clear();
781  // Now it is safe to delete the ColPartitions as parts goes out of scope.
782 }
BlobRegionType
Definition: blobbox.h:57
void set_poly_block(POLY_BLOCK *blk)
set the poly block
Definition: pdblock.h:63
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
inT16 right() const
Definition: rect.h:75
inT16 left() const
Definition: rect.h:68
Definition: ocrblock.h:30
inT16 bottom() const
Definition: rect.h:61
inT16 height() const
Definition: rect.h:104
TO_ROW_LIST * get_rows()
Definition: blobbox.h:700
inT16 width() const
Definition: rect.h:111
Definition: rect.h:30
float line_spacing
Definition: blobbox.h:775
static bool IsTextType(BlobRegionType type)
Definition: blobbox.h:403
#define NULL
Definition: host.h:144
PolyBlockType
Definition: publictypes.h:41
inT16 top() const
Definition: rect.h:54
float max_blob_size
Definition: blobbox.h:782
float line_size
Definition: blobbox.h:781
void tesseract::ColPartitionGrid::FindFigureCaptions ( )

Definition at line 1111 of file colpartitiongrid.cpp.

1111  {
1112  // For each image region find its best candidate text caption region,
1113  // if any and mark it as such.
1114  ColPartitionGridSearch gsearch(this);
1115  gsearch.StartFullSearch();
1116  ColPartition* part;
1117  while ((part = gsearch.NextFullSearch()) != NULL) {
1118  if (part->IsImageType()) {
1119  const TBOX& part_box = part->bounding_box();
1120  bool debug = AlignedBlob::WithinTestRegion(2, part_box.left(),
1121  part_box.bottom());
1122  ColPartition* best_caption = NULL;
1123  int best_dist = 0; // Distance to best_caption.
1124  int best_upper = 0; // Direction of best_caption.
1125  // Handle both lower and upper directions.
1126  for (int upper = 0; upper < 2; ++upper) {
1127  ColPartition_C_IT partner_it(upper ? part->upper_partners()
1128  : part->lower_partners());
1129  // If there are no image partners, then this direction is ok.
1130  for (partner_it.mark_cycle_pt(); !partner_it.cycled_list();
1131  partner_it.forward()) {
1132  ColPartition* partner = partner_it.data();
1133  if (partner->IsImageType()) {
1134  break;
1135  }
1136  }
1137  if (!partner_it.cycled_list()) continue;
1138  // Find the nearest totally overlapping text partner.
1139  for (partner_it.mark_cycle_pt(); !partner_it.cycled_list();
1140  partner_it.forward()) {
1141  ColPartition* partner = partner_it.data();
1142  if (!partner->IsTextType() || partner->type() == PT_TABLE) continue;
1143  const TBOX& partner_box = partner->bounding_box();
1144  if (debug) {
1145  tprintf("Finding figure captions for image part:");
1146  part_box.print();
1147  tprintf("Considering partner:");
1148  partner_box.print();
1149  }
1150  if (partner_box.left() >= part_box.left() &&
1151  partner_box.right() <= part_box.right()) {
1152  int dist = partner_box.y_gap(part_box);
1153  if (best_caption == NULL || dist < best_dist) {
1154  best_dist = dist;
1155  best_caption = partner;
1156  best_upper = upper;
1157  }
1158  }
1159  }
1160  }
1161  if (best_caption != NULL) {
1162  if (debug) {
1163  tprintf("Best caption candidate:");
1164  best_caption->bounding_box().print();
1165  }
1166  // We have a candidate caption. Qualify it as being separable from
1167  // any body text. We are looking for either a small number of lines
1168  // or a big gap that indicates a separation from the body text.
1169  int line_count = 0;
1170  int biggest_gap = 0;
1171  int smallest_gap = MAX_INT16;
1172  int total_height = 0;
1173  int mean_height = 0;
1174  ColPartition* end_partner = NULL;
1175  ColPartition* next_partner = NULL;
1176  for (ColPartition* partner = best_caption; partner != NULL &&
1177  line_count <= kMaxCaptionLines;
1178  partner = next_partner) {
1179  if (!partner->IsTextType()) {
1180  end_partner = partner;
1181  break;
1182  }
1183  ++line_count;
1184  total_height += partner->bounding_box().height();
1185  next_partner = partner->SingletonPartner(best_upper);
1186  if (next_partner != NULL) {
1187  int gap = partner->bounding_box().y_gap(
1188  next_partner->bounding_box());
1189  if (gap > biggest_gap) {
1190  biggest_gap = gap;
1191  end_partner = next_partner;
1192  mean_height = total_height / line_count;
1193  } else if (gap < smallest_gap) {
1194  smallest_gap = gap;
1195  }
1196  // If the gap looks big compared to the text size and the smallest
1197  // gap seen so far, then we can stop.
1198  if (biggest_gap > mean_height * kMinCaptionGapHeightRatio &&
1199  biggest_gap > smallest_gap * kMinCaptionGapRatio)
1200  break;
1201  }
1202  }
1203  if (debug) {
1204  tprintf("Line count=%d, biggest gap %d, smallest%d, mean height %d\n",
1205  line_count, biggest_gap, smallest_gap, mean_height);
1206  if (end_partner != NULL) {
1207  tprintf("End partner:");
1208  end_partner->bounding_box().print();
1209  }
1210  }
1211  if (next_partner == NULL && line_count <= kMaxCaptionLines)
1212  end_partner = NULL; // No gap, but line count is small.
1213  if (line_count <= kMaxCaptionLines) {
1214  // This is a qualified caption. Mark the text as caption.
1215  for (ColPartition* partner = best_caption; partner != NULL &&
1216  partner != end_partner;
1217  partner = next_partner) {
1218  partner->set_type(PT_CAPTION_TEXT);
1219  partner->SetBlobTypes();
1220  if (debug) {
1221  tprintf("Set caption type for partition:");
1222  partner->bounding_box().print();
1223  }
1224  next_partner = partner->SingletonPartner(best_upper);
1225  }
1226  }
1227  }
1228  }
1229  }
1230 }
static bool WithinTestRegion(int detail_level, int x, int y)
#define tprintf(...)
Definition: tprintf.h:31
void print() const
Definition: rect.h:270
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
inT16 right() const
Definition: rect.h:75
Definition: capi.h:78
inT16 left() const
Definition: rect.h:68
const int kMaxCaptionLines
int y_gap(const TBOX &box) const
Definition: rect.h:225
const double kMinCaptionGapRatio
inT16 bottom() const
Definition: rect.h:61
Definition: rect.h:30
#define MAX_INT16
Definition: host.h:119
const double kMinCaptionGapHeightRatio
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::FindOverlappingPartitions ( const TBOX box,
const ColPartition not_this,
ColPartition_CLIST *  parts 
)

Definition at line 364 of file colpartitiongrid.cpp.

366  {
367  ColPartitionGridSearch rsearch(this);
368  rsearch.StartRectSearch(box);
369  ColPartition* part;
370  while ((part = rsearch.NextRectSearch()) != NULL) {
371  if (part != not_this)
372  parts->add_sorted(SortByBoxLeft<ColPartition>, true, part);
373  }
374 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::FindPartitionPartners ( )

Definition at line 1236 of file colpartitiongrid.cpp.

1236  {
1237  ColPartitionGridSearch gsearch(this);
1238  gsearch.StartFullSearch();
1239  ColPartition* part;
1240  while ((part = gsearch.NextFullSearch()) != NULL) {
1241  if (part->IsVerticalType()) {
1242  FindVPartitionPartners(true, part);
1243  FindVPartitionPartners(false, part);
1244  } else {
1245  FindPartitionPartners(true, part);
1246  FindPartitionPartners(false, part);
1247  }
1248  }
1249 }
void FindVPartitionPartners(bool to_the_left, ColPartition *part)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::FindPartitionPartners ( bool  upper,
ColPartition part 
)

Definition at line 1253 of file colpartitiongrid.cpp.

1253  {
1254  if (part->type() == PT_NOISE)
1255  return; // Noise is not allowed to partner anything.
1256  const TBOX& box = part->bounding_box();
1257  int top = part->median_top();
1258  int bottom = part->median_bottom();
1259  int height = top - bottom;
1260  int mid_y = (bottom + top) / 2;
1261  ColPartitionGridSearch vsearch(this);
1262  // Search down for neighbour below
1263  vsearch.StartVerticalSearch(box.left(), box.right(), part->MidY());
1264  ColPartition* neighbour;
1265  ColPartition* best_neighbour = NULL;
1266  int best_dist = MAX_INT32;
1267  while ((neighbour = vsearch.NextVerticalSearch(!upper)) != NULL) {
1268  if (neighbour == part || neighbour->type() == PT_NOISE)
1269  continue; // Noise is not allowed to partner anything.
1270  int neighbour_bottom = neighbour->median_bottom();
1271  int neighbour_top = neighbour->median_top();
1272  int neighbour_y = (neighbour_bottom + neighbour_top) / 2;
1273  if (upper != (neighbour_y > mid_y))
1274  continue;
1275  if (!part->HOverlaps(*neighbour) && !part->WithinSameMargins(*neighbour))
1276  continue;
1277  if (!part->TypesMatch(*neighbour)) {
1278  if (best_neighbour == NULL)
1279  best_neighbour = neighbour;
1280  continue;
1281  }
1282  int dist = upper ? neighbour_bottom - top : bottom - neighbour_top;
1283  if (dist <= kMaxPartitionSpacing * height) {
1284  if (dist < best_dist) {
1285  best_dist = dist;
1286  best_neighbour = neighbour;
1287  }
1288  } else {
1289  break;
1290  }
1291  }
1292  if (best_neighbour != NULL)
1293  part->AddPartner(upper, best_neighbour);
1294 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
inT16 right() const
Definition: rect.h:75
inT16 left() const
Definition: rect.h:68
Definition: capi.h:79
#define MAX_INT32
Definition: host.h:120
const double kMaxPartitionSpacing
Definition: rect.h:30
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::FindVPartitionPartners ( bool  to_the_left,
ColPartition part 
)

Definition at line 1298 of file colpartitiongrid.cpp.

1299  {
1300  if (part->type() == PT_NOISE)
1301  return; // Noise is not allowed to partner anything.
1302  const TBOX& box = part->bounding_box();
1303  int left = part->median_left();
1304  int right = part->median_right();
1305  int width = right - left;
1306  int mid_x = (left + right) / 2;
1307  ColPartitionGridSearch hsearch(this);
1308  // Search left for neighbour to_the_left
1309  hsearch.StartSideSearch(mid_x, box.bottom(), box.top());
1310  ColPartition* neighbour;
1311  ColPartition* best_neighbour = NULL;
1312  int best_dist = MAX_INT32;
1313  while ((neighbour = hsearch.NextSideSearch(to_the_left)) != NULL) {
1314  if (neighbour == part || neighbour->type() == PT_NOISE)
1315  continue; // Noise is not allowed to partner anything.
1316  int neighbour_left = neighbour->median_left();
1317  int neighbour_right = neighbour->median_right();
1318  int neighbour_x = (neighbour_left + neighbour_right) / 2;
1319  if (to_the_left != (neighbour_x < mid_x))
1320  continue;
1321  if (!part->VOverlaps(*neighbour))
1322  continue;
1323  if (!part->TypesMatch(*neighbour))
1324  continue; // Only match to other vertical text.
1325  int dist = to_the_left ? left - neighbour_right : neighbour_left - right;
1326  if (dist <= kMaxPartitionSpacing * width) {
1327  if (dist < best_dist || best_neighbour == NULL) {
1328  best_dist = dist;
1329  best_neighbour = neighbour;
1330  }
1331  } else {
1332  break;
1333  }
1334  }
1335  // For vertical partitions, the upper partner is to the left, and lower is
1336  // to the right.
1337  if (best_neighbour != NULL)
1338  part->AddPartner(to_the_left, best_neighbour);
1339 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
Definition: capi.h:79
#define MAX_INT32
Definition: host.h:120
inT16 bottom() const
Definition: rect.h:61
const double kMaxPartitionSpacing
Definition: rect.h:30
#define NULL
Definition: host.h:144
inT16 top() const
Definition: rect.h:54
void tesseract::ColPartitionGrid::GridFindMargins ( ColPartitionSet **  best_columns)

Definition at line 1017 of file colpartitiongrid.cpp.

1017  {
1018  // Iterate the ColPartitions in the grid.
1019  ColPartitionGridSearch gsearch(this);
1020  gsearch.StartFullSearch();
1021  ColPartition* part;
1022  while ((part = gsearch.NextFullSearch()) != NULL) {
1023  // Set up a rectangle search x-bounded by the column and y by the part.
1024  ColPartitionSet* columns = best_columns != NULL
1025  ? best_columns[gsearch.GridY()]
1026  : NULL;
1027  FindPartitionMargins(columns, part);
1028  const TBOX& box = part->bounding_box();
1029  if (AlignedBlob::WithinTestRegion(2, box.left(), box.bottom())) {
1030  tprintf("Computed margins for part:");
1031  part->Print();
1032  }
1033  }
1034 }
static bool WithinTestRegion(int detail_level, int x, int y)
#define tprintf(...)
Definition: tprintf.h:31
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
Definition: rect.h:30
#define NULL
Definition: host.h:144
bool tesseract::ColPartitionGrid::GridSmoothNeighbours ( BlobTextFlowType  source_type,
Pix *  nontext_map,
const TBOX im_box,
const FCOORD rerotation 
)

Definition at line 636 of file colpartitiongrid.cpp.

639  {
640  // Iterate the ColPartitions in the grid.
641  ColPartitionGridSearch gsearch(this);
642  gsearch.StartFullSearch();
643  ColPartition* part;
644  bool any_changed = false;
645  while ((part = gsearch.NextFullSearch()) != NULL) {
646  if (part->flow() != source_type || BLOBNBOX::IsLineType(part->blob_type()))
647  continue;
648  const TBOX& box = part->bounding_box();
649  bool debug = AlignedBlob::WithinTestRegion(2, box.left(), box.bottom());
650  if (SmoothRegionType(nontext_map, im_box, rotation, debug, part))
651  any_changed = true;
652  }
653  return any_changed;
654 }
static bool WithinTestRegion(int detail_level, int x, int y)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
inT16 left() const
Definition: rect.h:68
static bool IsLineType(BlobRegionType type)
Definition: blobbox.h:411
inT16 bottom() const
Definition: rect.h:61
Definition: rect.h:30
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::HandleClick ( int  x,
int  y 
)
virtual

Reimplemented from tesseract::BBGrid< ColPartition, ColPartition_CLIST, ColPartition_C_IT >.

Definition at line 87 of file colpartitiongrid.cpp.

87  {
88  BBGrid<ColPartition,
89  ColPartition_CLIST, ColPartition_C_IT>::HandleClick(x, y);
90  // Run a radial search for partitions that overlap.
91  ColPartitionGridSearch radsearch(this);
92  radsearch.SetUniqueMode(true);
93  radsearch.StartRadSearch(x, y, 1);
94  ColPartition* neighbour;
95  FCOORD click(x, y);
96  while ((neighbour = radsearch.NextRadSearch()) != NULL) {
97  TBOX nbox = neighbour->bounding_box();
98  if (nbox.contains(click)) {
99  tprintf("Block box:");
100  neighbour->bounding_box().print();
101  neighbour->Print();
102  }
103  }
104 }
#define tprintf(...)
Definition: tprintf.h:31
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
void HandleClick(int x, int y)
Definition: rect.h:30
bool contains(const FCOORD pt) const
Definition: rect.h:323
#define NULL
Definition: host.h:144
Definition: points.h:189
void tesseract::ColPartitionGrid::ListFindMargins ( ColPartitionSet **  best_columns,
ColPartition_LIST *  parts 
)

Definition at line 1041 of file colpartitiongrid.cpp.

1042  {
1043  ColPartition_IT part_it(parts);
1044  for (part_it.mark_cycle_pt(); !part_it.cycled_list(); part_it.forward()) {
1045  ColPartition* part = part_it.data();
1046  ColPartitionSet* columns = NULL;
1047  if (best_columns != NULL) {
1048  TBOX part_box = part->bounding_box();
1049  // Get the columns from the y grid coord.
1050  int grid_x, grid_y;
1051  GridCoords(part_box.left(), part_box.bottom(), &grid_x, &grid_y);
1052  columns = best_columns[grid_y];
1053  }
1054  FindPartitionMargins(columns, part);
1055  }
1056 }
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
Definition: rect.h:30
#define NULL
Definition: host.h:144
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:54
bool tesseract::ColPartitionGrid::MakeColPartSets ( PartSetVector part_sets)

Definition at line 834 of file colpartitiongrid.cpp.

834  {
835  ColPartition_LIST* part_lists = new ColPartition_LIST[gridheight()];
836  part_sets->reserve(gridheight());
837  // Iterate the ColPartitions in the grid to get parts onto lists for the
838  // y bottom of each.
839  ColPartitionGridSearch gsearch(this);
840  gsearch.StartFullSearch();
841  ColPartition* part;
842  bool any_parts_found = false;
843  while ((part = gsearch.NextFullSearch()) != NULL) {
844  BlobRegionType blob_type = part->blob_type();
845  if (blob_type != BRT_NOISE &&
846  (blob_type != BRT_UNKNOWN || !part->boxes()->singleton())) {
847  int grid_x, grid_y;
848  const TBOX& part_box = part->bounding_box();
849  GridCoords(part_box.left(), part_box.bottom(), &grid_x, &grid_y);
850  ColPartition_IT part_it(&part_lists[grid_y]);
851  part_it.add_to_end(part);
852  any_parts_found = true;
853  }
854  }
855  if (any_parts_found) {
856  for (int grid_y = 0; grid_y < gridheight(); ++grid_y) {
857  ColPartitionSet* line_set = NULL;
858  if (!part_lists[grid_y].empty()) {
859  line_set = new ColPartitionSet(&part_lists[grid_y]);
860  }
861  part_sets->push_back(line_set);
862  }
863  }
864  delete [] part_lists;
865  return any_parts_found;
866 }
BlobRegionType
Definition: blobbox.h:57
int gridheight() const
Definition: bbgrid.h:69
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
Definition: rect.h:30
#define NULL
Definition: host.h:144
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:54
ColPartitionSet * tesseract::ColPartitionGrid::MakeSingleColumnSet ( WidthCallback cb)

Definition at line 872 of file colpartitiongrid.cpp.

872  {
873  ColPartition* single_column_part = NULL;
874  // Iterate the ColPartitions in the grid to get parts onto lists for the
875  // y bottom of each.
876  ColPartitionGridSearch gsearch(this);
877  gsearch.StartFullSearch();
878  ColPartition* part;
879  while ((part = gsearch.NextFullSearch()) != NULL) {
880  BlobRegionType blob_type = part->blob_type();
881  if (blob_type != BRT_NOISE &&
882  (blob_type != BRT_UNKNOWN || !part->boxes()->singleton())) {
883  // Consider for single column.
884  BlobTextFlowType flow = part->flow();
885  if ((blob_type == BRT_TEXT &&
886  (flow == BTFT_STRONG_CHAIN || flow == BTFT_CHAIN ||
887  flow == BTFT_LEADER || flow == BTFT_TEXT_ON_IMAGE)) ||
888  blob_type == BRT_RECTIMAGE || blob_type == BRT_POLYIMAGE) {
889  if (single_column_part == NULL) {
890  single_column_part = part->ShallowCopy();
891  single_column_part->set_blob_type(BRT_TEXT);
892  // Copy the tabs from itself to properly setup the margins.
893  single_column_part->CopyLeftTab(*single_column_part, false);
894  single_column_part->CopyRightTab(*single_column_part, false);
895  } else {
896  if (part->left_key() < single_column_part->left_key())
897  single_column_part->CopyLeftTab(*part, false);
898  if (part->right_key() > single_column_part->right_key())
899  single_column_part->CopyRightTab(*part, false);
900  }
901  }
902  }
903  }
904  if (single_column_part != NULL) {
905  // Make a ColPartitionSet out of the single_column_part as a candidate
906  // for the single column case.
907  single_column_part->SetColumnGoodness(cb);
908  return new ColPartitionSet(single_column_part);
909  }
910  return NULL;
911 }
BlobRegionType
Definition: blobbox.h:57
BlobTextFlowType
Definition: blobbox.h:99
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
#define NULL
Definition: host.h:144
bool tesseract::ColPartitionGrid::MergePart ( TessResultCallback2< bool, ColPartition *, TBOX * > *  box_cb,
TessResultCallback2< bool, const ColPartition *, const ColPartition * > *  confirm_cb,
ColPartition part 
)

Definition at line 134 of file colpartitiongrid.cpp.

138  {
139  if (part->IsUnMergeableType())
140  return false;
141  bool any_done = false;
142  // Repeatedly merge part while we find a best merge candidate that works.
143  bool merge_done = false;
144  do {
145  merge_done = false;
146  TBOX box = part->bounding_box();
147  bool debug = AlignedBlob::WithinTestRegion(2, box.left(), box.bottom());
148  if (debug) {
149  tprintf("Merge candidate:");
150  box.print();
151  }
152  // Set up a rectangle search bounded by the part.
153  if (!box_cb->Run(part, &box))
154  continue;
155  // Create a list of merge candidates.
156  ColPartition_CLIST merge_candidates;
157  FindMergeCandidates(part, box, debug, &merge_candidates);
158  // Find the best merge candidate based on minimal overlap increase.
159  int overlap_increase;
160  ColPartition* neighbour = BestMergeCandidate(part, &merge_candidates, debug,
161  confirm_cb,
162  &overlap_increase);
163  if (neighbour != NULL && overlap_increase <= 0) {
164  if (debug) {
165  tprintf("Merging:hoverlap=%d, voverlap=%d, OLI=%d\n",
166  part->HCoreOverlap(*neighbour), part->VCoreOverlap(*neighbour),
167  overlap_increase);
168  }
169  // Looks like a good candidate so merge it.
170  RemoveBBox(neighbour);
171  // We will modify the box of part, so remove it from the grid, merge
172  // it and then re-insert it into the grid.
173  RemoveBBox(part);
174  part->Absorb(neighbour, NULL);
175  InsertBBox(true, true, part);
176  merge_done = true;
177  any_done = true;
178  } else if (neighbour != NULL) {
179  if (debug) {
180  tprintf("Overlapped when merged with increase %d: ", overlap_increase);
181  neighbour->bounding_box().print();
182  }
183  } else if (debug) {
184  tprintf("No candidate neighbour returned\n");
185  }
186  } while (merge_done);
187  return any_done;
188 }
virtual R Run(A1, A2)=0
static bool WithinTestRegion(int detail_level, int x, int y)
#define tprintf(...)
Definition: tprintf.h:31
void print() const
Definition: rect.h:270
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
inT16 left() const
Definition: rect.h:68
inT16 bottom() const
Definition: rect.h:61
Definition: rect.h:30
#define NULL
Definition: host.h:144
ColPartition * BestMergeCandidate(const ColPartition *part, ColPartition_CLIST *candidates, bool debug, TessResultCallback2< bool, const ColPartition *, const ColPartition * > *confirm_cb, int *overlap_increase)
void tesseract::ColPartitionGrid::Merges ( TessResultCallback2< bool, ColPartition *, TBOX * > *  box_cb,
TessResultCallback2< bool, const ColPartition *, const ColPartition * > *  confirm_cb 
)

Definition at line 113 of file colpartitiongrid.cpp.

116  {
117  // Iterate the ColPartitions in the grid.
118  ColPartitionGridSearch gsearch(this);
119  gsearch.StartFullSearch();
120  ColPartition* part;
121  while ((part = gsearch.NextFullSearch()) != NULL) {
122  if (MergePart(box_cb, confirm_cb, part))
123  gsearch.RepositionIterator();
124  }
125  delete box_cb;
126  delete confirm_cb;
127 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
#define NULL
Definition: host.h:144
bool MergePart(TessResultCallback2< bool, ColPartition *, TBOX * > *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition * > *confirm_cb, ColPartition *part)
void tesseract::ColPartitionGrid::RecomputeBounds ( int  gridsize,
const ICOORD bleft,
const ICOORD tright,
const ICOORD vertical 
)

Definition at line 988 of file colpartitiongrid.cpp.

991  {
992  ColPartition_LIST saved_parts;
993  ColPartition_IT part_it(&saved_parts);
994  // Iterate the ColPartitions in the grid to get parts onto a list.
995  ColPartitionGridSearch gsearch(this);
996  gsearch.StartFullSearch();
997  ColPartition* part;
998  while ((part = gsearch.NextFullSearch()) != NULL) {
999  part_it.add_to_end(part);
1000  }
1001  // Reinitialize grid to the new size.
1002  Init(gridsize, bleft, tright);
1003  // Recompute the bounds of the parts and put them back in the new grid.
1004  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
1005  part = part_it.extract();
1006  part->set_vertical(vertical);
1007  part->ComputeLimits();
1008  InsertBBox(true, true, part);
1009  }
1010 }
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
int gridsize() const
Definition: bbgrid.h:63
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::RefinePartitionPartners ( bool  get_desperate)

Definition at line 1344 of file colpartitiongrid.cpp.

1344  {
1345  ColPartitionGridSearch gsearch(this);
1346  // Refine in type order so that chasing multiple partners can be done
1347  // before eliminating type mis-matching partners.
1348  for (int type = PT_UNKNOWN + 1; type <= PT_COUNT; type++) {
1349  // Iterate the ColPartitions in the grid.
1350  gsearch.StartFullSearch();
1351  ColPartition* part;
1352  while ((part = gsearch.NextFullSearch()) != NULL) {
1353  part->RefinePartners(static_cast<PolyBlockType>(type),
1354  get_desperate, this);
1355  // Iterator may have been messed up by a merge.
1356  gsearch.RepositionIterator();
1357  }
1358  }
1359 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
Definition: capi.h:79
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::ReflectInYAxis ( )

Definition at line 698 of file colpartitiongrid.cpp.

698  {
699  ColPartition_LIST parts;
700  ColPartition_IT part_it(&parts);
701  // Iterate the ColPartitions in the grid to extract them.
702  ColPartitionGridSearch gsearch(this);
703  gsearch.StartFullSearch();
704  ColPartition* part;
705  while ((part = gsearch.NextFullSearch()) != NULL) {
706  part_it.add_after_then_move(part);
707  }
708  ICOORD bot_left(-tright().x(), bleft().y());
709  ICOORD top_right(-bleft().x(), tright().y());
710  // Reinitializing the grid with reflected coords also clears all the
711  // pointers, so parts will now own the ColPartitions. (Briefly).
712  Init(gridsize(), bot_left, top_right);
713  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
714  part = part_it.extract();
715  part->ReflectInYAxis();
716  InsertBBox(true, true, part);
717  }
718 }
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
int gridsize() const
Definition: bbgrid.h:63
const ICOORD & bleft() const
Definition: bbgrid.h:72
integer coordinate
Definition: points.h:30
const ICOORD & tright() const
Definition: bbgrid.h:75
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::ReTypeBlobs ( BLOBNBOX_LIST *  im_blobs)

Definition at line 927 of file colpartitiongrid.cpp.

927  {
928  BLOBNBOX_IT im_blob_it(im_blobs);
929  ColPartition_LIST dead_parts;
930  ColPartition_IT dead_part_it(&dead_parts);
931  // Iterate the ColPartitions in the grid.
932  ColPartitionGridSearch gsearch(this);
933  gsearch.StartFullSearch();
934  ColPartition* part;
935  while ((part = gsearch.NextFullSearch()) != NULL) {
936  BlobRegionType blob_type = part->blob_type();
937  BlobTextFlowType flow = part->flow();
938  bool any_blobs_moved = false;
939  if (blob_type == BRT_POLYIMAGE || blob_type == BRT_RECTIMAGE) {
940  BLOBNBOX_C_IT blob_it(part->boxes());
941  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
942  BLOBNBOX* blob = blob_it.data();
943  im_blob_it.add_after_then_move(blob);
944  }
945  } else if (blob_type != BRT_NOISE) {
946  // Make sure the blobs are marked with the correct type and flow.
947  BLOBNBOX_C_IT blob_it(part->boxes());
948  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
949  BLOBNBOX* blob = blob_it.data();
950  if (blob->region_type() == BRT_NOISE) {
951  // TODO(rays) Deprecated. Change this section to an assert to verify
952  // and then delete.
953  ASSERT_HOST(blob->cblob()->area() != 0);
954  blob->set_owner(NULL);
955  blob_it.extract();
956  any_blobs_moved = true;
957  } else {
958  blob->set_region_type(blob_type);
959  if (blob->flow() != BTFT_LEADER)
960  blob->set_flow(flow);
961  }
962  }
963  }
964  if (blob_type == BRT_NOISE || part->boxes()->empty()) {
965  BLOBNBOX_C_IT blob_it(part->boxes());
966  part->DisownBoxes();
967  dead_part_it.add_to_end(part);
968  gsearch.RemoveBBox();
969  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
970  BLOBNBOX* blob = blob_it.data();
971  if (blob->cblob()->area() == 0) {
972  // Any blob with zero area is a fake image blob and should be deleted.
973  delete blob->cblob();
974  delete blob;
975  }
976  }
977  } else if (any_blobs_moved) {
978  gsearch.RemoveBBox();
979  part->ComputeLimits();
980  InsertBBox(true, true, part);
981  gsearch.RepositionIterator();
982  }
983  }
984 }
BlobRegionType
Definition: blobbox.h:57
BlobTextFlowType
Definition: blobbox.h:99
void set_owner(tesseract::ColPartition *new_owner)
Definition: blobbox.h:340
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
#define ASSERT_HOST(x)
Definition: errcode.h:84
void set_flow(BlobTextFlowType value)
Definition: blobbox.h:283
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
BlobRegionType region_type() const
Definition: blobbox.h:268
C_BLOB * cblob() const
Definition: blobbox.h:253
void set_region_type(BlobRegionType new_type)
Definition: blobbox.h:271
#define NULL
Definition: host.h:144
BlobTextFlowType flow() const
Definition: blobbox.h:280
inT32 area()
Definition: stepblob.cpp:270
void tesseract::ColPartitionGrid::SetTabStops ( TabFind tabgrid)

Definition at line 810 of file colpartitiongrid.cpp.

810  {
811  // Iterate the ColPartitions in the grid.
812  ColPartitionGridSearch gsearch(this);
813  gsearch.StartFullSearch();
814  ColPartition* part;
815  while ((part = gsearch.NextFullSearch()) != NULL) {
816  const TBOX& part_box = part->bounding_box();
817  TabVector* left_line = tabgrid->LeftTabForBox(part_box, true, false);
818  // If the overlapping line is not a left tab, try for non-overlapping.
819  if (left_line != NULL && !left_line->IsLeftTab())
820  left_line = tabgrid->LeftTabForBox(part_box, false, false);
821  if (left_line != NULL && left_line->IsLeftTab())
822  part->SetLeftTab(left_line);
823  TabVector* right_line = tabgrid->RightTabForBox(part_box, true, false);
824  if (right_line != NULL && !right_line->IsRightTab())
825  right_line = tabgrid->RightTabForBox(part_box, false, false);
826  if (right_line != NULL && right_line->IsRightTab())
827  part->SetRightTab(right_line);
828  part->SetColumnGoodness(tabgrid->WidthCB());
829  }
830 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
Definition: rect.h:30
#define NULL
Definition: host.h:144
void tesseract::ColPartitionGrid::SplitOverlappingPartitions ( ColPartition_LIST *  big_parts)

Definition at line 526 of file colpartitiongrid.cpp.

527  {
528  int ok_overlap =
529  static_cast<int>(kTinyEnoughTextlineOverlapFraction * gridsize() + 0.5);
530  // Iterate the ColPartitions in the grid.
531  ColPartitionGridSearch gsearch(this);
532  gsearch.StartFullSearch();
533  ColPartition* part;
534  while ((part = gsearch.NextFullSearch()) != NULL) {
535  // Set up a rectangle search bounded by the part.
536  const TBOX& box = part->bounding_box();
537  ColPartitionGridSearch rsearch(this);
538  rsearch.SetUniqueMode(true);
539  rsearch.StartRectSearch(box);
540  int unresolved_overlaps = 0;
541 
542  ColPartition* neighbour;
543  while ((neighbour = rsearch.NextRectSearch()) != NULL) {
544  if (neighbour == part)
545  continue;
546  const TBOX& neighbour_box = neighbour->bounding_box();
547  if (neighbour->OKMergeOverlap(*part, *part, ok_overlap, false) &&
548  part->OKMergeOverlap(*neighbour, *neighbour, ok_overlap, false))
549  continue; // The overlap is OK both ways.
550 
551  // If removal of the biggest box from either partition eliminates the
552  // overlap, and it is much bigger than the box left behind, then
553  // it is either a drop-cap, an inter-line join, or some junk that
554  // we don't want anyway, so put it in the big_parts list.
555  if (!part->IsSingleton()) {
556  BLOBNBOX* excluded = part->BiggestBox();
557  TBOX shrunken = part->BoundsWithoutBox(excluded);
558  if (!shrunken.overlap(neighbour_box) &&
559  excluded->bounding_box().height() >
560  kBigPartSizeRatio * shrunken.height()) {
561  // Removing the biggest box fixes the overlap, so do it!
562  gsearch.RemoveBBox();
563  RemoveBadBox(excluded, part, big_parts);
564  InsertBBox(true, true, part);
565  gsearch.RepositionIterator();
566  break;
567  }
568  } else if (box.contains(neighbour_box)) {
569  ++unresolved_overlaps;
570  continue; // No amount of splitting will fix it.
571  }
572  if (!neighbour->IsSingleton()) {
573  BLOBNBOX* excluded = neighbour->BiggestBox();
574  TBOX shrunken = neighbour->BoundsWithoutBox(excluded);
575  if (!shrunken.overlap(box) &&
576  excluded->bounding_box().height() >
577  kBigPartSizeRatio * shrunken.height()) {
578  // Removing the biggest box fixes the overlap, so do it!
579  rsearch.RemoveBBox();
580  RemoveBadBox(excluded, neighbour, big_parts);
581  InsertBBox(true, true, neighbour);
582  gsearch.RepositionIterator();
583  break;
584  }
585  }
586  int part_overlap_count = part->CountOverlappingBoxes(neighbour_box);
587  int neighbour_overlap_count = neighbour->CountOverlappingBoxes(box);
588  ColPartition* right_part = NULL;
589  if (neighbour_overlap_count <= part_overlap_count ||
590  part->IsSingleton()) {
591  // Try to split the neighbour to reduce overlap.
592  BLOBNBOX* split_blob = neighbour->OverlapSplitBlob(box);
593  if (split_blob != NULL) {
594  rsearch.RemoveBBox();
595  right_part = neighbour->SplitAtBlob(split_blob);
596  InsertBBox(true, true, neighbour);
597  ASSERT_HOST(right_part != NULL);
598  }
599  } else {
600  // Try to split part to reduce overlap.
601  BLOBNBOX* split_blob = part->OverlapSplitBlob(neighbour_box);
602  if (split_blob != NULL) {
603  gsearch.RemoveBBox();
604  right_part = part->SplitAtBlob(split_blob);
605  InsertBBox(true, true, part);
606  ASSERT_HOST(right_part != NULL);
607  }
608  }
609  if (right_part != NULL) {
610  InsertBBox(true, true, right_part);
611  gsearch.RepositionIterator();
612  rsearch.RepositionIterator();
613  break;
614  }
615  }
616  if (unresolved_overlaps > 2 && part->IsSingleton()) {
617  // This part is no good so just add to big_parts.
618  RemoveBBox(part);
619  ColPartition_IT big_it(big_parts);
620  part->set_block_owned(true);
621  big_it.add_to_end(part);
622  gsearch.RepositionIterator();
623  }
624  }
625 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:913
#define ASSERT_HOST(x)
Definition: errcode.h:84
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
int gridsize() const
Definition: bbgrid.h:63
const double kTinyEnoughTextlineOverlapFraction
const double kBigPartSizeRatio
inT16 height() const
Definition: rect.h:104
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
bool overlap(const TBOX &box) const
Definition: rect.h:345

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