Scikit Image - Image Collections



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

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.

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

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))

Output

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

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)

Output

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

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

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.

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

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)

Output

Dipaly the tifffile collection: 
['Images_\\file_example_TIFF_1MB.tiff', 'Images_\\file_example_TIFF_10MB.tiff']

Example 2

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)

Output

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

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

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

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()

Output

Running the above code gives us the following result −

Imshow Collection
Advertisements