Scikit-image 简明教程
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.
在 scikit-image 库中, io module 专门提供 push() 和 pop() 函数来处理图像堆栈。
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
pop() 函数用于从共享图像堆栈中移除图像。它返回一个从堆栈中弹出图像的 NumPy ndarray。
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.
push(img) 函数用于将特定图像添加到共享图像堆栈中。它以 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
以下示例演示了如何使用 io.push() 将图像推送到共享堆栈中,以及使用 io.pop(). 从堆栈中检索图像。它还将显示试图从空堆栈中弹出一个图像将引发 IndexError. 异常。
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
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
上述示例的最后两行将引发 IndexError 异常。这是因为只有两张图像被使用 io.push() 推送到共享堆栈中,但第三次调用 io.pop() 试图从堆栈中弹出一个图像,由于前两次弹出后堆栈已处于空状态,从而导致 IndexError 异常。
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.