Commit 2e47824e for tesseract

commit 2e47824e9122c201f2a766889e2dcbe4965058d8
Author: Stefan Weil <sw@weilnetz.de>
Date:   Wed Nov 25 17:10:39 2026 +0100

    Don't let an image take text which is not on its pixels

    The dark border of a scan is a frame around the page. As a component of
    the image mask it passes the test for a nearly rectangular image, which
    only looks at the edges, and is replaced by a solid rectangle of the size
    of the page. An image takes all partitions inside its bounding box which
    are not strong text, so page numbers and short running titles were lost
    on such scans (issue 3446).

    Keep the components of the mask as they were before they are made
    rectangles, and let an image take a weak text partition of at least two
    blobs only if the text has pixels of the component under it. Clear such
    text from the mask when the image partitions are written back to it, so
    that the second search for images gives the same result.

    Partitions of unknown type and single blobs are taken as before. Leaving
    them as well finds more of the missing text, but costs on newspaper
    pages. There the net of the rules makes the image of the whole page,
    which removes much noise.

    Results with tessdata_fast and the default page segmentation mode: on
    the ten pages of the issue the page numbers 416, 549, 96, 184 and 1064
    and the running title CURATOR are found now, on
    test/testing/8071_093.3B.tif the page number 93. Page numbers with a
    single digit, "65" with broken digits and the letter spaced "DECEMBER,
    1931" are still missing. On 161 pages of AustrianNewspapers 234,966
    instead of 235,290 of 333,385 words are correct; two pages with tables
    full of leaders lose 221 and 127. On 167 pages of the NZZ ground truth
    229,193 instead of 229,203 of 303,723 words are correct.

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

diff --git a/src/textord/imagefind.cpp b/src/textord/imagefind.cpp
index 07684d16..6cea744d 100644
--- a/src/textord/imagefind.cpp
+++ b/src/textord/imagefind.cpp
@@ -45,6 +45,10 @@ const double kMaxRectangularFraction = 0.75;
 const double kMaxRectangularGradient = 0.1; // About 6 degrees.
 // Minimum image size to be worth looking for images on.
 const int kMinImageFindSize = 100;
+// Min number of blobs in a weak text partition which an image only consumes
+// if the text is on the pixels of the image, see TextOffImage. Single blobs
+// are mostly noise.
+const int kMinWeakTextBlobs = 2;

 // Scans horizontally on x=[x_start,x_end), starting with y=*y_start,
 // stepping y+=y_step, until y=y_end. *ystart is input/output.
