Scikit Image - 多影像



多影像或多幀影像通常指的是一種影像格式,它可以在單個檔案中儲存和表示多個影像或幀。例如,動畫 GIF 和多幀 TIFF 檔案就是多影像格式的示例。

Scikit Image 中的 MultiImage 類

scikit-image 的 io 模組中的 MultiImage 類用於專門處理多幀 TIFF 影像。它提供了一種方便的方式來載入和操作多幀 TIFF 影像。

使用 MultiImage 類處理多幀 TIFF 時,它會返回一個影像資料陣列列表,類似於 ImageCollection 類。但是,它們處理多幀影像的方式有所不同。Multi-Image 將多幀 TIFF 影像的所有幀作為列表中的單個元素儲存,其形狀為 (N, W, H),其中 N 是幀數,W 和 H 分別是每幀的寬度和高度。

以下是此類的語法:

class skimage.io.MultiImage(filename, conserve_memory=True, dtype=None,
**imread_kwargs)

以下是類的引數:

  • filename - 指定要載入的模式或檔名(字串或字串列表)。路徑可以是絕對路徑或相對路徑。
  • conserve_memory(可選) - 布林值。如果設定為 True,則一次只保留一個影像在記憶體中。如果設定為 False,則載入後會快取影像以提高後續訪問速度。

示例 1

以下示例演示瞭如何使用 MultiImage 類載入多幀 TIFF 影像並獲取有關載入影像的資訊。

from skimage.io import MultiImage

# Load the multi-frame TIFF image
multi_image = MultiImage('Images_/Multi_Frame.tif')

# Access and display information about the loaded image file
print(multi_image)
print('Type:',type(multi_image))
print('Length:',len(multi_image))
print('Shape:',multi_image[0].shape)

輸出

['Images_/Multi_Frame.tif']
Type: < class 'skimage.io.collection.MultiImage' >
Length: 1
Shape: (6, 382, 363, 3)

示例 2

讓我們使用 ImageCollection 類讀取同一個多幀 TIFF 檔案“Multi_Frame.tif”,並觀察它與 MultiImage 類相比如何處理多幀影像。

from skimage.io import ImageCollection

# Load the multi-frame TIFF image
ic = ImageCollection('Images_/Multi_Frame.tif')

# Access and display information about the loaded image file
print(ic)
print('Type:',type(ic))
print('Length:',len(ic))
print('Shape:',ic[0].shape)

輸出

['Images_/Multi_Frame.tif']
Type: < class 'skimage.io.collection.ImageCollection' >
Length: 6
Shape: (382, 363, 3)

處理動畫 GIF 影像時,MultiImage 只讀取第一幀,而 ImageCollection 預設讀取所有幀。

示例 3

讓我們看一下下面的示例,並觀察 MultiImage 類如何處理動畫 GIF 影像。

from skimage.io import MultiImage

# Load an animated GIF image
multi_image = MultiImage('Images/dance-cartoon.gif')

# display the multi_image object
print(multi_image)
print('Type:',type(multi_image))
print('Length:',len(multi_image))
for i, frame in enumerate(multi_image):
   print('Image {} shape:{}'.format(i, frame.shape))

輸出

['Images/dance-cartoon.gif']
Type: < class 'skimage.io.collection.MultiImage'>
Length: 1
Image 0 shape:(300, 370, 4)

示例 4

讓我們使用 ImageCollection 類讀取同一個 GIF 檔案“dance-cartoon.gif”,並觀察它與 MultiImage 類相比如何處理動畫 GIF 影像。

from skimage.io import ImageCollection

# Load an animated GIF image
ic = ImageCollection('Images/dance-cartoon.gif')

# Access and display information about the loaded image file
print(ic)
print('Type:',type(ic))
print('Length:',len(ic))
for i, frame in enumerate(ic):
   print('Image {} shape:{}'.format(i, frame.shape))

輸入影像

Dance Cartoon

輸出

['Images/dance-cartoon.gif']
Type: <class 'skimage.io.collection.ImageCollection'>
Length: 12
Image 0 shape:(300, 370, 4)
Image 1 shape:(300, 370, 4)
Image 2 shape:(300, 370, 4)
Image 3 shape:(300, 370, 4)
Image 4 shape:(300, 370, 4)
Image 5 shape:(300, 370, 4)
Image 6 shape:(300, 370, 4)
Image 7 shape:(300, 370, 4)
Image 8 shape:(300, 370, 4)
Image 9 shape:(300, 370, 4)
Image 10 shape:(300, 370, 4)
Image 11 shape:(300, 370, 4)
廣告

© . All rights reserved.