Scikit-image 简明教程
Scikit Image - Image Collections
在计算机视觉和图像处理中, image collection 是一个术语,用于描述一群或一组被认为作为一个单一实体以同时管理和处理多幅图像之目的而放在一起的图像。
In computer vision and image processing, an image collection is a term used to describe a group or set of images that are considered together as a single entity for the purpose of managing and processing multiple images simultaneously.
它可以用来存储和管理一组相关图像,如一段视频中的帧序列或来自不同来源的图像集合。它简化了多幅图像的管理和处理,从而更易于处理图像处理和计算机视觉任务。
It can be used to store and manage a group of related images, such as a sequence of frames from a video, or a collection of images from various sources. And it simplifies the management and processing of multiple images, making it easier to handle image processing and computer vision tasks.
ImageCollection class in skimage
在scikit-image库中,图像集合由 ImageCollection 类表示,它提供了一个用于加载、管理和操作图像文件集合的功能。
In the scikit-image library, the image collection is represented by an ImageCollection class that provides functionality for loading, managing, and manipulating a collection of image files.
它允许你指定一个模式或文件名列表,将相应的图像加载到内存中,并方便地访问它们。以下是该类的语法示例:
It allows you to specify a pattern or a list of filenames, load the corresponding images into memory, and access them conveniently. Following is the syntax of this class −
class skimage.io.ImageCollection(load_pattern, conserve_memory=True, load_func=None, **load_func_kwargs)
以下是类的参数−
Here are the parameters of the class −
-
load_pattern − A string or a list of strings representing the pattern of the file name to load. The filename path can be absolute or relative.
-
conserve_memory (optional) − A boolean value. If set to True, only one image will be kept in memory at a time. If set to False, images will be cached after loading to improve subsequent access speed.
-
load_func (optional) − A callable object that is used to read the image files. By default, it uses the imread function from the scikit-image library. However, you can specify a different function if needed.
-
**load_func_kwargs (optional) − Additional keyword arguments that are passed to the load_func function.
它创建一个ImageCollection对象,允许你对加载的图像执行各种操作,如遍历集合、访问单个图像以及对整个集合应用操作。
It creates an ImageCollection object that allows you to perform various operations on the loaded images, such as iterating over the collection, accessing individual images, and applying operations to the entire collection.
Example 1
以下示例将演示如何加载指定目录中的所有JPEG文件。结果ImageCollection对象将存储在collection变量中。
The following example will demonstrate how to load all the JPEG files in the specified directory. And the resulting ImageCollection object will be stored in the collection variable.
from skimage import io
# Load all the JPEG files in a directory
collection = io.ImageCollection('Images_/*.jpg')
print('Type:',type(collection))
print('Total loaded JPEG files are',len(collection))
输出将显示集合对象的类型和加载的JPEG文件数量。
The output shows the type of the collection object and the number of loaded JPEG files.
Type: < class 'skimage.io.collection.ImageCollection'>
Total loaded JPEG files are 5
Example 2
以下示例演示如何使用ImageCollection对象的files属性访问扩展的文件名。
The following example demonstrate how to access the expanded file names using the files attribute of the ImageCollection object.
from skimage import io
# Load all the JPEG and PNG files in a directory
collection = io.ImageCollection(['Images_/*.jpg', 'Images_/*.png'])
# Access the expanded file list
file_list = collection.files
# Print the list of files one by one
print("Files:")
for image in file_list:
print(image)
Files:
Images_\Blank.png
Images_\Blank_img.png
Images_\ColorDots.png
Images_\Trees.jpg
Images_\WhiteDots2.jpg
Images_\WhiteDots4.jpg
Images_\Zoo.jpg
Images_\balloons_noisy.png
Images_\binary image.png
Images_\tree.jpg
你还可以使用skimage.io模块中名为io.imread_collection()的直接函数来读取图像集合。
You can also use a direct function called io.imread_collection() in the skimage.io module for reading the collection of images.
The imread_collection() function
imread_collection()函数用于加载图像集合,它将返回一个ImageCollection对象,表示加载的图像集合。
The imread_collection() function is used to load a collection of images. and it will return an ImageCollection object, representing the loaded image collection.
以下是此函数的语法和参数 -
Here’s the syntax and the parameters of the function −
skimage.io.imread_collection(load_pattern, conserve_memory=True, plugin=None, **plugin_args)
以下是该函数的参数 -
Following are the parameters of this function −
-
load_pattern − A string or a list of strings representing the pattern of the file name to load. The filename path can be absolute or relative.
-
conserve_memory (optional) − A Boolean value. If set to True, only one image will be kept in memory at a time. If set to False, images will be cached after loading to improve subsequent access speed.
-
plugin_args (optional) − Additional keyword arguments that will be passed to the chosen plugin.
imread_collection() 是一个便捷的包装函数,它在内部为加载一组图像创建一个 ImageCollection 对象。除了直接使用 ImageCollection 类外,在需要根据模式或文件名列表快速加载图像时,使用 imread_collection() 函数处理简单用例会很好。
The imread_collection() is a convenient wrapper function that internally creates an ImageCollection object for loading a collection of images. Other than using the ImageCollection class directly, It is good to use the imread_collection() function for simple use cases when you need to quickly load images based on a pattern or a list of filenames.
Example 1
以下示例演示如何加载特定目录中的所有 tiff 文件。
The following example demonstrates how to load all the tiff files in a specific directory.
from skimage import io
# Load all the tiff images
collection = io.imread_collection('Images_/*.tiff', plugin='tifffile')
print('Dipaly the tifffile collection:')
print(collection)
Dipaly the tifffile collection:
['Images_\\file_example_TIFF_1MB.tiff', 'Images_\\file_example_TIFF_10MB.tiff']
Example 2
以下示例将通过指定字符串列表(模式)来加载一组 tiff 和 JPEG 图像。
The following example will load a collection of tiff and JPEG images by specifying the list of strings(patterns).
from skimage import io
# Load a collection of JPEG and tifffile images
collection = io.imread_collection(['Image Collection/*.jpg', 'Image
Collection/*.tiff'])
print('Dipaly the JPEG and tifffile collection:')
print(collection)
Dipaly the JPEG and tifffile collection:
['Image Collection\\Trees.jpg', 'Image Collection\\WhiteDots2.jpg', 'Image
Collection\\WhiteDots4.jpg', 'Image Collection\\Zoo.jpg', 'Image
Collection\\file_example_TIFF_1MB.tiff', 'Image
Collection\\file_example_TIFF_10MB.tiff', 'Image Collection\\tree.jpg']
The Imread_collection_wrapper() function
imread_collection_wrapper 是装饰器函数,用于创建 imread_collection() 函数的自定义版本。此包装器函数封装了使用指定图像读取函数创建 ImageCollection 对象的逻辑。
The imread_collection_wrapper is a decorator function, that is used to create a custom version of the imread_collection() function. This wrapper function encapsulates the logic of creating an ImageCollection object with the specified image reading function.
以下是此函数的语法 -
Following is the syntax of this Function −
skimage.io.imread_collection_wrapper(imread)
The imshow_collection() function
imshow_collection() 函数用于显示一系列图像。它将 ImageCollection 对象作为输入并显示包含在此集合中的图像。
The imshow_collection() function is used to display a collection of images. It takes an ImageCollection object as input and displays the images contained in the collection.
在此处,此函数的语法和参数 -
Here, the syntax and the parameters of the function −
skimage.io.imshow_collection(ic, plugin=None, **plugin_args)
以下是参数 -
Following are the parameters −
-
ic − An ImageCollection object representing the collection of images to display.
-
plugin (optional) − A string specifying the name of the plugin to use for image display. By default, different plugins are attempted until a suitable one is found.
-
plugin_args − Additional keyword arguments that are passed to the selected plugin for image display.
Example
以下示例演示如何使用 imsave_collection() 函数显示一组图像。
The following example demonstrates how to use the imshow_collection() function to display a collection of images.
from skimage import io
# Load all the JPEG and PNG files in a directory
collection = io.ImageCollection('Images_/*.jpg')
# Access the expanded file list
file_list = collection.files
# Print the list of files one by one
print("Files:")
for image in file_list:
print(image)
# Display the collection of images
io.imshow_collection(collection)
io.show()
运行以上代码将产生以下结果 -
Running the above code gives us the following result −