def draw_segs_on_slice(vol_slice, seg_slice, include_labels=None, colors=None, draw_contours=False, use_gradient_colormap=False): ''' Overlays segmentations on a 2D slice. :param vol_slice: h x w image, the brain slice to overlay on top of :param seg_slice: h x w array, segmentations to overlay (in labels format, not one hot) :param include_labels: list, visualize only specific label values :param colors: n_labels x 3, specific colors to use for segmentations :param draw_contours: bool, visualize segmentations as contours rather than solid areas :param use_gradient_colormap: bool, create the colormap as a gradient of a single color rather than a rainbow :return: h x w x 3 image of brain slice with segmentations overlaid on top ''' # if no labels are specified, simply visualize all unique label values if include_labels is None: include_labels = list(np.unique(seg_slice).astype(int)) # if colors are not specified, make a color map if colors is None: if use_gradient_colormap: colors = make_cmap_gradient(len(include_labels) + 1, hue=0.5) else: colors = make_cmap_rainbow(len(include_labels) + 1) # make a new segmentation map with labels as ascending integers, # since this is what segutils expects pruned_slice = np.zeros(seg_slice.shape, dtype=int) for i, l in enumerate(include_labels): pruned_slice[seg_slice == l] = i + 1 seg_im = pynd_segutils.seg_overlap(np.squeeze(vol_slice), pruned_slice, cmap=colors, do_contour=draw_contours) return seg_im
def visualize_seg_contour(vol, seg, region_numbers=[4], slice_idx=[80, 90, 110], title=''): if title != '': title += ', ' normalized_vol = vol / np.max(vol) seg_one_region = np.zeros(seg.shape) for num in region_numbers: seg_one_region[seg == num] = num img = seg_overlap(normalized_vol, seg_one_region.astype(np.int)) plt.figure(figsize=(20, 10)) slices([ img[slice_idx[0], :, :, :], img[:, slice_idx[1], :, :], img[:, :, slice_idx[2], :] ], titles=[ title + 'x=%d' % slice_idx[0], title + 'y=%d' % slice_idx[1], title + 'z=%d' % slice_idx[2] ])