Skip to content

fix: add a dtype argument to convolve and keep the sign of gradients - #246

Merged
petercorke merged 1 commit into
mainfrom
bug/convolve-dtype
Nov 23, 2026
Merged

petercorke merged 1 commit into
mainfrom
bug/convolve-dtype

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Fixes the bug-list item where gradients() of a uint8 image lost every negative gradient. A falling ramp (slope -4) gave 0; the same image as float32 gave -4.

The cause was convolve(), which called cv2.filter2D with ddepth=-1, so the result had the datatype of the input. OpenCV sums the products in a wider type, so the sum does not overflow, but the result is then clipped (not wrapped) to the range of the output type. For uint8 that is 0 to 255.

  • convolve() takes a new dtype argument: 'same' (default, so smooth() and every existing caller are unchanged), 'int16', 'float32' or 'float64'. A combination OpenCV cannot do (for example 'float64' from a float32 image) raises a ValueError that lists the choices.
  • gradients() now returns float32 (float64 for a float64 image). With the normalised Sobel kernel, the gradients of a uint8 image lie within ±127.5.
  • convolve() built its result with Image(out), which narrows uint16 and int16 to uint8 and float64 to float32. It now names the datatype, so a uint16 image stays uint16 and smooth() of a float64 image stays float64.
  • The convolve() docstring now explains the clipping (the old note said "wraparound", which was wrong), has a dtype table, and .. versionchanged:: 2.5.0 notes; so does gradients().
  • The visible change: gradients() of an integer image is now float32, not the image datatype.

Tests: a falling-ramp uint8 image, uint8 and float32 images giving the same gradients, the ±127.5 bound, each dtype option, the refused combinations, clipping at both ends with the default, and wider datatypes surviving the default. mypy reports no issues and the full suite passes (2134 passed, 33 skipped).

AI-assisted: written with Claude Code (Claude Sonnet 5.5) and reviewed by the maintainer.

🤖 Generated with Claude Code

gradients() of a uint8 image returned uint8, so every negative gradient was
clipped to zero. convolve() now takes dtype ('same', 'int16', 'float32' or
'float64'), default 'same', and gradients() asks for float32 (float64 for a
float64 image). convolve() also named no datatype when building its result,
so uint16 and int16 images came back as uint8 and float64 as float32.

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 18 complexity · 0 duplication

Metric Results
Complexity 18
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 bc0f1b3 into main Nov 23, 2026
34 checks passed
@petercorke
petercorke deleted the bug/convolve-dtype branch November 23, 2026 05:39
@github-actions github-actions Bot mentioned this pull request Nov 23, 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