Commit 15579145 for tesseract

commit 15579145177d373fac45db4b33b6feee800eb769
Author: Stefan Weil <sw@weilnetz.de>
Date:   Wed Nov 25 17:41:27 2026 +0100

    Find text lines which touch the edge of the image

    The layout analysis takes a row of blobs for a text line if the
    projection map, a map of the padded blob boxes at about 100 dpi, has a
    gradient across the upper and lower edge of the row. The map had the
    size of the image, and positions outside were moved to its edge. A word
    with 3 or 4 pixels of margin fills the map up to the edge at 250 dpi and
    more, because the boxes are padded by one pixel of the map above and
    below. So there was no gradient, the partition stayed of unknown type
    and was deleted, and the result was "Empty page!!" (issue 3670). With
    220 dpi and less one empty row of the map was left and the word was
    found.

    Give the map a margin of four pixels around the image, and let the
    padding of the blobs go on into the margin where only the edge of the
    image stopped it, as it would in a larger image.

    Results with tessdata_fast and the default page segmentation mode: of
    the 112 images of single words of the issue 7 instead of 107 give no
    text. Three of the seven have a single character and four a word of two
    blobs. On 161 pages of AustrianNewspapers 235,302 instead of 235,271 of
    333,385 words are correct, on 167 pages of the NZZ ground truth 229,257
    instead of 229,252 of 303,723.

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

diff --git a/src/textord/textlineprojection.cpp b/src/textord/textlineprojection.cpp
index a10a9a9a..714ad2e9 100644
--- a/src/textord/textlineprojection.cpp
+++ b/src/textord/textlineprojection.cpp
@@ -42,6 +42,10 @@ const int kParaPerpDistRatio = 4;
 const int kMinLineSpacingFactor = 4;
 // Maximum tab-stop overrun for horizontal padding, in projection pixels.
 const int kMaxTabStopOverrun = 6;
+// Margin of the projection map around the image, in projection pixels.
+// The gradients are measured up to 3 pixels from the edge of a box, and the
+// map is smoothed over 3 pixels.
+const int kProjectionMargin = 4;

 namespace tesseract {

@@ -67,10 +71,16 @@ void TextlineProjection::ConstructProjection(TO_BLOCK *input_block, const FCOORD
                                              Image nontext_map) {
   pix_.destroy();
   TBOX image_box(0, 0, pixGetWidth(nontext_map), pixGetHeight(nontext_map));
-  x_origin_ = 0;
-  y_origin_ = image_box.height();
-  int width = (image_box.width() + scale_factor_ - 1) / scale_factor_;
-  int height = (image_box.height() + scale_factor_ - 1) / scale_factor_;
+  // The map has a margin around the image. Without it there is nothing to
+  // compare a text line with which touches the edge of the image, or is so
+  // close to it that the padding of its blobs reaches the edge. Such a line
+  // had no gradient across that edge and did not look like a text line.
+  x_origin_ = -kProjectionMargin * scale_factor_;
+  y_origin_ = image_box.height() + kProjectionMargin * scale_factor_;
+  int width = (image_box.width() + scale_factor_ - 1) / scale_factor_ +
+              2 * kProjectionMargin;
+  int height = (image_box.height() + scale_factor_ - 1) / scale_factor_ +
+               2 * kProjectionMargin;

   pix_ = pixCreate(width, height, 8);
   ProjectBlobs(&input_block->blobs, rotation, image_box, nontext_map);
@@ -666,10 +676,25 @@ void TextlineProjection::ProjectBlobs(BLOBNBOX_LIST *blobs, const FCOORD &rotati
       spreading_horizontally = !spreading_horizontally;
     }
     // Clip to the image before applying the increments.
+    const TBOX padded_box(bbox);
     bbox &= nontext_map_box; // This is in-place box intersection.
     // Check for image pixels before spreading.
     TruncateBoxToMissNonText(middle.x(), middle.y(), spreading_horizontally, nontext_map, &bbox);
     if (bbox.area() > 0) {
+      // Where the box was only stopped by the edge of the image, it goes on
+      // into the margin of the map, as it would in a larger image.
+      if (bbox.left() == nontext_map_box.left()) {
+        bbox.set_left(padded_box.left());
+      }
+      if (bbox.right() == nontext_map_box.right()) {
+        bbox.set_right(padded_box.right());
+      }
+      if (bbox.bottom() == nontext_map_box.bottom()) {
+        bbox.set_bottom(padded_box.bottom());
+      }
+      if (bbox.top() == nontext_map_box.top()) {
+        bbox.set_top(padded_box.top());
+      }
       IncrementRectangle8Bit(bbox);
     }
   }
diff --git a/unittest/layout_test.cc b/unittest/layout_test.cc
index 7514c8cf..3a9a0c07 100644
--- a/unittest/layout_test.cc
+++ b/unittest/layout_test.cc
@@ -247,14 +247,17 @@ protected:
   }

   // Takes a synthetic page, which is destroyed here, and runs the layout
-  // analysis on it.
-  void AnalysePage(Pix *page) {
+  // analysis on it. A resolution of 0 leaves it to Tesseract.
+  void AnalysePage(Pix *page, int resolution = 0) {
     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_.SetImage(src_pix_);
+    if (resolution > 0) {
+      api_.SetSourceResolution(resolution);
+    }
     page_it_.reset(api_.AnalyseLayout());
   }

@@ -382,4 +385,30 @@ TEST_F(LayoutTest, TextLineBetweenLeaders) {
   EXPECT_EQ(CountTextLinesIn(600, 1175, 1400, 1260), 1);
 }

+// Tests that a word is found in an image which has only a few pixels of
+// margin around it, like the images of single words or lines. The digits
+// all have the same height, so the word has straight edges.
+TEST_F(LayoutTest, WordWithLittleMargin) {
+  REQUIRE_TRAINEDDATA(TESSDATA_DIR, "eng");
+  const int kMargin = 4;
+  Pix *page = pixCreate(600, 200, 8);
+  pixSetAll(page);
+  DrawText(page, "6881", 100, 120);
+  // Cut out the word with its margin.
+  Pix *binary = pixThresholdToBinary(page, 128);
+  Box *word_box = nullptr;
+  pixClipToForeground(binary, nullptr, &word_box);
+  ASSERT_NE(word_box, nullptr);
+  Pix *word = pixClipRectangle(page, word_box, nullptr);
+  Pix *image = pixAddBorder(word, kMargin, 255);
+  const int width = pixGetWidth(image);
+  const int height = pixGetHeight(image);
+  pixDestroy(&binary);
+  boxDestroy(&word_box);
+  pixDestroy(&word);
+  pixDestroy(&page);
+  AnalysePage(image, 300);
+  EXPECT_EQ(CountTextLinesIn(0, 0, width, height), 1);
+}
+
 } // namespace tesseract