Commit d1529032 for tesseract
commit d15290320d412d374914944a423c566314b6a4cf
Author: Stefan Weil <sw@weilnetz.de>
Date: Sun Nov 22 21:59:40 2026 +0100
Ignore specks of noise in the font style estimation
The stroke width of a word is the median length of the horizontal runs
of text pixels. Each black pixel of noise was a run of the length 1, so
heavy noise hid the strokes of the text: the stroke width of the typical
word of a page dropped, and words with fewer specks were reported as bold.
Leave out the runs which touch no text in the row above or below, for the
stroke width and for the slant. Text which is only one pixel row high
still uses all runs.
Measured on 42 rendered pages with mixed styles (precision / recall of
bold per word):
Gaussian noise, sigma 60: 26.3 % / 53.1 % -> 99.3 % / 96.8 %
2 % salt-and-pepper noise: 98.1 % / 85.1 % -> 100.0 % / 93.6 %
no noise: 99.8 % / 93.6 % (unchanged)
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/ccmain/fontstyle.cpp b/src/ccmain/fontstyle.cpp
index 788f855f..307bdd76 100644
--- a/src/ccmain/fontstyle.cpp
+++ b/src/ccmain/fontstyle.cpp
@@ -78,30 +78,45 @@ bool MeasureTextShape(Image pix, TextShape *shape) {
return false;
}
- // Collect the runs of text pixels and count them by their length.
+ // Collect the runs of text pixels and count them by their length. A run
+ // which touches no text in the row above or below it is a speck of noise.
+ // Many of those would hide the strokes of the text, so they are left out.
std::vector<PixelRun> runs;
- std::vector<int> num_runs(width + 1);
+ std::vector<PixelRun> specks;
const l_uint32 *data = pixGetData(pix);
const int wpl = pixGetWpl(pix);
for (int y = 0; y < height; ++y) {
const l_uint32 *line = data + static_cast<size_t>(y) * wpl;
+ const l_uint32 *above = y > 0 ? line - wpl : nullptr;
+ const l_uint32 *below = y + 1 < height ? line + wpl : nullptr;
int start = -1;
+ bool connected = false;
for (int x = 0; x <= width; ++x) {
const bool text = x < width && GET_DATA_BIT(line, x) != 0;
if (text) {
if (start < 0) {
start = x;
+ connected = false;
}
+ connected = connected || (above != nullptr && GET_DATA_BIT(above, x) != 0) ||
+ (below != nullptr && GET_DATA_BIT(below, x) != 0);
} else if (start >= 0) {
- runs.push_back({y, start, x});
- ++num_runs[x - start];
+ (connected ? runs : specks).push_back({y, start, x});
start = -1;
}
}
}
+ if (runs.empty()) {
+ // The text has no stroke which is higher than a pixel.
+ runs.swap(specks);
+ }
if (runs.empty()) {
return false;
}
+ std::vector<int> num_runs(width + 1);
+ for (const auto &run : runs) {
+ ++num_runs[run.end - run.start];
+ }
// Most runs cross a single stroke, so the median of their lengths is the
// stroke width. The runs of the length l count as spread evenly from
diff --git a/unittest/fontstyle_test.cc b/unittest/fontstyle_test.cc
index e2232f82..8c4483fb 100644
--- a/unittest/fontstyle_test.cc
+++ b/unittest/fontstyle_test.cc
@@ -115,6 +115,46 @@ TEST(FontStyleTest, StrokeWidthBetweenPixels) {
}
}
+// Specks of noise are no strokes, even if there are more of them than rows
+// of the strokes.
+TEST(FontStyleTest, IgnoresSpecks) {
+ for (float slant : {0.0f, 0.2f}) {
+ Image pix = MakeBars(5, slant);
+ const int width = pixGetWidth(pix);
+ const int height = pixGetHeight(pix);
+ int num_specks = 0;
+ for (int y = 0; y < height; y += 2) {
+ // Single pixels left and right of the bars, which touch no other text.
+ for (int x = y % 4; x < width; x += 4) {
+ if (x < 35 || x >= 160) {
+ pixSetPixel(pix, x, y, 1);
+ ++num_specks;
+ }
+ }
+ }
+ // The bars have four runs of pixels in each row.
+ ASSERT_GT(num_specks, 4 * height);
+ TextShape shape;
+ ASSERT_TRUE(MeasureTextShape(pix, &shape));
+ EXPECT_FLOAT_EQ(shape.stroke_width, 5.0f) << "slant " << slant;
+ EXPECT_TRUE(shape.has_slant) << "slant " << slant;
+ EXPECT_NEAR(shape.slant, slant, 0.015f) << "slant " << slant;
+ pix.destroy();
+ }
+}
+
+// Text which is not higher than a pixel still has a stroke width.
+TEST(FontStyleTest, MeasuresSingleRow) {
+ Image pix = pixCreate(50, 1, 1);
+ for (int x = 10; x < 16; ++x) {
+ pixSetPixel(pix, x, 0, 1);
+ }
+ TextShape shape;
+ ASSERT_TRUE(MeasureTextShape(pix, &shape));
+ EXPECT_FLOAT_EQ(shape.stroke_width, 6.0f);
+ pix.destroy();
+}
+
TEST(FontStyleTest, RejectsUnusableImages) {
TextShape shape;
shape.slant = 7.0f;