fix: use inverse CDF for histogram matching - #808
Open
teddytennant wants to merge 1 commit into
Open
Conversation
teddytennant
force-pushed
the
fix/match-histogram-783
branch
from
August 8, 2026 17:08
12b76ed to
9344646
Compare
match_histogram_mut previously mapped each source level to the target level with nearest cumulative fraction. That could land in empty bins just before CDF jumps (for example 29 instead of 30), so the matched image histogram did not align with the target. Use the discrete inverse CDF instead: map source level i to the smallest target level y with target_cdf[y] >= source_cdf[i]. This matches the standard construction and fixes the example from issue image-rs#783. Update the gradient-to-step unit test expectations, add a regression test from the issue, and regenerate the elephant_matched truth image. Fixes image-rs#783
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
match_histogram_mutproduced suboptimal mappings for simple cases such as the one in #783:The old result invents gray levels (29, 129) that never appear in the target, so the matched histogram is farther from the target than necessary.
Root cause
histogram_lutchose, for each source level, the target level whose cumulative fraction was nearest in absolute distance. When the target CDF jumps (for example from 0 at level 29 to 0.5 at level 30), source fractions just above 0 are closer to the pre-jump empty bin than to the level where mass actually appears.Fix
Use the standard discrete inverse CDF:
lut[i] = min { y | target_cdf[y] >= source_cdf[i] }Comparisons are done with integer cross-multiplication to avoid floating point. Empty source or target images leave the LUT as zeros.
Tests
test_histogram_lut_gradient_to_step_contrastfor inverse-CDF expectations (maps onto 30/130 rather than 29/129).test_match_histogram_mut_maps_to_target_levelsfrom the issue example.tests/data/truth/elephant_matched.pngfor the regression suite.Fixes #783