示例#1
0
def batched_keypoints_to_segments(
    img: np.ndarray,
    keypoints: np.ndarray,
    segmentation_algorithm: SegmentationAlgorithm,
) -> Union[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
    n_keypoints = keypoints.shape[0]
    MAP = segmentation_algorithm(img, keypoints)
    MAP_colorized = imageutils.make_colors(n_keypoints + 1,
                                           with_background=True,
                                           background_id=0)[MAP]
    heatmaps = imageutils.keypoints_to_heatmaps(img.shape[:2], keypoints,
                                                segmentation_algorithm.var)
    heatmaps *= heatmaps > 0.8
    heatmaps_rgb = imageutils.colorize_heatmaps(
        heatmaps[np.newaxis, ...], imageutils.make_colors(n_keypoints))

    img_resized = cv2.resize(img, (256, 256), cv2.INTER_LINEAR)
    img_resized = imageutils.convert_range(img_resized, [0, 255], [0, 1])
    im_with_keypoints = imageutils.draw_keypoint_markers(
        img_resized,
        keypoints,
        marker_list=[str(i)
                     for i in range(10)] + ["x", "o", "v", "<", ">", "*"],
        font_scale=1,
        thickness=4,
    )
    im_with_keypoints = cv2.resize(im_with_keypoints,
                                   (img.shape[1], img.shape[1]),
                                   cv2.INTER_LINEAR)
    return MAP, MAP_colorized, heatmaps_rgb, im_with_keypoints
示例#2
0
    def test_segmentationFromKeypoints(self):
        from supermariopy.crf import SegmentationFromKeypoints
        from supermariopy import imageutils
        from skimage import data

        n_keypoints = 10
        var = 0.05
        keypoints = np.stack(
            [np.linspace(-1, 1, n_keypoints),
             np.linspace(-1, 1, n_keypoints)],
            axis=1)

        img = data.astronaut()
        segmentation_algorithm = SegmentationFromKeypoints(var)
        labels = segmentation_algorithm(img, keypoints)
        labels_rgb = imageutils.make_colors(n_keypoints + 1)[labels]
        heatmaps = imageutils.keypoints_to_heatmaps(img.shape[:2], keypoints,
                                                    var)
        heatmaps_rgb = imageutils.colorize_heatmaps(
            heatmaps[np.newaxis, ...], imageutils.make_colors(n_keypoints))
        fig, axes = plt.subplots(1, 2, figsize=(8, 4))
        axes[0].imshow(labels_rgb)
        axes[0].set_axis_off()

        axes[1].imshow(np.squeeze(heatmaps_rgb))
        axes[1].set_axis_off()
        return fig
    def test_colorize_heatmaps(self):
        from supermariopy.imageutils import (
            keypoints_to_heatmaps,
            make_colors,
            colorize_heatmaps,
        )
        from matplotlib import pyplot as plt

        keypoints = np.stack([np.linspace(-1, 1, 10),
                              np.linspace(-1, 1, 10)],
                             axis=1)
        heatmaps = keypoints_to_heatmaps((512, 512), keypoints)
        colors = make_colors(keypoints.shape[0])
        img_marked = colorize_heatmaps(heatmaps[np.newaxis, ...], colors)
        fig, ax = plt.subplots(1, 1, figsize=(6, 6))
        ax.imshow(np.squeeze(img_marked))
        return fig
示例#4
0
def batched_keypoints_to_segments(img, keypoints, segmentation_algorithm):
    n_keypoints = keypoints.shape[0]
    MAP = segmentation_algorithm(img, keypoints)
    MAP_colorized = imageutils.make_colors(n_keypoints + 1,
                                           with_background=True,
                                           background_id=0)[MAP]
    heatmaps = imageutils.keypoints_to_heatmaps(img.shape[:2], keypoints,
                                                segmentation_algorithm.var)
    heatmaps *= heatmaps > 0.8
    heatmaps_rgb = imageutils.colorize_heatmaps(
        heatmaps[np.newaxis, ...], imageutils.make_colors(n_keypoints))
    im_with_keypoints = imageutils.draw_keypoint_markers(
        imageutils.convert_range(img, [0, 255], [0, 1]),
        keypoints,
        marker_list=[str(i)
                     for i in range(10)] + ["x", "o", "v", "<", ">", "*"],
        font_scale=0.5,
    )
    return MAP, MAP_colorized, heatmaps_rgb, im_with_keypoints