Example #1
0
        except BaseException:
            print("Undable to find correspoding input image.")
        break

segmentation = np.array(Image.open(
    os.path.join(segmentations_folder, ann['file_name'])),
                        dtype=np.uint8)
segmentation_id = rgb2id(segmentation)
# find segments boundaries
boundaries = find_boundaries(segmentation_id, mode='thick')

if generate_new_colors:
    segmentation[:, :, :] = 0
    color_generator = IdGenerator(categegories)
    for segment_info in ann['segments_info']:
        color = color_generator.get_color(segment_info['category_id'])
        mask = segmentation_id == segment_info['id']
        segmentation[mask] = color

# depict boundaries
segmentation[boundaries] = [0, 0, 0]

if img is None:
    plt.figure()
    plt.imshow(segmentation)
    plt.axis('off')
else:
    plt.figure(figsize=(9, 5))
    plt.subplot(121)
    plt.imshow(img)
    plt.axis('off')
Example #2
0
def visualize(image_path, label_path):
    """
    Visualizes in a pyplot window an image and a label pair from
    provided paths. For reading Pillow is used so all paths and formats
    must be Pillow-compatible.

    Args:
        image_path: an image path provided to Pillow.Image.open
        label_path: a label path provided to Pillow.Image.open
    """
    assert op.exists(image_path)
    assert op.exists(label_path)

    # Prepare canvases and decode the labels.
    image = np.array(Image.open(image_path), dtype=np.uint8)
    label = np.array(Image.open(label_path), dtype=np.int32)
    uids_unique_org = np.unique(label)
    semantic_segmentation = np.zeros((image.shape[0], image.shape[1], 3),
                                     dtype=np.uint8)
    instance_segmentation = np.zeros((image.shape[0], image.shape[1], 3),
                                     dtype=np.uint8)
    parts_segmentation = np.zeros((image.shape[0], image.shape[1], 3),
                                  dtype=np.uint8)
    sids, iids, _ = decode_uids(label)

    # Color at the semantic level.
    color_generator = IdGenerator(CATEGORIES)
    for sid in np.unique(sids):
        mask = np.equal(sids, sid)
        color = CATEGORIES[sid]['color']
        semantic_segmentation[mask] = color

    # Color at the semantic and instance level and find the instance-level boundaries.
    sids_only = np.where(iids < 0, sids, np.zeros_like(iids))
    for sid in np.unique(sids_only):
        mask = np.equal(sids_only, sid)
        color = color_generator.get_color(sid)
        instance_segmentation[mask] = color

    sid_iids = np.where(iids >= 0, sids * 10**3 + iids, np.zeros_like(iids))
    boundaries = np.full(sid_iids.shape, False)
    for sid_iid in np.unique(sid_iids):
        if sid_iid != 0:
            mask = np.equal(sid_iids, sid_iid)
            color = color_generator.get_color(sid_iid // 1000)
            instance_segmentation[mask] = color
            boundary_horizon = ndimage.sobel(mask, 0)
            boundary_vertical = ndimage.sobel(mask, 1)
            boundaries = np.logical_or(
                np.hypot(boundary_horizon, boundary_vertical), boundaries)

    # Color at the part level.
    # Conver the original labels into the form for visualization with IdGenerator.
    for uid in uids_unique_org:
        # If uid is sid or sid_iid, encode them as they are.
        if uid <= 99_999:
            sid_iid = uid
        # If uid is sid_iid_pid, map sid_pid to its corresponding sid and create new label as sid_iid.
        else:
            sid, iid, pid = decode_uids(uid)
            sid_pid = sid * 10**2 + pid
            if sid_pid in SID_PID2PARTS_CID:
                sid_iid = SID_PID2PARTS_CID[sid_pid] * 10**3 + iid
            else:
                sid_iid = sid * 10**3 + iid

        label[label == uid] = sid_iid

    color_generator = IdGenerator(CATEGORIES_PARTS)

    for sid_iid in np.unique(label):
        # If sid_iid is in the format of sid , use sid for color generation (things and stuff classes differentiated by IdGenerator inherently).
        if sid_iid <= 99:
            id_ = sid_iid
        # If sid_iid is in the format of sid_iid, get sid.
        else:
            id_ = sid_iid // 1000
        mask = label == sid_iid
        color = color_generator.get_color(id_)
        parts_segmentation[mask] = color

    # Depict boundaries.
    instance_segmentation[boundaries] = [255, 255, 255]
    parts_segmentation[boundaries] = [255, 255, 255]

    # plot
    # initialize figure for plotting
    _, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
    # for ax in axes:
    #   ax.set_axis_off()
    ax1.imshow(image)
    ax1.set_title('image')
    ax2.imshow(semantic_segmentation)
    ax2.set_title('labels colored on semantic level')
    ax3.imshow(instance_segmentation)
    ax3.set_title('labels colored on semantic and instance levels')
    ax4.imshow(parts_segmentation)
    ax4.set_title('labels colored on semantic, instance, and parts levels')
    plt.show()