Exemplo n.º 1
0
 def test_from_array(self):
     from jicbioimage.core.image import Image
     ar = np.zeros((50,50), dtype=np.uint8)
     im = Image.from_array(ar)
     self.assertTrue(isinstance(im, Image))
     self.assertEqual(len(im.history), 0)
     self.assertEqual(im.history.creation, 'Created Image from array')
Exemplo n.º 2
0
 def test_from_array(self):
     from jicbioimage.core.image import Image
     ar = np.zeros((50, 50), dtype=np.uint8)
     im = Image.from_array(ar)
     self.assertTrue(isinstance(im, Image))
     self.assertEqual(len(im.history), 0)
     self.assertEqual(im.history.creation, 'Created Image from array')
Exemplo n.º 3
0
 def test_from_array_with_name(self):
     from jicbioimage.core.image import Image
     ar = np.zeros((50, 50), dtype=np.uint8)
     im = Image.from_array(ar, name='Test1')
     self.assertEqual(len(im.history), 0)
     self.assertEqual(im.history.creation,
                      'Created Image from array as Test1')
 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(unique_color_array(self[:, :, z]))
             fh.write(im.png())
Exemplo n.º 5
0
def create_mask(image):
    """Return a mask for the region of interest."""
    selem = skimage.morphology.disk(2)
    im = equalize_adaptive_clahe(image)
    im = threshold_otsu(im)
    im = erosion_binary(im, selem)
    mask = np.ones(im.shape, dtype=bool)
    segmentation = connected_components(im, background=0)
    for i in segmentation.identifiers:
        region = segmentation.region_by_identifier(i)
        if region.area > 5000:
            mask[np.where(region.convex_hull)] = False
    return Image.from_array(mask)
 def test_connected_components_acts_like_a_transform(self):
     from jicbioimage.segment import connected_components
     from jicbioimage.core.image import Image
     ar = np.array([[1, 1, 0, 0, 0],
                    [1, 1, 0, 0, 0],
                    [0, 0, 0, 0, 0],
                    [0, 0, 2, 2, 2],
                    [0, 0, 2, 2, 2]], dtype=np.uint8)
     im = Image.from_array(ar)
     self.assertEqual(len(im.history), 1)
     segmentation = connected_components(im)
     self.assertEqual(len(segmentation.history), 2)
     self.assertEqual(segmentation.history[-1],
                      "Applied connected_components transform")
