Skip to content
This repository was archived by the owner on Jul 2, 2021. It is now read-only.

Show in legend only classes that appear in an image for vis_label #345

Merged
merged 6 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 14 additions & 3 deletions chainercv/visualizations/vis_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def _default_cmap(label):

def vis_label(
label, label_names=None,
label_colors=None, ignore_label_color=(0, 0, 0), alpha=1, ax=None):
label_colors=None, ignore_label_color=(0, 0, 0), alpha=1,
all_label_names_in_legend=False, ax=None):
"""Visualize a label for semantic segmentation.

Example:
Expand Down Expand Up @@ -64,6 +65,12 @@ def vis_label(
value is :obj:`0`, the figure will be completely transparent.
The default value is :obj:`1`. This option is useful for
overlaying the label on the source image.
all_label_names_in_legend (bool): Determines whether to include
all label names in a legend. If this is :obj:`False`,
the legend does not contain the names of unused labels.
An unused label is defined as a label that does not appear in
:obj:`label`.
The default value is :obj:`False`.
ax (matplotlib.axes.Axis): The visualization is displayed on this
axis. If this is :obj:`None` (default), a new axis is created.

Expand Down Expand Up @@ -115,8 +122,12 @@ def vis_label(
ax.imshow(img)

legend_handles = list()
for l, label_name in enumerate(label_names):
if all_label_names_in_legend:
legend_labels = [l for l in np.unique(label) if l >= 0]
else:
legend_labels = range(len(label_names))
Copy link
Member

Choose a reason for hiding this comment

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

len(label_names) can be replaced with n_class.

for l in legend_labels:
legend_handles.append(
Patch(color=cmap(l / (n_class - 1)), label=label_name))
Patch(color=cmap(l / (n_class - 1)), label=label_names[l]))

return ax, legend_handles
4 changes: 3 additions & 1 deletion tests/visualizations_tests/test_vis_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@testing.parameterize(*testing.product({
'label_names': [None, ('class0', 'class1', 'class2')],
'label_colors': [None, ((255, 0, 0), (0, 255, 0), (0, 0, 255))],
'all_label_names_in_legend': [False, True],
}))
class TestVisLabel(unittest.TestCase):

Expand All @@ -26,7 +27,8 @@ def test_vis_label(self):
if optional_modules:
ax, legend_handles = vis_label(
self.label,
label_names=self.label_names, label_colors=self.label_colors)
label_names=self.label_names, label_colors=self.label_colors,
all_label_names_in_legend=self.all_label_names_in_legend)

self.assertIsInstance(ax, matplotlib.axes.Axes)
for handle in legend_handles:
Expand Down