unique_labels = list(set(label.flat))
// Ensure that the background label is in front to match call to `chain`.
if bg_label in unique_labels:
unique_labels.remove(bg_label)
unique_labels.insert(0, bg_label)
// Modify labels and color cycle so background color is used only once.
color_cycle = itertools.cycle(colors)
After Change
bg_color = _rgb_vector([bg_color])
// map labels to their ranks among all labels from small to large
unique_labels, mapped_labels = np.unique(label, return_inverse=True)
// get rank of bg_label
bg_label_rank_list = mapped_labels[label.flat == bg_label]