Scikit 图像 - 图像堆栈

堆栈通常是指一组独立组件,它们协作以实现应用程序的特定功能。

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

在 scikit-image 库中,io 模块 提供了专门用于处理图像堆栈的 push() 和 pop() 函数。

Io.pop() 和 io.push() 函数

pop() 函数用于从共享图像堆栈中删除图像。它将已从堆栈中弹出的图像作为 NumPy ndarray 返回。

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

示例

以下示例演示如何使用 io.push() 将图像推送到共享堆栈,以及如何使用 io.pop() 从堆栈中检索图像。它还将显示尝试从空堆栈中弹出图像将引发名为 IndexError

的错误。
import skimage.io as io
import numpy as np

# 生成带有随机数的图像
image1 = np.random.rand(2, 2)
image2 = np.random.rand(1, 3)

# 将所有图像逐一推送到共享堆栈
io.push(image1)
io.push(image2)

# 从堆栈中弹出图像stack
popped_image_array1 = io.pop()

# 显示弹出的图像数组
print("The array of popped image",popped_image_array1)

# 从堆栈中弹出另一个图像
popped_image_array2 = io.pop()

# 显示弹出的图像数组
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,因为前两次弹出后堆栈为空。