Scikit Image - 使用外掛



外掛指的是可以增強程式功能的擴充套件或外部軟體元件,透過向其中新增特定功能來提高其功能。

Python Scikit Image 中的外掛

Python scikit-image (skimage) 庫帶有一系列外掛,可用於處理影像 IO 操作,例如讀取、寫入和顯示影像。scikit-image 庫中可用的外掛包括 Matplotlib、PIL(Python Imaging Library)、GDAL、SimpleITK、tifffile、PyFITS 和 ImageIO 等流行庫。每個外掛都專門用於特定的影像 IO 操作。

scikit-image 庫根據需要載入外掛,以確保最佳效能並實現有效的資源利用。這意味著只有在明確需要時或設定為預設值時才會載入外掛。這種動態載入機制確保僅載入必要的外掛,具體取決於特定的影像 I/O 操作。

此外,scikit-image 庫還提供了一系列功能工具來處理和操作外掛。這些工具允許使用者自定義其影像 IO 操作,讓我們討論一下 skimage.io 模組中提供的一些關鍵外掛函式。

列出可用的外掛

函式io.find_available_plugins(loaded=False)用於列出 scikit-image (skimage.io) 中可用的外掛。它返回一個字典,其中外掛名稱作為鍵,公開的函式作為值。以下是此函式的語法:

skimage.io.find_available_plugins(loaded=False)

引數“loaded”採用布林值。如果將其設定為True,則僅顯示已載入的外掛。預設情況下,將顯示所有外掛。

示例 1

以下示例演示瞭如何使用io.find_available_plugins()函式列出所有可用的外掛及其對應的公開函式。

import skimage.io as io
# List all available plugins
available_plugins = io.find_available_plugins()
# Display the plugin names and their exposed functions
for plugin_name, exposed_functions in available_plugins.items():
   print('Plugin:', plugin_name)
   print("Exposed Functions:", exposed_functions)
   print()

輸出

Plugin: fits
Exposed Functions: ['imread', 'imread_collection']
Plugin: gdal
Exposed Functions: ['imread', 'imread_collection']
Plugin: gtk
Exposed Functions: ['imshow']
Plugin: imageio
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: imread
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: matplotlib
Exposed Functions: ['imshow', 'imread', 'imshow_collection',
'imread_collection']
Plugin: pil
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: qt
Exposed Functions: ['imshow', 'imsave', 'imread', 'imread_collection']
Plugin: simpleitk
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: tifffile
Exposed Functions: ['imread', 'imsave', 'imread_collection']

示例 2

讓我們僅獲取有關已載入外掛的資訊。

from skimage import io
# List loaded plugins only
available_plugins = io.find_available_plugins(loaded=True)
# Display the loaded plugin names and their exposed functions
for plugin_name, exposed_functions in available_plugins.items():
   print('Plugin:', plugin_name)
   print("Exposed Functions:", exposed_functions)
   print()

輸出

Plugin: imageio
Exposed Functions: ['imread', 'imsave', 'imread_collection']
Plugin: matplotlib
Exposed Functions: ['imshow', 'imread', 'imshow_collection',
'imread_collection']
Plugin: tifffile
Exposed Functions: ['imread', 'imsave', 'imread_collection']

檢索有關特定外掛的資訊

函式 io.plugin_info(plugin) 用於檢索有關特定外掛的資訊。它返回一個包含有關外掛資訊的字典,例如描述,並且僅提供可用的函式名稱。以下是此函式的語法:

skimage.io.plugin_info(plugin)

引數“plugin”採用字串,表示要檢索資訊的外掛的名稱。

示例 1

以下示例演示瞭如何使用io.plugin_info()函式檢索有關“pil”外掛的資訊。

from skimage import io
# Get information about the 'pil' plugin
plugin_info = io.plugin_info('pil')
# Print the plugin information
print("Description:", plugin_info['description'])
print("Available Functions:", plugin_info['provides'])
print()

輸出

Description: Image reading via the Python Imaging Library
Available Functions: imread, imsave

示例 2

在此示例中,我們將獲取有關“matplotlib”外掛的資訊。

from skimage import io
# Get information about the 'matplotlib' plugin
plugin_info = io.plugin_info('matplotlib')
# Print the plugin information
print("Description:", plugin_info['description'])
print("Available Functions:", plugin_info['provides'])
print()

輸出

Description: Display or save images using Matplotlib
Available Functions: imshow, imread, imshow_collection, _app_show

檢索外掛順序

io.plugin_order() 函式用於獲取當前首選的外掛順序。以下是此函式的語法:

skimage.io.plugin_order()

該函式返回一個字典,其中函式名稱作為鍵,對應值是按優先順序順序排列的外掛列表。

示例

以下是使用 plugin_order() 函式獲取當前首選外掛順序的示例。

from skimage import io
# Get the currently preferred plugin order
order_dict = io.plugin_order()
# Print the plugin loading order
print("Preferred Plugin Order:")
for function_name, plugins in order_dict.items():
   print("Function:", function_name)
   print("Plugins in order of preference:", plugins)
   print()

輸出

Preferred Plugin Order:
Function: imread
Plugins in order of preference: ['imageio', 'matplotlib']
Function: imsave
Plugins in order of preference: ['imageio']
Function: imshow
Plugins in order of preference: ['matplotlib']
Function: imread_collection
Plugins in order of preference: ['imageio', 'matplotlib']
Function: imshow_collection
Plugins in order of preference: ['matplotlib']
Function: _app_show
Plugins in order of preference: ['matplotlib']

設定預設外掛

io.use_plugin() 函式用於為指定的運算設定預設外掛。

如果尚未載入指定的外掛,則會載入它。以下是此函式的語法:

skimage.io.use_plugin(name, kind=None)

以下是此函式的引數:

  • name - 表示要設定為預設值的外掛名稱的字串。
  • kind(可選) - 表示為其設定外掛的特定函式的字串。它可以採用以下值之一:“imsave”、“imread”、“imshow”、“imread_collection”、“imshow_collection”。預設情況下,外掛設定為所有函式。

示例

以下示例顯示了設定預設外掛後外掛載入順序的變化。

from skimage import io

# Get the currently preferred plugin order 
order_dict_1 = io.plugin_order()

# Print the plugin loading order
print("Plugin Order before Setting the default plugin:")
for function_name, plugins in order_dict_1.items():
    print(function_name)
    print("Plugin order:", plugins)
    
    print()

# Set the default plugin for all functions
io.use_plugin('qt')

# Get the preferred plugin order after setting the default plugin
order_dict_2 = io.plugin_order()

# Print the plugin loading order
print("Plugin Order after setting the default plugin: ")
for function_name, plugins in order_dict_2.items():
    print(function_name)
	print("Plugin order:", plugins)
    
    print()

輸出

Plugin Order before Setting the default plugin:
imread
Plugin order: ['imageio', 'matplotlib']

imsave
Plugin order: ['imageio']

imshow
Plugin order: ['matplotlib']

imread_collection
Plugin order: ['imageio', 'matplotlib']

imshow_collection
Plugin order: ['matplotlib']

_app_show
Plugin order: ['matplotlib']

Plugin Order after setting the default plugin: 
imread
Plugin order: ['qt', 'imageio', 'matplotlib']

imsave
Plugin order: ['qt', 'imageio']

imshow
Plugin order: ['qt', 'matplotlib']

imread_collection
Plugin order: ['qt', 'imageio', 'matplotlib']
imshow_collection
Plugin order: ['matplotlib']

_app_show
Plugin order: ['qt', 'matplotlib']

將外掛重置為其預設狀態

函式 io.reset_plugins() 用於將外掛狀態重置為其預設/初始狀態。即,不載入任何外掛。以下是此函式的語法:

skimage.io.reset_plugins()

示例

以下示例演示瞭如何使用 io.reset_plugins() 函式將外掛狀態重置為其預設狀態。它在將外掛狀態重置為預設狀態之前和之後檢索外掛順序。

from skimage import io

# Get the currently preferred plugin order 
order_dict_1 = io.plugin_order()

# Print the plugin loading order
print("Plugin Order before Resetting to the default state:")
for function_name, plugins in order_dict_1.items():
   if function_name == 'imread':
      print('Function',function_name)
      print("Plugin order:", plugins)

      print()

# Reset the plugin state to the default
io.reset_plugins()

# Get the preferred plugin order after Resetting to the default state
order_dict_2 = io.plugin_order()
# Print the plugin loading order
print("Plugin Order after Resetting to the default state: ")
for function_name, plugins in order_dict_2.items():
   if function_name == 'imread':
      print('Function',function_name)
      print("Plugin order:", plugins)
      print()

輸出

Plugin Order before Resetting to the default state:
Function imread
Plugin order: ['pil', 'imageio', 'matplotlib']

Plugin Order after Resetting to the default state: 
Function imread
Plugin order: ['imageio', 'matplotlib']

特定函式的合適外掛

io.call_plugin() 函式用於查詢指定函式的合適外掛並執行它。以下是此函式的語法:

skimage.io.call_plugin(kind, *args, **kwargs)

以下是此函式的引數:

  • kind (str) - 要查詢的函式。它可以採用以下值之一:“imshow”、“imsave”、“imread”、“imread_collection”。
  • plugin (str, optional) - 要載入的特定外掛。如果未提供(預設為 None),則將使用第一個匹配的外掛。
  • *args, **kwargs - 將傳遞給外掛函式的其他引數和關鍵字引數。

示例

以下示例演示瞭如何使用 io.call_plugin() 函式使用影像讀取函式的合適外掛載入影像檔案。

import numpy as np
from skimage import io

img_array = io.call_plugin('imread', 'Images_/black rose.jpg')
print("Image Arary Shape:",img_array.shape)

輸出

Image Arary Shape: (2848, 4272, 3)

該函式載入影像檔案並將其作為 NumPy 陣列返回,該陣列分配給 img_array 變數。

廣告

© . All rights reserved.