Esempio n. 1
0
    def png(self, width=None):
        """Return png string of image.

        :param width: integer specifying the desired width
        :returns: png as a string
        """
        skimage.io.use_plugin('freeimage')

        def resize(im, width):
            x, y = im.shape[:2]
            f = float(width) / float(x)
            scale_factors = [1.0 for i in range(len(im.shape))]
            scale_factors[0] = f
            scale_factors[1] = f
            ar = scipy.ndimage.zoom(im, scale_factors, order=0)
            return Image.from_array(ar, log_in_history=False)

        safe_range_im = self

        if self.dtype != np.uint8:
            safe_range_im = 255 * normalise(self)

        if width is not None:
            safe_range_im = resize(safe_range_im, width)

        with _TemporaryFilePath(suffix='.png') as tmp:
            safe_range_im_uint8 = safe_range_im.astype(np.uint8)
            skimage.io.imsave(tmp.fpath, safe_range_im_uint8, "freeimage")
            with open(tmp.fpath, 'rb') as fh:
                return fh.read()
Esempio n. 2
0
    def png(self, width=None):
        """Return png string of image.

        :param width: integer specifying the desired width
        :returns: png as a string
        """
        skimage.io.use_plugin('freeimage')

        def resize(im, width):
            x, y = im.shape[:2]
            f = float(width) / float(x)
            scale_factors = [1.0 for i in range(len(im.shape))]
            scale_factors[0] = f
            scale_factors[1] = f
            ar = scipy.ndimage.zoom(im, scale_factors, order=0)
            return Image.from_array(ar, log_in_history=False)

        safe_range_im = self

        if self.dtype != np.uint8:
            safe_range_im = 255 * normalise(self)

        if width is not None:
            safe_range_im = resize(safe_range_im, width)

        with _TemporaryFilePath(suffix='.png') as tmp:
            safe_range_im_uint8 = safe_range_im.astype(np.uint8)
            skimage.io.imsave(tmp.fpath, safe_range_im_uint8, "freeimage")
            with open(tmp.fpath, 'rb') as fh:
                return fh.read()
Esempio n. 3
0
def analyse_image(image):
    image = normalise(image) * 255

    canvas = AnnotatedImage.from_grayscale(image)

    image = smooth_gaussian(image.astype(float), 5)
    image = threshold_abs(image, 30)

    image = erode_binary(image)
    image = remove_small_objects(image, 5)

    salem = skimage.morphology.disk(2)
    image = dilate_binary(image, salem)

    segmentation = connected_components(image, background=0)
    for i in segmentation.identifiers:
        color = pretty_color_from_identifier(i)

        region = segmentation.region_by_identifier(i)
        convex_hull = region.convex_hull
        outline = convex_hull.inner.border.dilate()

        canvas.mask_region(outline, color=color)

    return canvas
Esempio n. 4
0
def annotate(image, segmentation):
    """Return annotated image."""
    uint8_normalised = normalise(image) * 255
    annotation = AnnotatedImage.from_grayscale(uint8_normalised)
    for i in segmentation.identifiers:
        region = segmentation.region_by_identifier(i)
        annotation.mask_region(region.dilate(1).border,
                               color=pretty_color(i))
    return annotation
def annotate(input_file, output_dir):
    """Write an annotated image to disk."""
    logger.info("---")
    logger.info('Input image: "{}"'.format(os.path.abspath(input_file)))
    image = Image.from_file(input_file)
    intensity = mean_intensity_projection(image)
    norm_intensity = normalise(intensity)
    norm_rgb = np.dstack([norm_intensity, norm_intensity, norm_intensity])

    name = fpath2name(input_file)
    png_name = name + ".png"
    csv_name = name + ".csv"
    png_path = os.path.join(output_dir, png_name)
    csv_path = os.path.join(output_dir, csv_name)

    tubes = find_tubes(input_file, output_dir)
    grains, difficult = find_grains(input_file, output_dir)
    tubes = remove_tubes_not_touching_grains(tubes, grains)
    tubes = remove_tubes_that_are_grains(tubes, grains)

    ann = AnnotatedImage.from_grayscale(intensity)

    num_grains = 0
    for n, i in enumerate(grains.identifiers):
        n = n + 1
        region = grains.region_by_identifier(i)
        ann.mask_region(region.inner.inner.inner.border.dilate(),
                        color=(0, 255, 0))
        num_grains = n

    num_tubes = 0
    for n, i in enumerate(tubes.identifiers):
        n = n + 1
        region = tubes.region_by_identifier(i)
        highlight = norm_rgb * pretty_color(i)
        ann[region] = highlight[region]
        ann.mask_region(region.dilate(3).border.dilate(3),
                        color=pretty_color(i))
        num_tubes = n

    ann.text_at("Num grains: {:3d}".format(num_grains), (10, 10),
                antialias=True, color=(0, 255, 0), size=48)
    logger.info("Num grains: {:3d}".format(num_grains))

    ann.text_at("Num tubes : {:3d}".format(num_tubes), (60, 10),
                antialias=True, color=(255, 0, 255), size=48)
    logger.info("Num tubes : {:3d}".format(num_tubes))

    logger.info('Output image: "{}"'.format(os.path.abspath(png_path)))
    with open(png_path, "wb") as fh:
        fh.write(ann.png())

    logger.info('Output csv: "{}"'.format(os.path.abspath(csv_path)))
    with open(csv_path, "w") as fh:
        fh.write("{},{},{}\n".format(png_name, num_grains, num_tubes))

    return png_name, num_grains, num_tubes
Esempio n. 6
0
 def to_directory(self, directory):
     """Write slices from 3D image to directory.
     """
     if not os.path.isdir(directory):
         os.mkdir(directory)
     xdim, ydim, zdim = self.shape
     num_digits = Image3D._num_digits(zdim-1)
     ar = normalise(self) * 255
     ar = ar.astype(np.uint8)
     for z in range(zdim):
         num = str(z).zfill(num_digits)
         fname = "z{}.png".format(num)
         fpath = os.path.join(directory, fname)
         skimage.io.imsave(fpath, ar[:, :, z], "freeimage")
def annotate_segmentation(image, segmentation):

    grayscale = normalise(image) * 255
    canvas = AnnotatedImage.from_grayscale(grayscale)

    for i in segmentation.identifiers:
        region = segmentation.region_by_identifier(i)
        outline = region.inner.border.dilate()
        color = pretty_color_from_identifier(i)
        canvas.mask_region(outline, color=color)

    fpath = os.path.join(AutoName.directory, "segmentation.png")
    with open(fpath, "wb") as fh:
        fh.write(canvas.png())
Esempio n. 8
0
 def to_directory(self, directory):
     """Write slices from 3D image to directory.
     """
     if not os.path.isdir(directory):
         os.mkdir(directory)
     xdim, ydim, zdim = self.shape
     num_digits = Image3D._num_digits(zdim - 1)
     ar = normalise(self) * 255
     ar = ar.astype(np.uint8)
     for z in range(zdim):
         num = str(z).zfill(num_digits)
         fname = "z{}.png".format(num)
         fpath = os.path.join(directory, fname)
         skimage.io.imsave(fpath, ar[:, :, z], "freeimage")
 def test_normalise_123(self):
     from jicbioimage.core.util.array import normalise
     ar = np.array([1,2,3], dtype=np.uint8)
     normed = normalise(ar)
     expected = np.array([0., .5, 1.], dtype=np.float)
     self.assertTrue( np.array_equiv(normed, expected) )
 def test_only_negative(self):
     from jicbioimage.core.util.array import normalise
     zeros = np.zeros(5, dtype=np.float)
     normed = normalise(zeros - 1.)
     self.assertTrue( np.array_equiv(normed, zeros) )
 def test_only_positive(self):
     from jicbioimage.core.util.array import normalise
     ones = np.ones(5, dtype=np.float)
     normed = normalise(ones + 3.)
     self.assertTrue( np.array_equiv(normed, ones) )
 def test_normalise_returns_float_array(self):
     from jicbioimage.core.util.array import normalise
     zeros = np.array([1,2,3], dtype=np.uint8)
     normed = normalise(zeros)
     self.assertEqual(normed.dtype, np.float)