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
6 changes: 4 additions & 2 deletions src/machinevisiontoolbox/base/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,10 @@ def csconvert(name: str, cs: str) -> np.ndarray:
color = (color * np.iinfo(dtype).max).astype(dtype)
if colororder is not None:
# reorder the elements of the color if colororder is given
# colororder is a dict mapping plane name to index
color = [color[colororder[c]] for c in colorspace]
# colororder is a dict mapping plane name to index -- fancy
# index rather than a list comprehension, to keep the
# ndarray return type promised by this function's contract
color = color[[colororder[c] for c in colorspace]]
return color
except ValueError:
return None
Expand Down
15 changes: 14 additions & 1 deletion tests/base/test_base_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,20 @@ def test_name2color(self):
# Test different color spaces
g_xy = color.name2color('g', 'xy')
self.assertEqual(len(g_xy), 2)


def test_name2color_colororder(self):
"""colororder= must still return an ndarray, not a list -- regression
test for a bug where the reordering used a list comprehension and
silently dropped the ndarray return type"""
red_rgb = color.name2color('red', colororder={'R': 0, 'G': 1, 'B': 2})
self.assertIsInstance(red_rgb, np.ndarray)
nt.assert_array_almost_equal(red_rgb, [1, 0, 0])

# reordered: B first, so red's 1.0 moves to the last position
red_bgr = color.name2color('red', colororder={'B': 0, 'G': 1, 'R': 2})
self.assertIsInstance(red_bgr, np.ndarray)
nt.assert_array_almost_equal(red_bgr, [0, 0, 1])

def test_colorname(self):
"""Test color to name conversion"""
# Test basic lookup
Expand Down
12 changes: 12 additions & 0 deletions tests/test_image_point_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ def test_corners(self):
except:
pass

def test_draw2(self):
"""draw2() with a named color and a colorized (colororder-bearing)
image must not raise -- regression test for name2color() leaking a
plain list instead of an ndarray when colororder is given"""
img = Image.Read("monalisa.png", mono=True)
orb = img.ORB(nfeatures=20)
self.assertGreater(len(orb), 0)

color_img = img.colorize()
result = orb.draw2(color_img, color="y")
self.assertIsInstance(result, Image)

def test_features_list_operations(self):
"""Test feature list operations"""
img = Image.Read("monalisa.png", mono=True)
Expand Down