Skip to content

Correct adaptive threshold windows for narrow regions and borders - #3229

Merged
JimBobSquarePants merged 2 commits into
SixLabors:mainfrom
jafin:jf/adaptive-threshold-narrow-regions
Oct 27, 2026
Merged

JimBobSquarePants merged 2 commits into
SixLabors:mainfrom
jafin:jf/adaptive-threshold-narrow-regions

Conversation

@jafin

@jafin jafin commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull requests open
  • I have followed the existing coding patterns and practices
  • I have provided test coverage for my change

Description

Adaptive thresholding can produce an empty window for narrow regions and borders. The integral-image query also shifts the window away from the current pixel. Empty windows always select the lower color.

The window now stays centered on the current pixel and includes both endpoints. Its bounds are clipped to the processing rectangle. The sum and pixel count use those same inclusive bounds, including for one-pixel dimensions. The existing radius cap, bulk luminance conversion, integral-image allocation and parallel row iterator remain in use. Integral row spans are acquired once per output row.

The tests check every pixel, including borders. A separate direct-sum calculation checks centered windows, single-pixel dimensions, the radius transition, non-origin rectangles and unchanged pixels outside the rectangle. All 12 regression cases fail on the original PR and pass with this correction.

Five old reference images were independently checked before updating them. Direct neighborhood summation reproduced every old reference with the old shifted windows and matched every corrected output with centered windows. The changed pixel counts are 181 for Bradley01, 579 for Bradley02, 90 for Ducky, 55 for Issue2217 and 20 for the Bradley02 rectangle. Exact comparisons remain unchanged.

Verification

Release, .NET 10: 127 passed, 0 failed, 0 skipped for binarization and integral-image tests.

dotnet test tests/ImageSharp.Tests/ImageSharp.Tests.csproj -c Release -f net10.0 --filter "FullyQualifiedName~Binarization|FullyQualifiedName~IntegralImageTests"

Fixes #3241

…n 16 pixels

The cluster size is the region width divided by 16, truncated, so it was 0
for regions narrower than 16 pixels. Every averaging window was then empty
and each pixel took the lower color. Use a cluster size of at least 1, as
regions 16 to 31 pixels wide already do. Wider regions are unchanged.
Include the current pixel and both window endpoints when querying the inclusive integral image. Reuse integral row spans and hoist vertical bounds out of the pixel loop. Check all border pixels and single-pixel dimensions against direct neighborhood sums.

Independent direct summation reproduced every old reference with the old shifted windows and matched every corrected output with centered windows. Update those five proven incorrect references; exact comparisons remain unchanged.
@JimBobSquarePants JimBobSquarePants changed the title Keep the adaptive threshold window non-empty for regions narrower than 16 pixels Correct adaptive threshold windows for narrow regions and borders Oct 27, 2026
@JimBobSquarePants
JimBobSquarePants merged commit 1a3183e into SixLabors:main Oct 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AdaptiveThreshold turns every pixel black when the image or rectangle is narrower than 16 pixels

2 participants