2018-08-27 00:49:21 +08:00
|
|
|
// (C) Copyright 2017, Google Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2018-08-27 00:49:21 +08:00
|
|
|
#include "colpartition.h"
|
|
|
|
#include "colpartitiongrid.h"
|
|
|
|
#include "tablefind.h"
|
|
|
|
|
|
|
|
#include "include_gunit.h"
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2020-12-27 17:41:48 +08:00
|
|
|
namespace tesseract {
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
class TestableTableFinder : public tesseract::TableFinder {
|
2021-03-13 05:06:34 +08:00
|
|
|
public:
|
2018-08-24 21:07:48 +08:00
|
|
|
using TableFinder::GapInXProjection;
|
2018-09-29 15:19:13 +08:00
|
|
|
using TableFinder::HasLeaderAdjacent;
|
2018-08-24 21:07:48 +08:00
|
|
|
using TableFinder::InsertLeaderPartition;
|
|
|
|
using TableFinder::InsertTextPartition;
|
2018-09-29 15:19:13 +08:00
|
|
|
using TableFinder::set_global_median_blob_width;
|
|
|
|
using TableFinder::set_global_median_ledding;
|
|
|
|
using TableFinder::set_global_median_xheight;
|
2018-08-24 21:07:48 +08:00
|
|
|
using TableFinder::SplitAndInsertFragmentedTextPartition;
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void ExpectPartition(const TBOX &box) {
|
2018-08-24 21:07:48 +08:00
|
|
|
tesseract::ColPartitionGridSearch gsearch(&fragmented_text_grid_);
|
|
|
|
gsearch.SetUniqueMode(true);
|
|
|
|
gsearch.StartFullSearch();
|
2021-03-13 05:06:34 +08:00
|
|
|
ColPartition *part = nullptr;
|
2018-08-24 21:07:48 +08:00
|
|
|
bool found = false;
|
2018-09-29 15:27:12 +08:00
|
|
|
while ((part = gsearch.NextFullSearch()) != nullptr) {
|
2018-08-24 21:07:48 +08:00
|
|
|
if (part->bounding_box().left() == box.left() &&
|
|
|
|
part->bounding_box().bottom() == box.bottom() &&
|
2021-03-13 05:06:34 +08:00
|
|
|
part->bounding_box().right() == box.right() && part->bounding_box().top() == box.top()) {
|
2018-08-24 21:07:48 +08:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(found);
|
|
|
|
}
|
|
|
|
void ExpectPartitionCount(int expected_count) {
|
|
|
|
tesseract::ColPartitionGridSearch gsearch(&fragmented_text_grid_);
|
|
|
|
gsearch.SetUniqueMode(true);
|
|
|
|
gsearch.StartFullSearch();
|
2021-03-13 05:06:34 +08:00
|
|
|
ColPartition *part = nullptr;
|
2018-08-24 21:07:48 +08:00
|
|
|
int count = 0;
|
2018-09-29 15:27:12 +08:00
|
|
|
while ((part = gsearch.NextFullSearch()) != nullptr) {
|
2018-08-24 21:07:48 +08:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(expected_count, count);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TableFinderTest : public testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
2021-03-22 15:26:05 +08:00
|
|
|
void SetUp() override {
|
2019-05-17 00:12:06 +08:00
|
|
|
std::locale::global(std::locale(""));
|
2018-08-24 21:07:48 +08:00
|
|
|
free_boxes_it_.set_to_list(&free_boxes_);
|
2021-03-15 04:06:19 +08:00
|
|
|
finder_ = std::make_unique<TestableTableFinder>();
|
2018-08-24 21:07:48 +08:00
|
|
|
finder_->Init(1, ICOORD(0, 0), ICOORD(500, 500));
|
|
|
|
// gap finding
|
|
|
|
finder_->set_global_median_xheight(5);
|
|
|
|
finder_->set_global_median_blob_width(5);
|
|
|
|
}
|
|
|
|
|
2021-03-22 15:26:05 +08:00
|
|
|
void TearDown() override {
|
2021-03-22 15:48:50 +08:00
|
|
|
if (partition_.get() != nullptr) {
|
2021-03-13 05:06:34 +08:00
|
|
|
partition_->DeleteBoxes();
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
DeletePartitionListBoxes();
|
2018-09-29 15:27:12 +08:00
|
|
|
finder_.reset(nullptr);
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MakePartition(int x_min, int y_min, int x_max, int y_max) {
|
|
|
|
MakePartition(x_min, y_min, x_max, y_max, 0, 0);
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void MakePartition(int x_min, int y_min, int x_max, int y_max, int first_column,
|
|
|
|
int last_column) {
|
2021-03-22 15:48:50 +08:00
|
|
|
if (partition_.get() != nullptr) {
|
2021-03-13 05:06:34 +08:00
|
|
|
partition_->DeleteBoxes();
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
TBOX box;
|
|
|
|
box.set_to_given_coords(x_min, y_min, x_max, y_max);
|
2021-03-13 05:06:34 +08:00
|
|
|
partition_.reset(ColPartition::FakePartition(box, PT_UNKNOWN, BRT_UNKNOWN, BTFT_NONE));
|
2018-08-24 21:07:48 +08:00
|
|
|
partition_->set_first_column(first_column);
|
|
|
|
partition_->set_last_column(last_column);
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void InsertTextPartition(ColPartition *part) {
|
2018-08-24 21:07:48 +08:00
|
|
|
finder_->InsertTextPartition(part);
|
|
|
|
free_boxes_it_.add_after_then_move(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertLeaderPartition(int x_min, int y_min, int x_max, int y_max) {
|
|
|
|
InsertLeaderPartition(x_min, y_min, x_max, y_max, 0, 0);
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void InsertLeaderPartition(int x_min, int y_min, int x_max, int y_max, int first_column,
|
|
|
|
int last_column) {
|
2018-08-24 21:07:48 +08:00
|
|
|
TBOX box;
|
|
|
|
box.set_to_given_coords(x_min, y_min, x_max, y_max);
|
2021-03-13 05:06:34 +08:00
|
|
|
ColPartition *part =
|
|
|
|
ColPartition::FakePartition(box, PT_FLOWING_TEXT, BRT_UNKNOWN, BTFT_LEADER);
|
2018-08-24 21:07:48 +08:00
|
|
|
part->set_first_column(first_column);
|
|
|
|
part->set_last_column(last_column);
|
|
|
|
finder_->InsertLeaderPartition(part);
|
|
|
|
free_boxes_it_.add_after_then_move(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeletePartitionListBoxes() {
|
2021-03-13 05:06:34 +08:00
|
|
|
for (free_boxes_it_.mark_cycle_pt(); !free_boxes_it_.cycled_list(); free_boxes_it_.forward()) {
|
|
|
|
ColPartition *part = free_boxes_it_.data();
|
2018-08-24 21:07:48 +08:00
|
|
|
part->DeleteBoxes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<TestableTableFinder> finder_;
|
|
|
|
std::unique_ptr<ColPartition> partition_;
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
private:
|
2018-08-24 21:07:48 +08:00
|
|
|
tesseract::ColPartition_CLIST free_boxes_;
|
|
|
|
tesseract::ColPartition_C_IT free_boxes_it_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TableFinderTest, GapInXProjectionNoGap) {
|
|
|
|
int data[100];
|
2021-03-22 15:48:50 +08:00
|
|
|
for (int &i : data) {
|
2021-03-22 15:32:09 +08:00
|
|
|
i = 10;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_FALSE(finder_->GapInXProjection(data, 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TableFinderTest, GapInXProjectionEdgeGap) {
|
|
|
|
int data[100];
|
2021-03-22 15:48:50 +08:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2021-03-13 05:06:34 +08:00
|
|
|
data[i] = 2;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
|
|
|
for (int i = 10; i < 90; ++i) {
|
2021-03-13 05:06:34 +08:00
|
|
|
data[i] = 10;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
|
|
|
for (int i = 90; i < 100; ++i) {
|
2021-03-13 05:06:34 +08:00
|
|
|
data[i] = 2;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_FALSE(finder_->GapInXProjection(data, 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TableFinderTest, GapInXProjectionExists) {
|
|
|
|
int data[100];
|
2021-03-22 15:48:50 +08:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2021-03-13 05:06:34 +08:00
|
|
|
data[i] = 10;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
|
|
|
for (int i = 10; i < 90; ++i) {
|
2021-03-13 05:06:34 +08:00
|
|
|
data[i] = 2;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
|
|
|
for (int i = 90; i < 100; ++i) {
|
2021-03-13 05:06:34 +08:00
|
|
|
data[i] = 10;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_TRUE(finder_->GapInXProjection(data, 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TableFinderTest, HasLeaderAdjacentOverlapping) {
|
|
|
|
InsertLeaderPartition(90, 0, 150, 5);
|
|
|
|
MakePartition(0, 0, 100, 10);
|
|
|
|
EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
MakePartition(0, 25, 100, 40);
|
|
|
|
EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
MakePartition(145, 0, 200, 20);
|
|
|
|
EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
MakePartition(40, 0, 50, 4);
|
|
|
|
EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TableFinderTest, HasLeaderAdjacentNoOverlap) {
|
|
|
|
InsertLeaderPartition(90, 10, 150, 15);
|
|
|
|
MakePartition(0, 10, 85, 20);
|
|
|
|
EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
MakePartition(0, 25, 100, 40);
|
|
|
|
EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
MakePartition(0, 0, 100, 10);
|
|
|
|
EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
// TODO(nbeato): is this a useful metric? case fails
|
|
|
|
// MakePartition(160, 0, 200, 15); // leader is primarily above it
|
|
|
|
// EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TableFinderTest, HasLeaderAdjacentPreservesColumns) {
|
|
|
|
InsertLeaderPartition(90, 0, 150, 5, 1, 2);
|
|
|
|
MakePartition(0, 0, 85, 10, 0, 0);
|
|
|
|
EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
MakePartition(0, 0, 100, 10, 0, 1);
|
|
|
|
EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
MakePartition(0, 0, 200, 10, 0, 5);
|
|
|
|
EXPECT_TRUE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
MakePartition(155, 0, 200, 10, 5, 5);
|
|
|
|
EXPECT_FALSE(finder_->HasLeaderAdjacent(*partition_));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(nbeato): Only testing a splitting case. Add more...
|
|
|
|
// Also test non-split cases.
|
|
|
|
TEST_F(TableFinderTest, SplitAndInsertFragmentedPartitionsBasicPass) {
|
|
|
|
finder_->set_global_median_blob_width(3);
|
|
|
|
finder_->set_global_median_xheight(10);
|
|
|
|
|
|
|
|
TBOX part_box(10, 5, 100, 15);
|
2021-03-22 15:36:47 +08:00
|
|
|
auto *all = new ColPartition(BRT_UNKNOWN, ICOORD(0, 1));
|
2018-08-24 21:07:48 +08:00
|
|
|
all->set_type(PT_FLOWING_TEXT);
|
|
|
|
all->set_blob_type(BRT_TEXT);
|
|
|
|
all->set_flow(BTFT_CHAIN);
|
|
|
|
all->set_left_margin(10);
|
|
|
|
all->set_right_margin(100);
|
|
|
|
TBOX blob_box = part_box;
|
|
|
|
for (int i = 10; i <= 20; i += 5) {
|
2018-09-29 15:19:13 +08:00
|
|
|
blob_box.set_left(i + 1);
|
|
|
|
blob_box.set_right(i + 4);
|
2018-08-24 21:07:48 +08:00
|
|
|
all->AddBox(new BLOBNBOX(C_BLOB::FakeBlob(blob_box)));
|
|
|
|
}
|
|
|
|
for (int i = 35; i <= 55; i += 5) {
|
2018-09-29 15:19:13 +08:00
|
|
|
blob_box.set_left(i + 1);
|
|
|
|
blob_box.set_right(i + 4);
|
2018-08-24 21:07:48 +08:00
|
|
|
all->AddBox(new BLOBNBOX(C_BLOB::FakeBlob(blob_box)));
|
|
|
|
}
|
|
|
|
for (int i = 80; i <= 95; i += 5) {
|
2018-09-29 15:19:13 +08:00
|
|
|
blob_box.set_left(i + 1);
|
|
|
|
blob_box.set_right(i + 4);
|
2018-08-24 21:07:48 +08:00
|
|
|
all->AddBox(new BLOBNBOX(C_BLOB::FakeBlob(blob_box)));
|
|
|
|
}
|
|
|
|
// TODO(nbeato): Ray's newer code...
|
|
|
|
// all->ClaimBoxes();
|
2021-03-13 05:06:34 +08:00
|
|
|
all->ComputeLimits(); // This is to make sure median iinfo is set.
|
|
|
|
InsertTextPartition(all); // This is to delete blobs
|
|
|
|
ColPartition *fragment_me = all->CopyButDontOwnBlobs();
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
finder_->SplitAndInsertFragmentedTextPartition(fragment_me);
|
|
|
|
finder_->ExpectPartition(TBOX(11, 5, 24, 15));
|
|
|
|
finder_->ExpectPartition(TBOX(36, 5, 59, 15));
|
|
|
|
finder_->ExpectPartition(TBOX(81, 5, 99, 15));
|
|
|
|
finder_->ExpectPartitionCount(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TableFinderTest, SplitAndInsertFragmentedPartitionsBasicFail) {
|
|
|
|
finder_->set_global_median_blob_width(3);
|
|
|
|
finder_->set_global_median_xheight(10);
|
|
|
|
|
|
|
|
TBOX part_box(10, 5, 100, 15);
|
2021-03-22 15:36:47 +08:00
|
|
|
auto *all = new ColPartition(BRT_UNKNOWN, ICOORD(0, 1));
|
2018-08-24 21:07:48 +08:00
|
|
|
all->set_type(PT_FLOWING_TEXT);
|
|
|
|
all->set_blob_type(BRT_TEXT);
|
|
|
|
all->set_flow(BTFT_CHAIN);
|
|
|
|
all->set_left_margin(10);
|
|
|
|
all->set_right_margin(100);
|
|
|
|
TBOX blob_box = part_box;
|
|
|
|
for (int i = 10; i <= 95; i += 5) {
|
2018-09-29 15:19:13 +08:00
|
|
|
blob_box.set_left(i + 1);
|
|
|
|
blob_box.set_right(i + 4);
|
2018-08-24 21:07:48 +08:00
|
|
|
all->AddBox(new BLOBNBOX(C_BLOB::FakeBlob(blob_box)));
|
|
|
|
}
|
|
|
|
// TODO(nbeato): Ray's newer code...
|
|
|
|
// all->ClaimBoxes();
|
2021-03-13 05:06:34 +08:00
|
|
|
all->ComputeLimits(); // This is to make sure median iinfo is set.
|
|
|
|
InsertTextPartition(all); // This is to delete blobs
|
|
|
|
ColPartition *fragment_me = all->CopyButDontOwnBlobs();
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
finder_->SplitAndInsertFragmentedTextPartition(fragment_me);
|
|
|
|
finder_->ExpectPartition(TBOX(11, 5, 99, 15));
|
|
|
|
finder_->ExpectPartitionCount(1);
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|