18#include <allheaders.h>
24#define ENABLE_IdentifySpecialText_TEST 0
25#if ENABLE_IdentifySpecialText_TEST
26# define EQU_TRAINEDDATA_NAME "equ"
28# define EQU_TRAINEDDATA_NAME "equINTENTIONALLY_MISSING_FILE"
43 CHECK(part !=
nullptr);
44 CHECK_LE(math_blobs + digit_blobs, total_blobs);
46 for (
int i = 0;
i < math_blobs;
i++,
count++) {
51 for (
int i = 0;
i < digit_blobs;
i++,
count++) {
56 for (
int i =
count;
i < total_blobs;
i++) {
108 CHECK(part_grid !=
nullptr);
124 std::locale::global(std::locale(
""));
141 CHECK(pix !=
nullptr);
142 CHECK(blocks !=
nullptr);
143 BLOCK_IT block_it(blocks);
144 auto *block =
new BLOCK(
"",
true, 0, 0, 0, 0, pixGetWidth(pix), pixGetHeight(pix));
145 block_it.add_to_end(block);
150 std::vector<ColPartition *> *all_parts) {
151 const int kWidth = 10, kHeight = 10;
153 for (
int y = 0;
y < rows; ++
y) {
154 for (
int x = 0;
x < cols; ++
x) {
155 int left =
x * kWidth * 2, bottom =
y * kHeight * 2;
156 TBOX box(left, bottom, left + kWidth, bottom + kHeight);
159 all_parts->push_back(part);
165 for (
auto &all_part : *all_parts) {
166 all_part->DeleteBoxes();
173 CHECK(part !=
nullptr);
175 blob->set_bounding_box(tbox);
181#if !ENABLE_IdentifySpecialText_TEST
185 std::string imagefile =
file::JoinPath(testdata_dir_,
"equ_gt1.tif");
186 Image pix_binary = pixRead(imagefile.c_str());
187 CHECK(pix_binary !=
nullptr && pixGetDepth(pix_binary) == 1);
191 TO_BLOCK_LIST to_blocks;
192 AddPageBlock(pix_binary, &blocks);
193 Textord *textord = tesseract_->mutable_textord();
197 TO_BLOCK_IT to_block_it(&to_blocks);
198 std::map<int, int> stt_count;
199 for (to_block_it.mark_cycle_pt(); !to_block_it.cycled_list(); to_block_it.forward()) {
200 TO_BLOCK *to_block = to_block_it.data();
201 BLOBNBOX_IT blob_it(&(to_block->
blobs));
202 for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
205 equation_det_->RunIdentifySpecialText(blob, 0);
212 const int kCountRange = 3;
260 TBOX box1(0, 950, 999, 999);
263 TBOX box2(300, 920, 900, 940);
266 TBOX box3(0, 900, 600, 910);
269 TBOX box4(300, 890, 600, 899);
272 TBOX box5(300, 500, 900, 510);
308 TBOX box1(0, 950, 499, 999);
309 TBOX box2(500, 950, 999, 998);
310 TBOX box3(0, 900, 499, 949);
311 TBOX box4(0, 550, 499, 590);
315 EXPECT_TRUE(equation_det_->RunIsNearSmallNeighbor(box1, box2));
316 EXPECT_FALSE(equation_det_->RunIsNearSmallNeighbor(box2, box1));
318 EXPECT_TRUE(equation_det_->RunIsNearSmallNeighbor(box1, box3));
319 EXPECT_FALSE(equation_det_->RunIsNearSmallNeighbor(box2, box3));
321 EXPECT_FALSE(equation_det_->RunIsNearSmallNeighbor(box2, box3));
322 EXPECT_FALSE(equation_det_->RunIsNearSmallNeighbor(box3, box2));
325 EXPECT_FALSE(equation_det_->RunIsNearSmallNeighbor(box1, box4));
326 EXPECT_FALSE(equation_det_->RunIsNearSmallNeighbor(box2, box4));
327 EXPECT_FALSE(equation_det_->RunIsNearSmallNeighbor(box3, box4));
331 TBOX box(0, 950, 999, 999);
338 equation_det_->AddMathDigitBlobs(8, 0, 20, part1);
339 EXPECT_TRUE(equation_det_->RunCheckSeedBlobsCount(part1));
342 equation_det_->AddMathDigitBlobs(1, 8, 20, part2);
343 EXPECT_FALSE(equation_det_->RunCheckSeedBlobsCount(part2));
346 equation_det_->AddMathDigitBlobs(3, 8, 20, part3);
347 EXPECT_TRUE(equation_det_->RunCheckSeedBlobsCount(part3));
350 equation_det_->AddMathDigitBlobs(0, 0, 8, part4);
351 EXPECT_FALSE(equation_det_->RunCheckSeedBlobsCount(part4));
366 int width = 1024, height = 768;
367 Image pix = pixCreate(width, height, 1);
368 pixRasterop(pix, 0, 0, width, height / 2, PIX_SET,
nullptr, 0, 0);
369 TBOX box1(100, 0, 140, 140), box2(100, height / 2 - 20, 140, height / 2 + 20),
370 box3(100, height - 40, 140, height);
371 equation_det_->SetPixBinary(pix);
374 EXPECT_NEAR(0.0, equation_det_->RunComputeForegroundDensity(box1), 0.0001f);
375 EXPECT_NEAR(0.5, equation_det_->RunComputeForegroundDensity(box2), 0.0001f);
376 EXPECT_NEAR(1.0, equation_det_->RunComputeForegroundDensity(box3), 0.0001f);
380 std::vector<int> vec;
389 EXPECT_EQ(3, equation_det_->RunCountAlignment(vec, 1));
390 EXPECT_EQ(1, equation_det_->RunCountAlignment(vec, 100));
391 EXPECT_EQ(2, equation_det_->RunCountAlignment(vec, 200));
394 EXPECT_EQ(3, equation_det_->RunCountAlignment(vec, 3));
395 EXPECT_EQ(1, equation_det_->RunCountAlignment(vec, 99));
396 EXPECT_EQ(2, equation_det_->RunCountAlignment(vec, 202));
399 EXPECT_EQ(0, equation_det_->RunCountAlignment(vec, 150));
400 EXPECT_EQ(0, equation_det_->RunCountAlignment(vec, 50));
401 EXPECT_EQ(0, equation_det_->RunCountAlignment(vec, 250));
405 Image pix = pixCreate(1001, 1001, 1);
406 equation_det_->SetPixBinary(pix);
409 TBOX box1(0, 0, 999, 99);
411 TBOX box2(0, 100, 499, 199);
413 TBOX box3(500, 100, 999, 199);
415 TBOX box4(0, 200, 999, 299);
417 TBOX box5(0, 900, 999, 999);
424 TBOX super_box(0, 0, 999, 199);
425 equation_det_->TestComputeCPsSuperBBox(super_box, &part_grid);
429 TBOX super_box2(0, 0, 999, 299);
430 equation_det_->TestComputeCPsSuperBBox(super_box2, &part_grid);
434 TBOX super_box3(0, 0, 999, 999);
435 equation_det_->TestComputeCPsSuperBBox(super_box3, &part_grid);
451 TBOX box(0, 0, 999, 99);
455 std::vector<TBOX> splitted_boxes;
458 equation_det_->RunSplitCPHorLite(part, &splitted_boxes);
462 AddBlobIntoPart(
TBOX(0, 0, 10, 50), part);
463 equation_det_->RunSplitCPHorLite(part, &splitted_boxes);
468 AddBlobIntoPart(
TBOX(11, 0, 20, 60), part);
469 AddBlobIntoPart(
TBOX(25, 0, 30, 55), part);
470 AddBlobIntoPart(
TBOX(100, 0, 110, 15), part);
471 AddBlobIntoPart(
TBOX(125, 0, 140, 45), part);
472 AddBlobIntoPart(
TBOX(500, 0, 540, 35), part);
473 equation_det_->RunSplitCPHorLite(part, &splitted_boxes);
485 TBOX box(0, 0, 999, 99);
489 std::vector<ColPartition *> parts_splitted;
492 equation_det_->RunSplitCPHor(part, &parts_splitted);
495 AddBlobIntoPart(
TBOX(0, 0, 10, 50), part);
497 equation_det_->RunSplitCPHor(part, &parts_splitted);
502 AddBlobIntoPart(
TBOX(11, 0, 20, 60), part);
503 AddBlobIntoPart(
TBOX(25, 0, 30, 55), part);
504 AddBlobIntoPart(
TBOX(100, 0, 110, 15), part);
505 AddBlobIntoPart(
TBOX(125, 0, 140, 45), part);
506 AddBlobIntoPart(
TBOX(500, 0, 540, 35), part);
507 equation_det_->RunSplitCPHor(part, &parts_splitted);
512 EXPECT_TRUE(
TBOX(100, 0, 140, 45) == parts_splitted[1]->bounding_box());
513 EXPECT_TRUE(
TBOX(500, 0, 540, 35) == parts_splitted[2]->bounding_box());
515 for (
auto part_splitted : parts_splitted) {
516 delete part_splitted;
#define EQU_TRAINEDDATA_NAME
#define EXPECT_EQ(val1, val2)
#define EXPECT_NEAR(val1, val2, abs_error)
#define EXPECT_GE(val1, val2)
#define EXPECT_TRUE(condition)
#define EXPECT_LE(val1, val2)
#define EXPECT_FALSE(condition)
#define CHECK_EQ(test, value)
#define CHECK_LE(test, value)
TEST_F(EuroText, FastLatinOCR)
bool CheckSeedBlobsCount(ColPartition *part)
int CountAlignment(const std::vector< int > &sorted_vec, const int val) const
IndentType IsIndented(ColPartition *part)
void SplitCPHor(ColPartition *part, std::vector< ColPartition * > *parts_splitted)
ColPartitionGrid * part_grid_
BlobSpecialTextType EstimateTypeForUnichar(const UNICHARSET &unicharset, const UNICHAR_ID id) const
float ComputeForegroundDensity(const TBOX &tbox)
bool IsNearSmallNeighbor(const TBOX &seed_box, const TBOX &part_box) const
void ComputeCPsSuperBBox()
Tesseract * lang_tesseract_
void SplitCPHorLite(ColPartition *part, std::vector< TBOX > *splitted_boxes)
void SetLangTesseract(Tesseract *lang_tesseract)
void IdentifySpecialText()
Image * mutable_pix_binary()
BlobSpecialTextType special_text_type() const
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
void InsertBBox(bool h_spread, bool v_spread, BBC *bbox)
static ColPartition * FakePartition(const TBOX &box, PolyBlockType block_type, BlobRegionType blob_type, BlobTextFlowType flow)
void AddBox(BLOBNBOX *box)
void set_median_width(int width)
void find_components(Image pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
void SetPixBinary(Image pix)
void RunIdentifySpecialText(BLOBNBOX *blob, const int height_th)
EquationDetect::IndentType RunIsIndented(ColPartitionGrid *part_grid, ColPartition *part)
void RunSplitCPHor(ColPartition *part, std::vector< ColPartition * > *parts_splitted)
bool RunCheckSeedBlobsCount(ColPartition *part)
TestableEquationDetect(const char *tessdata, Tesseract *lang_tesseract)
float RunComputeForegroundDensity(const TBOX &tbox)
BlobSpecialTextType RunEstimateTypeForUnichar(const char *val)
void AddMathDigitBlobs(const int math_blobs, const int digit_blobs, const int total_blobs, ColPartition *part)
bool RunIsNearSmallNeighbor(const TBOX &seed_box, const TBOX &part_box)
void RunSplitCPHorLite(ColPartition *part, std::vector< TBOX > *splitted_boxes)
int RunCountAlignment(const std::vector< int > &sorted_vec, const int val)
void TestComputeCPsSuperBBox(const TBOX &box, ColPartitionGrid *part_grid)
std::unique_ptr< Tesseract > tesseract_
void ClearParts(std::vector< ColPartition * > *all_parts)
std::unique_ptr< TestableEquationDetect > equation_det_
std::string testdata_dir_
void AddPageBlock(Image pix, BLOCK_LIST *blocks)
void CreateColParts(const int rows, const int cols, ColPartitionGrid *part_grid, std::vector< ColPartition * > *all_parts)
void AddBlobIntoPart(const TBOX &tbox, ColPartition *part)
static std::string JoinPath(const std::string &s1, const std::string &s2)