Commit 5ff9208f for tesseract
commit 5ff9208fc74491c452c0c039c4b15eeac6871b06
Author: Stefan Weil <sw@weilnetz.de>
Date: Thu Nov 26 13:18:39 2026 +0100
Merge the partitions of a table in pairs
TableFinder::MakeTableBlocks makes a single partition from all text
partitions of a table. It added them one by one to the first one, and
each ColPartition::Absorb sorts all blobs of the merged partition and
computes its limits again, so the time was quadratic in the size of the
table. A page of stock exchange tables (ONB_nfp_19110701_018 from the
AustrianNewspapers ground truth) which is taken as a single table has
2518 partitions with 23,824 blobs. Its layout analysis needed 6.2 s,
of which 3.5 s were spent in these merges.
Collect the partitions of the table and merge neighbours in the list
until a single partition is left. Each pass halves the list and handles
each blob once. The order of the partitions is kept.
Results for the layout analysis of 328 newspaper pages
(AustrianNewspapers and NZZ ground truth, 419 tables on 177 pages,
aarch64): the CPU time falls from 328.1 s to 321.5 s. The page above
needs 2.7 s, another one
(ONB_nfp_18950706_011) 2.3 s instead of 3.7 s, and no page gets slower
by more than 3 %, which is the noise of the measurement. The text and
TSV output of all pages with tessdata_fast is the same as before.
The new unit test passes with the old code, too. It tests that all
partitions of the table are merged, also those without a partner in a
pass.
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/textord/tablefind.cpp b/src/textord/tablefind.cpp
index 8490b680..5d024ce2 100644
--- a/src/textord/tablefind.cpp
+++ b/src/textord/tablefind.cpp
@@ -23,6 +23,7 @@
#include <algorithm>
#include <cmath>
#include <utility>
+#include <vector>
#include "tablefind.h"
#include "colpartitionset.h"
@@ -2057,7 +2058,7 @@ void TableFinder::MakeTableBlocks(ColPartitionGrid *grid,
ColPartitionGridSearch rectsearch(grid);
rectsearch.StartRectSearch(table_box);
ColPartition *part;
- ColPartition *table_partition = nullptr;
+ std::vector<ColPartition *> table_parts;
while ((part = rectsearch.NextRectSearch()) != nullptr) {
// Do not consider image partitions
if (!part->IsTextType()) {
@@ -2068,13 +2069,28 @@ void TableFinder::MakeTableBlocks(ColPartitionGrid *grid,
// is covered by the table
if (part_box.overlap_fraction(table_box) > kMinOverlapWithTable) {
rectsearch.RemoveBBox();
- if (table_partition) {
- table_partition->Absorb(part, width_cb);
- } else {
- table_partition = part;
+ table_parts.push_back(part);
+ }
+ }
+ // Merge the partitions of the table in pairs until a single one is left.
+ // Absorb sorts all blobs of the merged partition and computes its limits
+ // again, so the time for adding the partitions one by one to a growing
+ // partition is quadratic in the size of the table. That took several
+ // seconds for a page which was a single table with 2500 partitions and
+ // 24000 blobs. Each pass over the list halves it and handles each blob
+ // only once. The order of the partitions is kept.
+ while (table_parts.size() > 1) {
+ size_t num_merged = 0;
+ for (size_t i = 0; i < table_parts.size(); i += 2) {
+ if (i + 1 < table_parts.size()) {
+ table_parts[i]->Absorb(table_parts[i + 1], width_cb);
}
+ table_parts[num_merged++] = table_parts[i];
}
+ table_parts.resize(num_merged);
}
+ ColPartition *table_partition =
+ table_parts.empty() ? nullptr : table_parts[0];
// Insert table colpartition back to part_grid_
if (table_partition) {
// To match the columns used when transforming to blocks, the new table
diff --git a/unittest/tablefind_test.cc b/unittest/tablefind_test.cc
index 245e9a8d..ff919e40 100644
--- a/unittest/tablefind_test.cc
+++ b/unittest/tablefind_test.cc
@@ -10,9 +10,11 @@
// limitations under the License.
#include <memory>
+#include <vector>
#include "colpartition.h"
#include "colpartitiongrid.h"
+#include "colpartitionset.h"
#include "tablefind.h"
#include "include_gunit.h"
@@ -25,6 +27,7 @@ public:
using TableFinder::HasLeaderAdjacent;
using TableFinder::InsertLeaderPartition;
using TableFinder::InsertTextPartition;
+ using TableFinder::MakeTableBlocks;
using TableFinder::set_global_median_blob_width;
using TableFinder::set_global_median_ledding;
using TableFinder::set_global_median_xheight;
@@ -57,6 +60,13 @@ public:
}
EXPECT_EQ(expected_count, count);
}
+ // Adds a table with the given box to the grid of detected tables.
+ // The finder deletes the tables in its grid.
+ void InsertTable(const TBOX &box) {
+ auto *table = new ColSegment();
+ table->set_bounding_box(box);
+ table_grid_.InsertBBox(true, true, table);
+ }
};
class TableFinderTest : public testing::Test {
@@ -277,4 +287,72 @@ TEST_F(TableFinderTest, SplitAndInsertFragmentedPartitionsBasicFail) {
finder_->ExpectPartitionCount(1);
}
+// Tests that all text partitions of a table are merged into a single table
+// partition which gets all their blobs. The number of partitions is odd, so
+// that the merge in pairs has a partition without a partner in two of its
+// three passes. An image in the table and text outside of it are kept.
+TEST_F(TableFinderTest, MakeTableBlocksMergesTextPartitions) {
+ ColPartitionGrid grid(10, ICOORD(0, 0), ICOORD(500, 500));
+ // All rows of the grid use the same single column.
+ ColPartition *column = ColPartition::FakePartition(
+ TBOX(0, 0, 500, 500), PT_FLOWING_TEXT, BRT_TEXT, BTFT_CHAIN);
+ ColPartitionSet column_set(column);
+ std::vector<ColPartitionSet *> all_columns(grid.gridheight(), &column_set);
+
+ finder_->InsertTable(TBOX(100, 100, 400, 300));
+ const TBOX cells[] = {TBOX(310, 250, 390, 270), TBOX(110, 250, 190, 270),
+ TBOX(210, 150, 290, 170), TBOX(110, 150, 190, 170),
+ TBOX(210, 250, 290, 270)};
+ TBOX cells_box;
+ for (const TBOX &cell : cells) {
+ grid.InsertBBox(true, true,
+ ColPartition::FakePartition(cell, PT_FLOWING_TEXT, BRT_TEXT,
+ BTFT_CHAIN));
+ cells_box += cell;
+ }
+ grid.InsertBBox(
+ true, true,
+ ColPartition::FakePartition(TBOX(210, 200, 290, 230), PT_FLOWING_IMAGE,
+ BRT_RECTIMAGE, BTFT_NONTEXT));
+ grid.InsertBBox(
+ true, true,
+ ColPartition::FakePartition(TBOX(110, 400, 390, 420), PT_FLOWING_TEXT,
+ BRT_TEXT, BTFT_CHAIN));
+
+ finder_->set_resolution(300);
+ finder_->MakeTableBlocks(&grid, all_columns.data(), [](int) { return true; });
+
+ std::vector<ColPartition *> parts;
+ ColPartitionGridSearch gsearch(&grid);
+ gsearch.SetUniqueMode(true);
+ gsearch.StartFullSearch();
+ ColPartition *found;
+ while ((found = gsearch.NextFullSearch()) != nullptr) {
+ parts.push_back(found);
+ }
+ EXPECT_EQ(parts.size(), 3U);
+ int num_tables = 0;
+ for (ColPartition *part : parts) {
+ if (part->type() == PT_TABLE) {
+ ++num_tables;
+ EXPECT_TRUE(part->bounding_box() == cells_box);
+ EXPECT_EQ(part->boxes()->length(), 5);
+ // The blobs are sorted by their left edge and belong to the table.
+ int last_left = 0;
+ BLOBNBOX_C_IT blob_it(part->boxes());
+ for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
+ BLOBNBOX *blob = blob_it.data();
+ EXPECT_GE(blob->bounding_box().left(), last_left);
+ last_left = blob->bounding_box().left();
+ EXPECT_EQ(blob->owner(), part);
+ EXPECT_EQ(blob->flow(), BTFT_CHAIN);
+ }
+ }
+ part->DeleteBoxes();
+ delete part;
+ }
+ EXPECT_EQ(num_tables, 1);
+ column->DeleteBoxes();
+}
+
} // namespace tesseract