Esempio n. 1
0
    def instance_image(self, image, instances_datatype):
        """
        Returns the stacked instance images for the current instance segmentation. The resulting np array contains
        images for instances that are stacked at the channel axis. Each entry of the channel axis corresponds to
        a certain instance, where pixels with value 1 indicate the instance, 2 other neighboring instances, and 0 background.
        :param image: The groundtruth instance segmentation.
        :param instances_datatype: The np datatype for the bitwise instance image.
        :return: The stacked instance image.
        """
        channel_axis = self.get_channel_axis(image, self.data_format)
        image_squeezed = np.squeeze(image, axis=channel_axis)
        labels, _ = split_label_image_with_unknown_labels(image_squeezed, np.uint8)
        if len(labels) == 1:
            return np.zeros_like(image)
        del labels[0]

        if self.max_num_instances is not None and len(labels) > self.max_num_instances:
            random.shuffle(labels)

        instance_image_pairs = []
        num_instances = 0
        for label in labels:
            other_instance_image = self.other_instance_image(target_label=label, other_labels=labels)
            instance_image_pairs.append((label, other_instance_image))
            num_instances += 1
            if self.max_num_instances is not None and num_instances >= self.max_num_instances:
                break
        return self.merge_instance_image_pairs(instance_image_pairs, instances_datatype, channel_axis)
Esempio n. 2
0
def relabel_seg(seg, tra):
    seg_label_images, _ = split_label_image_with_unknown_labels(seg)
    tra_label_images, tra_labels = split_label_image_with_unknown_labels(tra)
    del tra_label_images[0]
    del tra_labels[0]
    new_seg_labels = [0]
    for i in range(1, len(seg_label_images)):
        seg_label_image = seg_label_images[i]
        tra_label_overlap = [
            (tra_label,
             np.sum(np.bitwise_and(seg_label_image == 1,
                                   tra_label_image == 1)))
            for tra_label_image, tra_label in zip(tra_label_images, tra_labels)
        ]
        best_tra_label, best_overlap = max(tra_label_overlap,
                                           key=lambda x: x[1])
        new_seg_labels.append(best_tra_label)
    assert len(
        np.unique(new_seg_labels)) == len(new_seg_labels), 'duplicate labels'
    new_seg = merge_label_images(seg_label_images, new_seg_labels)
    return new_seg
Esempio n. 3
0
 def label_image_postprocessing(self, image):
     """
     Processes label instance segmentation np arrays. Smoothes the image, if self.label_gaussian_blur_sigma > 0.
     :param image: The instance segmentation np array.
     :return: The processed np array.
     """
     channel_axis = self.get_channel_axis(image, self.data_format)
     if self.label_gaussian_blur_sigma > 0.0: # and self.label_gaussian_blur_sigma != 1.0:
         images, labels = split_label_image_with_unknown_labels(np.squeeze(image, axis=channel_axis), dtype=np.uint16)
         gaussians = [0, self.label_gaussian_blur_sigma, self.label_gaussian_blur_sigma] if image.ndim == 4 else [self.label_gaussian_blur_sigma, self.label_gaussian_blur_sigma]
         images = smooth_label_images(images, gaussians, dtype=np.uint16)
         image = np.expand_dims(merge_label_images(images, labels), channel_axis)
     return image
 def label_image_postprocessing(self, image):
     """
     Processes label instance segmentation np arrays. Smoothes the image, if self.label_gaussian_blur_sigma > 0.
     :param image: The instance segmentation np array.
     :return: The processed np array.
     """
     if self.label_gaussian_blur_sigma > 0:
         images, labels = split_label_image_with_unknown_labels(
             image, dtype=np.uint16)
         images = smooth_label_images(images,
                                      self.label_gaussian_blur_sigma,
                                      dtype=np.uint16)
         image = merge_label_images(images, labels)
     return image