@@ -196,12 +200,17 @@ static bool pixNearlyRectangular(Image pix, double min_fraction, double max_frac
 // rectangular are replaced with solid rectangles.
 // The returned boxa, pixa may be nullptr, meaning no images found.
 // If not nullptr, they must be destroyed by the caller.
+// mask_boxa and mask_pixa get the same components as they are in the mask,
+// before any of them was made a rectangle.
 // Resolution of pix should match the source image (Tesseract::pix_binary_)
 // so the output coordinate systems match.
-static void ConnCompAndRectangularize(Image pix, DebugPixa *pixa_debug, Boxa **boxa,
-                                      Pixa **pixa) {
+static void ConnCompAndRectangularize(Image pix, DebugPixa *pixa_debug,
+                                      Boxa **boxa, Pixa **pixa,
+                                      Boxa **mask_boxa, Pixa **mask_pixa) {
   *boxa = nullptr;
   *pixa = nullptr;
+  *mask_boxa = nullptr;
+  *mask_pixa = nullptr;

   if (textord_tabfind_show_images && pixa_debug != nullptr) {
     pixa_debug->AddPix(pix, "Conncompimage");
@@ -214,6 +223,8 @@ static void ConnCompAndRectangularize(Image pix, DebugPixa *pixa_debug, Boxa **b
   int npixes = 0;
   if (*boxa != nullptr && *pixa != nullptr) {
     npixes = pixaGetCount(*pixa);
+    *mask_boxa = boxaCopy(*boxa, L_COPY);
+    *mask_pixa = pixaCopy(*pixa, L_COPY_CLONE);
   }
   for (int i = 0; i < npixes; ++i) {
     int x_start, x_end, y_start, y_end;
@@ -843,6 +854,36 @@ static void MaximalImageBoundingBox(ColPartitionGrid *part_grid, TBOX *im_box) {
   *im_box = text_box;
 }

+// A connected component of the image mask as it is in the mask, which can
+// be much less than the rectangle which is made of it.
+struct MaskComponent {
+  Image pix;         // The pixels of the component.
+  TBOX box;          // Their bounding box in the coordinates of the partitions.
+  FCOORD rerotation; // Rotates these coordinates to match the image.
+};
+
+// Returns true if part is weak text which can be more than noise: a row of
+// at least kMinWeakTextBlobs blobs which is not a leader.
+static bool WeakTextLine(const ColPartition &part) {
+  BlobRegionType type = part.blob_type();
+  BlobTextFlowType flow = part.flow();
+  return (type == BRT_TEXT || type == BRT_VERT_TEXT) &&
+         (flow == BTFT_NEIGHBOURS || flow == BTFT_CHAIN) &&
+         part.boxes_count() >= kMinWeakTextBlobs;
+}
+
+// Returns true if part is a weak text line which has no pixel of the image
+// component under it. An image takes the weak text inside its bounding box,
+// as text in a photo is mostly noise. But the bounding box can be much
+// larger than the image: the dark border of a scan is a frame around the
+// whole page, which would take a page number or a short running title.
+static bool TextOffImage(const ColPartition &part,
+                         const MaskComponent &component) {
+  return WeakTextLine(part) && ImageFind::CountPixelsInRotatedBox(
+                                   part.bounding_box(), component.box,
+                                   component.rerotation, component.pix) == 0;
+}
+
 // Helper deletes the given partition but first marks up all the blobs as
 // noise, so they get deleted later, and disowns them.
 // If the initial type of the partition is image, then it actually deletes
@@ -868,14 +909,18 @@ static void DeletePartition(ColPartition *part) {
 // expanded to consume overlapping and nearby ColPartitions of uncertain type
 // and other BRT_RECTIMAGE partitions, but NOT to be expanded beyond
 // max_image_box. *part_ptr is NOT in the part_grid.
+// component has the pixels of the image, which text must touch to be consumed.
 // rectsearch is already constructed on the part_grid, and is used for
 // searching for overlapping and nearby ColPartitions.
 // ExpandImageIntoParts is called iteratively until it returns false. Each
 // time it absorbs the nearest non-contained candidate, and everything that
 // is fully contained within part_ptr's bounding box.
 // TODO(rays) what if it just eats everything inside max_image_box in one go?
-static bool ExpandImageIntoParts(const TBOX &max_image_box, ColPartitionGridSearch *rectsearch,
-                                 ColPartitionGrid *part_grid, ColPartition **part_ptr) {
+static bool ExpandImageIntoParts(const TBOX &max_image_box,
+                                 const MaskComponent &component,
+                                 ColPartitionGridSearch *rectsearch,
+                                 ColPartitionGrid *part_grid,
+                                 ColPartition **part_ptr) {
   ColPartition *image_part = *part_ptr;
   TBOX im_part_box = image_part->bounding_box();
   if (textord_tabfind_show_images > 1) {
@@ -902,8 +947,9 @@ static bool ExpandImageIntoParts(const TBOX &max_image_box, ColPartitionGridSear
         tprintf("Real candidate\n");
       }
     }
-    if (part->flow() == BTFT_STRONG_CHAIN || part->flow() == BTFT_TEXT_ON_IMAGE ||
-        part->blob_type() == BRT_POLYIMAGE) {
+    if (part->flow() == BTFT_STRONG_CHAIN ||
+        part->flow() == BTFT_TEXT_ON_IMAGE ||
+        part->blob_type() == BRT_POLYIMAGE || TextOffImage(*part, component)) {
       continue;
     }
     TBOX box = part->bounding_box();
@@ -966,9 +1012,12 @@ static int IntersectArea(const TBOX &box, ColPartition_LIST *part_list) {
 // Tests whether part is to be consumed by the polygonal image.
 // Returns true if part is weak text and more than half of its area is
 // intersected by parts from the part_list, and it is contained within im_box.
-static bool TestWeakIntersectedPart(const TBOX &im_box, ColPartition_LIST *part_list,
+// Text which has no pixel of the image component under it is not consumed.
+static bool TestWeakIntersectedPart(const TBOX &im_box,
+                                    const MaskComponent &component,
+                                    ColPartition_LIST *part_list,
                                     ColPartition *part) {
-  if (part->flow() < BTFT_STRONG_CHAIN) {
+  if (part->flow() < BTFT_STRONG_CHAIN && !TextOffImage(*part, component)) {
     // A weak partition intersects the box.
     const TBOX &part_box = part->bounding_box();
     if (im_box.contains(part_box)) {
@@ -987,13 +1036,16 @@ static bool TestWeakIntersectedPart(const TBOX &im_box, ColPartition_LIST *part_
 // (basically anything that is not BRT_STRONG_CHAIN or better) from both the
 // part_grid and the big_parts list that are contained within im_box and
 // overlapped enough by the possibly polygonal image.
-static void EliminateWeakParts(const TBOX &im_box, ColPartitionGrid *part_grid,
-                               ColPartition_LIST *big_parts, ColPartition_LIST *part_list) {
+static void EliminateWeakParts(const TBOX &im_box,
+                               const MaskComponent &component,
+                               ColPartitionGrid *part_grid,
+                               ColPartition_LIST *big_parts,
+                               ColPartition_LIST *part_list) {
   ColPartitionGridSearch rectsearch(part_grid);
   ColPartition *part;
   rectsearch.StartRectSearch(im_box);
   while ((part = rectsearch.NextRectSearch()) != nullptr) {
-    if (TestWeakIntersectedPart(im_box, part_list, part)) {
+    if (TestWeakIntersectedPart(im_box, component, part_list, part)) {
       BlobRegionType type = part->blob_type();
       if (type == BRT_POLYIMAGE || type == BRT_RECTIMAGE) {
         rectsearch.RemoveBBox();
@@ -1010,7 +1062,7 @@ static void EliminateWeakParts(const TBOX &im_box, ColPartitionGrid *part_grid,
   ColPartition_IT big_it(big_parts);
   for (big_it.mark_cycle_pt(); !big_it.cycled_list(); big_it.forward()) {
     part = big_it.data();
-    if (TestWeakIntersectedPart(im_box, part_list, part)) {
+    if (TestWeakIntersectedPart(im_box, component, part_list, part)) {
       // Once marked, the blobs will be swept up by TidyBlobs.
       DeletePartition(big_it.extract());
     }
@@ -1044,6 +1096,29 @@ static bool ScanForOverlappingText(ColPartitionGrid *part_grid, TBOX *box) {
   return false;
 }

+// Clears the boxes of the weak text lines in box from the image_pix.
+// An image has not consumed such text only if the text has no pixel of the
+// image under it, see TextOffImage, and this must still be so when the
+// images are found again in the image_pix.
+// Box coordinates are rotated by rerotate to match the image.
+static void ClearWeakTextFromMask(const FCOORD &rerotate, const TBOX &box,
+                                  ColPartitionGrid *part_grid,
+                                  Image image_pix) {
+  int imageheight = pixGetHeight(image_pix);
+  ColPartitionGridSearch rectsearch(part_grid);
+  rectsearch.SetUniqueMode(true);
+  rectsearch.StartRectSearch(box);
+  ColPartition *part;
+  while ((part = rectsearch.NextRectSearch()) != nullptr) {
+    if (WeakTextLine(*part)) {
+      TBOX part_box = part->bounding_box();
+      part_box.rotate(rerotate);
+      pixRasterop(image_pix, part_box.left(), imageheight - part_box.top(),
+                  part_box.width(), part_box.height(), PIX_CLR, nullptr, 0, 0);
+    }
+  }
+}
+
 // Renders the boxes of image parts from the supplied list onto the image_pix,
 // except where they interfere with existing strong text in the part_grid,
 // and then deletes them.
@@ -1063,11 +1138,15 @@ static void MarkAndDeleteImageParts(const FCOORD &rerotate, ColPartitionGrid *pa
         type == BRT_POLYIMAGE) {
       // Mark the box on the image.
       // All coords need to be rotated to match the image.
-      part_box.rotate(rerotate);
-      int left = part_box.left();
-      int top = part_box.top();
-      pixRasterop(image_pix, left, imageheight - top, part_box.width(), part_box.height(), PIX_SET,
-                  nullptr, 0, 0);
+      TBOX mask_box(part_box);
+      mask_box.rotate(rerotate);
+      int left = mask_box.left();
+      int top = mask_box.top();
+      pixRasterop(image_pix, left, imageheight - top, mask_box.width(),
+                  mask_box.height(), PIX_SET, nullptr, 0, 0);
+      if (type == BRT_RECTIMAGE || type == BRT_POLYIMAGE) {
+        ClearWeakTextFromMask(rerotate, part_box, part_grid, image_pix);
+      }
     }
     DeletePartition(part);
   }
@@ -1140,7 +1219,10 @@ void ImageFind::FindImagePartitions(Image image_pix, const FCOORD &rotation,
   int imageheight = pixGetHeight(image_pix);
   Boxa *boxa;
   Pixa *pixa;
-  ConnCompAndRectangularize(image_pix, pixa_debug, &boxa, &pixa);
+  Boxa *mask_boxa;
+  Pixa *mask_pixa;
+  ConnCompAndRectangularize(image_pix, pixa_debug, &boxa, &pixa, &mask_boxa,
+                            &mask_pixa);
   // Iterate the connected components in the image regions mask.
   int nboxes = 0;
   if (boxa != nullptr && pixa != nullptr) {
@@ -1152,6 +1234,14 @@ void ImageFind::FindImagePartitions(Image image_pix, const FCOORD &rotation,
     Image pix = pixaGetPix(pixa, i, L_CLONE);
     TBOX im_box(x, imageheight - y - height, x + width, imageheight - y);
     im_box.rotate(rotation); // Now matches all partitions and blobs.
+    // The component as it is in the mask, to test which text is on the image.
+    MaskComponent component;
+    boxaGetBoxGeometry(mask_boxa, i, &x, &y, &width, &height);
+    component.pix = pixaGetPix(mask_pixa, i, L_CLONE);
+    component.box =
+        TBOX(x, imageheight - y - height, x + width, imageheight - y);
+    component.box.rotate(rotation);
+    component.rerotation = rerotation;
     ColPartitionGridSearch rectsearch(part_grid);
     rectsearch.SetUniqueMode(true);
     ColPartition_LIST part_list;
@@ -1170,14 +1260,15 @@ void ImageFind::FindImagePartitions(Image image_pix, const FCOORD &rotation,
         ColPartition *part = part_it.extract();
         TBOX text_box(im_box);
         MaximalImageBoundingBox(part_grid, &text_box);
-        while (ExpandImageIntoParts(text_box, &rectsearch, part_grid, &part)) {
+        while (ExpandImageIntoParts(text_box, component, &rectsearch, part_grid,
+                                    &part)) {
           ;
         }
         part_it.set_to_list(&part_list);
         part_it.add_after_then_move(part);
         im_box = part->bounding_box();
       }
-      EliminateWeakParts(im_box, part_grid, big_parts, &part_list);
+      EliminateWeakParts(im_box, component, part_grid, big_parts, &part_list);
       // Iterate the part_list and put the parts into the grid.
       for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
         ColPartition *image_part = part_it.extract();
@@ -1190,9 +1281,12 @@ void ImageFind::FindImagePartitions(Image image_pix, const FCOORD &rotation,
         }
       }
     }
+    component.pix.destroy();
   }
   boxaDestroy(&boxa);
   pixaDestroy(&pixa);
+  boxaDestroy(&mask_boxa);
+  pixaDestroy(&mask_pixa);
   DeleteSmallImages(part_grid);
 #ifndef GRAPHICS_DISABLED
   if (textord_tabfind_show_images) {
diff --git a/unittest/layout_test.cc b/unittest/layout_test.cc
index 6242b8ec..3d9b73df 100644
--- a/unittest/layout_test.cc
+++ b/unittest/layout_test.cc
@@ -9,11 +9,14 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.

+#include <iterator>
+#include <memory>
 #include <string>
 #include <utility>

 #include "include_gunit.h"

+#include <allheaders.h>
 #include <tesseract/baseapi.h>
 #include <tesseract/resultiterator.h>
 #include "coutln.h"
@@ -51,6 +54,21 @@ const char *kStrings8087_054[] = {"dat", "Dalmatian", "", "DAMAGED DURING", "mar
 const PolyBlockType kBlocks8087_054[] = {PT_HEADING_TEXT, PT_FLOWING_TEXT, PT_PULLOUT_IMAGE,
                                          PT_CAPTION_TEXT, PT_FLOWING_TEXT};

+// Size of the synthetic pages, about A5 at 300 dpi.
+const int kPageWidth = 2000;
+const int kPageHeight = 2800;
+// Left end, baseline and size of the page number of the synthetic pages.
+const int kPageNumberLeft = 300;
+const int kPageNumberBaseline = 2580;
+const int kPageNumberHeight = 60;
+// Width of the dark border of a scan.
+const int kScanBorder = 60;
+// Words for the text of the synthetic pages.
+const char *kPageWords[] = {"layout", "analysis", "finds", "the",     "text",
+                            "lines",  "of",       "a",     "printed", "page",
+                            "and",    "puts",     "them",  "into",    "blocks",
+                            "which",  "are",      "read",  "in",      "order"};
+
 // The fixture for testing Tesseract.
 class LayoutTest : public testing::Test {
 protected:
@@ -182,9 +200,96 @@ protected:
     } while (it->Next(tesseract::RIL_BLOCK));
   }

+  // 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);
+    int width = 0;
+    int overflow = 0;
+    pixSetTextline(pix, font, text, 0, x, y, &width, &overflow);
+    return width;
+  }
+
+  // Makes a white 8 bit page with 32 lines of text.
+  static Pix *MakeTextPage() {
+    Pix *pix = pixCreate(kPageWidth, kPageHeight, 8);
+    pixSetAll(pix);
+    int word = 0;
+    for (int y = 400; y <= 2300; y += 60) {
+      for (int x = 300; x < 1450;) {
+        x += DrawText(pix, kPageWords[word++ % std::size(kPageWords)], x, y) +
+             16;
+      }
+    }
+    return pix;
+  }
+
+  // Paints the given rectangle black, or white if not black.
+  static void FillRect(Pix *pix, int left, int top, int width, int height,
+                       bool black) {
+    Box *box = boxCreate(left, top, width, height);
+    if (black) {
+      pixClearInRect(pix, box);
+    } else {
+      pixSetInRect(pix, box);
+    }
+    boxDestroy(&box);
+  }
+
+  // Adds a dark border on all sides of the page, like the scan of a page
+  // which is smaller than the scanned area.
+  static void AddScanBorder(Pix *pix) {
+    FillRect(pix, 0, 0, kPageWidth, kScanBorder, true);
+    FillRect(pix, 0, kPageHeight - kScanBorder, kPageWidth, kScanBorder, true);
+    FillRect(pix, 0, 0, kScanBorder, kPageHeight, true);
+    FillRect(pix, kPageWidth - kScanBorder, 0, kScanBorder, kPageHeight, true);
+  }
+
+  // Takes a synthetic page, which is destroyed here, and runs the layout
+  // analysis on it.
+  void AnalysePage(Pix *page) {
+    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_);
+    page_it_.reset(api_.AnalyseLayout());
+  }
+
+  // Returns the number of text lines which the last AnalysePage found
+  // completely inside the given rectangle.
+  int CountTextLinesIn(int left, int top, int right, int bottom) {
+    int count = 0;
+    if (page_it_ == nullptr) {
+      return count;
+    }
+    page_it_->Begin();
+    do {
+      int line_left, line_top, line_right, line_bottom;
+      if (PTIsTextType(page_it_->BlockType()) &&
+          page_it_->BoundingBox(tesseract::RIL_TEXTLINE, &line_left, &line_top,
+                                &line_right, &line_bottom) &&
+          line_left >= left && line_right <= right && line_top >= top &&
+          line_bottom <= bottom) {
+        ++count;
+      }
+    } while (page_it_->Next(tesseract::RIL_TEXTLINE));
+    return count;
+  }
+
+  // Returns the number of text lines found where the page number is.
+  int CountPageNumberLines() {
+    return CountTextLinesIn(kPageNumberLeft - 50,
+                            kPageNumberBaseline - kPageNumberHeight,
+                            kPageNumberLeft + 250, kPageNumberBaseline + 30);
+  }
+
   Image src_pix_;
   std::string ocr_text_;
   tesseract::TessBaseAPI api_;
+  std::unique_ptr<tesseract::PageIterator> page_it_;
 };

 // Tests that array sizes match their intended size.
@@ -235,4 +340,29 @@ TEST_F(LayoutTest, HebrewOrderingAndSkew) {
   delete it;
 }

+// Tests that a page number is found on a clean page. The following tests
+// need this to mean anything.
+TEST_F(LayoutTest, PageNumber) {
+  REQUIRE_TRAINEDDATA(TESSDATA_DIR, "eng");
+  Pix *page = MakeTextPage();
+  DrawText(page, "184", kPageNumberLeft, kPageNumberBaseline);
+  AnalysePage(page);
+  EXPECT_EQ(CountTextLinesIn(250, 340, 1750, 2330), 32);
+  EXPECT_EQ(CountPageNumberLines(), 1);
+}
+
+// Tests that the dark border of a scan, which is a frame around the page,
+// does not take the page number and a running title as part of an image.
+TEST_F(LayoutTest, PageNumberInsideScanBorder) {
+  REQUIRE_TRAINEDDATA(TESSDATA_DIR, "eng");
+  Pix *page = MakeTextPage();
+  DrawText(page, "CURATOR", 300, 240);
+  DrawText(page, "184", kPageNumberLeft, kPageNumberBaseline);
+  AddScanBorder(page);
+  AnalysePage(page);
+  EXPECT_EQ(CountTextLinesIn(250, 340, 1750, 2330), 32);
+  EXPECT_EQ(CountTextLinesIn(250, 180, 750, 270), 1);
+  EXPECT_EQ(CountPageNumberLines(), 1);
+}
+
 } // namespace tesseract