Repository navigation
fix: add a dtype argument to convolve and keep the sign of gradients - #246
Merged
Merged
Conversation
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>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 18 |
| Duplication | 0 |
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.
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.
Fixes the bug-list item where
gradients()of auint8image lost every negative gradient. A falling ramp (slope -4) gave 0; the same image asfloat32gave -4.The cause was
convolve(), which calledcv2.filter2Dwithddepth=-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. Foruint8that is 0 to 255.convolve()takes a newdtypeargument:'same'(default, sosmooth()and every existing caller are unchanged),'int16','float32'or'float64'. A combination OpenCV cannot do (for example'float64'from afloat32image) raises aValueErrorthat lists the choices.gradients()now returnsfloat32(float64for afloat64image). With the normalised Sobel kernel, the gradients of auint8image lie within ±127.5.convolve()built its result withImage(out), which narrowsuint16andint16touint8andfloat64tofloat32. It now names the datatype, so auint16image staysuint16andsmooth()of afloat64image staysfloat64.convolve()docstring now explains the clipping (the old note said "wraparound", which was wrong), has adtypetable, and.. versionchanged:: 2.5.0notes; so doesgradients().gradients()of an integer image is nowfloat32, not the image datatype.Tests: a falling-ramp
uint8image,uint8andfloat32images giving the same gradients, the ±127.5 bound, eachdtypeoption, 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