Commit cc187a7e for tesseract

commit cc187a7e514096773790e7348fe243dc0f1a1fa1
Author: Stefan Weil <sw@weilnetz.de>
Date:   Thu Nov 26 13:24:38 2026 +0100

    Don't reject a row made from two columns as noise

    In the modes for a single block or a single column, the text lines of
    two columns with different line spacing share their rows. Such a row
    gets the baseline of one of the lines. If the characters of the other
    line reach only a little above that baseline, the row gets an x-height
    which is much too small. The noise test for rows counts every blob of
    more than twice the x-height as two dots, so the row looked like noise
    and was deleted (issue 2109: the line "No. : 91385763" beside
    "GAJOVA 13" was missing with --psm 6 and --psm 4).

    Blobs of more than twice the x-height must be an exception in a row. If
    a quarter of the blobs is that tall, the x-height does not fit the row,
    and the median height of the blobs is the better size for the noise
    test if it is larger.

    A row which the noise test rejects is now tested again with that size,
    and it is deleted only if both tests reject it. Using the other size
    instead of the x-height would not be enough: it also raises the limit
    below which an outline counts as a dot, and on 328 newspaper pages that
    deleted 12 rows which are kept with the x-height. With both tests the
    change can only keep rows which were deleted before.

    Results with tessdata_fast: the image of the issue gives the line with
    --psm 4 and --psm 6. In the default page segmentation mode 65 of 328
    newspaper pages change, on which rows of timetables and rows made from
    two columns are kept now. These rows are badly recognized, so recall
    rises and precision falls a little: on 161 pages of AustrianNewspapers
    336,136 instead of 335,830 words are found, of which 235,358 instead of
    235,302 of 333,385 are correct, on 167 pages of the NZZ ground truth
    308,064 instead of 307,925 words, of which 229,287 instead of 229,257
    of 303,723 are correct.

    Assisted-by: Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/src/textord/textord.h b/src/textord/textord.h
index 894104c4..6cc5c02f 100644
--- a/src/textord/textord.h
+++ b/src/textord/textord.h
@@ -205,6 +205,7 @@ private:
   void cleanup_nontext_block(BLOCK *block);
   void cleanup_blocks(bool clean_noise, BLOCK_LIST *blocks);
   bool clean_noise_from_row(ROW *row);
+  bool row_is_noise(ROW *row, float x_height);
   void clean_noise_from_words(ROW *row);
   // Remove outlines that are a tiny fraction in either width or height
   // of the word height.
diff --git a/src/textord/tordmain.cpp b/src/textord/tordmain.cpp
index 778711eb..43b48c6e 100644
--- a/src/textord/tordmain.cpp
+++ b/src/textord/tordmain.cpp
@@ -49,10 +49,12 @@
 #include "tprintf.h"     // for tprintf
 #include "werd.h"        // for WERD_IT, WERD, WERD_LIST, W_DONT_CHOP

+#include <algorithm> // for std::sort
 #include <cfloat>  // for FLT_MAX
 #include <cmath>   // for ceil, floor, M_PI
 #include <cstdint> // for INT16_MAX, uint32_t, int32_t, int16_t
 #include <memory>
+#include <vector>

 namespace tesseract {

@@ -470,6 +472,35 @@ void Textord::cleanup_blocks(bool clean_noise, BLOCK_LIST *blocks) {
   }
 }

