def test_keypoints_to_heatmaps(self): from supermariopy.imageutils import keypoints_to_heatmaps, RangeError keypoints = np.stack([np.linspace(-1, 1, 10), np.linspace(-1, 1, 10)], axis=1) heatmaps = keypoints_to_heatmaps((512, 512), keypoints) assert heatmaps.shape == (512, 512, 10) keypoints = np.stack([np.arange(10), np.arange(10)], axis=1) with pytest.raises(RangeError): heatmaps = keypoints_to_heatmaps((512, 512), keypoints)
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
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 __call__(self, image: np.ndarray, keypoints: np.ndarray) -> np.ndarray: if not imageutils.is_in_range(image, [0, 255]): raise imageutils.RangeError(image, "image", [0, 255]) dynamic_range = image.max() / image.min() is_low_range = dynamic_range <= 2.0 if is_low_range: import warnings warnings.warn( Warning( "image has low dynamic range. Maybe convert to [0, 255]?")) h, w, c = image.shape keypoint_probs = imageutils.keypoints_to_heatmaps((h, w), keypoints, var=self.var) keypoint_probs = np.rollaxis(keypoint_probs, 2, 0) bg_prob = 1.0 - np.amax(keypoint_probs, axis=0, keepdims=True) probs = np.concatenate([bg_prob, keypoint_probs], axis=0) n_labels = probs.shape[0] probs_flat = np.reshape(probs, (n_labels, -1)) d = dcrf.DenseCRF(h * w, n_labels) # flatten everything for dense crf # Set unary according to keypoints U = -np.log(probs_flat + 1.0e-6).astype(np.float32) d.setUnaryEnergy(U.copy(order="C")) # This creates the color-independent features and then add them to the CRF feats = create_pairwise_gaussian(sdims=(3, 3), shape=image.shape[:2]) d.addPairwiseEnergy( feats, compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC, ) # This creates the color-dependent features and then add them to the CRF feats = create_pairwise_bilateral(sdims=(80, 80), schan=(13, 13, 13), img=image, chdim=2) d.addPairwiseEnergy( feats, compat=10, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC, ) # Run five inference steps. Q = d.inference(self.n_steps) # Find out the most probable class for each pixel. MAP = np.argmax(Q, axis=0) MAP = MAP.reshape((h, w)) return MAP
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
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