22 #pragma warning(disable:4244) // Conversion warnings 60 false,
"Show partition bounds");
62 false,
"Show blobs rejected as noise");
64 "Show partition bounds, waiting if >1");
79 int resolution,
bool cjk_script,
80 double aligned_gap_fraction,
81 TabVector_LIST* vlines, TabVector_LIST* hlines,
82 int vertical_x,
int vertical_y)
83 :
TabFind(gridsize, bleft, tright, vlines, vertical_x, vertical_y,
85 cjk_script_(cjk_script),
87 mean_column_gap_(tright.x() - bleft.x()),
88 tabfind_aligned_gap_fraction_(aligned_gap_fraction),
89 reskew_(1.0f, 0.0f), rotation_(1.0f, 0.0f), rerotate_(1.0f, 0.0f),
90 best_columns_(NULL), stroke_width_(NULL),
91 part_grid_(gridsize, bleft, tright), nontext_map_(NULL),
92 projection_(resolution),
93 denorm_(NULL), input_blobs_win_(NULL), equation_detect_(NULL) {
94 TabVector_IT h_it(&horizontal_lines_);
95 h_it.add_list_after(hlines);
100 if (best_columns_ != NULL) {
101 delete [] best_columns_;
103 if (stroke_width_ != NULL)
104 delete stroke_width_;
105 delete input_blobs_win_;
106 pixDestroy(&nontext_map_);
107 while (denorm_ != NULL) {
108 DENORM* dead_denorm = denorm_;
115 ColPartition_IT part_it(&noise_parts_);
116 for (part_it.mark_cycle_pt(); !part_it.cycled_list(); part_it.forward()) {
123 part_it.set_to_list(&good_parts_);
124 for (part_it.mark_cycle_pt(); !part_it.cycled_list(); part_it.forward()) {
132 BLOBNBOX_IT bb_it(&image_bblobs_);
133 for (bb_it.mark_cycle_pt(); !bb_it.cycled_list(); bb_it.forward()) {
135 delete bblob->
cblob();
150 if (stroke_width_ != NULL)
151 delete stroke_width_;
155 #ifndef GRAPHICS_DISABLED 157 input_blobs_win_ =
MakeWindow(0, 0,
"Filtered Input Blobs");
160 #endif // GRAPHICS_DISABLED 162 pixDestroy(&nontext_map_);
168 photo_mask_pix, input_block);
172 stroke_width_->
Clear();
186 BLOBNBOX_CLIST* osd_blobs) {
203 bool vertical_text_lines,
204 int recognition_rotation) {
205 const FCOORD anticlockwise90(0.0f, 1.0f);
206 const FCOORD clockwise90(0.0f, -1.0f);
207 const FCOORD rotation180(-1.0f, 0.0f);
208 const FCOORD norotation(1.0f, 0.0f);
210 text_rotation_ = norotation;
213 rotation_ = norotation;
214 if (recognition_rotation == 1) {
215 rotation_ = anticlockwise90;
216 }
else if (recognition_rotation == 2) {
217 rotation_ = rotation180;
218 }
else if (recognition_rotation == 3) {
219 rotation_ = clockwise90;
225 if (recognition_rotation & 1) {
226 vertical_text_lines = !vertical_text_lines;
232 if (vertical_text_lines) {
233 rotation_.
rotate(anticlockwise90);
234 text_rotation_.
rotate(clockwise90);
237 rerotate_ =
FCOORD(rotation_.
x(), -rotation_.
y());
238 if (rotation_.
x() != 1.0f || rotation_.
y() != 0.0f) {
254 tprintf(
"Vertical=%d, orientation=%d, final rotation=(%f, %f)+(%f,%f)\n",
255 vertical_text_lines, recognition_rotation,
256 rotation_.
x(), rotation_.
y(),
257 text_rotation_.
x(), text_rotation_.
y());
263 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f);
291 int scaled_factor,
TO_BLOCK* input_block,
292 Pix* photo_mask_pix, Pix* thresholds_pix,
293 Pix* grey_pix, BLOCK_LIST* blocks,
294 BLOBNBOX_LIST* diacritic_blobs,
295 TO_BLOCK_LIST* to_blocks) {
296 pixOr(photo_mask_pix, photo_mask_pix, nontext_map_);
303 pageseg_mode, rerotate_, input_block, nontext_map_, denorm_, cjk_script_,
304 &projection_, diacritic_blobs, &part_grid_, &big_parts_);
307 input_block,
this, &part_grid_, &big_parts_);
311 input_block,
this, &part_grid_, &big_parts_);
317 ColPartition_IT p_it(&big_parts_);
318 for (p_it.mark_cycle_pt(); !p_it.cycled_list(); p_it.forward())
319 p_it.data()->DisownBoxesNoAssert();
321 delete stroke_width_;
322 stroke_width_ = NULL;
348 ReflectForRtl(input_block, &image_bblobs_);
361 min_gutter_width_, tabfind_aligned_gap_fraction_,
362 &part_grid_, &deskew_, &reskew_);
366 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f);
367 denorm_ = new_denorm;
373 if (!MakeColumns(
false)) {
382 #ifndef GRAPHICS_DISABLED 387 #endif // GRAPHICS_DISABLED 393 GridSplitPartitions();
397 GridMergePartitions();
400 InsertRemainingNoise(input_block);
402 GridInsertHLinePartitions();
403 GridInsertVLinePartitions();
415 if (equation_detect_) {
430 GridRemoveUnderlinePartitions();
440 #ifndef GRAPHICS_DISABLED 443 if (window != NULL) {
455 #endif // GRAPHICS_DISABLED 461 ReleaseBlobsAndCleanupUnused(input_block);
469 TransformToBlocks(blocks, to_blocks);
471 tprintf(
"Found %d blocks, %d to_blocks\n",
472 blocks->length(), to_blocks->length());
475 DisplayBlocks(blocks);
476 RotateAndReskewBlocks(input_is_rtl, to_blocks);
478 #ifndef GRAPHICS_DISABLED 479 if (blocks_win_ != NULL) {
480 bool waiting =
false;
484 if (event->type ==
SVET_INPUT && event->parameter != NULL) {
485 if (*event->parameter ==
'd')
497 #endif // GRAPHICS_DISABLED 505 deskew->
set_y(-deskew->
y());
509 equation_detect_ = detect;
515 void ColumnFinder::DisplayBlocks(BLOCK_LIST* blocks) {
516 #ifndef GRAPHICS_DISABLED 518 if (blocks_win_ == NULL)
521 blocks_win_->
Clear();
527 BLOCK_IT block_it(blocks);
529 for (block_it.mark_cycle_pt(); !block_it.cycled_list();
530 block_it.forward()) {
531 BLOCK* block = block_it.data();
532 block->
plot(blocks_win_, serial++,
543 void ColumnFinder::DisplayColumnBounds(
PartSetVector* sets) {
544 #ifndef GRAPHICS_DISABLED 553 ColPartitionSet* columns = best_columns_[i];
562 bool ColumnFinder::MakeColumns(
bool single_column) {
567 if (!single_column) {
572 bool good_only =
true;
575 ColPartitionSet* line_set = part_sets.get(i);
576 if (line_set != NULL && line_set->LegalColumnCandidate()) {
577 ColPartitionSet* column_candidate = line_set->Copy(good_only);
578 if (column_candidate != NULL)
579 column_candidate->AddToColumnSetsIfUnique(&column_sets_,
WidthCB());
582 good_only = !good_only;
583 }
while (column_sets_.
empty() && !good_only);
585 PrintColumnCandidates(
"Column candidates");
587 ImproveColumnCandidates(&column_sets_, &column_sets_);
589 PrintColumnCandidates(
"Improved columns");
591 ImproveColumnCandidates(&part_sets, &column_sets_);
593 ColPartitionSet* single_column_set =
595 if (single_column_set != NULL) {
601 PrintColumnCandidates(
"Final Columns");
602 bool has_columns = !column_sets_.
empty();
605 bool any_multi_column = AssignColumns(part_sets);
607 DisplayColumnBounds(&part_sets);
609 ComputeMeanColumnGap(any_multi_column);
611 for (
int i = 0; i < part_sets.size(); ++i) {
612 ColPartitionSet* line_set = part_sets.get(i);
613 if (line_set != NULL) {
614 line_set->RelinquishParts();
625 void ColumnFinder::ImproveColumnCandidates(
PartSetVector* src_sets,
628 temp_cols.
move(column_sets);
629 if (src_sets == column_sets)
630 src_sets = &temp_cols;
631 int set_size = temp_cols.size();
633 bool good_only =
true;
635 for (
int i = 0; i < set_size; ++i) {
636 ColPartitionSet* column_candidate = temp_cols.get(i);
638 ColPartitionSet* improved = column_candidate->Copy(good_only);
639 if (improved != NULL) {
640 improved->ImproveColumnCandidate(
WidthCB(), src_sets);
641 improved->AddToColumnSetsIfUnique(column_sets,
WidthCB());
644 good_only = !good_only;
645 }
while (column_sets->empty() && !good_only);
646 if (column_sets->empty())
647 column_sets->move(&temp_cols);
649 temp_cols.delete_data_pointers();
653 void ColumnFinder::PrintColumnCandidates(
const char* title) {
654 int set_size = column_sets_.
size();
655 tprintf(
"Found %d %s:\n", set_size, title);
657 for (
int i = 0; i < set_size; ++i) {
658 ColPartitionSet* column_set = column_sets_.
get(i);
673 bool ColumnFinder::AssignColumns(
const PartSetVector& part_sets) {
674 int set_count = part_sets.size();
677 best_columns_ =
new ColPartitionSet*[set_count];
678 for (
int y = 0; y < set_count; ++y)
679 best_columns_[y] = NULL;
680 int column_count = column_sets_.
size();
690 bool* any_columns_possible =
new bool[set_count];
691 int* assigned_costs =
new int[set_count];
692 int** column_set_costs =
new int*[set_count];
695 for (
int part_i = 0; part_i < set_count; ++part_i) {
696 ColPartitionSet* line_set = part_sets.get(part_i);
697 bool debug = line_set != NULL &&
699 line_set->bounding_box().bottom());
700 column_set_costs[part_i] =
new int[column_count];
701 any_columns_possible[part_i] =
false;
703 for (
int col_i = 0; col_i < column_count; ++col_i) {
704 if (line_set != NULL &&
705 column_sets_.
get(col_i)->CompatibleColumns(debug, line_set,
707 column_set_costs[part_i][col_i] =
708 column_sets_.
get(col_i)->UnmatchedWidth(line_set);
709 any_columns_possible[part_i] =
true;
711 column_set_costs[part_i][col_i] =
MAX_INT32;
713 tprintf(
"Set id %d did not match at y=%d, lineset =%p\n",
714 col_i, part_i, line_set);
718 bool any_multi_column =
false;
722 while (BiggestUnassignedRange(set_count, any_columns_possible,
725 tprintf(
"Biggest unassigned range = %d- %d\n", start, end);
727 int column_set_id = RangeModalColumnSet(column_set_costs,
728 assigned_costs, start, end);
730 tprintf(
"Range modal column id = %d\n", column_set_id);
731 column_sets_.
get(column_set_id)->Print();
734 ShrinkRangeToLongestRun(column_set_costs, assigned_costs,
735 any_columns_possible,
736 column_set_id, &start, &end);
738 tprintf(
"Shrunk range = %d- %d\n", start, end);
742 ExtendRangePastSmallGaps(column_set_costs, assigned_costs,
743 any_columns_possible,
744 column_set_id, -1, -1, &start);
746 ExtendRangePastSmallGaps(column_set_costs, assigned_costs,
747 any_columns_possible,
748 column_set_id, 1, set_count, &end);
751 tprintf(
"Column id %d applies to range = %d - %d\n",
752 column_set_id, start, end);
754 AssignColumnToRange(column_set_id, start, end, column_set_costs,
756 if (column_sets_.
get(column_set_id)->GoodColumnCount() > 1)
757 any_multi_column =
true;
761 if (best_columns_[0] == NULL) {
762 AssignColumnToRange(0, 0,
gridheight_, column_set_costs, assigned_costs);
765 for (
int i = 0; i < set_count; ++i) {
766 delete [] column_set_costs[i];
768 delete [] assigned_costs;
769 delete [] any_columns_possible;
770 delete [] column_set_costs;
771 return any_multi_column;
776 bool ColumnFinder::BiggestUnassignedRange(
int set_count,
777 const bool* any_columns_possible,
778 int* best_start,
int* best_end) {
779 int best_range_size = 0;
780 *best_start = set_count;
781 *best_end = set_count;
783 for (
int start = 0; start <
gridheight_; start = end) {
785 while (start < set_count) {
786 if (best_columns_[start] == NULL && any_columns_possible[start])
793 while (end < set_count) {
794 if (best_columns_[end] != NULL)
796 if (any_columns_possible[end])
800 if (start < set_count && range_size > best_range_size) {
801 best_range_size = range_size;
806 return *best_start < *best_end;
810 int ColumnFinder::RangeModalColumnSet(
int** column_set_costs,
811 const int* assigned_costs,
812 int start,
int end) {
813 int column_count = column_sets_.
size();
814 STATS column_stats(0, column_count);
815 for (
int part_i = start; part_i < end; ++part_i) {
816 for (
int col_j = 0; col_j < column_count; ++col_j) {
817 if (column_set_costs[part_i][col_j] < assigned_costs[part_i])
818 column_stats.add(col_j, 1);
822 return column_stats.mode();
829 void ColumnFinder::ShrinkRangeToLongestRun(
int** column_set_costs,
830 const int* assigned_costs,
831 const bool* any_columns_possible,
833 int* best_start,
int* best_end) {
835 int orig_start = *best_start;
836 int orig_end = *best_end;
837 int best_range_size = 0;
838 *best_start = orig_end;
839 *best_end = orig_end;
841 for (
int start = orig_start; start < orig_end; start = end) {
843 while (start < orig_end) {
844 if (column_set_costs[start][column_set_id] < assigned_costs[start] ||
845 !any_columns_possible[start])
851 while (end < orig_end) {
852 if (column_set_costs[end][column_set_id] >= assigned_costs[start] &&
853 any_columns_possible[end])
857 if (start < orig_end && end - start > best_range_size) {
858 best_range_size = end - start;
868 void ColumnFinder::ExtendRangePastSmallGaps(
int** column_set_costs,
869 const int* assigned_costs,
870 const bool* any_columns_possible,
872 int step,
int end,
int* start) {
874 tprintf(
"Starting expansion at %d, step=%d, limit=%d\n",
879 int barrier_size = 0;
885 for (i = *start + step; i != end; i += step) {
886 if (column_set_costs[i][column_set_id] < assigned_costs[i])
889 if (any_columns_possible[i])
893 tprintf(
"At %d, Barrier size=%d\n", i, barrier_size);
903 for (i += step; i != end; i += step) {
904 if (column_set_costs[i][column_set_id] < assigned_costs[i])
906 else if (any_columns_possible[i])
910 tprintf(
"At %d, good size = %d\n", i, good_size);
912 if (good_size >= barrier_size)
914 }
while (good_size >= barrier_size);
918 void ColumnFinder::AssignColumnToRange(
int column_set_id,
int start,
int end,
919 int** column_set_costs,
920 int* assigned_costs) {
921 ColPartitionSet* column_set = column_sets_.
get(column_set_id);
922 for (
int i = start; i < end; ++i) {
923 assigned_costs[i] = column_set_costs[i][column_set_id];
924 best_columns_[i] = column_set;
929 void ColumnFinder::ComputeMeanColumnGap(
bool any_multi_column) {
933 int width_samples = 0;
941 mean_column_gap_ = any_multi_column && gap_samples > 0
942 ? total_gap / gap_samples : total_width / width_samples;
950 static void ReleaseAllBlobsAndDeleteUnused(BLOBNBOX_LIST* blobs) {
951 for (BLOBNBOX_IT blob_it(blobs); !blob_it.empty(); blob_it.forward()) {
953 if (blob->
owner() == NULL) {
954 delete blob->
cblob();
963 void ColumnFinder::ReleaseBlobsAndCleanupUnused(
TO_BLOCK* block) {
964 ReleaseAllBlobsAndDeleteUnused(&block->
blobs);
965 ReleaseAllBlobsAndDeleteUnused(&block->
small_blobs);
966 ReleaseAllBlobsAndDeleteUnused(&block->
noise_blobs);
967 ReleaseAllBlobsAndDeleteUnused(&block->
large_blobs);
968 ReleaseAllBlobsAndDeleteUnused(&image_bblobs_);
972 void ColumnFinder::GridSplitPartitions() {
974 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
975 gsearch(&part_grid_);
976 gsearch.StartFullSearch();
977 ColPartition* dont_repeat = NULL;
979 while ((part = gsearch.NextFullSearch()) != NULL) {
980 if (part->blob_type() <
BRT_UNKNOWN || part == dont_repeat)
982 ColPartitionSet* column_set = best_columns_[gsearch.GridY()];
986 part->ColumnRange(
resolution_, column_set, &first_col, &last_col);
995 if (last_col != first_col + 1)
998 int y = part->MidY();
999 TBOX margin_box = part->bounding_box();
1003 tprintf(
"Considering partition for GridSplit:");
1006 ColPartition* column = column_set->GetColumnByIndex(first_col);
1009 margin_box.
set_left(column->RightAtY(y) + 2);
1010 column = column_set->GetColumnByIndex(last_col);
1013 margin_box.
set_right(column->LeftAtY(y) - 2);
1017 GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> rectsearch(
this);
1019 tprintf(
"Searching box (%d,%d)->(%d,%d)\n",
1021 margin_box.
right(), margin_box.
top());
1024 rectsearch.StartRectSearch(margin_box);
1026 while ((bbox = rectsearch.NextRectSearch()) != NULL) {
1032 gsearch.RemoveBBox();
1033 int x_middle = (margin_box.
left() + margin_box.
right()) / 2;
1035 tprintf(
"Splitting part at %d:", x_middle);
1038 ColPartition* split_part = part->SplitAt(x_middle);
1039 if (split_part != NULL) {
1043 split_part->Print();
1045 part_grid_.
InsertBBox(
true,
true, split_part);
1049 tprintf(
"Split had no effect\n");
1053 gsearch.RepositionIterator();
1055 tprintf(
"Part cannot be split: blob (%d,%d)->(%d,%d) in column gap\n",
1064 void ColumnFinder::GridMergePartitions() {
1066 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1067 gsearch(&part_grid_);
1068 gsearch.StartFullSearch();
1070 while ((part = gsearch.NextFullSearch()) != NULL) {
1071 if (part->IsUnMergeableType())
1074 ColPartitionSet* columns = best_columns_[gsearch.GridY()];
1075 TBOX box = part->bounding_box();
1078 tprintf(
"Considering part for merge at:");
1081 int y = part->MidY();
1082 ColPartition* left_column = columns->ColumnContaining(box.
left(), y);
1083 ColPartition* right_column = columns->ColumnContaining(box.
right(), y);
1084 if (left_column == NULL || right_column != left_column) {
1086 tprintf(
"In different columns\n");
1089 box.
set_left(left_column->LeftAtY(y));
1090 box.
set_right(right_column->RightAtY(y));
1092 bool modified_box =
false;
1093 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1094 rsearch(&part_grid_);
1095 rsearch.SetUniqueMode(
true);
1096 rsearch.StartRectSearch(box);
1097 ColPartition* neighbour;
1099 while ((neighbour = rsearch.NextRectSearch()) != NULL) {
1100 if (neighbour == part || neighbour->IsUnMergeableType())
1102 const TBOX& neighbour_box = neighbour->bounding_box();
1104 tprintf(
"Considering merge with neighbour at:");
1107 if (neighbour_box.
right() < box.
left() ||
1110 if (part->VSignificantCoreOverlap(*neighbour) &&
1111 part->TypesMatch(*neighbour)) {
1117 const TBOX& part_box = part->bounding_box();
1120 if (neighbour_box.
left() > part->right_margin() &&
1121 part_box.
right() < neighbour->left_margin())
1123 if (neighbour_box.
right() < part->left_margin() &&
1124 part_box.
left() > neighbour->right_margin())
1126 int h_gap =
MAX(part_box.
left(), neighbour_box.
left()) -
1129 part_box.
width() < mean_column_gap_ ||
1130 neighbour_box.
width() < mean_column_gap_) {
1132 tprintf(
"Running grid-based merge between:\n");
1136 rsearch.RemoveBBox();
1137 if (!modified_box) {
1139 gsearch.RemoveBBox();
1140 rsearch.RepositionIterator();
1141 modified_box =
true;
1143 part->Absorb(neighbour,
WidthCB());
1145 tprintf(
"Neighbour failed hgap test\n");
1148 tprintf(
"Neighbour failed overlap or typesmatch test\n");
1159 gsearch.RepositionIterator();
1166 void ColumnFinder::InsertRemainingNoise(
TO_BLOCK* block) {
1168 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
1170 if (blob->
owner() != NULL)
continue;
1176 rsearch.SetUniqueMode(
true);
1177 rsearch.StartRectSearch(search_box);
1179 ColPartition* best_part = NULL;
1180 int best_distance = 0;
1181 while ((part = rsearch.NextRectSearch()) != NULL) {
1182 if (part->IsUnMergeableType())
1186 if (best_part == NULL || distance < best_distance) {
1188 best_distance = distance;
1191 if (best_part != NULL &&
1192 best_distance < kMaxDistToPartSizeRatio * best_part->median_size()) {
1195 tprintf(
"Adding noise blob with distance %d, thr=%g:box:",
1203 best_part->AddBox(blob);
1204 part_grid_.
InsertBBox(
true,
true, best_part);
1218 static TBOX BoxFromHLine(
const TabVector* hline) {
1219 int top =
MAX(hline->startpt().y(), hline->endpt().y());
1220 int bottom =
MIN(hline->startpt().y(), hline->endpt().y());
1221 top += hline->mean_width();
1222 if (top == bottom) {
1228 return TBOX(hline->startpt().x(), bottom, hline->endpt().x(), top);
1233 void ColumnFinder::GridRemoveUnderlinePartitions() {
1234 TabVector_IT hline_it(&horizontal_lines_);
1235 for (hline_it.mark_cycle_pt(); !hline_it.cycled_list(); hline_it.forward()) {
1236 TabVector* hline = hline_it.data();
1237 if (hline->intersects_other_lines())
1239 TBOX line_box = BoxFromHLine(hline);
1240 TBOX search_box = line_box;
1243 part_search.SetUniqueMode(
true);
1244 part_search.StartRectSearch(search_box);
1245 ColPartition* covered;
1246 bool touched_table =
false;
1247 bool touched_text =
false;
1248 ColPartition* line_part = NULL;
1249 while ((covered = part_search.NextRectSearch()) != NULL) {
1251 touched_table =
true;
1253 }
else if (covered->IsTextType()) {
1255 int text_bottom = covered->median_bottom();
1256 if (line_box.
bottom() <= text_bottom && text_bottom <= search_box.
top())
1257 touched_text =
true;
1258 }
else if (covered->blob_type() ==
BRT_HLINE &&
1259 line_box.
contains(covered->bounding_box())) {
1260 line_part = covered;
1263 if (line_part != NULL && !touched_table && touched_text) {
1271 void ColumnFinder::GridInsertHLinePartitions() {
1272 TabVector_IT hline_it(&horizontal_lines_);
1273 for (hline_it.mark_cycle_pt(); !hline_it.cycled_list(); hline_it.forward()) {
1274 TabVector* hline = hline_it.data();
1275 TBOX line_box = BoxFromHLine(hline);
1280 bool any_image =
false;
1282 part_search.SetUniqueMode(
true);
1283 part_search.StartRectSearch(line_box);
1284 ColPartition* covered;
1285 while ((covered = part_search.NextRectSearch()) != NULL) {
1286 if (covered->IsImageType()) {
1299 void ColumnFinder::GridInsertVLinePartitions() {
1301 for (vline_it.mark_cycle_pt(); !vline_it.cycled_list(); vline_it.forward()) {
1302 TabVector* vline = vline_it.data();
1303 if (!vline->IsSeparator())
1305 int left =
MIN(vline->startpt().x(), vline->endpt().x());
1306 int right =
MAX(vline->startpt().x(), vline->endpt().x());
1307 right += vline->mean_width();
1308 if (left == right) {
1316 left, vline->startpt().
y(), right, vline->endpt().y());
1318 bool any_image =
false;
1320 part_search.SetUniqueMode(
true);
1321 part_search.StartRectSearch(part->bounding_box());
1322 ColPartition* covered;
1323 while ((covered = part_search.NextRectSearch()) != NULL) {
1324 if (covered->IsImageType()) {
1338 void ColumnFinder::SetPartitionTypes() {
1339 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1340 gsearch(&part_grid_);
1341 gsearch.StartFullSearch();
1343 while ((part = gsearch.NextFullSearch()) != NULL) {
1344 part->SetPartitionType(
resolution_, best_columns_[gsearch.GridY()]);
1350 void ColumnFinder::SmoothPartnerRuns() {
1352 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1353 gsearch(&part_grid_);
1354 gsearch.StartFullSearch();
1356 while ((part = gsearch.NextFullSearch()) != NULL) {
1357 ColPartition* partner = part->SingletonPartner(
true);
1358 if (partner != NULL) {
1359 if (partner->SingletonPartner(
false) != part) {
1360 tprintf(
"Ooops! Partition:(%d partners)",
1361 part->upper_partners()->length());
1363 tprintf(
"has singleton partner:(%d partners",
1364 partner->lower_partners()->length());
1366 tprintf(
"but its singleton partner is:");
1367 if (partner->SingletonPartner(
false) == NULL)
1370 partner->SingletonPartner(
false)->Print();
1372 ASSERT_HOST(partner->SingletonPartner(
false) == part);
1373 }
else if (part->SingletonPartner(
false) != NULL) {
1374 ColPartitionSet* column_set = best_columns_[gsearch.GridY()];
1376 part->SmoothPartnerRun(column_count * 2 + 1);
1383 void ColumnFinder::AddToTempPartList(ColPartition* part,
1384 ColPartition_CLIST* temp_list) {
1385 int mid_y = part->MidY();
1386 ColPartition_C_IT it(temp_list);
1387 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1388 ColPartition* test_part = it.data();
1391 if (test_part == part->SingletonPartner(
false))
1393 int neighbour_bottom = test_part->median_bottom();
1394 int neighbour_top = test_part->median_top();
1395 int neighbour_y = (neighbour_bottom + neighbour_top) / 2;
1396 if (neighbour_y < mid_y)
1398 if (!part->HOverlaps(*test_part) && !part->WithinSameMargins(*test_part))
1401 if (it.cycled_list()) {
1402 it.add_to_end(part);
1404 it.add_before_stay_put(part);
1409 void ColumnFinder::EmptyTempPartList(ColPartition_CLIST* temp_list,
1410 WorkingPartSet_LIST* work_set) {
1411 ColPartition_C_IT it(temp_list);
1412 while (!it.empty()) {
1414 &good_parts_, work_set);
1420 void ColumnFinder::TransformToBlocks(BLOCK_LIST* blocks,
1421 TO_BLOCK_LIST* to_blocks) {
1422 WorkingPartSet_LIST work_set;
1423 ColPartitionSet* column_set = NULL;
1424 ColPartition_IT noise_it(&noise_parts_);
1428 ColPartition_CLIST temp_part_list;
1430 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1431 gsearch(&part_grid_);
1432 gsearch.StartFullSearch();
1433 int prev_grid_y = -1;
1435 while ((part = gsearch.NextFullSearch()) != NULL) {
1436 int grid_y = gsearch.GridY();
1437 if (grid_y != prev_grid_y) {
1438 EmptyTempPartList(&temp_part_list, &work_set);
1439 prev_grid_y = grid_y;
1441 if (best_columns_[grid_y] != column_set) {
1442 column_set = best_columns_[grid_y];
1446 &good_parts_, &work_set);
1448 tprintf(
"Changed column groups at grid index %d, y=%d\n",
1449 gsearch.GridY(), gsearch.GridY() *
gridsize());
1452 noise_it.add_to_end(part);
1454 AddToTempPartList(part, &temp_part_list);
1457 EmptyTempPartList(&temp_part_list, &work_set);
1459 WorkingPartSet_IT work_it(&work_set);
1460 while (!work_it.empty()) {
1461 WorkingPartSet* working_set = work_it.extract();
1463 &good_parts_, blocks, to_blocks);
1471 static void ReflectBlobList(BLOBNBOX_LIST* bblobs) {
1472 BLOBNBOX_IT it(bblobs);
1473 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1474 it.data()->reflect_box_in_y_axis();
1484 void ColumnFinder::ReflectForRtl(
TO_BLOCK* input_block, BLOBNBOX_LIST* bblobs) {
1485 ReflectBlobList(bblobs);
1486 ReflectBlobList(&input_block->
blobs);
1493 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f);
1494 denorm_ = new_denorm;
1500 static void RotateAndExplodeBlobList(
const FCOORD& blob_rotation,
1501 BLOBNBOX_LIST* bblobs,
1504 BLOBNBOX_IT it(bblobs);
1505 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1508 C_OUTLINE_LIST* outlines = cblob->
out_list();
1509 C_OUTLINE_IT ol_it(outlines);
1510 if (!outlines->singleton()) {
1513 for (;!ol_it.empty(); ol_it.forward()) {
1519 it.add_after_stay_put(new_blob);
1525 if (blob_rotation.
x() != 1.0f || blob_rotation.
y() != 0.0f) {
1526 cblob->
rotate(blob_rotation);
1547 void ColumnFinder::RotateAndReskewBlocks(
bool input_is_rtl,
1548 TO_BLOCK_LIST* blocks) {
1555 TO_BLOCK_IT it(blocks);
1556 int block_index = 1;
1557 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1571 FCOORD blob_rotation = ComputeBlockAndClassifyRotation(block);
1576 RotateAndExplodeBlobList(blob_rotation, &to_block->
blobs,
1578 TO_ROW_IT row_it(to_block->
get_rows());
1579 for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
1580 TO_ROW* row = row_it.data();
1581 RotateAndExplodeBlobList(blob_rotation, row->
blob_list(),
1585 static_cast<int>(heights.
median() + 0.5));
1587 tprintf(
"Block median size = (%d, %d)\n",
1597 FCOORD ColumnFinder::ComputeBlockAndClassifyRotation(
BLOCK* block) {
1606 FCOORD classify_rotation(text_rotation_);
1607 FCOORD block_rotation(1.0f, 0.0f);
1613 if (rerotate_.
x() == 0.0f)
1614 block_rotation = rerotate_;
1616 block_rotation =
FCOORD(0.0f, -1.0f);
1617 block->
rotate(block_rotation);
1618 classify_rotation =
FCOORD(1.0f, 0.0f);
1620 block_rotation.rotate(rotation_);
1624 FCOORD blob_rotation(block_rotation);
1625 block_rotation.set_y(-block_rotation.y());
1629 tprintf(
"Blk %d, type %d rerotation(%.2f, %.2f), char(%.2f,%.2f), box:",
1632 classify_rotation.x(), classify_rotation.y());
1635 return blob_rotation;
bool textord_tabfind_show_initial_partitions
bool PSM_COL_FIND_ENABLED(int pageseg_mode)
tesseract::ColPartition * owner() const
const TBOX & bounding_box() const
void Image(struct Pix *image, int x_pos, int y_pos)
void CorrectOrientation(TO_BLOCK *block, bool vertical_text_lines, int recognition_rotation)
void SetBlockRuleEdges(TO_BLOCK *block)
bool textord_tabfind_show_blocks
bool overlap(const TBOX &box) const
void rotate(const FCOORD &rotation)
void SetEquationDetect(EquationDetectBase *detect)
void FindFigureCaptions()
void SetupAndFilterNoise(PageSegMode pageseg_mode, Pix *photo_mask_pix, TO_BLOCK *input_block)
const ICOORD & tright() const
void rotate(const FCOORD &rotation)
bool textord_debug_images
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
void TidyBlobs(TO_BLOCK *block)
void ResetForVerticalText(const FCOORD &rotate, const FCOORD &rerotate, TabVector_LIST *horizontal_lines, int *min_gutter_width)
static bool WithinTestRegion(int detail_level, int x, int y)
bool TestVerticalTextDirection(double find_vertical_text_ratio, TO_BLOCK *block, BLOBNBOX_CLIST *osd_blobs)
int textord_tabfind_show_partitions
const ICOORD & median_size() const
static void TransferImagePartsToImageMask(const FCOORD &rerotation, ColPartitionGrid *part_grid, Pix *image_mask)
const DENORM * predecessor() const
void SetTabStops(TabFind *tabgrid)
void add(inT32 value, inT32 count)
void GridFindMargins(ColPartitionSet **best_columns)
ScrollView * DisplayTabVectors(ScrollView *tab_win)
void RemoveLineResidue(ColPartition_LIST *big_part_list)
void ReSetAndReFilterBlobs()
void LocateTables(ColPartitionGrid *grid, ColPartitionSet **columns, WidthCallback *width_cb, const FCOORD &reskew)
bool FindTabVectors(TabVector_LIST *hlines, BLOBNBOX_LIST *image_blobs, TO_BLOCK *block, int min_gutter_width, double tabfind_aligned_gap_fraction, ColPartitionGrid *part_grid, FCOORD *deskew, FCOORD *reskew)
bool PSM_SPARSE(int pageseg_mode)
void SetupNormalization(const BLOCK *block, const FCOORD *rotation, const DENORM *predecessor, float x_origin, float y_origin, float x_scale, float y_scale, float final_xshift, float final_yshift)
PolyBlockType isA() const
bool IsVerticallyAlignedText(double find_vertical_text_ratio, TO_BLOCK *block, BLOBNBOX_CLIST *osd_blobs)
void set_flow(BlobTextFlowType value)
C_OUTLINE_LIST * out_list()
void set_skew(const FCOORD &skew)
bool textord_tabfind_find_tables
void move(GenericVector< T > *from)
const double kMinGutterWidthGrid
ColPartitionSet * MakeSingleColumnSet(WidthCallback *cb)
bool MakeColPartSets(PartSetVector *part_sets)
void DisplayBoxes(ScrollView *window)
void Init(int grid_size, const ICOORD &bottom_left, const ICOORD &top_right)
void set_right_to_left(bool value)
void DeleteUnownedNoise()
BLOBNBOX_LIST * blob_list()
void GetDeskewVectors(FCOORD *deskew, FCOORD *reskew)
void AccumulateColumnWidthsAndGaps(int *total_width, int *width_samples, int *total_gap, int *gap_samples)
POLY_BLOCK * poly_block() const
void SetNeighboursOnMediumBlobs(TO_BLOCK *block)
static const STRING & textord_debug_pix()
void set_index(int value)
FCOORD re_rotation() const
void DeleteUnknownParts(TO_BLOCK *block)
TabVector_LIST * dead_vectors()
void AddToColumnSetsIfUnique(PartSetVector *column_sets, WidthCallback *cb)
void FindLeaderPartitions(TO_BLOCK *block, ColPartitionGrid *part_grid)
const double kHorizontalGapMergeFraction
void rotate(const FCOORD vec)
void set_left_to_right_language(bool order)
BLOBNBOX_LIST noise_blobs
ScrollView * MakeWindow(int x, int y, const char *window_name)
void RemoveBBox(BBC *bbox)
ColumnFinder(int gridsize, const ICOORD &bleft, const ICOORD &tright, int resolution, bool cjk_script, double aligned_gap_fraction, TabVector_LIST *vlines, TabVector_LIST *hlines, int vertical_x, int vertical_y)
bool contains(const FCOORD pt) const
inT16 x() const
access function
void plot(ScrollView *window, inT32 serial, ScrollView::Color colour)
void set_re_rotation(const FCOORD &rotation)
void DisplayColumnEdges(int y_bottom, int y_top, ScrollView *win)
void InsertBlobsToGrid(bool h_spread, bool v_spread, BLOBNBOX_LIST *blobs, BBGrid< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT > *grid)
BLOBNBOX_LIST large_blobs
SVEvent * AwaitEvent(SVEventType type)
void set_y(float yin)
rewrite function
Pix * ComputeNonTextMask(bool debug, Pix *photo_map, TO_BLOCK *blob_block)
void FindTextlineDirectionAndFixBrokenCJK(PageSegMode pageseg_mode, bool cjk_merge, TO_BLOCK *input_block)
void delete_data_pointers()
const double kMaxDistToPartSizeRatio
static void FindImagePartitions(Pix *image_pix, const FCOORD &rotation, const FCOORD &rerotation, TO_BLOCK *block, TabFind *tab_grid, ColPartitionGrid *part_grid, ColPartition_LIST *big_parts)
void ReTypeBlobs(BLOBNBOX_LIST *im_blobs)
void AssertNoDuplicates()
bool textord_debug_printable
virtual int FindEquationParts(ColPartitionGrid *part_grid, ColPartitionSet **best_columns)=0
int textord_debug_tabfind
static BLOBNBOX * RealBlob(C_OUTLINE *outline)
void GradeBlobsIntoPartitions(PageSegMode pageseg_mode, const FCOORD &rerotation, TO_BLOCK *block, Pix *nontext_pix, const DENORM *denorm, bool cjk_script, TextlineProjection *projection, BLOBNBOX_LIST *diacritic_blobs, ColPartitionGrid *part_grid, ColPartition_LIST *big_parts)
static ColPartition * MakeLinePartition(BlobRegionType blob_type, const ICOORD &vertical, int left, int bottom, int right, int top)
void CorrectForRotation(const FCOORD &rerotation, ColPartitionGrid *part_grid)
const ICOORD & image_origin() const
BLOBNBOX_LIST small_blobs
void set_resolution(int resolution)
void set_classify_rotation(const FCOORD &rotation)
void plot_graded_blobs(ScrollView *to_win)
void ComputeEdgeOffsets(Pix *thresholds, Pix *grey)
void compute_bounding_box()
bool right_to_left() const
void pad(int xpad, int ypad)
void reflect_polygon_in_y_axis()
void InsertBBox(bool h_spread, bool v_spread, BBC *bbox)
ScrollView * FindInitialTabVectors(BLOBNBOX_LIST *image_blobs, int min_gutter_width, double tabfind_aligned_gap_fraction, TO_BLOCK *block)
const ICOORD & bleft() const
void InsertCleanPartitions(ColPartitionGrid *grid, TO_BLOCK *block)
WidthCallback * WidthCB()
GenericVector< ColPartitionSet * > PartSetVector
void set_region_type(BlobRegionType new_type)
void set_owner(tesseract::ColPartition *new_owner)
void RefinePartitionPartners(bool get_desperate)
int DistanceOfBoxFromPartition(const TBOX &box, const ColPartition &part, const DENORM *denorm, bool debug) const
void set_median_size(int x, int y)
const int kMaxIncompatibleColumnCount
#define BOOL_VAR(name, val, comment)
bool textord_tabfind_show_reject_blobs
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
static void RotateBlobList(const FCOORD &rotation, BLOBNBOX_LIST *blobs)
void DontFindTabVectors(BLOBNBOX_LIST *image_blobs, TO_BLOCK *block, FCOORD *deskew, FCOORD *reskew)
void set_type(PolyBlockType t)
int FindBlocks(PageSegMode pageseg_mode, Pix *scaled_color, int scaled_factor, TO_BLOCK *block, Pix *photo_mask_pix, Pix *thresholds_pix, Pix *grey_pix, BLOCK_LIST *blocks, BLOBNBOX_LIST *diacritic_blobs, TO_BLOCK_LIST *to_blocks)
void ExtractPartitionsAsBlocks(BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
#define INT_VAR(name, val, comment)
bool textord_tabfind_show_columns
void FindPartitionPartners()
inT16 y() const
access_function