def smooth_label_images(images, sigma=1, dtype=None): if dtype is None: dtype = images[0].dtype smoothed_images = [gaussian(image, sigma=sigma) for image in images] smoothed = np.stack(smoothed_images, 0) label_images = np.argmax(smoothed, axis=0) return split_label_image(label_images, range(0, len(images)), dtype=dtype)
def split_and_smooth_single_label(self, image, landmark_index): """ Splits a groundtruth label image into a stack of one-hot encoded images. :param image: The groundtruth label image. :return: The one-hot encoded image. """ label_image = (image == self.landmark_mapping[landmark_index]).astype(np.float32) label_image_smoothed = gaussian(label_image, self.label_gaussian_sigma) return (label_image_smoothed > 0.5).astype(np.uint8)
def split_labels(self, image): """ Splits a groundtruth label image into a stack of one-hot encoded images. :param image: The groundtruth label image. :return: The one-hot encoded image. """ split = split_label_image(np.squeeze(image, 0), list(range(8)), np.uint8) split_smoothed = [ gaussian(i, self.label_gaussian_sigma) for i in split ] smoothed = np.stack(split_smoothed, 0) image_smoothed = np.argmax(smoothed, axis=0) split = split_label_image(image_smoothed, list(range(8)), np.uint8) return np.stack(split, 0)
def get_multiple_maximum_coordinates(self, image): """ Return local maxima of the image that are larger than self.min_max_value. If self.smoothing_sigma > 0, perform Gaussian smoothing to reduce the number of local maxima. :param image: Heatmap image. :return: List of value, coord tuples of local maxima. """ if self.smoothing_sigma > 0.0: image_smoothed = gaussian(image, self.smoothing_sigma) indizes, values = utils.np_image.local_maxima(image_smoothed) else: indizes, values = utils.np_image.local_maxima(image) values_greater = values > self.min_max_value values = values[values_greater] coords = indizes[values_greater] refined_coords = [ utils.np_image.refine_coordinate_subpixel(image, c) for c in coords ] return list( reversed(sorted(zip(values, refined_coords), key=lambda x: x[0])))
def data_generators(self, iterator, datasources, transformation, image_post_processing, random_translation_single_landmark, image_size): """ Returns the data generators that process one input. See datasources() for dict values. :param datasources: datasources dict. :param transformation: transformation. :param image_post_processing: The np postprocessing function for the image data generator. :return: A dict of data generators. """ generators_dict = {} generators_dict['image'] = ImageGenerator( self.dim, image_size, self.image_spacing, interpolator='linear', post_processing_np=image_post_processing, data_format=self.data_format, resample_default_pixel_value=self.image_default_pixel_value, name='image', parents=[datasources['image'], transformation]) if self.generate_landmark_mask: generators_dict['landmark_mask'] = ImageGenerator( self.dim, image_size, self.image_spacing, interpolator='nearest', data_format=self.data_format, resample_default_pixel_value=0, name='landmark_mask', parents=[datasources['landmark_mask'], transformation]) if self.generate_labels or self.generate_single_vertebrae: generators_dict['labels'] = ImageGenerator( self.dim, image_size, self.image_spacing, interpolator='nearest', post_processing_np=self.split_labels, data_format=self.data_format, name='labels', parents=[datasources['labels'], transformation]) if self.generate_heatmaps or self.generate_spine_heatmap: generators_dict['heatmaps'] = LandmarkGeneratorHeatmap( self.dim, image_size, self.image_spacing, sigma=self.heatmap_sigma, scale_factor=1.0, normalize_center=True, data_format=self.data_format, name='heatmaps', parents=[datasources['landmarks'], transformation]) if self.generate_landmarks: generators_dict['landmarks'] = LandmarkGenerator( self.dim, image_size, self.image_spacing, data_format=self.data_format, name='landmarks', parents=[datasources['landmarks'], transformation]) if self.generate_single_vertebrae_heatmap: single_landmark = LambdaNode( lambda id_dict, landmarks: landmarks[int(id_dict[ 'landmark_id']):int(id_dict['landmark_id']) + 1], name='single_landmark', parents=[iterator, datasources['landmarks']]) if random_translation_single_landmark: single_landmark = LambdaNode( lambda l: [ Landmark( l[0].coords + float_uniform( -self.random_translation_single_landmark, self. random_translation_single_landmark, [self.dim ]), True) ], name='single_landmark_translation', parents=[single_landmark]) generators_dict['single_heatmap'] = LandmarkGeneratorHeatmap( self.dim, image_size, self.image_spacing, sigma=self.heatmap_sigma, scale_factor=1.0, normalize_center=True, data_format=self.data_format, name='single_heatmap', parents=[single_landmark, transformation]) if self.generate_single_vertebrae: if self.data_format == 'channels_first': generators_dict['single_label'] = LambdaNode( lambda id_dict, images: images[int(id_dict[ 'landmark_id']) + 1:int(id_dict['landmark_id']) + 2, ...], name='single_label', parents=[iterator, generators_dict['labels']]) else: generators_dict['single_label'] = LambdaNode( lambda id_dict, images: images[..., int(id_dict['landmark_id']) + 1:int(id_dict[ 'landmark_id']) + 2], name='single_label', parents=[iterator, generators_dict['labels']]) if self.generate_spine_heatmap: generators_dict['spine_heatmap'] = LambdaNode( lambda images: gaussian(np.sum(images, axis=0 if self.data_format == 'channels_first' else -1, keepdims=True), sigma=self.spine_heatmap_sigma), name='spine_heatmap', parents=[generators_dict['heatmaps']]) return generators_dict
def data_generators(self, iterator, datasources, transformation, image_post_processing, random_translation_single_landmark, image_size, crop=False): """ Returns the data generators that process one input. See datasources() for dict values. :param datasources: datasources dict. :param transformation: transformation. :param image_post_processing: The np postprocessing function for the image data generator. :return: A dict of data generators. """ generators_dict = {} kwparents = {'output_size': image_size} image_datasource = datasources['image'] if not crop else LambdaNode(self.landmark_based_crop, name='image_cropped', kwparents={'image': datasources['image'], 'landmarks': datasources['landmarks']}) generators_dict['image'] = ImageGenerator(self.dim, None, self.image_spacing, interpolator='linear', post_processing_np=image_post_processing, data_format=self.data_format, resample_default_pixel_value=self.image_default_pixel_value, np_pixel_type=self.output_image_type, name='image', parents=[image_datasource, transformation], kwparents=kwparents) # generators_dict['image'] = ImageGenerator(self.dim, # None, # self.image_spacing, # interpolator='linear', # post_processing_np=image_post_processing, # data_format=self.data_format, # resample_default_pixel_value=self.image_default_pixel_value, # np_pixel_type=self.output_image_type, # name='image_cropped', # parents=[LambdaNode(self.landmark_based_crop, name='image_cropped', kwparents={'image': datasources['image'], 'landmarks': datasources['landmarks']}), transformation], # kwparents=kwparents) if self.generate_landmark_mask: generators_dict['landmark_mask'] = ImageGenerator(self.dim, None, self.image_spacing, interpolator='nearest', data_format=self.data_format, resample_default_pixel_value=0, name='landmark_mask', parents=[datasources['landmark_mask'], transformation], kwparents=kwparents) if self.generate_labels: generators_dict['labels'] = ImageGenerator(self.dim, None, self.image_spacing, interpolator='nearest', post_processing_np=self.split_labels, data_format=self.data_format, name='labels', parents=[datasources['labels'], transformation], kwparents=kwparents) if self.generate_heatmaps or self.generate_spine_heatmap: generators_dict['heatmaps'] = LandmarkGeneratorHeatmap(self.dim, None, self.image_spacing, sigma=self.heatmap_sigma, scale_factor=1.0, normalize_center=True, data_format=self.data_format, name='heatmaps', parents=[datasources['landmarks'], transformation], kwparents=kwparents) if self.generate_landmarks: generators_dict['landmarks'] = LandmarkGenerator(self.dim, None, self.image_spacing, data_format=self.data_format, name='landmarks', parents=[datasources['landmarks'], transformation], kwparents=kwparents) if self.generate_single_vertebrae_heatmap: single_landmark = LambdaNode(lambda id_dict, landmarks: landmarks[int(id_dict['landmark_id']):int(id_dict['landmark_id']) + 1], name='single_landmark', parents=[iterator, datasources['landmarks']]) if random_translation_single_landmark: single_landmark = LambdaNode(lambda l: [Landmark(l[0].coords + float_uniform(-self.random_translation_single_landmark, self.random_translation_single_landmark, [self.dim]), True)], name='single_landmark_translation', parents=[single_landmark]) generators_dict['single_heatmap'] = LandmarkGeneratorHeatmap(self.dim, None, self.image_spacing, sigma=self.single_heatmap_sigma, scale_factor=1.0, normalize_center=True, data_format=self.data_format, np_pixel_type=self.output_image_type, name='single_heatmap', parents=[single_landmark, transformation], kwparents=kwparents) if self.generate_single_vertebrae: if self.generate_labels: if self.data_format == 'channels_first': generators_dict['single_label'] = LambdaNode(lambda id_dict, images: images[int(id_dict['landmark_id']) + 1:int(id_dict['landmark_id']) + 2, ...], name='single_label', parents=[iterator, generators_dict['labels']]) else: generators_dict['single_label'] = LambdaNode(lambda id_dict, images: images[..., int(id_dict['landmark_id']) + 1:int(id_dict['landmark_id']) + 2], name='single_label', parents=[iterator, generators_dict['labels']]) else: labels_unsmoothed = ImageGenerator(self.dim, None, self.image_spacing, interpolator='nearest', post_processing_np=None, data_format=self.data_format, name='labels_unsmoothed', parents=[datasources['labels'], transformation], kwparents=kwparents) generators_dict['single_label'] = LambdaNode(lambda id_dict, labels: self.split_and_smooth_single_label(labels, int(id_dict['landmark_id'])), name='single_label', parents=[iterator, labels_unsmoothed]) if self.generate_spine_heatmap: generators_dict['spine_heatmap'] = LambdaNode(lambda images: normalize(gaussian(np.sum(images, axis=0 if self.data_format == 'channels_first' else -1, keepdims=True), sigma=self.spine_heatmap_sigma), out_range=(0, 1)), name='spine_heatmap', parents=[generators_dict['heatmaps']]) return generators_dict