Skip to content

fix: make stereo_simple return outputs the size of its inputs - #240

Merged
petercorke merged 4 commits into
mainfrom
bug/stereo-simple-size
Nov 22, 2026
Merged

petercorke merged 4 commits into
mainfrom
bug/stereo-simple-size

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Image.stereo_simple() had three faults that all come from how it builds its stack of windows.

  1. The disparity, similarity and disparity space images were larger than the inputs by 2*hw in each direction, although the docstring says they are the same size, and the result for pixel (u, v) was at (u+hw, v+hw). The window stack is now cropped to the windows centred on the image's pixels. The existing strict xfail test recorded this.
  2. For an integer image the windows were padded with NaN, which numpy casts to 0 (with a RuntimeWarning), so windows that extend beyond the image were treated as valid zero padding, and a uint8 image gave different results from the same image as float32. The images are now converted to float32 first, so the padding is a real NaN for every image type.
  3. The best disparity was found with np.argmax and np.max, so a single NaN similarity at one disparity made the whole pixel NaN, even where other disparities were valid. A pixel now takes the best of its valid disparities, and is NaN only if none is valid.

Behaviour change

Outputs are the size of the inputs. A band of hw NaN pixels runs along the top, bottom and right edges, and the left band is hw + drange[0] wide (in each plane of the DSI, hw + drange[0] + d). The redundant disparity[:, :drange[0]] = nan line is removed, since those columns are already NaN. In the interior, values are identical to before. I compared the old and new output on a random 30x40 uint8 pair: where both gave a number the disparity, similarity and DSI values were identical.

Windows that extend beyond the left edge of a uint8 image used to be matched against zero padding, and now give NaN. This is a visible change for uint8 images. For any image type, a pixel near the left edge that has some valid disparities used to be NaN and now has a disparity (change 3).

A .. note:: in the docstring describes the NaN bands, and .. versionchanged:: 2.5.0 describes the change.

Tests

The strict xfail test is now an ordinary test. New tests cover the NaN band on all four sides and in each DSI plane, uint8 and float32 giving the same result with warnings turned into errors, a pixel keeping its valid disparities, and a pixel with none being NaN. tests/test_image_multiview.py passes (25 tests) and mypy reports no errors. I have not run the full suite locally. CI will.

DSI_refine() has related faults (it uses np.argmax on a DSI that can contain NaN, and it treats drange as plane indices). They are not changed here.

This change was written with AI assistance (Claude Code) and I have reviewed the diff.

🤖 Generated with Claude Code

petercorke and others added 3 commits November 23, 2026 07:12
The window stack is 2*hw larger than the image in each direction, and the
outputs kept that padding, against the docstring. Crop the stack to the
windows centred on the image's pixels, so the disparity, similarity and
disparity space images are the size of the inputs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
An integer image was padded with NaN, which numpy casts to 0, so windows
beyond the image were treated as valid zero padding, and a float image gave
a different result. Convert to float32 first. A NaN similarity at one
disparity also made the whole pixel NaN, because np.max propagates NaN, so
take the best of the valid disparities and give NaN only to a pixel with
none.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Add a note on the width of the NaN band in the disparity, similarity and
DSI images, remove the now redundant assignment of the left columns, and
test the band on every side and in each plane of the DSI.

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 12 complexity · 4 duplication

Metric Results
Complexity 12
Duplication 4

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 ce37ae3 into main Nov 22, 2026
32 checks passed
@petercorke
petercorke deleted the bug/stereo-simple-size branch November 22, 2026 22:08
@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