ImageStack

class ImageStack(dtype=<class 'numpy.float32'>)[source]

This object holds multiple images. All images are converted to the same datatype. This ensures that all images have the same characteristics for further processing.

All images are represented as numpy arrays. The same convention for representing images is used as in skimage.

If an image with mismatching dtype is added it is automatically converted to match the dtype. Read more about conversion details at skimage.util.dtype.

This object behaves a lot like a list. Individual images or groups of images can be retrieved with slicing. Setitem and delitem behaviour is like with normal python lists but mages can only be added with add_image.

Parameters
dtype: optional, default=np.float32

The dtype all images will be converted to. E.g. np.float32, bool, etc.

Examples

>>> # make an ImageStack object where all images are represented as unsigned integer arrays [0-255]
>>> stack = ImageStack(dtype=np.uint8)
>>> # Add an image to it.
>>> img = (np.random.rand(200,200) * np.arange(200))/200  # floating point images must be in range [-1,1]
>>> stack.add_image(img)
This ImageStack can be indexed.
>>> stack[0]  # getting the image with index 0 from the stack
Changing an image in the stack. The input will also be converted to the dtype of the stack.
>>> stack[0] = (np.random.rand(200,200) * np.arange(200))/200[::-1]  # setting an image in the stack
Or deleting an image form the stack
>>> del stack[0]

Methods

add_image(img)

Add an image to the stack.

change_dtype(dtype)

Change the dtype of all images in the stack.

copy()

Copy the current image stack.

execute_function(func, *args, **kwargs)

Perform an operation on all the images in the stack.

execute_rolling_function(func[, keep_first])

Perform an rolling operation on all the images in the stack.

from_paths(paths[, dtype])

Make an ImageStack object directly form paths of images.

remove_image([i])

Remove an image from the stack.

add_image(img)[source]

Add an image to the stack. The image must be a numpy array

The input array will be converted to the dtype of the ImageStack

Parameters
img: np.ndarray
change_dtype(dtype)[source]

Change the dtype of all images in the stack. All images will be converted to the new dtype.

Parameters
dtype
copy()[source]

Copy the current image stack.

The copy is shallow until images are changed in the new stack.

Returns
out: ImageStack
execute_function(func, *args, **kwargs)[source]

Perform an operation on all the images in the stack.

The operation can be any function which takes one images and other arguments as input and returns only one image.

This operation changes the images in the stack. If the current state should be kept copy the stack first.

Parameters
func: function

A function which takes ONE image as first input and returns ONE image.

args:

args are forwarded to the func.

kwargs:

kwargs are forwarded to the func.

Examples

>>> def fun(img, to_add):
>>>     return img + to_add
>>> stack.execute_function(fun, to_add=4)
This will apply the function *fun* to all images in the stack.
execute_rolling_function(func, keep_first=False, *args, **kwargs)[source]

Perform an rolling operation on all the images in the stack.

The operation can be any function which takes two images and other arguments as input and returns only one image.

\(I_{new} = func(I_{n-1}, I_n)\)

This operation changes the images in the stack. If the current state should be kept copy the stack first.

Since the 0-th image in the stack will remain unchanged because the rolling operation starts at the 1-st image, the 0-th image is removed if keep_first is set to False (default).

Parameters
func: function

A function which takes TWO images and other arguments as input and returns ONE image. The function must have the following input structure: fun(img1, img2, args, kwargs). img1 will be the n-1st image in the calls.

keep_first: bool

If True, keeps the first image in the stack. Delete it otherwise.

args:

args are forwarded to the func.

kwargs:

kwargs are forwarded to the func.

Examples

>>> def fun(img1, img2):
>>>     mask = img1 > img1.max()/2
>>>     return img2[mask]
>>> stack.execute_rolling_function(fun, keep_first=False)
This will apply the function *fun* to all images in the stack.

img1 is always the n-1st image in the rolling operation.

classmethod from_paths(paths, dtype=None, **kwargs)[source]

Make an ImageStack object directly form paths of images. The images will be loaded, converted to the dtype of the ImageStack and added.

Parameters
paths: list

paths of the images to be added

dtype: optional

The dtype all images will be converted to. E.g. np.float32, bool, etc. If this is not set, the dtype of the first image loaded will determine the dtype of the stack.

kwargs:

kwargs are forwarded to skimage.io.imread For grayscale images simply add as_gray = True. For the kwargs for colored images use parameters for reading. Keep in mind that some images might have alpha channels and some not even if they have the same format.

Returns
out: ImageStack

An ImageStack with all images from paths as arrays.

Examples

>>> paths = ['list of image paths']
>>> stack = ImageStack.from_paths(paths, as_gray=True)
remove_image(i=-1)[source]

Remove an image from the stack.

Parameters
i: int

Index of the image to be removed