Skip to content

fix(utils): plot (H, W, 1) grayscale images with the colormap - #3024

Open
MohammadHijjawi97 wants to merge 4 commits into
roboflow:developfrom
MohammadHijjawi97:fix/plot-single-channel-images
Open

MohammadHijjawi97 wants to merge 4 commits into
roboflow:developfrom
MohammadHijjawi97:fix/plot-single-channel-images

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown
Contributor

Description

sv.plot_image and sv.plot_images_grid only send (H, W) arrays to the colormap branch. An (H, W, 1) grayscale image goes through BGR to RGB conversion instead:

  • without OpenCV it raises ValueError: BGR/RGB conversion requires a three- or four-channel image
  • with opencv-python it is converted to a 3-channel RGB image, so cmap is ignored

The 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:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import supervision as sv

plt.show = lambda: None
image = np.arange(16, dtype=np.uint8).reshape(4, 4, 1)
sv.plot_image(image, cmap="viridis")
(drawn,) = plt.gcf().axes[0].get_images()
print(drawn.get_array().shape, drawn.get_cmap().name)
develop this PR
no OpenCV ValueError: BGR/RGB conversion requires a three- or four-channel image (4, 4) viridis
opencv-python 5.0.0 (4, 4, 3), drawn as RGB, colormap not applied (4, 4) viridis

Motivation 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_image and plot_images_grid use the existing _is_single_channel helper and drop the channel axis before drawing with cmap.
  • The cmap argument docs name the accepted shapes.
  • Added a changelog entry.
  • Out of scope: how (H, W, 4) images are displayed.

Testing

  • New parametrized tests in tests/utils/test_notebook.py for both functions (two-dimensional, trailing-channel-axis). On develop the trailing-channel-axis cases fail: with a ValueError without OpenCV, and on the drawn shape (4, 4, 3) with opencv-python. They pass with this change in both setups.

  • uv run pytest with opencv-python installed: 8098 passed, 36 skipped.

  • uv run pytest tests/utils without OpenCV: 400 passed, 2 skipped (including the notebook.py doctests).

  • pre-commit run on 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.md under Unreleased (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.

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.
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95%. Comparing base (cdb0fa0) to head (dd8af95).
⚠️ Report is 1 commits behind head on develop.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda requested a balanced review from Copilot November 18, 2026 21:42
@Borda Borda added the bug Something isn't working label Nov 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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_channel and 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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working has conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants