def to_directory(self, directory):
     if not os.path.isdir(directory):
         os.mkdir(directory)
     xdim, ydim, zdim = self.shape
     num_digits = Image3D._num_digits(zdim-1)
     for z in range(zdim):
         num = str(z).zfill(num_digits)
         fname = "z{}.png".format(num)
         fpath = os.path.join(directory, fname)
         with open(fpath, "wb") as fh:
             im = Image.from_array(pretty_color_array(self[:, :, z]))
             fh.write(im.png())
    def test_pretty_colour_image(self):

        from jicbioimage.segment import SegmentedImage

        input_array = np.array([[0, 0, 0],
                                [1, 1, 1],
                                [2, 2, 2]])

        segmented_image = SegmentedImage.from_array(input_array)

        pretty_color_image = segmented_image.pretty_color_image

        from jicbioimage.core.util.array import pretty_color_array

        self.assertTrue(np.array_equal(pretty_color_image,
                                       pretty_color_array(input_array)))
def analyse(microscopy_collection, wall_channel, marker_channel, threshold, max_cell_size):
    """Do the analysis."""
    # Prepare the input data for the segmentations.
    (wall_intensity2D,
     wall_intensity3D,
     wall_mask2D,
     wall_mask3D) = get_wall_intensity_and_mask_images(microscopy_collection, wall_channel)
    (marker_intensity2D,
     marker_intensity3D) = get_marker_intensity_images(microscopy_collection, marker_channel)

    # Perform the segmentation.
    cells = cell_segmentation(wall_intensity2D, wall_mask2D, max_cell_size)
    markers = marker_segmentation(marker_intensity3D, wall_mask3D, threshold)

    # Get marker in cell wall and project to 2D.
    wall_marker = marker_intensity3D * wall_mask3D
    wall_marker = max_intensity_projection(wall_marker)

    # Get tensors.
    tensors = get_tensors(cells, markers)

    # Write out tensors to a text file.
    fpath = os.path.join(AutoName.directory, "raw_tensors.txt")
    with open(fpath, "w") as fh:
        tensors.write_raw_tensors(fh)

    # Write out intensity images.
    fpath = os.path.join(AutoName.directory, "wall_intensity.png")
    with open(fpath, "wb") as fh:
        fh.write(wall_intensity2D.png())
    fpath = os.path.join(AutoName.directory, "marker_intensity.png")
    with open(fpath, "wb") as fh:
        fh.write(wall_marker.png())

    # Shrink the segments to make them clearer.
    for i in cells.identifiers:
        region = cells.region_by_identifier(i)
        mask = region - region.inner.inner
        cells[mask] = 0
    colorful = pretty_color_array(cells)
    pil_im = PIL.Image.fromarray(colorful.view(dtype=np.uint8))
    pil_im = make_transparent(pil_im, 60)
    fpath = os.path.join(AutoName.directory, "segmentation.png")
    pil_im.save(fpath)
    def test_pretty_color_array_with_black_background(self):

        from jicbioimage.core.util.array import pretty_color_array

        input_array = np.array([[0, 0, 0],
                                [1, 1, 1],
                                [2, 2, 2]])

        c1 = [0, 0, 0]
        c2 = [132, 27, 117]
        c3 = [20, 134, 44]

        expected_output = np.array([[c1, c1, c1],
                                    [c2, c2, c2],
                                    [c3, c3, c3]], dtype=np.uint8)

        actual_output = pretty_color_array(input_array)

        self.assertTrue(np.array_equal(actual_output, expected_output))
def analyse(microscopy_collection, wall_channel, marker_channel,
            threshold, max_cell_size):
    """Do the analysis."""
    # Prepare the input data for the segmentations.
    cell_wall_stack = microscopy_collection.zstack_array(c=wall_channel)
    marker_stack = microscopy_collection.zstack_array(c=marker_channel)
    surface = generate_surface_from_stack(cell_wall_stack)
    cell_wall_projection = projection_from_stack_and_surface(cell_wall_stack,
                                                             surface, 1, 9)
    marker_projection = projection_from_stack_and_surface(marker_stack,
                                                          surface, 1, 9)

    # Perform the segmentation.
    cells, wall = segment_cells(cell_wall_projection, max_cell_size)
    markers = segment_markers(marker_projection, wall, threshold)

    # Get tensors.
    tensors = get_tensors(cells, markers)

    # Write out tensors to a text file.
    fpath = os.path.join(AutoName.directory, "raw_tensors.txt")
    with open(fpath, "w") as fh:
        tensors.write_raw_tensors(fh)

    # Write out intensity images.
    fpath = os.path.join(AutoName.directory, "wall_intensity.png")
    with open(fpath, "wb") as fh:
        fh.write(cell_wall_projection.png())
    fpath = os.path.join(AutoName.directory, "marker_intensity.png")
    marker_im = marker_in_wall(marker_projection, wall)
    with open(fpath, "wb") as fh:
        fh.write(marker_im.png())

    # Shrink the segments to make them clearer.
    for i in cells.identifiers:
        region = cells.region_by_identifier(i)
        mask = region - region.inner.inner
        cells[mask] = 0
    colorful = pretty_color_array(cells)
    pil_im = PIL.Image.fromarray(colorful.view(dtype=np.uint8))
    pil_im = make_transparent(pil_im, 60)
    fpath = os.path.join(AutoName.directory, "segmentation.png")
    pil_im.save(fpath)
Example #6
0
    def pretty_color_image(self):
        """Return segmentation as a pretty color image.

        :returns: `jicbioimage.core.image.Image`
        """
        return Image.from_array(pretty_color_array(self))