Exemplo n.º 7
0
    def func_as_transformation(*args, **kwargs):

        # Take copies of the args and kwargs for use in the history.
        # We will need to remove the image from either the kwargs
        # or the args before we use h_args and h_kwargs to create a
        # history event.
        h_args = list(args[:])
        h_kwargs = kwargs.copy()

        # Get the input image, so that we can get the history from it.
        # Also, strip the image for h_args/h_kwargs.
        input_image = kwargs.get("image", None)
        if input_image is None:
            # The image is the first item of args.
            input_image = args[0]
            h_args.pop(0)
        else:
            # The image is in kwargs.
            h_kwargs.pop("image")

        def array_to_str(value):
            if isinstance(value, np.ndarray):
                value = repr(value)
            return value

        h_args = [array_to_str(v) for v in h_args]
        for key, value in h_kwargs.items():
            h_kwargs[key] = array_to_str(value)

        # Get the history from the image.
        history = History()
        if hasattr(input_image, "history"):
            history.extend(input_image.history)

        image = func(*args, **kwargs)
        if not isinstance(image, _BaseImageWithHistory):
            image = Image.from_array(image, log_in_history=False)

        # Update the history of the image.
        image.history = history
        image.history.add_event(func, h_args, h_kwargs)

        if AutoWrite.on:
            fpath = AutoName.name(func)
            image.write(fpath)
        return image
    def test_can_return_segmented_image(self):
        from jicbioimage.core.image import Image
        from jicbioimage.segment import SegmentedImage
        from jicbioimage.core.transform import transformation
        from jicbioimage.core.io import AutoName
        AutoName.directory = TMP_DIR

        @transformation
        def test_segmentation(image):
            return image.view(SegmentedImage)

        image = Image.from_array(np.zeros((50, 50), dtype=np.uint8))
        self.assertTrue(isinstance(image, Image))
        segmentation = test_segmentation(image)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(len(segmentation.history), 2)
        self.assertEqual(segmentation.history[-1],
                         "Applied test_segmentation transform")
    def test_from_directory(self):
        from jicbioimage.core.image import Image3D, Image
        directory = os.path.join(TMP_DIR)
        with open(os.path.join(directory, 'junk.txt'), "w") as fh:
            fh.write("junk")

        z0 = np.zeros((50, 50), dtype=np.uint8)
        z1 = np.ones((50, 50), dtype=np.uint8) * 255
        for i, z in enumerate([z0, z1]):
            im = Image.from_array(z)
            fpath = os.path.join(directory, "z{}.png".format(i))
            with open(fpath, "wb") as fh:
                fh.write(im.png())

        im3d = Image3D.from_directory(TMP_DIR)
        self.assertTrue(isinstance(im3d, Image3D))
        self.assertEqual(im3d.shape, (50, 50, 2))

        stack = np.dstack([z0, z1])
        self.assertTrue(np.array_equal(im3d, stack))
    def test_from_directory(self):
        from jicbioimage.core.image import Image3D, Image
        directory = os.path.join(TMP_DIR)
        with open(os.path.join(directory, 'junk.txt'), "w") as fh:
            fh.write("junk")

        z0 = np.zeros((50,50), dtype=np.uint8)
        z1 = np.ones((50, 50), dtype=np.uint8) * 255
        for i, z in enumerate([z0, z1]):
            im = Image.from_array(z)
            fpath = os.path.join(directory, "z{}.png".format(i))
            with open(fpath, "wb") as fh:
                fh.write(im.png())

        im3d = Image3D.from_directory(TMP_DIR)
        self.assertTrue(isinstance(im3d, Image3D))
        self.assertEqual(im3d.shape, (50, 50, 2))

        stack = np.dstack([z0, z1])
        self.assertTrue(np.array_equal(im3d, stack))
    def test_watershed_with_seeds_acts_like_a_transform(self):
        from jicbioimage.segment import watershed_with_seeds
        from jicbioimage.core.image import Image

        ar = np.array([[0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 9, 0, 0],
                       [9, 9, 9, 9, 9, 9],
                       [0, 0, 0, 9, 0, 0],
                       [0, 0, 0, 9, 0, 0]], dtype=np.uint8)
        im = Image.from_array(ar)
        self.assertEqual(len(im.history), 1)

        sd = np.array([[1, 0, 0, 0, 0, 2],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [3, 0, 0, 0, 0, 4]], dtype=np.uint8)

        segmentation = watershed_with_seeds(im, seeds=sd)
        self.assertEqual(len(segmentation.history), 2)
        self.assertEqual(segmentation.history[-1],
                         "Applied watershed_with_seeds transform")
Exemplo n.º 12
0
    def unique_color_image(self):
        """Return segmentation as a unique color image.

        :returns: `jicbioimage.core.image.Image`
        """
        return Image.from_array(unique_color_array(self))
Exemplo n.º 13
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))
Exemplo n.º 14
0
 def test_from_array_no_history(self):
     from jicbioimage.core.image import Image
     ar = np.zeros((50,50), dtype=np.uint8)
     im = Image.from_array(ar, log_in_history=False)
     self.assertEqual(len(im.history), 0)
Exemplo n.º 15
0
 def test_from_array_with_name(self):
     from jicbioimage.core.image import Image
     ar = np.zeros((50,50), dtype=np.uint8)
     im = Image.from_array(ar, name='Test1')
     self.assertEqual(len(im.history), 0)
     self.assertEqual(im.history.creation, 'Created Image from array as Test1')
Exemplo n.º 16
0
 def test_from_array_no_history(self):
     from jicbioimage.core.image import Image
     ar = np.zeros((50, 50), dtype=np.uint8)
     im = Image.from_array(ar, log_in_history=False)
     self.assertEqual(len(im.history), 0)
 def average_projection(stack):
     xmax, ymax, zmax = stack.shape
     projection = np.sum(stack, axis=2, dtype=np.uint8) // zmax
     return Image.from_array(projection)