Repository navigation
fix(utils): plot (H, W, 1) grayscale images with the colormap - #3024
Open
MohammadHijjawi97 wants to merge 4 commits into
Open
MohammadHijjawi97 wants to merge 4 commits into
MohammadHijjawi97 wants to merge 4 commits into
Conversation
plot_image and plot_images_grid only sent (H, W) arrays to the colormap branch. An (H, W, 1) image went through BGR to RGB conversion instead, which raises ValueError in the NumPy backend and, with OpenCV, turns the image into RGB so cmap is ignored. Treat both shapes as grayscale, as grayscale_image and tint_image already do.
# Conflicts: # docs/changelog.md
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #3024 +/- ##
=======================================
Coverage 95% 95%
=======================================
Files 84 84
Lines 15408 15409 +1
=======================================
+ Hits 14677 14680 +3
+ Misses 731 729 -2 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The grayscale plotting fix has no active review issues.
No open issues 🎉
What changed in this PR
This PR fixes Supervision’s plotting utilities so grayscale images shaped (H, W, 1) use the requested colormap, matching (H, W) behavior with or without OpenCV.
Changes:
- Reuse
_is_single_channeland reshape grayscale inputs before plotting. - Add parametrized regression tests for both grayscale shapes in both plotting functions.
- Document accepted shapes and record the fix in the changelog.
| File | Description |
|---|---|
tests/utils/test_notebook.py |
Tests grayscale image values, displayed shape, and colormap for both plotting functions. |
src/supervision/utils/notebook.py |
Handles trailing single-channel axes and updates cmap documentation. |
docs/changelog.md |
Adds an Unreleased entry describing the grayscale plotting fix. |
Review effort: Balanced
# Conflicts: # docs/changelog.md
This branch has not been deployed
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.
Description
sv.plot_imageandsv.plot_images_gridonly send(H, W)arrays to the colormap branch. An(H, W, 1)grayscale image goes through BGR to RGB conversion instead:ValueError: BGR/RGB conversion requires a three- or four-channel imageopencv-pythonit is converted to a 3-channel RGB image, socmapis ignoredThe rest of the library already treats
(H, W, 1)as grayscale (sv.grayscale_image,sv.tint_image, the annotators), so such an image cannot currently be plotted the way an(H, W)image is.Minimal repro:
developValueError: BGR/RGB conversion requires a three- or four-channel image(4, 4) viridisopencv-python5.0.0(4, 4, 3), drawn as RGB, colormap not applied(4, 4) viridisMotivation and Context
Grayscale images often come with a trailing channel axis, for example after slicing one channel with
image[..., :1]. Plotting them should behave like plotting(H, W)images, and the same way in both backends.Changes Made
plot_imageandplot_images_griduse the existing_is_single_channelhelper and drop the channel axis before drawing withcmap.cmapargument docs name the accepted shapes.(H, W, 4)images are displayed.Testing
New parametrized tests in
tests/utils/test_notebook.pyfor both functions (two-dimensional,trailing-channel-axis). Ondevelopthetrailing-channel-axiscases fail: with aValueErrorwithout OpenCV, and on the drawn shape(4, 4, 3)withopencv-python. They pass with this change in both setups.uv run pytestwithopencv-pythoninstalled: 8098 passed, 36 skipped.uv run pytest tests/utilswithout OpenCV: 400 passed, 2 skipped (including thenotebook.pydoctests).pre-commit runon the changed files: passed (ruff, mypy, docformatter, mdformat, codespell).Added/updated tests, and the full suite passes locally
Updated docs (docstrings / mkdocs entry) for new or changed public API
Added a changelog entry in
docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
The channel axis is dropped with
reshape(image.shape[:2]), so(H, W)input is unchanged.