tesseract  3.05.02
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

◆ ColPartitionGrid() [1/2]

tesseract::ColPartitionGrid::ColPartitionGrid ( )

Definition at line 67 of file colpartitiongrid.cpp.

67  {
68 }

◆ ColPartitionGrid() [2/2]

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

Definition at line 69 of file colpartitiongrid.cpp.

71  : BBGrid<ColPartition, ColPartition_CLIST, ColPartition_C_IT>(gridsize,
72  bleft, tright) {
73 }
const ICOORD & tright() const
Definition: bbgrid.h:75
int gridsize() const
Definition: bbgrid.h:63
const ICOORD & bleft() const
Definition: bbgrid.h:72

◆ ~ColPartitionGrid()

tesseract::ColPartitionGrid::~ColPartitionGrid ( )
virtual

Definition at line 75 of file colpartitiongrid.cpp.

75  {
76 }

Member Function Documentation

◆ BestMergeCandidate()

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 409 of file colpartitiongrid.cpp.

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

◆ ClaimBoxes()

void tesseract::ColPartitionGrid::ClaimBoxes ( )

Definition at line 906 of file colpartitiongrid.cpp.

906  {
907  // Iterate the ColPartitions in the grid.
908  ColPartitionGridSearch gsearch(this);
909  gsearch.StartFullSearch();
910  ColPartition* part;
911  while ((part = gsearch.NextFullSearch()) != NULL) {
912  part->ClaimBoxes();
913  }
914 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932

◆ ComputePartitionColors()

void tesseract::ColPartitionGrid::ComputePartitionColors ( Pix *  scaled_color,
int  scaled_factor,
const FCOORD rerotation 
)

Definition at line 650 of file colpartitiongrid.cpp.

652  {
653  if (scaled_color == NULL)
654  return;
655  Pix* color_map1 = NULL;
656  Pix* color_map2 = NULL;
657  Pix* rms_map = NULL;
659  int width = pixGetWidth(scaled_color);
660  int height = pixGetHeight(scaled_color);
661  color_map1 = pixCreate(width, height, 32);
662  color_map2 = pixCreate(width, height, 32);
663  rms_map = pixCreate(width, height, 8);
664  }
665  // Iterate the ColPartitions in the grid.
666  ColPartitionGridSearch gsearch(this);
667  gsearch.StartFullSearch();
668  ColPartition* part;
669  while ((part = gsearch.NextFullSearch()) != NULL) {
670  TBOX part_box = part->bounding_box();
671  part_box.rotate_large(rerotation);
672  ImageFind::ComputeRectangleColors(part_box, scaled_color,
673  scaled_factor,
674  color_map1, color_map2, rms_map,
675  part->color1(), part->color2());
676  }
677  if (color_map1 != NULL) {
678  pixWrite("swcolorinput.png", scaled_color, IFF_PNG);
679  pixWrite("swcolor1.png", color_map1, IFF_PNG);
680  pixWrite("swcolor2.png", color_map2, IFF_PNG);
681  pixWrite("swrms.png", rms_map, IFF_PNG);
682  pixDestroy(&color_map1);
683  pixDestroy(&color_map2);
684  pixDestroy(&rms_map);
685  }
686 }
bool textord_tabfind_show_color_fit
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
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:400
void rotate_large(const FCOORD &vec)
Definition: rect.cpp:72
Definition: rect.h:30

◆ ComputeTotalOverlap()

int tesseract::ColPartitionGrid::ComputeTotalOverlap ( ColPartitionGrid **  overlap_grid)

Definition at line 322 of file colpartitiongrid.cpp.

322  {
323  int total_overlap = 0;
324  // Iterate the ColPartitions in the grid.
325  ColPartitionGridSearch gsearch(this);
326  gsearch.StartFullSearch();
327  ColPartition* part;
328  while ((part = gsearch.NextFullSearch()) != NULL) {
329  ColPartition_CLIST neighbors;
330  const TBOX& part_box = part->bounding_box();
331  FindOverlappingPartitions(part_box, part, &neighbors);
332  ColPartition_C_IT n_it(&neighbors);
333  bool any_part_overlap = false;
334  for (n_it.mark_cycle_pt(); !n_it.cycled_list(); n_it.forward()) {
335  const TBOX& n_box = n_it.data()->bounding_box();
336  int overlap = n_box.intersection(part_box).area();
337  if (overlap > 0 && overlap_grid != NULL) {
338  if (*overlap_grid == NULL) {
339  *overlap_grid = new ColPartitionGrid(gridsize(), bleft(), tright());
340  }
341  (*overlap_grid)->InsertBBox(true, true, n_it.data()->ShallowCopy());
342  if (!any_part_overlap) {
343  (*overlap_grid)->InsertBBox(true, true, part->ShallowCopy());
344  }
345  }
346  any_part_overlap = true;
347  total_overlap += overlap;
348  }
349  }
350  return total_overlap;
351 }
const ICOORD & tright() const
Definition: bbgrid.h:75
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
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 ICOORD & bleft() const
Definition: bbgrid.h:72
Definition: rect.h:30
TBOX intersection(const TBOX &box) const
Definition: rect.cpp:87

