23 #include "config_auto.h" 58 false,
"Show partition bounds");
60 false,
"Show blobs rejected as noise");
62 "Show partition bounds, waiting if >1");
67 ScrollView* ColumnFinder::blocks_win_ =
nullptr;
77 int resolution,
bool cjk_script,
78 double aligned_gap_fraction,
79 TabVector_LIST* vlines, TabVector_LIST* hlines,
80 int vertical_x,
int vertical_y)
81 :
TabFind(gridsize, bleft, tright, vlines, vertical_x, vertical_y,
83 cjk_script_(cjk_script),
85 mean_column_gap_(tright.x() - bleft.x()),
86 tabfind_aligned_gap_fraction_(aligned_gap_fraction),
88 reskew_(1.0f, 0.0f), rotation_(1.0f, 0.0f), rerotate_(1.0f, 0.0f),
89 text_rotation_(0.0f, 0.0f),
90 best_columns_(nullptr), stroke_width_(nullptr),
91 part_grid_(gridsize, bleft, tright), nontext_map_(nullptr),
92 projection_(resolution),
93 denorm_(nullptr), input_blobs_win_(nullptr), equation_detect_(nullptr) {
94 TabVector_IT h_it(&horizontal_lines_);
95 h_it.add_list_after(hlines);
100 delete [] best_columns_;
101 delete stroke_width_;
102 delete input_blobs_win_;
103 pixDestroy(&nontext_map_);
104 while (denorm_ !=
nullptr) {
105 DENORM* dead_denorm = denorm_;
112 ColPartition_IT part_it(&noise_parts_);
113 for (part_it.mark_cycle_pt(); !part_it.cycled_list(); part_it.forward()) {
120 part_it.set_to_list(&good_parts_);
121 for (part_it.mark_cycle_pt(); !part_it.cycled_list(); part_it.forward()) {
129 BLOBNBOX_IT bb_it(&image_bblobs_);
130 for (bb_it.mark_cycle_pt(); !bb_it.cycled_list(); bb_it.forward()) {
132 delete bblob->
cblob();
147 delete stroke_width_;
151 #ifndef GRAPHICS_DISABLED 153 input_blobs_win_ =
MakeWindow(0, 0,
"Filtered Input Blobs");
156 #endif // GRAPHICS_DISABLED 158 pixDestroy(&nontext_map_);
164 photo_mask_pix, input_block);
168 stroke_width_->
Clear();
182 BLOBNBOX_CLIST* osd_blobs) {
199 bool vertical_text_lines,
200 int recognition_rotation) {
201 const FCOORD anticlockwise90(0.0f, 1.0f);
202 const FCOORD clockwise90(0.0f, -1.0f);
203 const FCOORD rotation180(-1.0f, 0.0f);
204 const FCOORD norotation(1.0f, 0.0f);
206 text_rotation_ = norotation;
209 rotation_ = norotation;
210 if (recognition_rotation == 1) {
211 rotation_ = anticlockwise90;
212 }
else if (recognition_rotation == 2) {
213 rotation_ = rotation180;
214 }
else if (recognition_rotation == 3) {
215 rotation_ = clockwise90;
221 if (recognition_rotation & 1) {
222 vertical_text_lines = !vertical_text_lines;
228 if (vertical_text_lines) {
229 rotation_.
rotate(anticlockwise90);
230 text_rotation_.
rotate(clockwise90);
233 rerotate_ =
FCOORD(rotation_.
x(), -rotation_.
y());
234 if (rotation_.
x() != 1.0f || rotation_.
y() != 0.0f) {
250 tprintf(
"Vertical=%d, orientation=%d, final rotation=(%f, %f)+(%f,%f)\n",
251 vertical_text_lines, recognition_rotation,
252 rotation_.
x(), rotation_.
y(),
253 text_rotation_.
x(), text_rotation_.
y());
259 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f);
287 int scaled_factor,
TO_BLOCK* input_block,
288 Pix* photo_mask_pix, Pix* thresholds_pix,
290 BLOCK_LIST* blocks, BLOBNBOX_LIST* diacritic_blobs,
291 TO_BLOCK_LIST* to_blocks) {
292 pixOr(photo_mask_pix, photo_mask_pix, nontext_map_);
299 pageseg_mode, rerotate_, input_block, nontext_map_, denorm_, cjk_script_,
300 &projection_, diacritic_blobs, &part_grid_, &big_parts_);
303 input_block,
this, pixa_debug, &part_grid_,
308 input_block,
this, pixa_debug, &part_grid_,
315 ColPartition_IT p_it(&big_parts_);
316 for (p_it.mark_cycle_pt(); !p_it.cycled_list(); p_it.forward())
317 p_it.data()->DisownBoxesNoAssert();
319 delete stroke_width_;
320 stroke_width_ =
nullptr;
346 ReflectForRtl(input_block, &image_bblobs_);
359 min_gutter_width_, tabfind_aligned_gap_fraction_,
360 &part_grid_, &deskew_, &reskew_);
364 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f);
365 denorm_ = new_denorm;
371 if (!MakeColumns(
false)) {
380 #ifndef GRAPHICS_DISABLED 385 #endif // GRAPHICS_DISABLED 391 GridSplitPartitions();
395 GridMergePartitions();
398 InsertRemainingNoise(input_block);
400 GridInsertHLinePartitions();
401 GridInsertVLinePartitions();
413 if (equation_detect_) {
428 GridRemoveUnderlinePartitions();
438 #ifndef GRAPHICS_DISABLED 441 if (window !=
nullptr) {
450 #endif // GRAPHICS_DISABLED 456 ReleaseBlobsAndCleanupUnused(input_block);
464 TransformToBlocks(blocks, to_blocks);
466 tprintf(
"Found %d blocks, %d to_blocks\n",
467 blocks->length(), to_blocks->length());
470 DisplayBlocks(blocks);
471 RotateAndReskewBlocks(input_is_rtl, to_blocks);
473 #ifndef GRAPHICS_DISABLED 474 if (blocks_win_ !=
nullptr) {
475 bool waiting =
false;
479 if (event->type ==
SVET_INPUT && event->parameter !=
nullptr) {
480 if (*event->parameter ==
'd')
485 blocks_win_ =
nullptr;
492 #endif // GRAPHICS_DISABLED 500 deskew->
set_y(-deskew->
y());
504 equation_detect_ = detect;
510 void ColumnFinder::DisplayBlocks(BLOCK_LIST* blocks) {
511 #ifndef GRAPHICS_DISABLED 513 if (blocks_win_ ==
nullptr)
516 blocks_win_->
Clear();
518 BLOCK_IT block_it(blocks);
520 for (block_it.mark_cycle_pt(); !block_it.cycled_list();
521 block_it.forward()) {
522 BLOCK* block = block_it.data();
534 void ColumnFinder::DisplayColumnBounds(
PartSetVector* sets) {
535 #ifndef GRAPHICS_DISABLED 540 ColPartitionSet* columns = best_columns_[i];
541 if (columns !=
nullptr)
549 bool ColumnFinder::MakeColumns(
bool single_column) {
554 if (!single_column) {
559 bool good_only =
true;
562 ColPartitionSet* line_set = part_sets.get(i);
563 if (line_set !=
nullptr && line_set->LegalColumnCandidate()) {
564 ColPartitionSet* column_candidate = line_set->Copy(good_only);
565 if (column_candidate !=
nullptr)
566 column_candidate->AddToColumnSetsIfUnique(&column_sets_,
WidthCB());
569 good_only = !good_only;
570 }
while (column_sets_.
empty() && !good_only);
572 PrintColumnCandidates(
"Column candidates");
574 ImproveColumnCandidates(&column_sets_, &column_sets_);
576 PrintColumnCandidates(
"Improved columns");
578 ImproveColumnCandidates(&part_sets, &column_sets_);
580 ColPartitionSet* single_column_set =
582 if (single_column_set !=
nullptr) {
588 PrintColumnCandidates(
"Final Columns");
589 bool has_columns = !column_sets_.
empty();
592 bool any_multi_column = AssignColumns(part_sets);
594 DisplayColumnBounds(&part_sets);
596 ComputeMeanColumnGap(any_multi_column);
598 for (
int i = 0; i < part_sets.size(); ++i) {
599 ColPartitionSet* line_set = part_sets.get(i);
600 if (line_set !=
nullptr) {
601 line_set->RelinquishParts();
612 void ColumnFinder::ImproveColumnCandidates(
PartSetVector* src_sets,
615 temp_cols.
move(column_sets);
616 if (src_sets == column_sets)
617 src_sets = &temp_cols;
618 int set_size = temp_cols.size();
620 bool good_only =
true;
622 for (
int i = 0; i < set_size; ++i) {
623 ColPartitionSet* column_candidate = temp_cols.get(i);
625 ColPartitionSet* improved = column_candidate->Copy(good_only);
626 if (improved !=
nullptr) {
627 improved->ImproveColumnCandidate(
WidthCB(), src_sets);
628 improved->AddToColumnSetsIfUnique(column_sets,
WidthCB());
631 good_only = !good_only;
632 }
while (column_sets->empty() && !good_only);
633 if (column_sets->empty())
634 column_sets->move(&temp_cols);
636 temp_cols.delete_data_pointers();
640 void ColumnFinder::PrintColumnCandidates(
const char* title) {
641 int set_size = column_sets_.
size();
642 tprintf(
"Found %d %s:\n", set_size, title);
644 for (
int i = 0; i < set_size; ++i) {
645 ColPartitionSet* column_set = column_sets_.
get(i);
660 bool ColumnFinder::AssignColumns(
const PartSetVector& part_sets) {
661 int set_count = part_sets.size();
664 best_columns_ =
new ColPartitionSet*[set_count];
665 for (
int y = 0; y < set_count; ++y)
666 best_columns_[y] =
nullptr;
667 int column_count = column_sets_.
size();
677 bool* any_columns_possible =
new bool[set_count];
678 int* assigned_costs =
new int[set_count];
679 int** column_set_costs =
new int*[set_count];
682 for (
int part_i = 0; part_i < set_count; ++part_i) {
683 ColPartitionSet* line_set = part_sets.get(part_i);
684 bool debug = line_set !=
nullptr &&
686 line_set->bounding_box().bottom());
687 column_set_costs[part_i] =
new int[column_count];
688 any_columns_possible[part_i] =
false;
689 assigned_costs[part_i] = INT32_MAX;
690 for (
int col_i = 0; col_i < column_count; ++col_i) {
691 if (line_set !=
nullptr &&
692 column_sets_.
get(col_i)->CompatibleColumns(debug, line_set,
694 column_set_costs[part_i][col_i] =
695 column_sets_.
get(col_i)->UnmatchedWidth(line_set);
696 any_columns_possible[part_i] =
true;
698 column_set_costs[part_i][col_i] = INT32_MAX;
700 tprintf(
"Set id %d did not match at y=%d, lineset =%p\n",
701 col_i, part_i, line_set);
705 bool any_multi_column =
false;
709 while (BiggestUnassignedRange(set_count, any_columns_possible,
712 tprintf(
"Biggest unassigned range = %d- %d\n", start, end);
714 int column_set_id = RangeModalColumnSet(column_set_costs,
715 assigned_costs, start, end);
717 tprintf(
"Range modal column id = %d\n", column_set_id);
718 column_sets_.
get(column_set_id)->Print();
721 ShrinkRangeToLongestRun(column_set_costs, assigned_costs,
722 any_columns_possible,
723 column_set_id, &start, &end);
725 tprintf(
"Shrunk range = %d- %d\n", start, end);
729 ExtendRangePastSmallGaps(column_set_costs, assigned_costs,
730 any_columns_possible,
731 column_set_id, -1, -1, &start);
733 ExtendRangePastSmallGaps(column_set_costs, assigned_costs,
734 any_columns_possible,
735 column_set_id, 1, set_count, &end);
738 tprintf(
"Column id %d applies to range = %d - %d\n",
739 column_set_id, start, end);
741 AssignColumnToRange(column_set_id, start, end, column_set_costs,
743 if (column_sets_.
get(column_set_id)->GoodColumnCount() > 1)
744 any_multi_column =
true;
748 if (best_columns_[0] ==
nullptr) {
749 AssignColumnToRange(0, 0,
gridheight_, column_set_costs, assigned_costs);
752 for (
int i = 0; i < set_count; ++i) {
753 delete [] column_set_costs[i];
755 delete [] assigned_costs;
756 delete [] any_columns_possible;
757 delete [] column_set_costs;
758 return any_multi_column;
763 bool ColumnFinder::BiggestUnassignedRange(
int set_count,
764 const bool* any_columns_possible,
765 int* best_start,
int* best_end) {
766 int best_range_size = 0;
767 *best_start = set_count;
768 *best_end = set_count;
770 for (
int start = 0; start <
gridheight_; start = end) {
772 while (start < set_count) {
773 if (best_columns_[start] ==
nullptr && any_columns_possible[start])
780 while (end < set_count) {
781 if (best_columns_[end] !=
nullptr)
783 if (any_columns_possible[end])
787 if (start < set_count && range_size > best_range_size) {
788 best_range_size = range_size;
793 return *best_start < *best_end;
797 int ColumnFinder::RangeModalColumnSet(
int** column_set_costs,
798 const int* assigned_costs,
799 int start,
int end) {
800 int column_count = column_sets_.
size();
801 STATS column_stats(0, column_count);
802 for (
int part_i = start; part_i < end; ++part_i) {
803 for (
int col_j = 0; col_j < column_count; ++col_j) {
804 if (column_set_costs[part_i][col_j] < assigned_costs[part_i])
805 column_stats.add(col_j, 1);
809 return column_stats.mode();
816 void ColumnFinder::ShrinkRangeToLongestRun(
int** column_set_costs,
817 const int* assigned_costs,
818 const bool* any_columns_possible,
820 int* best_start,
int* best_end) {
822 int orig_start = *best_start;
823 int orig_end = *best_end;
824 int best_range_size = 0;
825 *best_start = orig_end;
826 *best_end = orig_end;
828 for (
int start = orig_start; start < orig_end; start = end) {
830 while (start < orig_end) {
831 if (column_set_costs[start][column_set_id] < assigned_costs[start] ||
832 !any_columns_possible[start])
838 while (end < orig_end) {
839 if (column_set_costs[end][column_set_id] >= assigned_costs[start] &&
840 any_columns_possible[end])
844 if (start < orig_end && end - start > best_range_size) {
845 best_range_size = end - start;
855 void ColumnFinder::ExtendRangePastSmallGaps(
int** column_set_costs,
856 const int* assigned_costs,
857 const bool* any_columns_possible,
859 int step,
int end,
int* start) {
861 tprintf(
"Starting expansion at %d, step=%d, limit=%d\n",
866 int barrier_size = 0;
872 for (i = *start + step; i != end; i += step) {
873 if (column_set_costs[i][column_set_id] < assigned_costs[i])
876 if (any_columns_possible[i])
880 tprintf(
"At %d, Barrier size=%d\n", i, barrier_size);
890 for (i += step; i != end; i += step) {
891 if (column_set_costs[i][column_set_id] < assigned_costs[i])
893 else if (any_columns_possible[i])
897 tprintf(
"At %d, good size = %d\n", i, good_size);
899 if (good_size >= barrier_size)
901 }
while (good_size >= barrier_size);
905 void ColumnFinder::AssignColumnToRange(
int column_set_id,
int start,
int end,
906 int** column_set_costs,
907 int* assigned_costs) {
908 ColPartitionSet* column_set = column_sets_.
get(column_set_id);
909 for (
int i = start; i < end; ++i) {
910 assigned_costs[i] = column_set_costs[i][column_set_id];
911 best_columns_[i] = column_set;
916 void ColumnFinder::ComputeMeanColumnGap(
bool any_multi_column) {
920 int width_samples = 0;
928 mean_column_gap_ = any_multi_column && gap_samples > 0
929 ? total_gap / gap_samples : width_samples > 0
930 ? total_width / width_samples : 0;
938 static void ReleaseAllBlobsAndDeleteUnused(BLOBNBOX_LIST* blobs) {
939 for (BLOBNBOX_IT blob_it(blobs); !blob_it.empty(); blob_it.forward()) {
941 if (blob->
owner() ==
nullptr) {
942 delete blob->
cblob();
951 void ColumnFinder::ReleaseBlobsAndCleanupUnused(
TO_BLOCK* block) {
952 ReleaseAllBlobsAndDeleteUnused(&block->
blobs);
953 ReleaseAllBlobsAndDeleteUnused(&block->
small_blobs);
954 ReleaseAllBlobsAndDeleteUnused(&block->
noise_blobs);
955 ReleaseAllBlobsAndDeleteUnused(&block->
large_blobs);
956 ReleaseAllBlobsAndDeleteUnused(&image_bblobs_);
960 void ColumnFinder::GridSplitPartitions() {
962 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
963 gsearch(&part_grid_);
964 gsearch.StartFullSearch();
965 ColPartition* dont_repeat =
nullptr;
967 while ((part = gsearch.NextFullSearch()) !=
nullptr) {
968 if (part->blob_type() <
BRT_UNKNOWN || part == dont_repeat)
970 ColPartitionSet* column_set = best_columns_[gsearch.GridY()];
974 part->ColumnRange(
resolution_, column_set, &first_col, &last_col);
983 if (last_col != first_col + 1)
986 int y = part->MidY();
987 TBOX margin_box = part->bounding_box();
991 tprintf(
"Considering partition for GridSplit:");
994 ColPartition* column = column_set->GetColumnByIndex(first_col);
995 if (column ==
nullptr)
997 margin_box.
set_left(column->RightAtY(y) + 2);
998 column = column_set->GetColumnByIndex(last_col);
999 if (column ==
nullptr)
1001 margin_box.
set_right(column->LeftAtY(y) - 2);
1005 GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> rectsearch(
this);
1007 tprintf(
"Searching box (%d,%d)->(%d,%d)\n",
1009 margin_box.
right(), margin_box.
top());
1012 rectsearch.StartRectSearch(margin_box);
1014 while ((bbox = rectsearch.NextRectSearch()) !=
nullptr) {
1018 if (bbox ==
nullptr) {
1020 gsearch.RemoveBBox();
1021 int x_middle = (margin_box.
left() + margin_box.
right()) / 2;
1023 tprintf(
"Splitting part at %d:", x_middle);
1026 ColPartition* split_part = part->SplitAt(x_middle);
1027 if (split_part !=
nullptr) {
1031 split_part->Print();
1033 part_grid_.
InsertBBox(
true,
true, split_part);
1037 tprintf(
"Split had no effect\n");
1041 gsearch.RepositionIterator();
1043 tprintf(
"Part cannot be split: blob (%d,%d)->(%d,%d) in column gap\n",
1052 void ColumnFinder::GridMergePartitions() {
1054 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1055 gsearch(&part_grid_);
1056 gsearch.StartFullSearch();
1058 while ((part = gsearch.NextFullSearch()) !=
nullptr) {
1059 if (part->IsUnMergeableType())
1062 ColPartitionSet* columns = best_columns_[gsearch.GridY()];
1063 TBOX box = part->bounding_box();
1066 tprintf(
"Considering part for merge at:");
1069 int y = part->MidY();
1070 ColPartition* left_column = columns->ColumnContaining(box.
left(), y);
1071 ColPartition* right_column = columns->ColumnContaining(box.
right(), y);
1072 if (left_column ==
nullptr || right_column != left_column) {
1074 tprintf(
"In different columns\n");
1077 box.
set_left(left_column->LeftAtY(y));
1078 box.
set_right(right_column->RightAtY(y));
1080 bool modified_box =
false;
1081 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1082 rsearch(&part_grid_);
1083 rsearch.SetUniqueMode(
true);
1084 rsearch.StartRectSearch(box);
1085 ColPartition* neighbour;
1087 while ((neighbour = rsearch.NextRectSearch()) !=
nullptr) {
1088 if (neighbour == part || neighbour->IsUnMergeableType())
1090 const TBOX& neighbour_box = neighbour->bounding_box();
1092 tprintf(
"Considering merge with neighbour at:");
1095 if (neighbour_box.
right() < box.
left() ||
1098 if (part->VSignificantCoreOverlap(*neighbour) &&
1099 part->TypesMatch(*neighbour)) {
1105 const TBOX& part_box = part->bounding_box();
1108 if (neighbour_box.
left() > part->right_margin() &&
1109 part_box.
right() < neighbour->left_margin())
1111 if (neighbour_box.
right() < part->left_margin() &&
1112 part_box.
left() > neighbour->right_margin())
1114 int h_gap = std::max(part_box.
left(), neighbour_box.
left()) -
1115 std::min(part_box.
right(), neighbour_box.
right());
1117 part_box.
width() < mean_column_gap_ ||
1118 neighbour_box.
width() < mean_column_gap_) {
1120 tprintf(
"Running grid-based merge between:\n");
1124 rsearch.RemoveBBox();
1125 if (!modified_box) {
1127 gsearch.RemoveBBox();
1128 rsearch.RepositionIterator();
1129 modified_box =
true;
1131 part->Absorb(neighbour,
WidthCB());
1133 tprintf(
"Neighbour failed hgap test\n");
1136 tprintf(
"Neighbour failed overlap or typesmatch test\n");
1147 gsearch.RepositionIterator();
1154 void ColumnFinder::InsertRemainingNoise(
TO_BLOCK* block) {
1156 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
1158 if (blob->
owner() !=
nullptr)
continue;
1164 rsearch.SetUniqueMode(
true);
1165 rsearch.StartRectSearch(search_box);
1167 ColPartition* best_part =
nullptr;
1168 int best_distance = 0;
1169 while ((part = rsearch.NextRectSearch()) !=
nullptr) {
1170 if (part->IsUnMergeableType())
1174 if (best_part ==
nullptr || distance < best_distance) {
1176 best_distance = distance;
1179 if (best_part !=
nullptr &&
1180 best_distance < kMaxDistToPartSizeRatio * best_part->median_height()) {
1183 tprintf(
"Adding noise blob with distance %d, thr=%g:box:",
1191 best_part->AddBox(blob);
1192 part_grid_.
InsertBBox(
true,
true, best_part);
1206 static TBOX BoxFromHLine(
const TabVector* hline) {
1207 int top = std::max(hline->startpt().y(), hline->endpt().y());
1208 int bottom = std::min(hline->startpt().y(), hline->endpt().y());
1209 top += hline->mean_width();
1210 if (top == bottom) {
1216 return TBOX(hline->startpt().x(), bottom, hline->endpt().x(), top);
1221 void ColumnFinder::GridRemoveUnderlinePartitions() {
1222 TabVector_IT hline_it(&horizontal_lines_);
1223 for (hline_it.mark_cycle_pt(); !hline_it.cycled_list(); hline_it.forward()) {
1224 TabVector* hline = hline_it.data();
1225 if (hline->intersects_other_lines())
1227 TBOX line_box = BoxFromHLine(hline);
1228 TBOX search_box = line_box;
1231 part_search.SetUniqueMode(
true);
1232 part_search.StartRectSearch(search_box);
1233 ColPartition* covered;
1234 bool touched_table =
false;
1235 bool touched_text =
false;
1236 ColPartition* line_part =
nullptr;
1237 while ((covered = part_search.NextRectSearch()) !=
nullptr) {
1239 touched_table =
true;
1241 }
else if (covered->IsTextType()) {
1243 int text_bottom = covered->median_bottom();
1244 if (line_box.
bottom() <= text_bottom && text_bottom <= search_box.
top())
1245 touched_text =
true;
1246 }
else if (covered->blob_type() ==
BRT_HLINE &&
1247 line_box.
contains(covered->bounding_box())) {
1248 line_part = covered;
1251 if (line_part !=
nullptr && !touched_table && touched_text) {
1259 void ColumnFinder::GridInsertHLinePartitions() {
1260 TabVector_IT hline_it(&horizontal_lines_);
1261 for (hline_it.mark_cycle_pt(); !hline_it.cycled_list(); hline_it.forward()) {
1262 TabVector* hline = hline_it.data();
1263 TBOX line_box = BoxFromHLine(hline);
1268 bool any_image =
false;
1270 part_search.SetUniqueMode(
true);
1271 part_search.StartRectSearch(line_box);
1272 ColPartition* covered;
1273 while ((covered = part_search.NextRectSearch()) !=
nullptr) {
1274 if (covered->IsImageType()) {
1287 void ColumnFinder::GridInsertVLinePartitions() {
1289 for (vline_it.mark_cycle_pt(); !vline_it.cycled_list(); vline_it.forward()) {
1290 TabVector* vline = vline_it.data();
1291 if (!vline->IsSeparator())
1293 int left = std::min(vline->startpt().x(), vline->endpt().x());
1294 int right = std::max(vline->startpt().x(), vline->endpt().x());
1295 right += vline->mean_width();
1296 if (left == right) {
1304 left, vline->startpt().
y(), right, vline->endpt().y());
1306 bool any_image =
false;
1308 part_search.SetUniqueMode(
true);
1309 part_search.StartRectSearch(part->bounding_box());
1310 ColPartition* covered;
1311 while ((covered = part_search.NextRectSearch()) !=
nullptr) {
1312 if (covered->IsImageType()) {
1326 void ColumnFinder::SetPartitionTypes() {
1327 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1328 gsearch(&part_grid_);
1329 gsearch.StartFullSearch();
1331 while ((part = gsearch.NextFullSearch()) !=
nullptr) {
1332 part->SetPartitionType(
resolution_, best_columns_[gsearch.GridY()]);
1338 void ColumnFinder::SmoothPartnerRuns() {
1340 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1341 gsearch(&part_grid_);
1342 gsearch.StartFullSearch();
1344 while ((part = gsearch.NextFullSearch()) !=
nullptr) {
1345 ColPartition* partner = part->SingletonPartner(
true);
1346 if (partner !=
nullptr) {
1347 if (partner->SingletonPartner(
false) != part) {
1348 tprintf(
"Ooops! Partition:(%d partners)",
1349 part->upper_partners()->length());
1351 tprintf(
"has singleton partner:(%d partners",
1352 partner->lower_partners()->length());
1354 tprintf(
"but its singleton partner is:");
1355 if (partner->SingletonPartner(
false) ==
nullptr)
1358 partner->SingletonPartner(
false)->Print();
1360 ASSERT_HOST(partner->SingletonPartner(
false) == part);
1361 }
else if (part->SingletonPartner(
false) !=
nullptr) {
1362 ColPartitionSet* column_set = best_columns_[gsearch.GridY()];
1364 part->SmoothPartnerRun(column_count * 2 + 1);
1371 void ColumnFinder::AddToTempPartList(ColPartition* part,
1372 ColPartition_CLIST* temp_list) {
1373 int mid_y = part->MidY();
1374 ColPartition_C_IT it(temp_list);
1375 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1376 ColPartition* test_part = it.data();
1379 if (test_part == part->SingletonPartner(
false))
1381 int neighbour_bottom = test_part->median_bottom();
1382 int neighbour_top = test_part->median_top();
1383 int neighbour_y = (neighbour_bottom + neighbour_top) / 2;
1384 if (neighbour_y < mid_y)
1386 if (!part->HOverlaps(*test_part) && !part->WithinSameMargins(*test_part))
1389 if (it.cycled_list()) {
1390 it.add_to_end(part);
1392 it.add_before_stay_put(part);
1397 void ColumnFinder::EmptyTempPartList(ColPartition_CLIST* temp_list,
1398 WorkingPartSet_LIST* work_set) {
1399 ColPartition_C_IT it(temp_list);
1400 while (!it.empty()) {
1402 &good_parts_, work_set);
1408 void ColumnFinder::TransformToBlocks(BLOCK_LIST* blocks,
1409 TO_BLOCK_LIST* to_blocks) {
1410 WorkingPartSet_LIST work_set;
1411 ColPartitionSet* column_set =
nullptr;
1412 ColPartition_IT noise_it(&noise_parts_);
1416 ColPartition_CLIST temp_part_list;
1418 GridSearch<ColPartition, ColPartition_CLIST, ColPartition_C_IT>
1419 gsearch(&part_grid_);
1420 gsearch.StartFullSearch();
1421 int prev_grid_y = -1;
1423 while ((part = gsearch.NextFullSearch()) !=
nullptr) {
1424 int grid_y = gsearch.GridY();
1425 if (grid_y != prev_grid_y) {
1426 EmptyTempPartList(&temp_part_list, &work_set);
1427 prev_grid_y = grid_y;
1429 if (best_columns_[grid_y] != column_set) {
1430 column_set = best_columns_[grid_y];
1434 &good_parts_, &work_set);
1436 tprintf(
"Changed column groups at grid index %d, y=%d\n",
1437 gsearch.GridY(), gsearch.GridY() *
gridsize());
1440 noise_it.add_to_end(part);
1442 AddToTempPartList(part, &temp_part_list);
1445 EmptyTempPartList(&temp_part_list, &work_set);
1447 WorkingPartSet_IT work_it(&work_set);
1448 while (!work_it.empty()) {
1449 WorkingPartSet* working_set = work_it.extract();
1451 &good_parts_, blocks, to_blocks);
1459 static void ReflectBlobList(BLOBNBOX_LIST* bblobs) {
1460 BLOBNBOX_IT it(bblobs);
1461 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1462 it.data()->reflect_box_in_y_axis();
1472 void ColumnFinder::ReflectForRtl(
TO_BLOCK* input_block, BLOBNBOX_LIST* bblobs) {
1473 ReflectBlobList(bblobs);
1474 ReflectBlobList(&input_block->
blobs);
1481 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f);
1482 denorm_ = new_denorm;
1488 static void RotateAndExplodeBlobList(
const FCOORD& blob_rotation,
1489 BLOBNBOX_LIST* bblobs,
1492 BLOBNBOX_IT it(bblobs);
1493 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1496 C_OUTLINE_LIST* outlines = cblob->
out_list();
1497 C_OUTLINE_IT ol_it(outlines);
1498 if (!outlines->singleton()) {
1501 for (;!ol_it.empty(); ol_it.forward()) {
1507 it.add_after_stay_put(new_blob);
1513 if (blob_rotation.
x() != 1.0f || blob_rotation.
y() != 0.0f) {
1514 cblob->
rotate(blob_rotation);
1535 void ColumnFinder::RotateAndReskewBlocks(
bool input_is_rtl,
1536 TO_BLOCK_LIST* blocks) {
1543 TO_BLOCK_IT it(blocks);
1544 int block_index = 1;
1545 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1559 FCOORD blob_rotation = ComputeBlockAndClassifyRotation(block);
1564 RotateAndExplodeBlobList(blob_rotation, &to_block->
blobs,
1566 TO_ROW_IT row_it(to_block->
get_rows());
1567 for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
1568 TO_ROW* row = row_it.data();
1569 RotateAndExplodeBlobList(blob_rotation, row->
blob_list(),
1573 static_cast<int>(heights.
median() + 0.5));
1575 tprintf(
"Block median size = (%d, %d)\n",
1585 FCOORD ColumnFinder::ComputeBlockAndClassifyRotation(
BLOCK* block) {
1594 FCOORD classify_rotation(text_rotation_);
1595 FCOORD block_rotation(1.0f, 0.0f);
1601 if (rerotate_.
x() == 0.0f)
1602 block_rotation = rerotate_;
1604 block_rotation =
FCOORD(0.0f, -1.0f);
1605 block->
rotate(block_rotation);
1606 classify_rotation =
FCOORD(1.0f, 0.0f);
1608 block_rotation.rotate(rotation_);
1612 FCOORD blob_rotation(block_rotation);
1613 block_rotation.set_y(-block_rotation.y());
1617 tprintf(
"Blk %d, type %d rerotation(%.2f, %.2f), char(%.2f,%.2f), box:",
1620 classify_rotation.x(), classify_rotation.y());
1623 return blob_rotation;
void ReTypeBlobs(BLOBNBOX_LIST *im_blobs)
void ReSetAndReFilterBlobs()
void AssertNoDuplicates()
static void FindImagePartitions(Pix *image_pix, const FCOORD &rotation, const FCOORD &rerotation, TO_BLOCK *block, TabFind *tab_grid, DebugPixa *pixa_debug, ColPartitionGrid *part_grid, ColPartition_LIST *big_parts)
static void TransferImagePartsToImageMask(const FCOORD &rerotation, ColPartitionGrid *part_grid, Pix *image_mask)
bool textord_tabfind_find_tables
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)
ScrollView * MakeWindow(int x, int y, const char *window_name)
void DeleteUnknownParts(TO_BLOCK *block)
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)
void GridFindMargins(ColPartitionSet **best_columns)
int FindBlocks(PageSegMode pageseg_mode, Pix *scaled_color, int scaled_factor, TO_BLOCK *block, Pix *photo_mask_pix, Pix *thresholds_pix, Pix *grey_pix, DebugPixa *pixa_debug, BLOCK_LIST *blocks, BLOBNBOX_LIST *diacritic_blobs, TO_BLOCK_LIST *to_blocks)
void SetNeighboursOnMediumBlobs(TO_BLOCK *block)
void FindPartitionPartners()
#define BOOL_VAR(name, val, comment)
static ColPartition * MakeLinePartition(BlobRegionType blob_type, const ICOORD &vertical, int left, int bottom, int right, int top)
bool PSM_SPARSE(int pageseg_mode)
bool right_to_left() const
FCOORD re_rotation() const
void reflect_polygon_in_y_axis()
bool textord_tabfind_show_columns
const ICOORD & median_size() const
void move(GenericVector< T > *from)
void set_resolution(int resolution)
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)
const ICOORD & bleft() const
void rotate(const FCOORD vec)
int16_t y() const
access_function
void InsertCleanPartitions(ColPartitionGrid *grid, TO_BLOCK *block)
void TidyBlobs(TO_BLOCK *block)
bool textord_tabfind_show_blocks
static bool WithinTestRegion(int detail_level, int x, int y)
void CorrectOrientation(TO_BLOCK *block, bool vertical_text_lines, int recognition_rotation)
ScrollView * DisplayTabVectors(ScrollView *tab_win)
void RemoveLineResidue(ColPartition_LIST *big_part_list)
void set_re_rotation(const FCOORD &rotation)
const double kMaxDistToPartSizeRatio
void SetTabStops(TabFind *tabgrid)
bool textord_tabfind_show_reject_blobs
void plot(ScrollView *window, int32_t serial, ScrollView::Color colour)
void FindFigureCaptions()
bool PSM_COL_FIND_ENABLED(int pageseg_mode)
bool textord_tabfind_show_initial_partitions
void plot_graded_blobs(ScrollView *to_win)
bool textord_debug_printable
void FindLeaderPartitions(TO_BLOCK *block, ColPartitionGrid *part_grid)
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
const double kHorizontalGapMergeFraction
int DistanceOfBoxFromPartition(const TBOX &box, const ColPartition &part, const DENORM *denorm, bool debug) const
bool IsVerticallyAlignedText(double find_vertical_text_ratio, TO_BLOCK *block, BLOBNBOX_CLIST *osd_blobs)
int textord_tabfind_show_partitions
PolyBlockType isA() const
void set_region_type(BlobRegionType new_type)
SVEvent * AwaitEvent(SVEventType type)
void set_classify_rotation(const FCOORD &rotation)
static BLOBNBOX * RealBlob(C_OUTLINE *outline)
void set_owner(tesseract::ColPartition *new_owner)
GenericVector< ColPartitionSet * > PartSetVector
int16_t x() const
access function
bool TestVerticalTextDirection(double find_vertical_text_ratio, TO_BLOCK *block, BLOBNBOX_CLIST *osd_blobs)
void RemoveBBox(BBC *bbox)
bool MakeColPartSets(PartSetVector *part_sets)
void AccumulateColumnWidthsAndGaps(int *total_width, int *width_samples, int *total_gap, int *gap_samples)
void AddToColumnSetsIfUnique(PartSetVector *column_sets, WidthCallback *cb)
int textord_debug_tabfind
virtual int FindEquationParts(ColPartitionGrid *part_grid, ColPartitionSet **best_columns)=0
void FindTextlineDirectionAndFixBrokenCJK(PageSegMode pageseg_mode, bool cjk_merge, TO_BLOCK *input_block)
void InsertBBox(bool h_spread, bool v_spread, BBC *bbox)
POLY_BLOCK * poly_block() const
void set_median_size(int x, int y)
WidthCallback * WidthCB()
ScrollView * FindInitialTabVectors(BLOBNBOX_LIST *image_blobs, int min_gutter_width, double tabfind_aligned_gap_fraction, TO_BLOCK *block)
DLLSYM void tprintf(const char *format,...)
void SetEquationDetect(EquationDetectBase *detect)
void ComputeEdgeOffsets(Pix *thresholds, Pix *grey)
void SetupAndFilterNoise(PageSegMode pageseg_mode, Pix *photo_mask_pix, TO_BLOCK *input_block)
const DENORM * predecessor() const
void DeleteUnownedNoise()
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)
void add(int32_t value, int32_t count)
void DisplayColumnEdges(int y_bottom, int y_top, ScrollView *win)
void set_flow(BlobTextFlowType value)
void set_skew(const FCOORD &skew)
void DontFindTabVectors(BLOBNBOX_LIST *image_blobs, TO_BLOCK *block, FCOORD *deskew, FCOORD *reskew)
bool overlap(const TBOX &box) const
void InsertBlobsToGrid(bool h_spread, bool v_spread, BLOBNBOX_LIST *blobs, BBGrid< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT > *grid)
void LocateTables(ColPartitionGrid *grid, ColPartitionSet **columns, WidthCallback *width_cb, const FCOORD &reskew)
bool contains(const FCOORD pt) const
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
C_OUTLINE_LIST * out_list()
void CorrectForRotation(const FCOORD &rerotation, ColPartitionGrid *part_grid)
void delete_data_pointers()
const int kMaxIncompatibleColumnCount
void set_index(int value)
void RefinePartitionPartners(bool get_desperate)
TabVector_LIST * dead_vectors()
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
const TBOX & bounding_box() const
ColPartitionSet * MakeSingleColumnSet(WidthCallback *cb)
tesseract::ColPartition * owner() const
static void RotateBlobList(const FCOORD &rotation, BLOBNBOX_LIST *blobs)
BLOBNBOX_LIST large_blobs
Pix * ComputeNonTextMask(bool debug, Pix *photo_map, TO_BLOCK *blob_block)
void ExtractPartitionsAsBlocks(BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
void GetDeskewVectors(FCOORD *deskew, FCOORD *reskew)
void set_left_to_right_language(bool order)
void set_right_to_left(bool value)
void Init(int grid_size, const ICOORD &bottom_left, const ICOORD &top_right)
void set_y(float yin)
rewrite function
const double kMinGutterWidthGrid
void rotate(const FCOORD &rotation)
BLOBNBOX_LIST * blob_list()
void set_type(PolyBlockType t)
void pad(int xpad, int ypad)
void DisplayBoxes(ScrollView *window)
#define INT_VAR(name, val, comment)
void SetBlockRuleEdges(TO_BLOCK *block)
const ICOORD & tright() const
void compute_bounding_box()
BLOBNBOX_LIST small_blobs
void ResetForVerticalText(const FCOORD &rotate, const FCOORD &rerotate, TabVector_LIST *horizontal_lines, int *min_gutter_width)
BLOBNBOX_LIST noise_blobs
void rotate(const FCOORD &rotation)