Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/machinevisiontoolbox/ImageIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ def showpixels(
if isinstance(cmap, str):
cmap = plt.get_cmap(cmap)
if badcolor is not None:
cmap.set_bad(color=badcolor)
# with_extremes returns a copy, so the caller's colormap is not changed
cmap = cmap.with_extremes(bad=badcolor)
ax.imshow(image, cmap=cmap, zorder=1)

# label the cells with the pixel values
Expand Down
20 changes: 12 additions & 8 deletions src/machinevisiontoolbox/base/imageio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import copy
import glob
import urllib.error
import urllib.request
Expand Down Expand Up @@ -778,13 +777,18 @@ def _idisp_matplotlib(
#
# only works for greyscale image
if im.ndim == 2:
cmap = copy.copy(cmap)
if undercolor is not None:
cmap.set_under(color=undercolor)
if overcolor is not None:
cmap.set_over(color=overcolor)
if badcolor is not None:
cmap.set_bad(color=badcolor)
# with_extremes returns a copy, so the caller's colormap is not changed
extremes = {
key: color
for key, color in (
("under", undercolor),
("over", overcolor),
("bad", badcolor),
)
if color is not None
}
if extremes:
cmap = cmap.with_extremes(**extremes)
# elif im.ndim == 3:
# if badcolor is not None:
# cmap.set_bad(color=badcolor)
Expand Down
27 changes: 27 additions & 0 deletions tests/base/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,33 @@ def test_idisp_badcolor_option(self):
result = idisp(im, matplotlib=False, badcolor="red")
self.assertIsNone(result)

def test_idisp_extreme_colors_without_a_deprecation_warning(self):
"""Colormap.set_under, set_over and set_bad are pending deprecation"""
import warnings

import matplotlib
import matplotlib.pyplot as plt

from machinevisiontoolbox.base.imageio import idisp

matplotlib.use("Agg")
im = np.random.randint(100, 200, (30, 30), dtype=np.uint8)
with warnings.catch_warnings():
warnings.simplefilter("error", PendingDeprecationWarning)
idisp(
im,
undercolor="blue",
overcolor="yellow",
badcolor="red",
vrange=(120, 180),
block=False,
)
cmap = plt.gca().images[0].get_cmap()
np.testing.assert_allclose(cmap.get_under(), (0.0, 0.0, 1.0, 1.0))
np.testing.assert_allclose(cmap.get_over(), (1.0, 1.0, 0.0, 1.0))
np.testing.assert_allclose(cmap.get_bad(), (1.0, 0.0, 0.0, 1.0))
plt.close("all")

def test_idisp_undercolor_overcolor(self):
"""Test idisp with undercolor and overcolor"""
im = np.random.randint(100, 200, (30, 30), dtype=np.uint8)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_image_io_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ def test_colormap_by_name_and_bad_color(self):
ax = im.showpixels(cmap="gray", badcolor="red")
assert ax.images[0].get_cmap().name == "gray"

def test_bad_color_without_a_deprecation_warning(self):
# Colormap.set_bad is pending deprecation in Matplotlib
import warnings

im = Image(np.array([[1.0, np.nan]]))
with warnings.catch_warnings():
warnings.simplefilter("error", PendingDeprecationWarning)
ax = im.showpixels(cmap="gray", badcolor="red")
nt.assert_allclose(ax.images[0].get_cmap().get_bad(), (1.0, 0.0, 0.0, 1.0))

def test_bad_color_leaves_the_callers_colormap_alone(self):
from matplotlib.colors import LinearSegmentedColormap

cmap = LinearSegmentedColormap.from_list("mine", ["#000000", "#ffffff"])
before = np.array(cmap.get_bad())
Image(np.array([[1.0, np.nan]])).showpixels(cmap=cmap, badcolor="red")
nt.assert_allclose(cmap.get_bad(), before)

def test_default_colormap(self):
ax = Image(np.zeros((2, 2), dtype=np.uint8)).showpixels()
assert ax.images[0].get_cmap().name == "midnight"
Expand Down
Loading