Scikit-image 简明教程

Scikit Image - Image Stack

栈通常是指独立组件的集合,这些组件协作以实现应用程序的特定功能。

另一方面,图像栈组合了一组具有公共引用的图像。虽然栈中的图像在质量或内容方面可能有所不同,但它们被组织在一起以便于处理和分析。图像栈被分组在一起以用于分析或处理目的,从而可以进行高效的批处理操作。

在 scikit-image 库中, io module 专门提供 push() 和 pop() 函数来处理图像堆栈。

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

pop() 函数用于从共享图像堆栈中移除图像。它返回一个从堆栈中弹出图像的 NumPy ndarray。

push(img) 函数用于将特定图像添加到共享图像堆栈中。它以 NumPy ndarray (图像数组)作为输入。

Example

以下示例演示了如何使用 io.push() 将图像推送到共享堆栈中,以及使用 io.pop(). 从堆栈中检索图像。它还将显示试图从空堆栈中弹出一个图像将引发 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 异常。