21 #pragma warning(disable:4244) // Conversion warnings 31 #include "allheaders.h" 149 "Debug table marking steps in detail");
151 "Show page stats used in table finding");
153 "Enables the table recognizer for table layout and filtering.");
166 global_median_xheight_(0),
167 global_median_blob_width_(0),
168 global_median_ledding_(0),
169 left_to_right_language_(true) {
187 const ICOORD& top_right) {
226 BLOBNBOX_CLIST* part_boxes = part->
boxes();
227 BLOBNBOX_C_IT pit(part_boxes);
228 for (pit.mark_cycle_pt(); !pit.cycled_list(); pit.forward()) {
235 if (leader_part == NULL) {
239 leader_part->
AddBox(pblob);
241 clean_part->
AddBox(pblob);
248 if (leader_part != NULL) {
271 #ifndef GRAPHICS_DISABLED 280 table_win =
MakeWindow(100, 300,
"Fragmented Text");
283 #endif // GRAPHICS_DISABLED 290 ColSegment_LIST column_blocks;
304 ColSegment_LIST table_columns;
310 ColSegment_LIST table_regions;
313 #ifndef GRAPHICS_DISABLED 319 #endif // GRAPHICS_DISABLED 335 #ifndef GRAPHICS_DISABLED 342 #endif // GRAPHICS_DISABLED 349 #ifndef GRAPHICS_DISABLED 356 #endif // GRAPHICS_DISABLED 364 #ifndef GRAPHICS_DISABLED 371 #endif // GRAPHICS_DISABLED 447 if (part->
boxes()->empty()) {
457 bool found_split =
true;
458 while (found_split) {
460 BLOBNBOX_C_IT box_it(right_part->
boxes());
468 for (box_it.mark_cycle_pt(); !box_it.cycled_list(); box_it.forward()) {
469 const TBOX& box = box_it.data()->bounding_box();
471 box.
left() - previous_right > kThreshold) {
474 int mid_x = (box.
left() + previous_right) / 2;
476 right_part = left_part->
SplitAt(mid_x);
484 previous_right =
MAX(previous_right, box.
right());
517 return box.
height() > kHeightRequired &&
518 box.
width() > kWidthRequired &&
519 box.
area() > kAreaRequired;
532 ColSegment_LIST* column_blocks) {
535 if (columns != NULL) {
536 ColSegment_LIST new_blocks;
547 ColSegment_LIST* column_blocks) {
548 ColSegment_IT src_it(new_blocks);
549 ColSegment_IT dest_it(column_blocks);
551 for (src_it.mark_cycle_pt(); !src_it.cycled_list(); src_it.forward()) {
554 bool match_found =
false;
556 for (dest_it.mark_cycle_pt(); !dest_it.cycled_list(); dest_it.forward()) {
564 delete src_it.extract();
570 dest_it.add_after_then_move(src_it.extract());
579 return (abs(b1.
left() - b2.
left()) < x_margin) &&
603 int y = part->
MidY();
627 if (right < box.
left()) {
640 if (left > box.
right()) {
692 if (neighbor == part)
698 if (neighbor_box.
top() < part_box.
bottom() &&
699 gap < min_space_below) {
700 min_space_below = gap;
701 below_neighbor = neighbor;
703 else if (part_box.
top() < neighbor_box.
bottom() &&
704 gap < min_space_above) {
705 min_space_above = gap;
706 above_neighbor = neighbor;
739 BLOBNBOX_C_IT it(part->
boxes());
740 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
741 xheight_stats.
add(it.data()->bounding_box().height(), 1);
742 width_stats.
add(it.data()->bounding_box().width(), 1);
753 #ifndef GRAPHICS_DISABLED 755 const char* kWindowName =
"X-height (R), X-width (G), and ledding (B)";
761 #endif // GRAPHICS_DISABLED 869 BLOBNBOX_CLIST* part_boxes = part->
boxes();
870 BLOBNBOX_C_IT it(part_boxes);
880 int previous_x1 = -1;
882 int largest_partition_gap_found = -1;
889 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
893 if (previous_x1 != -1) {
894 int gap = current_x0 - previous_x1;
906 previous_x1 =
MAX(previous_x1, current_x1);
920 if (gap > largest_partition_gap_found)
921 largest_partition_gap_found = gap;
923 previous_x1 = current_x1;
937 if (largest_partition_gap_found == -1)
943 return largest_partition_gap_found < min_gap;
963 const int top = box.
top() + search_size;
964 const int bottom = box.
bottom() - search_size;
968 int x = right_to_left ? box.
right() : box.
left();
971 while ((leader = hsearch.
NextSideSearch(right_to_left)) != NULL) {
1030 int current_spacing = 0;
1031 int upper_spacing = 0;
1037 current_spacing = mid - left;
1038 upper_spacing = upper_mid - left;
1044 current_spacing = right - mid;
1045 upper_spacing = right - upper_mid;
1096 if (top > max_top) {
1100 if (bottom < min_bottom) {
1101 min_bottom = bottom;
1126 if (!upper_part || !lower_part)
1152 ColSegment_IT it(column_blocks);
1153 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1156 int num_table_cells = 0;
1157 int num_text_cells = 0;
1172 if (!num_table_cells && !num_text_cells) {
1173 delete it.extract();
1186 ColSegment_IT it(segments);
1187 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1214 bool neighbor_found =
false;
1215 bool modified =
false;
1225 neighbor_found =
false;
1231 if (neighbor == seg)
1253 neighbor_found =
true;
1260 }
while (neighbor_found);
1282 ColSegment_IT it(table_columns);
1302 bool found_neighbours =
false;
1318 found_neighbours =
true;
1320 if (found_neighbours) {
1321 it.add_after_then_move(col);
1332 ColSegment_LIST* table_regions) {
1333 ColSegment_IT cit(table_columns);
1334 ColSegment_IT rit(table_regions);
1343 bool* table_region =
new bool[page_height];
1347 for (
int i = 0; i < page_height; i++) {
1348 table_region[i] =
false;
1352 cit.move_to_first();
1353 for (cit.mark_cycle_pt(); !cit.cycled_list(); cit.forward()) {
1354 TBOX col_box = cit.data()->bounding_box();
1358 for (
int i = intersection_box.
bottom(); i < intersection_box.
top(); i++) {
1359 table_region[i -
bleft().
y()] =
true;
1363 TBOX current_table_box;
1368 for (
int i = 1; i < page_height; i++) {
1370 if (!table_region[i - 1] && table_region[i]) {
1375 if (table_region[i - 1] && !table_region[i]) {
1377 if (!current_table_box.
null_box()) {
1380 rit.add_after_then_move(seg);
1385 delete[] table_region;
1401 bool neighbor_found =
false;
1402 bool modified =
false;
1406 TBOX search_region(box);
1409 neighbor_found =
false;
1415 if (neighbor == seg)
1433 neighbor_found =
true;
1440 }
while (neighbor_found);
1496 ColSegment_CLIST adjusted_tables;
1497 ColSegment_C_IT it(&adjusted_tables);
1503 TBOX grown_box = table_box;
1511 it.add_after_then_move(col);
1522 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1537 TBOX search_box = table_box;
1551 const TBOX& search_range,
1555 for (
int i = 0; i < 2; ++i) {
1579 const TBOX& search_range,
1594 if (result_box->
contains(part_box))
1609 const TBOX& table_box) {
1627 int num_extra_partitions = 0;
1628 int extra_space_to_right = 0;
1629 int extra_space_to_left = 0;
1632 for (
int i = 0; i < 2; ++i) {
1649 num_extra_partitions++;
1653 extra_space_to_right++;
1654 extra_space_to_left++;
1659 extra_space_to_right++;
1661 extra_space_to_left++;
1666 return (extra_space_to_right > num_extra_partitions / 2) ||
1667 (extra_space_to_left > num_extra_partitions / 2);
1683 int table_top = table_box->
top();
1686 if (box.
bottom() - table_top > max_distance)
1692 previous_neighbor = NULL;
1697 if (previous_neighbor == NULL) {
1698 previous_neighbor = neighbor;
1715 int* table_xprojection =
new int[page_width];
1724 for (
int i = 0; i < page_width; i++) {
1725 table_xprojection[i] = 0;
1742 BLOBNBOX_CLIST* part_boxes = part->
boxes();
1743 BLOBNBOX_C_IT pit(part_boxes);
1750 int next_position_to_write = 0;
1752 for (pit.mark_cycle_pt(); !pit.cycled_list(); pit.forward()) {
1759 xstart =
MAX(xstart, next_position_to_write);
1760 for (
int i = xstart; i < xend; i++)
1761 table_xprojection[i -
bleft().
x()]++;
1762 next_position_to_write = xend;
1771 delete[] table_xprojection;
1779 for (
int i = 0; i < length; i++) {
1780 if (xprojection[i] > peak_value) {
1781 peak_value = xprojection[i];
1793 for (
int i = 0; i < length; i++) {
1794 xprojection[i] = (xprojection[i] >= projection_threshold) ? 1 : 0;
1797 int largest_gap = 0;
1799 for (
int i = 1; i < length; i++) {
1801 if (xprojection[i - 1] && !xprojection[i]) {
1805 if (run_start != -1 && !xprojection[i - 1] && xprojection[i]) {
1806 int gap = i - run_start;
1807 if (gap > largest_gap)
1828 table_win =
MakeWindow(0, 0,
"Table Structure");
1843 ColSegment_CLIST good_tables;
1844 ColSegment_C_IT good_it(&good_tables);
1859 if (table_structure != NULL) {
1864 delete table_structure;
1865 good_it.add_after_then_move(found_table);
1874 for (good_it.mark_cycle_pt(); !good_it.cycled_list(); good_it.forward())
1880 ColSegment_LIST *segments,
1882 #ifndef GRAPHICS_DISABLED 1885 ColSegment_IT it(segments);
1886 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1889 int left_x = box.
left();
1890 int right_x = box.
right();
1891 int top_y = box.
top();
1892 int bottom_y = box.
bottom();
1893 win->
Rectangle(left_x, bottom_y, right_x, top_y);
1901 #ifndef GRAPHICS_DISABLED 1909 int left_x = box.
left();
1910 int right_x = box.
right();
1911 int top_y = box.
top();
1912 int bottom_y = box.
bottom();
1915 win->
Rectangle(left_x, bottom_y, right_x, top_y);
1928 #ifndef GRAPHICS_DISABLED 1936 color = default_color;
1938 color = table_color;
1941 int left_x = box.
left();
1942 int right_x = box.
right();
1943 int top_y = box.
top();
1944 int bottom_y = box.
bottom();
1947 win->
Rectangle(left_x, bottom_y, right_x, top_y);
1962 #ifndef GRAPHICS_DISABLED 1970 int left_x = box.
left();
1971 int right_x = box.
right();
1972 int top_y = box.
top();
1973 int bottom_y = box.
bottom();
1978 int mid_x = (left_x + right_x) / 2;
1979 int mid_y = (top_y + bottom_y) / 2;
1980 int other_x = (upper_box.
left() + upper_box.
right()) / 2;
1981 int other_y = (upper_box.
top() + upper_box.
bottom()) / 2;
1984 win->
Line(mid_x, mid_y, other_x, other_y);
1989 int mid_x = (left_x + right_x) / 2;
1990 int mid_y = (top_y + bottom_y) / 2;
1991 int other_x = (lower_box.
left() + lower_box.
right()) / 2;
1992 int other_y = (lower_box.
top() + lower_box.
bottom()) / 2;
1995 win->
Line(mid_x, mid_y, other_x, other_y);
2008 PIX* pix = pixRead(
"test1.tif");
2010 tprintf(
"Input file test1.tif not found.\n");
2013 int img_height = pixGetHeight(pix);
2014 int img_width = pixGetWidth(pix);
2017 BOXA* text_box_array = boxaCreate(num_boxes);
2018 BOXA* table_box_array = boxaCreate(num_boxes);
2027 BOX* lept_box = boxCreate(box.
left(), img_height - box.
top(),
2031 boxaAddBox(table_box_array, lept_box, L_INSERT);
2033 boxaAddBox(text_box_array, lept_box, L_INSERT);
2036 PIX* out = pixDrawBoxa(pix, text_box_array, 3, 0xff000000);
2037 out = pixDrawBoxa(out, table_box_array, 3, 0x0000ff00);
2039 BOXA* table_array = boxaCreate(num_boxes);
2041 FILE* fptr = fopen(
"tess-table.txt",
"wb");
2056 BOX* lept_box = boxCreate(box.
left(), img_height - box.
top(),
2059 boxaAddBox(table_array, lept_box, L_INSERT);
2060 fprintf(fptr,
"%d %d %d %d TABLE\n", box.
left(),
2065 out = pixDrawBoxa(out, table_array, 5, 0x7fff0000);
2067 pixWrite(
"out.png", out, IFF_PNG);
2069 boxaDestroy(&text_box_array);
2070 boxaDestroy(&table_box_array);
2071 boxaDestroy(&table_array);
2117 if (table_partition) {
2118 table_partition->
Absorb(part, width_cb);
2120 table_partition = part;
2125 if (table_partition) {
2137 grid->
InsertBBox(
true,
true, table_partition);
2146 num_table_cells_(0),
2161 return kBoxColors[type_];
2174 else if (num_text_cells_ > num_table_cells_)
void set_line_grid(ColPartitionGrid *lines)
const double kAllowTextWidth
const TBOX & bounding_box() const
void GrowTableBox(const TBOX &table_box, TBOX *result_box)
TBOX bounding_union(const TBOX &box) const
const double kAllowTextArea
const double kAllowBlobHeight
ColSegmentGrid col_seg_grid_
int global_median_ledding_
const double kSplitPartitionSize
const TBOX & bounding_box() const
void set_flow(BlobTextFlowType f)
bool AllowBlob(const BLOBNBOX &blob) const
void InsertLeaderPartition(ColPartition *part)
void set_global_median_ledding(int ledding)
int space_to_right() const
const double kSmallTableProjectionThreshold
void MoveColSegmentsToGrid(ColSegment_LIST *segments, ColSegmentGrid *col_seg_grid)
void InsertBox(const TBOX &other)
bool overlap(const TBOX &box) const
void MarkPartitionsUsingLocalInformation()
void AddBox(BLOBNBOX *box)
const ICOORD & tright() const
bool left_to_right_language_
void InsertRulingPartition(ColPartition *part)
void Display(ScrollView *window, ScrollView::Color color)
void GetColumnBlocks(ColPartitionSet **columns, ColSegment_LIST *col_segments)
ScrollView::Color BoxColor() const
const TBOX & bounding_box() const
PolyBlockType type() const
ColPartitionGrid clean_part_grid_
bool textord_tablefind_show_stats
const double kAllowBlobArea
const double kMaxGapInTextPartition
BBC * NextVerticalSearch(bool top_to_bottom)
void set_min_height(int height)
const double kMinOverlapWithTable
void GetTableRegions(ColSegment_LIST *table_columns, ColSegment_LIST *table_regions)
void Line(int x1, int y1, int x2, int y2)
void add(inT32 value, inT32 count)
void set_num_table_cells(int n)
void MakeTableBlocks(ColPartitionGrid *grid, ColPartitionSet **columns, WidthCallback *width_cb)
const int kMaxBoxesInDataPartition
void SetUniqueMode(bool mode)
void set_inside_table_column(bool val)
void GridMergeColumnBlocks()
void StartVerticalSearch(int xmin, int xmax, int y)
bool AllowTextPartition(const ColPartition &part) const
void ClearGridData(void(*free_method)(BBC *))
void FilterHeaderAndFooter()
void LocateTables(ColPartitionGrid *grid, ColPartitionSet **columns, WidthCallback *width_cb, const FCOORD &reskew)
BlobTextFlowType flow() const
void DeleteObject(T *object)
void GroupColumnBlocks(ColSegment_LIST *current_segments, ColSegment_LIST *col_segments)
ColSegmentGrid table_grid_
const int kMaxVerticalSpacing
bool textord_tablefind_show_mark
const double kParagraphEndingPreviousLineRatio
bool inside_table_column()
void DisplayBoxes(ScrollView *window)
void Init(int grid_size, const ICOORD &bottom_left, const ICOORD &top_right)
ColPartition * nearest_neighbor_above() const
void GetTableColumns(ColSegment_LIST *table_columns)
const int kSideSpaceMargin
void Absorb(ColPartition *other, WidthCallback *cb)
void set_text_grid(ColPartitionGrid *text)
void set_global_median_blob_width(int width)
bool major_x_overlap(const TBOX &box) const
void set_space_above(int space)
bool BelongToOneTable(const TBOX &box1, const TBOX &box2)
const double kMaxTableCellXheight
void GetColumnBoxes(int y_bottom, int y_top, ColSegment_LIST *segments)
void set_space_to_left(int space)
void set_space_to_right(int space)
ColPartition * nearest_neighbor_below() const
bool textord_tablefind_recognize_tables
void InsertImagePartition(ColPartition *part)
void DisplayColPartitions(ScrollView *win, ColPartitionGrid *grid, ScrollView::Color text_color, ScrollView::Color table_color)
void set_left_to_right_language(bool order)
bool HasLeaderAdjacent(const ColPartition &part)
ScrollView * MakeWindow(int x, int y, const char *window_name)
void StartSideSearch(int x, int ymin, int ymax)
const double kAllowTextHeight
const int kLargeTableRowCount
bool contains(const FCOORD pt) const
void set_nearest_neighbor_below(ColPartition *part)
int space_to_left() const
void GridMergeTableRegions()
bool MatchingStrokeWidth(const ColPartition &other, double fractional_tolerance, double constant_tolerance) const
const double kMaxParagraphEndingLeftSpaceMultiple
void DisplayColPartitionConnections(ScrollView *win, ColPartitionGrid *grid, ScrollView::Color default_color)
bool HasWideOrNoInterWordGap(ColPartition *part) const
void set_num_text_cells(int n)
const double kMaxBlobOverlapFactor
bool HLineBelongsToTable(const ColPartition &part, const TBOX &table_box)
inT16 x() const
access function
StructuredTable * RecognizeTable(const TBOX &guess_box)
double overlap_fraction(const TBOX &box) const
bool IsInSameColumnAs(const ColPartition &part) const
BBC * NextSideSearch(bool right_to_left)
ColPartitionGrid fragmented_text_grid_
ColPartition * SingletonPartner(bool upper)
int RightAtY(int y) const
bool textord_dump_table_images
void SetPartitionType(int resolution, ColPartitionSet *columns)
ColPartitionGrid leader_and_ruling_grid_
bool VSignificantCoreOverlap(const ColPartition &other) const
const int kRulingVerticalMargin
int median_bottom() const
void InsertFragmentedTextPartition(ColPartition *part)
void set_nearest_neighbor_above(ColPartition *part)
const ICOORD & bleft() const
bool IsHorizontalLine() const
int direction(EDGEPT *point)
void InitializePartitions(ColPartitionSet **all_columns)
const int kMinRowsInTable
void Rectangle(int x1, int y1, int x2, int y2)
ColPartition * ShallowCopy() const
void set_bounding_box(const TBOX &other)
void DisplayColSegmentGrid(ScrollView *win, ColSegmentGrid *grid, ScrollView::Color color)
const double kMinParagraphEndingTextToWhitespaceRatio
const double kLargeTableProjectionThreshold
void set_global_median_xheight(int xheight)
void SetGlobalSpacings(ColPartitionGrid *grid)
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
ColPartition * CopyButDontOwnBlobs()
const double kAllowBlobWidth
const double kMaxXProjectionGapFactor
void GrowTableToIncludePartials(const TBOX &table_box, const TBOX &search_range, TBOX *result_box)
void GrowTableToIncludeLines(const TBOX &table_box, const TBOX &search_range, TBOX *result_box)
void rotate_large(const FCOORD &vec)
const int kMaxColumnHeaderDistance
void DeleteSingleColumnTables()
void set_max_text_height(int height)
void FilterParagraphEndings()
void SmoothTablePartitionRuns()
void InsertBBox(bool h_spread, bool v_spread, BBC *bbox)
void MarkTablePartitions()
bool ConsecutiveBoxes(const TBOX &b1, const TBOX &b2)
void AdjustTableBoundaries()
const ICOORD & bleft() const
void set_space_below(int space)
void SetVerticalSpacing(ColPartition *part)
void InsertCleanPartitions(ColPartitionGrid *grid, TO_BLOCK *block)
void InsertTextPartition(ColPartition *part)
const ICOORD & tright() const
ScrollView * MakeWindow(int x, int y, const char *window_name)
ColPartition * ColumnContaining(int x, int y)
int global_median_blob_width_
BlobTextFlowType flow() const
void SplitAndInsertFragmentedTextPartition(ColPartition *part)
TBOX intersection(const TBOX &box) const
const double kMinMaxGapInTextPartition
int global_median_xheight_
bool GapInXProjection(int *xprojection, int length)
void RefinePartitionPartners(bool get_desperate)
void set_blob_type(BlobRegionType t)
const TBOX & bounding_box() const
void StartRectSearch(const TBOX &rect)
void DisplayColSegments(ScrollView *win, ColSegment_LIST *cols, ScrollView::Color color)
BlobRegionType region_type() const
bool MatchingSizes(const ColPartition &other) const
ColPartition * SplitAt(int split_x)
#define BOOL_VAR(name, val, comment)
BlobRegionType blob_type() const
#define CLISTIZE(CLASSNAME)
void SetColumnsType(ColSegment_LIST *col_segments)
bool major_y_overlap(const TBOX &box) const
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
const double kTableColumnThreshold
const double kStrokeWidthFractionalTolerance
void RepositionIterator()
void IncludeLeftOutColumnHeaders(TBOX *table_box)
void WriteToPix(const FCOORD &reskew)
void plot(ScrollView *window, float xorigin, float yorigin, float xscale, float yscale, ScrollView::Color colour) const
const int kAdjacentLeaderSearchPadding
static void SetPartitionSpacings(ColPartitionGrid *grid, ColPartitionSet **all_columns)
const double kStrokeWidthConstantTolerance
void FindPartitionPartners()
inT16 y() const
access_function
const int kMinBoxesInTextPartition