Python skimage.color.label2rgb() Examples

The following are 13 code examples of skimage.color.label2rgb(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module skimage.color , or try the search function .
Example #1
Source File: spectral_roi.py    From Pic-Numero with MIT License 7 votes vote down vote up
def experiment_with_parameters():
    '''
    Apply spectral clustering to test wheat image using k-means clustering with
    different values of k and different compactness values.

    Saves the results to the Clusters folder for inspection.
    '''
    img = misc.imread("../Assets/wheat.png")

    compactness_values = [30, 50, 70, 100, 200, 300, 500, 700, 1000]
    n_segments_values = [3,4,5,6,7,8,9,10]

    for compactness_val in compactness_values:
        for n in n_segments_values:
            labels1 = segmentation.slic(img, compactness=compactness_val, n_segments=n)
            out1 = color.label2rgb(labels1, img, kind='overlay')

            fig, ax = plt.subplots()
            ax.imshow(out1, interpolation='nearest')
            ax.set_title("Compactness: {} | Segments: {}".format(compactness_val, n))
            plt.savefig("../Clusters/c{}_k{}.png".format(compactness_val, n))
            plt.close(fig) 
Example #2
Source File: utils.py    From segmentation with Apache License 2.0 6 votes vote down vote up
def mask_to_overlay_image(
    image: np.ndarray, mask: np.ndarray, mask_strength: float
) -> np.ndarray:
    """Draw mask over image.

    Args:
        image (np.ndarray): RGB image used as underlay for masks
        mask (np.ndarray): mask to draw
        mask_strength (float): opacity of colorized masks

    Returns:
        np.ndarray: HxWx3 image with overlay
    """
    mask = label2rgb(mask, bg_label=0)
    image_with_overlay = image * (1 - mask_strength) + mask * mask_strength
    image_with_overlay = (
        (image_with_overlay * 255).clip(0, 255).round().astype(np.uint8)
    )
    return image_with_overlay 
Example #3
Source File: spectral_roi.py    From Pic-Numero with MIT License 6 votes vote down vote up
def spectral_cluster(filename, compactness_val=30, n=6):
    '''
    Apply spectral clustering to a given image using k-means clustering and
    display results.

    Args:
        filename: name of the image to segment.

        compactness_val: Controls the "boxyness" of each segment. Higher values
          mean a more boxed shape.

        n = number of clusters.
     '''
    img = misc.imread(filename)
    labels1 = segmentation.slic(img, compactness=compactness_val, n_segments=n)
    out1 = color.label2rgb(labels1, img, kind='overlay', colors=['red','green','blue','cyan','magenta','yellow'])

    fig, ax = plt.subplots()
    ax.imshow(out1, interpolation='nearest')
    ax.set_title("Compactness: {} | Segments: {}".format(compactness_val, n))
    plt.show() 
Example #4
Source File: RAG_threshold.py    From Pic-Numero with MIT License 6 votes vote down vote up
def experiment_with_parameters():
    img = misc.imread("wheat.png")

    compactness_values = [30, 50, 70, 100, 200, 300, 500, 700, 1000]
    n_segments_values = [3,4,5,6,7,8,9,10]

    for compactness_val in compactness_values:
        for n in n_segments_values:
            labels1 = segmentation.slic(img, compactness=compactness_val, n_segments=n)
            out1 = color.label2rgb(labels1, img, kind='overlay')

            fig, ax = plt.subplots()
            ax.imshow(out1, interpolation='nearest')
            ax.set_title("Compactness: {} | Segments: {}".format(compactness_val, n))
            plt.savefig("RAG/c{}_k{}.png".format(compactness_val, n))
            plt.close(fig) 
Example #5
Source File: canvas_simple_segmentation.py    From dash-docs with MIT License 5 votes vote down vote up
def segmentation(string):
    if string:
        mask = parse_jsonstring(string, io.imread(filename, as_gray=True).shape)
        seg = watershed_segmentation(io.imread(filename, as_gray=True), mask)
        src = color.label2rgb(seg, image=io.imread(filename, as_gray=True))
    else:
        raise PreventUpdate
    return array_to_data_url(img_as_ubyte(src)) 
Example #6
Source File: RAG_threshold.py    From Pic-Numero with MIT License 5 votes vote down vote up
def main():
    img = misc.imread("wheat.png")

    # labels1 = segmentation.slic(img, compactness=100, n_segments=9)
    labels1 = segmentation.slic(img, compactness=50, n_segments=4)
    out1 = color.label2rgb(labels1, img, kind='overlay')
    print(labels1.shape)

    g = graph.rag_mean_color(img, labels1)
    labels2 = graph.cut_threshold(labels1, g, 29)
    out2 = color.label2rgb(labels2, img, kind='overlay')

    # get roi
    # logicalIndex = (labels2 != 1)
    # gray = rgb2gray(img);
    # gray[logicalIndex] = 0;


    plt.figure()
    io.imshow(out1)
    plt.figure()
    io.imshow(out2)
    io.show() 
Example #7
Source File: RAG_threshold.py    From Pic-Numero with MIT License 5 votes vote down vote up
def spectral_cluster(filename, compactness_val=30, n=6):
    img = misc.imread(filename)
    labels1 = segmentation.slic(img, compactness=compactness_val, n_segments=n)
    out1 = color.label2rgb(labels1, img, kind='overlay', colors=['red','green','blue','cyan','magenta','yellow'])

    fig, ax = plt.subplots()
    ax.imshow(out1, interpolation='nearest')
    ax.set_title("Compactness: {} | Segments: {}".format(compactness_val, n))
    plt.show() 
Example #8
Source File: task_viz.py    From taskonomy with MIT License 5 votes vote down vote up
def semseg_single_image( predicted, img, to_store_name ):
    label = np.argmax(predicted, axis=-1)
    COLORS = ('white','red', 'blue', 'yellow', 'magenta', 
            'green', 'indigo', 'darkorange', 'cyan', 'pink', 
            'yellowgreen', 'black', 'darkgreen', 'brown', 'gray',
            'purple', 'darkviolet')
    rgb = (img + 1.) / 2.
    preds = [color.label2rgb(np.squeeze(x), np.squeeze(y), colors=COLORS, kind='overlay')[np.newaxis,:,:,:] for x,y in zip(label, rgb)]
    predicted = preds[0].squeeze()
    process_semseg_frame(predicted, to_store_name) 
Example #9
Source File: vid_task_viz.py    From taskonomy with MIT License 5 votes vote down vote up
def semseg_single_image( predicted, img, to_store_name ):
    label = np.argmax(predicted, axis=-1)
    COLORS = ('white','red', 'blue', 'yellow', 'magenta', 
            'green', 'indigo', 'darkorange', 'cyan', 'pink', 
            'yellowgreen', 'black', 'darkgreen', 'brown', 'gray',
            'purple', 'darkviolet')
    rgb = (img + 1.) / 2.
    preds = [color.label2rgb(np.squeeze(x), np.squeeze(y), colors=COLORS, kind='overlay')[np.newaxis,:,:,:] for x,y in zip(label, rgb)]
    predicted = preds[0].squeeze()
    process_semseg_frame(predicted, to_store_name) 
Example #10
Source File: solver.py    From FastSurfer with Apache License 2.0 5 votes vote down vote up
def plot_predictions(images_batch, labels_batch, batch_output, plt_title, file_save_name):
    """
    Function to plot predictions from validation set.
    :param images_batch:
    :param labels_batch:
    :param batch_output:
    :param plt_title:
    :param file_save_name:
    :return:
    """

    f = plt.figure(figsize=(20, 20))
    n, c, h, w = images_batch.shape
    mid_slice = c // 2
    images_batch = torch.unsqueeze(images_batch[:, mid_slice, :, :], 1)
    grid = utils.make_grid(images_batch.cpu(), nrow=4)

    plt.subplot(131)
    plt.imshow(grid.numpy().transpose((1, 2, 0)))
    plt.title('Slices')

    grid = utils.make_grid(labels_batch.unsqueeze_(1).cpu(), nrow=4)[0]
    color_grid = color.label2rgb(grid.numpy(), bg_label=0)
    plt.subplot(132)
    plt.imshow(color_grid)
    plt.title('Ground Truth')

    grid = utils.make_grid(batch_output.unsqueeze_(1).cpu(), nrow=4)[0]
    color_grid = color.label2rgb(grid.numpy(), bg_label=0)
    plt.subplot(133)
    plt.imshow(color_grid)
    plt.title('Prediction')

    plt.suptitle(plt_title)
    plt.tight_layout()

    f.savefig(file_save_name, bbox_inches='tight')
    plt.close(f)
    plt.gcf().clear() 
Example #11
Source File: image.py    From catalyst with Apache License 2.0 5 votes vote down vote up
def mask_to_overlay_image(
    image: np.ndarray,
    masks: List[np.ndarray],
    threshold: float = 0,
    mask_strength: float = 0.5,
) -> np.ndarray:
    """Draws every mask for with some color over image.

    Args:
        image (np.ndarray): RGB image used as underlay for masks
        masks (List[np.ndarray]): list of masks
        threshold (float): threshold for masks binarization
        mask_strength (float): opacity of colorized masks

    Returns:
        np.ndarray: HxWx3 image with overlay
    """
    h, w = image.shape[:2]
    labels = np.zeros((h, w), np.uint8)

    for idx, mask in enumerate(masks, start=1):
        labels[mask > threshold] = idx

    mask = label2rgb(labels, bg_label=0)

    image = np.array(image) / 255.0
    image_with_overlay = image * (1 - mask_strength) + mask * mask_strength
    image_with_overlay = (
        (image_with_overlay * 255).clip(0, 255).round().astype(np.uint8)
    )

    return image_with_overlay 
Example #12
Source File: utils.py    From Semantic-Segmentation-using-Adversarial-Networks with MIT License 4 votes vote down vote up
def draw_label(label, img, n_class, label_titles, bg_label=0):
    """Convert label to rgb with label titles.

    @param label_title: label title for each labels.
    @type label_title: dict
    """
    from PIL import Image
    from scipy.misc import fromimage
    from skimage.color import label2rgb
    from skimage.transform import resize
    colors = labelcolormap(n_class)
    label_viz = label2rgb(label, img, colors=colors[1:], bg_label=bg_label)
    # label 0 color: (0, 0, 0, 0) -> (0, 0, 0, 255)
    label_viz[label == 0] = 0

    # plot label titles on image using matplotlib
    plt.subplots_adjust(left=0, right=1, top=1, bottom=0,
                        wspace=0, hspace=0)
    plt.margins(0, 0)
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.gca().yaxis.set_major_locator(plt.NullLocator())
    plt.axis('off')
    # plot image
    plt.imshow(label_viz)
    # plot legend
    plt_handlers = []
    plt_titles = []
    for label_value in np.unique(label):
        if label_value not in label_titles:
            continue
        fc = colors[label_value]
        p = plt.Rectangle((0, 0), 1, 1, fc=fc)
        plt_handlers.append(p)
        plt_titles.append(label_titles[label_value])
    plt.legend(plt_handlers, plt_titles, loc='lower right', framealpha=0.5)
    # convert plotted figure to np.ndarray
    f = StringIO.StringIO()
    plt.savefig(f, bbox_inches='tight', pad_inches=0)
    result_img_pil = Image.open(f)
    result_img = fromimage(result_img_pil, mode='RGB')
    result_img = resize(result_img, img.shape, preserve_range=True)
    result_img = result_img.astype(img.dtype)
    return result_img 
Example #13
Source File: mask_inference.py    From catalyst with Apache License 2.0 4 votes vote down vote up
def on_batch_end(self, runner: IRunner):
        """Batch end hook.

        Args:
            runner (IRunner): current runner
        """
        lm = runner.loader_name
        names = runner.input.get(self.name_key, [])

        features = runner.input[self.input_key].detach().cpu()
        images = utils.tensor_to_ndimage(features)

        logits = runner.output[self.output_key]
        logits = (
            torch.unsqueeze_(logits, dim=1)
            if len(logits.shape) < 4
            else logits
        )

        if self.mask_type == "soft":
            probabilities = torch.sigmoid(logits)
        else:
            probabilities = F.softmax(logits, dim=1)
        probabilities = probabilities.detach().cpu().numpy()

        masks = []
        for probability in probabilities:
            mask = np.zeros_like(probability[0], dtype=np.int32)
            for i, ch in enumerate(probability):
                mask[ch >= self.threshold] = i + 1
            masks.append(mask)

        for index, (image, mask) in enumerate(zip(images, masks)):
            try:
                suffix = names[index]
            except IndexError:
                suffix = f"{self.counter:06d}"
            self.counter += 1

            mask = label2rgb(mask, bg_label=0)

            image = (
                image * (1 - self.mask_strength) + mask * self.mask_strength
            )
            image = (image * 255).clip(0, 255).round().astype(np.uint8)

            filename = f"{self.out_prefix}/{lm}/{suffix}.jpg"
            imageio.imwrite(filename, image)