Пример #1
0
    def other_instance_image(self, target_label, other_labels):
        """
        Returns the mask, of neighboring other_labels for a given target_label.
        :param target_label: Image, where pixels of the target label are set to 1, all other pixels are 0.
        :param other_labels: List of images of all labels. Pixels of the label are set to 1, all other pixels are 0.
        :return: The image (np array), where pixels of neighboring labels are set to 2, all other pixels are 0.
        """
        channel_axis = self.get_channel_axis(target_label, self.data_format)
        dim = len(target_label.shape)

        mask = np.zeros_like(target_label)
        circle_radius = self.image_size[0] * self.instance_image_radius_factor
        # handle images with dim 3 (videos) differently
        if dim == 3:
            com = center_of_mass(target_label)
            com = com[1:]
            for i in range(target_label.shape[channel_axis]):
                current_slice = [slice(None), slice(None)]
                current_slice.insert(channel_axis, slice(i, i + 1))
                current_mask = np.squeeze(mask[tuple(current_slice)],
                                          axis=channel_axis)
                draw_circle(current_mask, com, circle_radius)
        else:
            com = center_of_mass(target_label)
            draw_circle(mask, com, circle_radius)
        other_instances = np.zeros_like(mask)
        for other_label in other_labels:
            if np.any(np.bitwise_and(mask == 1, other_label == 1)):
                other_instances += other_label
        other_instances[other_instances > 0] = 2

        return other_instances
    def other_instance_image(self, target_label, other_labels):
        """
        Returns the mask, of neighboring other_labels for a given target_label.
        :param target_label: Image, where pixels of the target label are set to 1, all other pixels are 0.
        :param other_labels: List of images of all labels. Pixels of the label are set to 1, all other pixels are 0.
        :return: The image (np array), where pixels of neighboring labels are set to 2, all other pixels are 0.
        """
        frame_axis = 0
        dim = len(target_label.shape)

        mask = np.copy(target_label)  #np.zeros_like(target_label)
        if self.loss_mask_dilation_size > 0:
            if dim == 3:
                for i in range(target_label.shape[frame_axis]):
                    current_slice = [slice(None), slice(None)]
                    current_slice.insert(frame_axis, slice(i, i + 1))
                    mask[tuple(current_slice)] = dilation_circle(
                        np.squeeze(mask[tuple(current_slice)]),
                        (self.loss_mask_dilation_size,
                         self.loss_mask_dilation_size))
            else:
                mask = dilation_circle(mask, (self.loss_mask_dilation_size,
                                              self.loss_mask_dilation_size))
        circle_radius = self.image_size[
            0] * self.instance_image_radius_factor / self.scale_factor[0]
        # handle images with dim 3 (videos) differently
        if dim == 3:
            com = center_of_mass(target_label)
            com = com[1:]
            is_in_frame = np.any(np.any(target_label, axis=1), axis=1)
            min_index = np.maximum(np.min(np.where(is_in_frame)) - 1, 0)
            max_index = np.minimum(
                np.max(np.where(is_in_frame)) + 1,
                target_label.shape[frame_axis])
            #for i in range(target_label.shape[frame_axis]):
            for i in range(min_index, max_index):
                current_slice = [slice(None), slice(None)]
                current_slice.insert(frame_axis, slice(i, i + 1))
                #com = center_of_mass(np.squeeze(target_label[tuple(current_slice)]))
                current_mask = np.squeeze(mask[tuple(current_slice)],
                                          axis=frame_axis)
                draw_circle(current_mask, com, circle_radius)
        else:
            com = center_of_mass(target_label)
            draw_circle(mask, com, circle_radius)
        other_instances = np.zeros_like(mask)
        for other_label in other_labels:
            if np.array_equal(target_label, other_label):
                continue
            if np.any(np.bitwise_and(mask == 1, other_label == 1)):
                other_instances += other_label
        other_instances[other_instances > 0] = 1

        return other_instances
Пример #3
0
 def visualize_landmark(self, image_canvas, landmark, color, annotation, annotation_color):
     """
     Visualize a single landmark.
     :param image_canvas: The image canvas object.
     :param landmark: The landmark.
     :param color: The landmark color.
     :param annotation: The annotation string.
     :param annotation_color: The annotation color.
     """
     coords = landmark.coords
     if self.spacing is not None:
         coords = coords / self.spacing
     draw_circle(image_canvas, [coords[1], coords[0]], self.radius, color)
Пример #4
0
def current_instance_neighbors(target_label, label_image, image_size, instance_image_radius_factor, loss_mask_dilation_size):
    """
    Returns the mask, of neighboring other_labels for a given target_label.
    :param target_label: Image, where pixels of the target label are set to 1, all other pixels are 0.
    :param other_labels: List of images of all labels. Pixels of the label are set to 1, all other pixels are 0.
    :return: The image (np array), where pixels of neighboring labels are set to 2, all other pixels are 0.
    """
    frame_axis = 0
    dim = len(label_image.shape)

    mask = (label_image == target_label).astype(np.uint8)
    if loss_mask_dilation_size > 0:
        if dim == 3:
            for i in range(label_image.shape[frame_axis]):
                current_slice = [slice(None), slice(None)]
                current_slice.insert(frame_axis, slice(i, i + 1))
                mask[tuple(current_slice)] = dilation_circle(np.squeeze(mask[tuple(current_slice)]), (loss_mask_dilation_size, loss_mask_dilation_size))
        else:
            mask = dilation_circle(mask, (loss_mask_dilation_size, loss_mask_dilation_size))
    circle_radius = image_size[0] * instance_image_radius_factor
    # handle images with dim 3 (videos) differently
    if dim == 3:
        com = center_of_mass(mask)
        com = com[1:]
        is_in_frame = np.any(np.any(mask, axis=1), axis=1)
        min_index = np.maximum(np.min(np.where(is_in_frame)) - 1, 0)
        max_index = np.minimum(np.max(np.where(is_in_frame)) + 2, label_image.shape[frame_axis])
        for i in range(min_index, max_index):
            current_slice = [slice(None), slice(None)]
            current_slice.insert(frame_axis, slice(i, i + 1))
            if i == min_index:
                next_slice = [slice(None), slice(None)]
                next_slice.insert(frame_axis, slice(i + 1, i + 2))
                mask[tuple(current_slice)] = np.logical_or(mask[tuple(next_slice)], mask[tuple(current_slice)])
            if i == max_index - 1:
                previous_slice = [slice(None), slice(None)]
                previous_slice.insert(frame_axis, slice(i - 1, i))
                mask[tuple(current_slice)] = np.logical_or(mask[tuple(previous_slice)], mask[tuple(current_slice)])
            current_mask = np.squeeze(mask[tuple(current_slice)], axis=frame_axis)
            draw_circle(current_mask, com, circle_radius)
    else:
        com = center_of_mass(label_image)
        draw_circle(mask, com, circle_radius)
    current_neighbors = [int(x) for x in np.unique(label_image * mask) if x != 0 and x != target_label]
    return current_neighbors