◆ DeleteNonLeaderParts()

void tesseract::ColPartitionGrid::DeleteNonLeaderParts ( )

Definition at line 1085 of file colpartitiongrid.cpp.

1085  {
1086  ColPartitionGridSearch gsearch(this);
1087  gsearch.StartFullSearch();
1088  ColPartition* part;
1089  while ((part = gsearch.NextFullSearch()) != NULL) {
1090  if (part->flow() != BTFT_LEADER) {
1091  gsearch.RemoveBBox();
1092  if (part->ReleaseNonLeaderBoxes()) {
1093  InsertBBox(true, true, part);
1094  gsearch.RepositionIterator();
1095  } else {
1096  delete part;
1097  }
1098  }
1099  }
1100 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:489

◆ DeleteParts()

void tesseract::ColPartitionGrid::DeleteParts ( )

Definition at line 1051 of file colpartitiongrid.cpp.

1051  {
1052  ColPartition_LIST dead_parts;
1053  ColPartition_IT dead_it(&dead_parts);
1054  ColPartitionGridSearch gsearch(this);
1055  gsearch.StartFullSearch();
1056  ColPartition* part;
1057  while ((part = gsearch.NextFullSearch()) != NULL) {
1058  part->DisownBoxes();
1059  dead_it.add_to_end(part); // Parts will be deleted on return.
1060  }
1061  Clear();
1062 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932

◆ DeleteUnknownParts()

void tesseract::ColPartitionGrid::DeleteUnknownParts ( TO_BLOCK block)

Definition at line 1066 of file colpartitiongrid.cpp.

1066  {
1067  ColPartitionGridSearch gsearch(this);
1068  gsearch.StartFullSearch();
1069  ColPartition* part;
1070  while ((part = gsearch.NextFullSearch()) != NULL) {
1071  if (part->blob_type() == BRT_UNKNOWN) {
1072  gsearch.RemoveBBox();
1073  // Once marked, the blobs will be swept up by DeleteUnownedNoise.
1074  part->set_flow(BTFT_NONTEXT);
1075  part->set_blob_type(BRT_NOISE);
1076  part->SetBlobTypes();
1077  part->DisownBoxes();
1078  delete part;
1079  }
1080  }
1081  block->DeleteUnownedNoise();
1082 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
void DeleteUnownedNoise()
Definition: blobbox.cpp:1033

◆ Deskew()

void tesseract::ColPartitionGrid::Deskew ( const FCOORD deskew)

Definition at line 778 of file colpartitiongrid.cpp.

778  {
779  ColPartition_LIST parts;
780  ColPartition_IT part_it(&parts);
781  // Iterate the ColPartitions in the grid to extract them.
782  ColPartitionGridSearch gsearch(this);
783  gsearch.StartFullSearch();
784  ColPartition* part;
785  while ((part = gsearch.NextFullSearch()) != NULL) {
786  part_it.add_after_then_move(part);
787  }
788  // Rebuild the grid to the new size.
789  TBOX grid_box(bleft_, tright_);
790  grid_box.rotate_large(deskew);
791  Init(gridsize(), grid_box.botleft(), grid_box.topright());
792  // Reinitializing the grid with rotated coords also clears all the
793  // pointers, so parts will now own the ColPartitions. (Briefly).
794  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
795  part = part_it.extract();
796  part->ComputeLimits();
797  InsertBBox(true, true, part);
798  }
799 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
int gridsize() const
Definition: bbgrid.h:63
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:489
Definition: rect.h:30
ICOORD tright_
Definition: bbgrid.h:91
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.h:447

◆ ExtractPartitionsAsBlocks()

void tesseract::ColPartitionGrid::ExtractPartitionsAsBlocks ( BLOCK_LIST *  blocks,
TO_BLOCK_LIST *  to_blocks 
)

Definition at line 718 of file colpartitiongrid.cpp.

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

◆ FindFigureCaptions()

void tesseract::ColPartitionGrid::FindFigureCaptions ( )

Definition at line 1103 of file colpartitiongrid.cpp.

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

◆ FindOverlappingPartitions()

void tesseract::ColPartitionGrid::FindOverlappingPartitions ( const TBOX box,
const ColPartition not_this,
ColPartition_CLIST *  parts 
)

Definition at line 356 of file colpartitiongrid.cpp.

358  {
359  ColPartitionGridSearch rsearch(this);
360  rsearch.StartRectSearch(box);
361  ColPartition* part;
362  while ((part = rsearch.NextRectSearch()) != NULL) {
363  if (part != not_this)
364  parts->add_sorted(SortByBoxLeft<ColPartition>, true, part);
365  }
366 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932

◆ FindPartitionPartners() [1/2]

void tesseract::ColPartitionGrid::FindPartitionPartners ( )

Definition at line 1228 of file colpartitiongrid.cpp.

1228  {
1229  ColPartitionGridSearch gsearch(this);
1230  gsearch.StartFullSearch();
1231  ColPartition* part;
1232  while ((part = gsearch.NextFullSearch()) != NULL) {
1233  if (part->IsVerticalType()) {
1234  FindVPartitionPartners(true, part);
1235  FindVPartitionPartners(false, part);
1236  } else {
1237  FindPartitionPartners(true, part);
1238  FindPartitionPartners(false, part);
1239  }
1240  }
1241 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
void FindVPartitionPartners(bool to_the_left, ColPartition *part)

◆ FindPartitionPartners() [2/2]

void tesseract::ColPartitionGrid::FindPartitionPartners ( bool  upper,
ColPartition part 
)

Definition at line 1245 of file colpartitiongrid.cpp.

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

◆ FindVPartitionPartners()

void tesseract::ColPartitionGrid::FindVPartitionPartners ( bool  to_the_left,
ColPartition part 
)

Definition at line 1290 of file colpartitiongrid.cpp.

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

◆ GridFindMargins()

void tesseract::ColPartitionGrid::GridFindMargins ( ColPartitionSet **  best_columns)

Definition at line 1009 of file colpartitiongrid.cpp.

1009  {
1010  // Iterate the ColPartitions in the grid.
1011  ColPartitionGridSearch gsearch(this);
1012  gsearch.StartFullSearch();
1013  ColPartition* part;
1014  while ((part = gsearch.NextFullSearch()) != NULL) {
1015  // Set up a rectangle search x-bounded by the column and y by the part.
1016  ColPartitionSet* columns = best_columns != NULL
1017  ? best_columns[gsearch.GridY()]
1018  : NULL;
1019  FindPartitionMargins(columns, part);
1020  const TBOX& box = part->bounding_box();
1021  if (AlignedBlob::WithinTestRegion(2, box.left(), box.bottom())) {
1022  tprintf("Computed margins for part:");
1023  part->Print();
1024  }
1025  }
1026 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
static bool WithinTestRegion(int detail_level, int x, int y)
inT16 bottom() const
Definition: rect.h:61
inT16 left() const
Definition: rect.h:68
#define tprintf(...)
Definition: tprintf.h:31
Definition: rect.h:30

◆ GridSmoothNeighbours()

bool tesseract::ColPartitionGrid::GridSmoothNeighbours ( BlobTextFlowType  source_type,
Pix *  nontext_map,
const TBOX im_box,
const FCOORD rerotation 
)

Definition at line 628 of file colpartitiongrid.cpp.

631  {
632  // Iterate the ColPartitions in the grid.
633  ColPartitionGridSearch gsearch(this);
634  gsearch.StartFullSearch();
635  ColPartition* part;
636  bool any_changed = false;
637  while ((part = gsearch.NextFullSearch()) != NULL) {
638  if (part->flow() != source_type || BLOBNBOX::IsLineType(part->blob_type()))
639  continue;
640  const TBOX& box = part->bounding_box();
641  bool debug = AlignedBlob::WithinTestRegion(2, box.left(), box.bottom());
642  if (SmoothRegionType(nontext_map, im_box, rotation, debug, part))
643  any_changed = true;
644  }
645  return any_changed;
646 }
static bool IsLineType(BlobRegionType type)
Definition: blobbox.h:411
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
static bool WithinTestRegion(int detail_level, int x, int y)
inT16 bottom() const
Definition: rect.h:61
inT16 left() const
Definition: rect.h:68
Definition: rect.h:30

◆ HandleClick()

void tesseract::ColPartitionGrid::HandleClick ( int  x,
int  y 
)
virtual

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

Definition at line 79 of file colpartitiongrid.cpp.

79  {
80  BBGrid<ColPartition,
81  ColPartition_CLIST, ColPartition_C_IT>::HandleClick(x, y);
82  // Run a radial search for partitions that overlap.
83  ColPartitionGridSearch radsearch(this);
84  radsearch.SetUniqueMode(true);
85  radsearch.StartRadSearch(x, y, 1);
86  ColPartition* neighbour;
87  FCOORD click(x, y);
88  while ((neighbour = radsearch.NextRadSearch()) != NULL) {
89  const TBOX& nbox = neighbour->bounding_box();
90  if (nbox.contains(click)) {
91  tprintf("Block box:");
92  neighbour->bounding_box().print();
93  neighbour->Print();
94  }
95  }
96 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
void HandleClick(int x, int y)
bool contains(const FCOORD pt) const
Definition: rect.h:323
#define tprintf(...)
Definition: tprintf.h:31
Definition: points.h:189
Definition: rect.h:30

◆ ListFindMargins()

void tesseract::ColPartitionGrid::ListFindMargins ( ColPartitionSet **  best_columns,
ColPartition_LIST *  parts 
)

Definition at line 1033 of file colpartitiongrid.cpp.

1034  {
1035  ColPartition_IT part_it(parts);
1036  for (part_it.mark_cycle_pt(); !part_it.cycled_list(); part_it.forward()) {
1037  ColPartition* part = part_it.data();
1038  ColPartitionSet* columns = NULL;
1039  if (best_columns != NULL) {
1040  const TBOX& part_box = part->bounding_box();
1041  // Get the columns from the y grid coord.
1042  int grid_x, grid_y;
1043  GridCoords(part_box.left(), part_box.bottom(), &grid_x, &grid_y);
1044  columns = best_columns[grid_y];
1045  }
1046  FindPartitionMargins(columns, part);
1047  }
1048 }
inT16 bottom() const
Definition: rect.h:61
inT16 left() const
Definition: rect.h:68
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:54
Definition: rect.h:30

◆ MakeColPartSets()

bool tesseract::ColPartitionGrid::MakeColPartSets ( PartSetVector part_sets)

Definition at line 826 of file colpartitiongrid.cpp.

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

◆ MakeSingleColumnSet()

ColPartitionSet * tesseract::ColPartitionGrid::MakeSingleColumnSet ( WidthCallback cb)

Definition at line 864 of file colpartitiongrid.cpp.

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

◆ MergePart()

bool tesseract::ColPartitionGrid::MergePart ( TessResultCallback2< bool, ColPartition *, TBOX *> *  box_cb,
TessResultCallback2< bool, const ColPartition *, const ColPartition *> *  confirm_cb,
ColPartition part 
)

Definition at line 126 of file colpartitiongrid.cpp.

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

◆ Merges()

void tesseract::ColPartitionGrid::Merges ( TessResultCallback2< bool, ColPartition *, TBOX *> *  box_cb,
TessResultCallback2< bool, const ColPartition *, const ColPartition *> *  confirm_cb 
)

Definition at line 105 of file colpartitiongrid.cpp.

108  {
109  // Iterate the ColPartitions in the grid.
110  ColPartitionGridSearch gsearch(this);
111  gsearch.StartFullSearch();
112  ColPartition* part;
113  while ((part = gsearch.NextFullSearch()) != NULL) {
114  if (MergePart(box_cb, confirm_cb, part))
115  gsearch.RepositionIterator();
116  }
117  delete box_cb;
118  delete confirm_cb;
119 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
bool MergePart(TessResultCallback2< bool, ColPartition *, TBOX *> *box_cb, TessResultCallback2< bool, const ColPartition *, const ColPartition *> *confirm_cb, ColPartition *part)

◆ RecomputeBounds()

void tesseract::ColPartitionGrid::RecomputeBounds ( int  gridsize,
const ICOORD bleft,
const ICOORD tright,
const ICOORD vertical 
)

Definition at line 980 of file colpartitiongrid.cpp.

983  {
984  ColPartition_LIST saved_parts;
985  ColPartition_IT part_it(&saved_parts);
986  // Iterate the ColPartitions in the grid to get parts onto a list.
987  ColPartitionGridSearch gsearch(this);
988  gsearch.StartFullSearch();
989  ColPartition* part;
990  while ((part = gsearch.NextFullSearch()) != NULL) {
991  part_it.add_to_end(part);
992  }
993  // Reinitialize grid to the new size.
995  // Recompute the bounds of the parts and put them back in the new grid.
996  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
997  part = part_it.extract();
998  part->set_vertical(vertical);
999  part->ComputeLimits();
1000  InsertBBox(true, true, part);
1001  }
1002 }
const ICOORD & tright() const
Definition: bbgrid.h:75
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
int gridsize() const
Definition: bbgrid.h:63
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:489
const ICOORD & bleft() const
Definition: bbgrid.h:72
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.h:447

◆ RefinePartitionPartners()

void tesseract::ColPartitionGrid::RefinePartitionPartners ( bool  get_desperate)

Definition at line 1336 of file colpartitiongrid.cpp.

1336  {
1337  ColPartitionGridSearch gsearch(this);
1338  // Refine in type order so that chasing multiple partners can be done
1339  // before eliminating type mis-matching partners.
1340  for (int type = PT_UNKNOWN + 1; type <= PT_COUNT; type++) {
1341  // Iterate the ColPartitions in the grid.
1342  gsearch.StartFullSearch();
1343  ColPartition* part;
1344  while ((part = gsearch.NextFullSearch()) != NULL) {
1345  part->RefinePartners(static_cast<PolyBlockType>(type),
1346  get_desperate, this);
1347  // Iterator may have been messed up by a merge.
1348  gsearch.RepositionIterator();
1349  }
1350  }
1351 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
Definition: capi.h:98

◆ ReflectInYAxis()

void tesseract::ColPartitionGrid::ReflectInYAxis ( )

Definition at line 690 of file colpartitiongrid.cpp.

690  {
691  ColPartition_LIST parts;
692  ColPartition_IT part_it(&parts);
693  // Iterate the ColPartitions in the grid to extract them.
694  ColPartitionGridSearch gsearch(this);
695  gsearch.StartFullSearch();
696  ColPartition* part;
697  while ((part = gsearch.NextFullSearch()) != NULL) {
698  part_it.add_after_then_move(part);
699  }
700  ICOORD bot_left(-tright().x(), bleft().y());
701  ICOORD top_right(-bleft().x(), tright().y());
702  // Reinitializing the grid with reflected coords also clears all the
703  // pointers, so parts will now own the ColPartitions. (Briefly).
704  Init(gridsize(), bot_left, top_right);
705  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
706  part = part_it.extract();
707  part->ReflectInYAxis();
708  InsertBBox(true, true, part);
709  }
710 }
const ICOORD & tright() const
Definition: bbgrid.h:75
integer coordinate
Definition: points.h:30
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
int gridsize() const
Definition: bbgrid.h:63
void InsertBBox(bool h_spread, bool v_spread, ColPartition *bbox)
Definition: bbgrid.h:489
const ICOORD & bleft() const
Definition: bbgrid.h:72
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.h:447

◆ ReTypeBlobs()

void tesseract::ColPartitionGrid::ReTypeBlobs ( BLOBNBOX_LIST *  im_blobs)

Definition at line 919 of file colpartitiongrid.cpp.

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

◆ SetTabStops()

void tesseract::ColPartitionGrid::SetTabStops ( TabFind tabgrid)

Definition at line 802 of file colpartitiongrid.cpp.

802  {
803  // Iterate the ColPartitions in the grid.
804  ColPartitionGridSearch gsearch(this);
805  gsearch.StartFullSearch();
806  ColPartition* part;
807  while ((part = gsearch.NextFullSearch()) != NULL) {
808  const TBOX& part_box = part->bounding_box();
809  TabVector* left_line = tabgrid->LeftTabForBox(part_box, true, false);
810  // If the overlapping line is not a left tab, try for non-overlapping.
811  if (left_line != NULL && !left_line->IsLeftTab())
812  left_line = tabgrid->LeftTabForBox(part_box, false, false);
813  if (left_line != NULL && left_line->IsLeftTab())
814  part->SetLeftTab(left_line);
815  TabVector* right_line = tabgrid->RightTabForBox(part_box, true, false);
816  if (right_line != NULL && !right_line->IsRightTab())
817  right_line = tabgrid->RightTabForBox(part_box, false, false);
818  if (right_line != NULL && right_line->IsRightTab())
819  part->SetRightTab(right_line);
820  part->SetColumnGoodness(tabgrid->WidthCB());
821  }
822 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
Definition: rect.h:30

◆ SplitOverlappingPartitions()

void tesseract::ColPartitionGrid::SplitOverlappingPartitions ( ColPartition_LIST *  big_parts)

Definition at line 518 of file colpartitiongrid.cpp.

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

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