def _plot_class_regions(img, regions, alpha, colors):
    """Plot class regions using optional alpha.

    Plot class regions from a dictionary of flattened matrix indices as
    overlay over 'img'; 'alpha' must have the same number of elements as
    'regions' and indicate the alpha value of the pixel.
    """
    res = img.copy()
    for kls, reg in regions.items():
        xx_, yy_ = reg.T

        if alpha is None:
            res[yy_, xx_, :] = colors[kls]
        else:
            k_alpha = alpha[kls][:, None]
            res[yy_, xx_, :] = (1 - k_alpha) * res[yy_, xx_, :] + \
                k_alpha * colors[kls]
    return res