+// Returns the size which the noise tests of a row use as its x-height.
+// A row which was made from text lines with different baselines, like those
+// of two columns with different line spacing in a single block, can get an
+// x-height which is much too small. Many of its characters then look like
+// blobs which are too tall for the row, and the row looks like noise.
+// Blobs of more than twice the x-height must be an exception: if a quarter
+// of the blobs is that tall, the x-height does not fit the row, and the
+// median height of the blobs is used in its place if it is larger.
+static float NoiseXHeight(ROW *row) {
+  std::vector<int> heights;
+  WERD_IT word_it(row->word_list());
+  for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
+    C_BLOB_IT blob_it(word_it.data()->cblob_list());
+    for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
+      heights.push_back(blob_it.data()->bounding_box().height());
+    }
+  }
+  float x_height = row->x_height();
+  if (!heights.empty()) {
+    std::sort(heights.begin(), heights.end());
+    int upper_quartile = heights[heights.size() * 3 / 4];
+    int median = heights[heights.size() / 2];
+    if (upper_quartile > 2 * x_height && median > x_height) {
+      x_height = median;
+    }
+  }
+  return x_height;
+}
+
 /**********************************************************************
  * clean_noise_from_row
  *
@@ -478,6 +509,27 @@ void Textord::cleanup_blocks(bool clean_noise, BLOCK_LIST *blocks) {

 bool Textord::clean_noise_from_row( // remove empties
     ROW *row                        // row to clean
+) {
+  // The row must look like noise with its own x-height. If its blobs give
+  // a different size, it must look like noise with that size, too, so the
+  // second test can only keep a row which the first test rejects.
+  if (!row_is_noise(row, row->x_height())) {
+    return false;
+  }
+  const float x_height = NoiseXHeight(row);
+  return x_height == row->x_height() || row_is_noise(row, x_height);
+}
+
+/**********************************************************************
+ * row_is_noise
+ *
+ * Return true if the row looks like garbage when its blobs are compared
+ * with the given x-height.
+ **********************************************************************/
+
+bool Textord::row_is_noise( // test for garbage
+    ROW *row,               // row to test
+    float x_height          // x-height for the tests
 ) {
   bool testing_on;
   TBOX blob_box;            // bounding box
@@ -513,14 +565,14 @@ bool Textord::clean_noise_from_row( // remove empties
           outline = out_it.data();
           blob_box = outline->bounding_box();
           blob_size = blob_box.width() > blob_box.height() ? blob_box.width() : blob_box.height();
-          if (blob_size < textord_noise_sizelimit * row->x_height()) {
+          if (blob_size < textord_noise_sizelimit * x_height) {
             dot_count++; // count small outlines
           }
           if (!outline->child()->empty() &&
-              blob_box.height() < (1 + textord_noise_syfract) * row->x_height() &&
-              blob_box.height() > (1 - textord_noise_syfract) * row->x_height() &&
-              blob_box.width() < (1 + textord_noise_sxfract) * row->x_height() &&
-              blob_box.width() > (1 - textord_noise_sxfract) * row->x_height()) {
+              blob_box.height() < (1 + textord_noise_syfract) * x_height &&
+              blob_box.height() > (1 - textord_noise_syfract) * x_height &&
+              blob_box.width() < (1 + textord_noise_sxfract) * x_height &&
+              blob_box.width() > (1 - textord_noise_sxfract) * x_height) {
             super_norm_count++; // count small outlines
           }
         }
@@ -529,8 +581,8 @@ bool Textord::clean_noise_from_row( // remove empties
       }
       blob_box = blob->bounding_box();
       blob_size = blob_box.width() > blob_box.height() ? blob_box.width() : blob_box.height();
-      if (blob_size >= textord_noise_sizelimit * row->x_height() &&
-          blob_size < row->x_height() * 2) {
+      if (blob_size >= textord_noise_sizelimit * x_height &&
+          blob_size < x_height * 2) {
         // Avoid an integer division by zero if the parameter is not positive.
         trans_threshold = textord_noise_sizefraction > 0 ? blob_size / textord_noise_sizefraction
                                                          : blob_size;
@@ -538,7 +590,7 @@ bool Textord::clean_noise_from_row( // remove empties
         if (trans_count < textord_noise_translimit) {
           norm_count++;
         }
-      } else if (blob_box.height() > row->x_height() * 2 &&
+      } else if (blob_box.height() > x_height * 2 &&
                  (!word_it.at_first() || !blob_it.at_first())) {
         dot_count += 2;
       }
diff --git a/unittest/layout_test.cc b/unittest/layout_test.cc
index 3a9a0c07..ee870122 100644
--- a/unittest/layout_test.cc
+++ b/unittest/layout_test.cc
@@ -201,13 +201,18 @@ protected:
   }

   // Writes text with its baseline at y from x to the right on pix, with a
-  // font which is built in Leptonica. Returns the width of the text.
-  static int DrawText(Pix *pix, const char *text, int x, int y) {
-    static L_BMF *font = bmfCreate(nullptr, 10);
-    CHECK(font != nullptr);
+  // font of the given size (an even number from 4 to 20) which is built in
+  // Leptonica. Returns the width of the text.
+  static int DrawText(Pix *pix, const char *text, int x, int y, int size = 10) {
+    static L_BMF *fonts[21];
+    CHECK(size >= 4 && size <= 20);
+    if (fonts[size] == nullptr) {
+      fonts[size] = bmfCreate(nullptr, size);
+    }
+    CHECK(fonts[size] != nullptr);
     int width = 0;
     int overflow = 0;
-    pixSetTextline(pix, font, text, 0, x, y, &width, &overflow);
+    pixSetTextline(pix, fonts[size], text, 0, x, y, &width, &overflow);
     return width;
   }

@@ -248,12 +253,13 @@ protected:

   // Takes a synthetic page, which is destroyed here, and runs the layout
   // analysis on it. A resolution of 0 leaves it to Tesseract.
-  void AnalysePage(Pix *page, int resolution = 0) {
+  void AnalysePage(Pix *page, int resolution = 0,
+                   tesseract::PageSegMode mode = tesseract::PSM_AUTO) {
     src_pix_.destroy();
     src_pix_ = pixThresholdToBinary(page, 128);
     pixDestroy(&page);
     ASSERT_EQ(api_.Init(TessdataPath().c_str(), "eng"), 0);
-    api_.SetPageSegMode(tesseract::PSM_AUTO);
+    api_.SetPageSegMode(mode);
     api_.SetImage(src_pix_);
     if (resolution > 0) {
       api_.SetSourceResolution(resolution);
@@ -411,4 +417,21 @@ TEST_F(LayoutTest, WordWithLittleMargin) {
   EXPECT_EQ(CountTextLinesIn(0, 0, width, height), 1);
 }

+// Tests that a row which is made from the lines of two columns is not taken
+// for noise. In the mode for a single block the lines of both columns share
+// their rows. Here the left line is lower than the right one, which has the
+// larger font, so the row gets the baseline of the right line and an
+// x-height which is less than half the height of its characters.
+TEST_F(LayoutTest, RowFromTwoColumns) {
+  REQUIRE_TRAINEDDATA(TESSDATA_DIR, "eng");
+  const int kBaseline = 1230;
+  Pix *page = MakeTextPage();
+  // Make room in the middle of the text.
+  FillRect(page, 200, 1090, 1600, 250, false);
+  DrawText(page, "GAJOVA 13", 300, kBaseline + 14);
+  DrawText(page, "No. : 91385763", 900, kBaseline, 12);
+  AnalysePage(page, 0, tesseract::PSM_SINGLE_BLOCK);
+  EXPECT_EQ(CountTextLinesIn(250, kBaseline - 60, 1750, kBaseline + 60), 1);
+}
+
 } // namespace tesseract