# Very simple script showing how to generate new images with random contrast import os from lab2im import utils from lab2im.image_generator import ImageGenerator # path of the input label map path_label_map = './data_example/brain_label_map.nii.gz' # path where to save the generated image result_dir = './generated_images' # generate an image from the label map. # Because the image is spatially deformed, we also output the corresponding deformed label map. brain_generator = ImageGenerator(path_label_map) im, lab = brain_generator.generate_image() # save output image and label map if not os.path.exists(os.path.join(result_dir)): os.mkdir(result_dir) utils.save_volume(im, brain_generator.aff, brain_generator.header, os.path.join(result_dir, 'brain.nii.gz')) utils.save_volume(lab, brain_generator.aff, brain_generator.header, os.path.join(result_dir, 'labels.nii.gz'))
output_labels = './data_example/segmentation_labels.npy' # By default, each label will be associated to a Gaussian distribution when sampling a new image. We can also group # labels in classes, to force them to share the same Gaussian. This can be done by providing generation classes, which # should be a sequence, a 1d numpy array, or the path to such an array, with the *same length* as generation_labels. # Values in generation_classes should be between 0 and K-1, where K is the total number of classes. generation_classes = './data_example/generation_classes.npy' ######################################################################################################## # instantiate BrainGenerator object brain_generator = ImageGenerator(labels_dir=path_label_map, generation_labels=generation_labels, output_labels=output_labels, generation_classes=generation_classes) # create result dir if not os.path.exists(os.path.join(result_dir)): os.mkdir(result_dir) for n in range(n_examples): # generate new image and corresponding labels im, lab = brain_generator.generate_image() # save output image and label map save_volume(im, brain_generator.aff, brain_generator.header, os.path.join(result_dir, 'brain_%s.nii.gz' % n)) save_volume(lab, brain_generator.aff, brain_generator.header, os.path.join(result_dir, 'labels_%s.nii.gz' % n))
if cmap is None: nb_labels = np.max(seg) + 1 colors = np.random.random((nb_labels, 3)) * 0.5 + 0.5 colors[0, :] = [0, 0, 0] else: colors = cmap[:, 0:3] seg_flat = colors[seg.flat, :] seg_rgb = np.reshape(seg_flat, vol.shape + (3, )) # get the overlap image olap = seg_rgb * seg_wt + np.expand_dims(vol, -1) * (1 - seg_wt) else: olap = seg * seg_wt + vol * (1 - seg_wt) return olap if __name__ == '__main__': from lab2im.utils import load_volume, save_volume path_seg = '/home/benjamin/data/mit/testing/flair/asegs/ADNI_002_S_0295_Axial_T2-FLAIR_S110475_I238624.nii.gz' path_overlap = '/home/benjamin/Pictures/images/hippocampus/figure_suppl_materials/flair/gt_countours.nii.gz' seg, aff, h = load_volume(path_seg, im_only=False) countour = seg2contour(seg) save_volume(countour, aff, h, path_overlap)