Skip to content

fix: do not change the template in similarity with a z metric - #237

Merged
petercorke merged 2 commits into
mainfrom
bug/similarity-z-template
Nov 22, 2026
Merged

petercorke merged 2 commits into
mainfrom
bug/similarity-z-template

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

image.similarity(T, "zncc") and "zssd" removed the mean from the template with T_im -= np.mean(T_im), which changes the caller's template in place. Calling it a second time, or using the template for anything else afterwards, used a different template. On a uint8 image or template the same line raised UFuncTypeError, because a float mean cannot be subtracted in place from a uint8 array, so the z metrics did not work on 8-bit images at all.

The offsets of the template and of the image are now removed into new float32 arrays, so nothing the caller owns is changed, and 8-bit images work.

Found by the new similarity tests (PR 223), which recorded both as strict xfail tests. They are now ordinary tests. A zncc on uint8 images finds the template at its position with a score of 1, leaves the image and the template unchanged, and a zssd test does the same for its minimum.

Testing

tests/test_image_spatial_extra.py and tests/test_image_spatial.py: 120 passed and 2 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

similarity(T, "zncc") and "zssd" removed the template's mean with
T_im -= np.mean(T_im), which changes the caller's template in place, so
a second call used a different template. On a uint8 image or template the same
line raised UFuncTypeError, because a float mean cannot be subtracted in place
from uint8.

The offsets are now removed into new float32 arrays, which also is the type
cv2.matchTemplate needs for the result to be meaningful.

The two xfail tests that recorded the bugs are now ordinary tests, with a z-SSD
test on uint8 images.

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 · 2 duplication

Metric Results
Complexity 0
Duplication 2

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 08d7184 into main Nov 22, 2026
32 checks passed
@petercorke
petercorke deleted the bug/similarity-z-template branch November 22, 2026 21:33
@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