Scikit Image - Image Stack



A stack, in general, refers to a collection of independent components that collaborate to enable a particular functionality of an application.

On the other hand, an image stack combines a group of images that share a common reference. While the images in the stack may vary in terms of quality or content, they are organized together for efficient processing and analysis. The image stack is grouped together for analysis or processing purposes, allowing for efficient batch operations.

In the scikit-image library, the io module offers functions push() and pop() specifically for working with the image stack.

The Io.pop() and io.push() functions

The pop() function is used to remove an image from the shared image stack. It returns the image that has been popped from the stack as a NumPy ndarray.

And the push(img) function is used to add a specific image to the shared image stack. It takes a NumPy ndarray (image array) as input.

Example

The following example demonstrates how to push images onto the shared stack using io.push() and retrieve images from the stack using io.pop(). It will also show that attempting to pop an image from an empty stack will raise an error named IndexError.

import skimage.io as io
import numpy as np

# Generate images with random numbers
image1 = np.random.rand(2, 2)
image2 = np.random.rand(1, 3)

# Push all image onto the shared stack one by one
io.push(image1)
io.push(image2)

# Pop an image from the stack
popped_image_array1 = io.pop()

# Display the popped image array
print("The array of popped image",popped_image_array1)

# Pop another image from the stack
popped_image_array2 = io.pop()

# Display the popped image array
print("The array of popped image",popped_image_array2)
popped_image_array3 = io.pop() # Output IndexError
popped_image_array3

Output

The array of popped image [[0.58981037 0.04246133 0.78413075]]
The array of popped image [[0.47972125 0.55525751]
[0.02514485 0.15683907]]
---------------------------------------------------------------------------
IndexError       Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_792\2226447525.py in < module >
     23
     24 # will rice an IndexError
---> 25 popped_image_array3 = io.pop()
     26 popped_image_array3
~\anaconda3\lib\site-packages\skimage\io\_image_stack.py in pop()
     33
     34       """
---> 35       return image_stack.pop()
IndexError: pop from empty list

The last two lines of the above example will raise an IndexError. This is because there are only two images pushed onto the shared stack using io.push(), but the third call to io.pop() attempts to pop an image from the stack, causing an IndexError since the stack is empty after the first two pops.

Advertisements