Scikit-image 简明教程
Scikit Image - Using Plugins
plugin 指扩展或外部软件组件,它可以通过为程序添加特定功能从而增强程序功能,从而提高其功能。
A plugin refers to an extension or external software component that can enhance the functionality of the program by adding specific features to it thus improving its functionality.
Plugins in Python Scikit Image
Python scikit-image (skimage) 库提供了多种插件,可以用来处理图像 I/O 操作,如读取、写入和显示图像。scikit-image 库中的可用插件包括如 Matplotlib、PIL(Python 图像库)、GDAL、SimpleITK、tiffile、PyFITS 和 ImageIO 等流行的库。每个插件都专门用于一项特定的图像 I/O 操作。
The Python scikit-image (skimage) library comes with a variety of plugins that can be used to handle image IO operations, such as reading, writing, and displaying images. The available plugins in the scikit-image library include popular libraries like Matplotlib, PIL (Python Imaging Library), GDAL, SimpleITK, tifffile, PyFITS, and ImageIO. Each plugin specializes in a specific image IO operation.
scikit-image 库根据需要加载插件,以确保最佳性能并实现有效的资源利用。这意味着只有在明确需要或设为默认值时才加载插件。这种动态加载机制确保仅加载必需的插件,具体取决于特定的图像 I/O 操作。
The scikit-image library loads the plugins as needed, to ensure optimal performance, and to allow efficient resource utilization. This means that plugins are only loaded when explicitly required or when set as default. This dynamic loading mechanism ensures that only the necessary plugins are loaded, depending on the specific image I/O operation.
此外,scikit-image 库还提供了一系列功能工具来处理和操作插件。这些工具允许用户自定义其图像 I/O 操作,让我们讨论一下 skimage.io 模块中一些重要的插件函数。
Also, the scikit-image library provides a range of functional tools to handle and operate plugins. These tools allow users to customize their image IO operations, let’s discuss some key plugin functions available in the skimage.io module.
Listing the available plugins
函数 io.find_available_plugins(loaded=False) 用于列出 scikit-image(skimage.io)中的可用插件。它返回一个字典,以插件名称作为键,以公开函数作为值。以下是该函数的语法:
The function io.find_available_plugins(loaded=False) is used to list the available plugins in scikit-image (skimage.io). And it returns a dictionary with plugin names as keys and exposed functions as values. Following is the syntax of this function −
skimage.io.find_available_plugins(loaded=False)
参数 "loaded" 接受一个布尔值。如果将其设为 True ,只显示已加载的插件。默认情况下,显示所有插件。
The parameter "loaded" takes a boolean value. If it is set to True, only the loaded plugins will be shown. By default, all plugins are displayed.
Example 1
以下示例演示了如何使用 *io.find_available_plugins() *函数列出所有可用插件及其对应的公开函数。
The following example demonstrates the use of *io.find_available_plugins() *function to list all the available plugins and their corresponding exposed functions.
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']
Example 2
我们仅获取有关加载的插件的信息。
Let’s get the information about loaded plugins only.
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']
Retrieving Info about a specific plugin
函数 io.plugin_info(plugin) 用于检索特定插件的信息。它返回一个字典,其中包含有关插件的信息,如描述,并且只提供可用函数名称。以下是该函数的语法:
The function io.plugin_info(plugin) is used to retrieve information about a specific plugin. It returns a dictionary containing information about the plugin, such as the description, and provides nothing but available function names. Following is the syntax of this function −
skimage.io.plugin_info(plugin)
参数 "plugin" 接受一个字符串,表示要检索其信息的插件名称。
The parameter "plugin" takes a string representing the name of the plugin for which you want to retrieve information.
Example 1
以下示例演示了如何使用 io.plugin_info() 函数检索 'pil' 插件的信息。
The following example demonstrates how to use the io.plugin_info() function to retrieve the information about the 'pil' plugin.
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
Example 2
在此示例中,我们将获取有关 'matplotlib' 插件的信息。
In this example, we will get information about the 'matplotlib' plugin.
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
Retrieving the plugin order
io.plugin_order() 函数用于获取当前首选的插件顺序。以下为此函数的语法 −
The io.plugin_order() function is used to obtain the currently preferred plugin order. Following is the syntax of this function −
skimage.io.plugin_order()
该函数返回一个字典,其中函数名称作为键,对应值是按优先顺序排列的插件列表。
The function returns a dictionary with function names as keys and the corresponding values are lists of plugins in the order of preference.
Example
以下是如何使用 plugin_order() 函数获取当前首选的插件顺序的示例。
Following is an example of using the plugin_order() function to obtain the currently preferred 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']
Setting the default plugin
io.use_plugin() 函数用于设置指定操作的默认插件。
The io.use_plugin() function is used to set the default plugin for a specified operation.
如果尚未加载指定的插件,则将加载它。以下为此函数的语法 −
The specified plugin will be loaded if it hasn’t been loaded already. Following is the syntax of this function −
skimage.io.use_plugin(name, kind=None)
以下是此函数的参数 −
Here are the parameters of this function −
-
name − A string representing the name of the plugin is to be set as the default.
-
kind (optional) − A string represents the specific function for which the plugin is being set. It can take one of the following values: 'imsave', 'imread', 'imshow', 'imread_collection', 'imshow_collection'. By default, the plugin is set for all functions.
Example
以下示例显示了在设置默认插件后插件加载顺序的更改。
The following example shows the change in the plugin loading order after setting the default plugin.
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']
Resetting the plugin to its default state
io.reset_plugins() 函数用于将插件状态重置为其默认/初始状态。即,没有加载任何插件。以下为此函数的语法 −
The function io.reset_plugins() is used to reset the plugin state to its default/initial state. i.e., where no plugins are loaded. Following is the syntax of this function −
skimage.io.reset_plugins()
Example
以下示例演示如何使用 io.reset_plugins() 函数将插件状态重置为其默认状态。并在将插件状态重置为默认状态之前和之后检索插件顺序。
The following example demonstrates the use of io.reset_plugins() function to reset the plugin state to its default state. And it retrieves the plugin order before and after resetting the plugin state to the default.
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']
Appropriate plugin for a specific function
io.call_plugin() 函数用于查找特定函数的适当插件并执行它。以下为此函数的语法 −
The io.call_plugin() function is used to find the appropriate plugin for a specified function and execute it. Following is the syntax of this function −
skimage.io.call_plugin(kind, *args, **kwargs)
以下是此函数的参数 −
Here are the parameters of this function −
-
kind (str) − The function to look up. It can take one of the following values: 'imshow', 'imsave', 'imread', 'imread_collection'.
-
plugin (str, optional) − The specific plugin to load. If not provided (default is None), the first matching plugin will be used.
-
args, *kwargs − Additional arguments and keyword arguments that will be passed to the plugin function.
Example
以下示例演示如何使用 io.call_plugin() 函数使用图像读取函数的相应插件加载图像文件。
The following example demonstrates the use of io.call_plugin() function to load an image file using the appropriate plugin for the image reading function.
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 变量。
The function loads the image file and returns it as a NumPy array, which is assigned to the img_array variable.