Skip to content

fix: stop sad, ssd and ncc wrapping around on uint8 images - #236

Merged
petercorke merged 2 commits into
mainfrom
bug/similarity-uint8-wraparound
Nov 22, 2026
Merged

petercorke merged 2 commits into
mainfrom
bug/similarity-uint8-wraparound

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Image.sad(), ssd() and ncc() computed with the pixel arrays as they came, so on uint8 images the arithmetic wrapped around at 256. For the images [[10, 200]] and [[200, 10]]:

before correct
sad 256 380
ssd 8 72,200
ncc 2.54 0.0998

sad subtracted the arrays, where 10 - 200 is 66 and not -190. ssd also subtracted and then squared in uint8. ncc squared and multiplied uint8 values, so it could exceed 1, which a normalized correlation cannot. On small differences the wraparound sometimes cancels (ssd happened to be right on the two small images in the existing tests), which is why it went unnoticed.

The three now convert to float first. The z versions (zsad, zssd, zncc) already did, since they subtract the mean, so they were right.

Found by the new similarity tests (PR 223), which recorded it as a strict xfail test. That test is now an ordinary test, with a second one for the large differences above.

Testing

tests/test_image_spatial_extra.py and tests/test_image_spatial.py: 119 passed and 3 xfail, each of which belongs to a different bug. mypy reports no issues. Not run: CI on Windows or OpenCV 4 (CI will).

This change was written with AI assistance (Claude Code) and the diff has been read by the maintainer.

🤖 Generated with Claude Code

image1._A - image2._A on uint8 arrays wraps at 256, so sad of two uint8 images
was wrong (10 - 200 is 66, not -190), and ncc squared the pixel values in uint8
and could exceed 1. ssd was only right by luck of arithmetic modulo 256 in some
cases. The three now convert to float before subtracting, squaring or
multiplying. The z versions already did, through the mean.

The xfail test that recorded the bug is now an ordinary test, with a second test
of large differences.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit df7ab0a into main Nov 22, 2026
34 checks passed
@petercorke
petercorke deleted the bug/similarity-uint8-wraparound branch November 22, 2026 21:23
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant