Esempio n. 1
0
    def get_itk_image(self):
        """
        Get the currently active image as a ITK image instead of a Numpy array.
        """
        return itkutils.convert_from_numpy(self.data[self.active_image][:],

                                           self.data[self.active_image].attrs["spacing"])
Esempio n. 2
0
def __bioformats(filename, series=0, channel=0, return_itk=False):
    """
    Read an image using the Bioformats importer. Good for most microscopy formats.

    :param filename:
    :param series:
    :param return_itk:
    :return:
    """
    assert pims.bioformats.available(), "Please install jpype in order to use " \
                                        "the bioformats reader."
    image = pims.bioformats.BioformatsReader(filename, series=series)

    # Get Pixel/Voxel size information
    if 'z' not in image.axes:
        spacing = (image.metadata.PixelsPhysicalSizeY(0),
                   image.metadata.PixelsPhysicalSizeX(0))
    else:
        spacing = (image.metadata.PixelsPhysicalSizeZ(0),
                   image.metadata.PixelsPhysicalSizeY(0),
                   image.metadata.PixelsPhysicalSizeX(0))

    # Get color channel
    if 'c' in image.sizes:
        image.iter_axes = 'c'
        assert len(image) > channel
        image = image[channel]
    else:
        image = image[0]

    if return_itk:
        return itkutils.convert_from_numpy(image, spacing)
    else:
        return Image(image, spacing)
Esempio n. 3
0
def __tiff(filename, memmap=False, return_itk=False):
    """
    ImageJ has a bit peculiar way of saving image metadata, especially the tags
    for voxel spacing, which is of main interest in miplib. This function reads
    a 3D TIFF into a Numpy array and also calculates the voxel spacing parameters
    from the TIFF tags. I will not guarantee that this will work with any other TIFF
    files.

    :param filename:    Path to a TIFF.
    :param memmap:      Enables Memory mapping in case the TIFF file is too large to
                        be read in memory completely.
    :param return_itk  Converts the Image data into a sitk.Image. This can be used
                        when single images are needed, instead of using the HDF5
                        structure adopted in miplib.
    :return:            Image data either as a Numpy array, voxel spacing tuple or a
                        sitk.Image
    """
    assert filename.endswith((".tif", ".tiff"))
    tags = {}
    # Read images and tags
    with tiffile.TiffFile(filename) as image:
        # Get images
        images = image.asarray(memmap=memmap)
        # Get tags
        page = image[0]
        for tag in list(page.tags.values()):
            tags[tag.name] = tag.value

    # Figure out z-spacing, which in ImageJ is hidden in the "image_description"
    # header (why, one might ask).
    image_descriptor = tags["image_description"].split("\n")
    z_spacing = None
    for line in image_descriptor:
        if "spacing" in line:
            z_spacing = float(line.split("=")[-1])
            break
    assert z_spacing is not None

    # Create a tuple for zxy-spacing. The order of the dimensions follows that of the
    # image data
    spacing = (z_spacing, scale_c / tags["x_resolution"][0],
               scale_c / tags["y_resolution"][0])

    #print spacing
    if return_itk:
        return itkutils.convert_from_numpy(images, spacing)
    else:
        return images, spacing
    def __init__(self, shape, d_bin, d_angle):
        """
        :param shape: Shape of the data
        :param d_bin: The radius increment size (pixels)
        :param d_angle: The angle increment size (degrees)
        """

        assert len(shape) == 3, "This iterator assumes a 3D shape"

        FourierShellIterator.__init__(self, shape, d_bin)

        plane = nputils.expand_to_shape(np.ones((1, shape[1], shape[2])),
                                        shape)

        self.plane = itkutils.convert_from_numpy(plane, (1, 1, 1))

        self.rotated_plane = plane > 0

        self.rotation_start = 0
        self.rotation_stop = 360 / d_angle - 1
        self.current_rotation = self.rotation_start

        self.angles = np.arange(0, 360, d_angle, dtype=int)