Scikit-Image - 影像棧



總的來說,棧是指一組協同工作以實現應用程式特定功能的獨立元件的集合。

另一方面,影像棧組合了一組共享共同參考的影像。雖然棧中的影像在質量或內容方面可能有所不同,但它們被組織在一起以便於高效處理和分析。影像棧被分組在一起用於分析或處理目的,從而實現高效的批次操作。

在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

# 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。

廣告
© . All rights reserved.