def visualize_image_with_mask(line_id):
    '''
    Function to visualize the image and the mask.
    INPUT:
        line_id - id of the line to visualize the masks
    '''
    # replace null values with '-1'
    im_df = train_df.fillna('-1')

    # get segmentation mask
    np_mask = get_mask(line_id)

    # open the image
    image = Image.open(TRAIN_PATH + im_df.loc[line_id]['Image'])

    # create segmentation map
    segmap = SegmentationMapOnImage(np_mask, np_mask.shape, nb_classes=2)

    # visualize the image and map
    side_by_side = np.hstack([segmap.draw_on_image(np.asarray(image))
                              ]).reshape(np.asarray(image).shape)

    fig, ax = plt.subplots(figsize=(6, 4))
    ax.axis('off')
    plt.title(im_df.loc[line_id]['Label'])

    ax.imshow(side_by_side)
def plot_training_images_and_masks(n_images=3):
    '''
    Function to plot several random images with segmentation masks.
    INPUT:
        n_images - number of images to visualize
    '''

    # get a list of images from training set
    images = sorted(glob(TRAIN_PATH + '*.jpg'))

    fig, ax = plt.subplots(n_images, 4, figsize=(20, 10))

    # create a list of random indices
    rnd_indices = [
        np.random.choice(range(0, len(images))) for i in range(n_images)
    ]

    for im in range(0, n_images):
        # open image with a random index
        image = Image.open(images[rnd_indices[im]])

        # get segmentation masks
        fish = get_mask_by_image_id(images[rnd_indices[im]], 'Fish')
        flower = get_mask_by_image_id(images[rnd_indices[im]], 'Flower')
        gravel = get_mask_by_image_id(images[rnd_indices[im]], 'Gravel')
        sugar = get_mask_by_image_id(images[rnd_indices[im]], 'Sugar')

        # draw masks on images
        shape = (np.asarray(image).shape[0], np.asarray(image).shape[1])
        if np.sum(fish) > 0:
            segmap_fish = SegmentationMapOnImage(fish,
                                                 shape=shape,
                                                 nb_classes=2)
            im_fish = np.array(segmap_fish.draw_on_image(
                np.asarray(image))).reshape(np.asarray(image).shape)
        else:
            im_fish = np.asarray(image)

        if np.sum(flower) > 0:
            segmap_flower = SegmentationMapOnImage(flower,
                                                   shape=shape,
                                                   nb_classes=2)
            im_flower = np.array(segmap_flower.draw_on_image(
                np.asarray(image))).reshape(np.asarray(image).shape)
        else:
            im_flower = np.asarray(image)

        if np.sum(gravel) > 0:
            segmap_gravel = SegmentationMapOnImage(gravel,
                                                   shape=shape,
                                                   nb_classes=2)
            im_gravel = np.array(segmap_gravel.draw_on_image(
                np.asarray(image))).reshape(np.asarray(image).shape)
        else:
            im_gravel = np.asarray(image)

        if np.sum(sugar) > 0:
            segmap_sugar = SegmentationMapOnImage(sugar,
                                                  shape=shape,
                                                  nb_classes=2)
            im_sugar = np.array(segmap_sugar.draw_on_image(
                np.asarray(image))).reshape(np.asarray(image).shape)
        else:
            im_sugar = np.asarray(image)

        # plot images and masks
        ax[im, 0].imshow(im_fish)
        ax[im, 0].axis('off')
        ax[im, 0].set_title('Fish')

        # plot images and masks
        ax[im, 1].imshow(im_flower)
        ax[im, 1].axis('off')
        ax[im, 1].set_title('Flower')

        # plot images and masks
        ax[im, 2].imshow(im_gravel)
        ax[im, 2].axis('off')
        ax[im, 2].set_title('Gravel')

        # plot images and masks
        ax[im, 3].imshow(im_sugar)
        ax[im, 3].axis('off')
        ax[im, 3].set_title('Sugar')

    plt.suptitle('Sample images from the train set')
    plt.show()
Example #3
0
                cv2.imwrite(os.path.join('./mask_aug','aug2_'+str(k)+'_'+img.split('.')[0] + '_color_mask.png'), 
                            segmaps_aug_i.draw(size=(h,w)))
                
                #images_aug.append(images_aug_i)
                #segmaps_aug.append(segmaps_aug_i)
        
        
    
    # We want to generate an image of original input images and segmaps
    # before/after augmentation.
    # It is supposed to have five columns: (1) original image, (2) original
    # image with segmap, (3) augmented image, (4) augmented
    # segmap on augmented image, (5) augmented segmap on its own in.
    # We now generate the cells of these columns.
    
    
    if images_aug:
        cells = []
        for image_aug, segmap_aug in zip(images_aug, segmaps_aug):
            cells.append(image)                                      # column 1
            cells.append(segmap.draw_on_image(image))                # column 2
            cells.append(image_aug)                                  # column 3
            cells.append(segmap_aug.draw_on_image(image_aug))        # column 4
            cells.append(segmap_aug.draw(size=image_aug.shape[:2]))  # column 5

        # Convert cells to grid image and save.
        grid_image = ia.draw_grid(cells, cols=5)
        ia.imshow(grid_image)
        #imageio.imwrite(os.path.join('./test','aug'+img), grid_image)