Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Image Preprocessing Module Tutorial

This Python module provides functionality for reading and preprocessing images. The features of this module are:

  • Resizing images
  • Converting images to grayscale
  • Reading images from a specified directory
  • Saving processed images as NumPy arrays
  • Saving labels of images

Dependencies

Before starting, make sure that you have the following libraries installed. If not, you can install them using pip:

pip install numpy opencv-python scikit-image

Function: get_images()

The function get_images() reads images from a given directory and returns them as a NumPy array. If specified, the images are resized and converted to grayscale.

Parameters:

The function accepts an argparse.Namespace object that contains the following parameters:

  • dir: The directory containing the images. The default is ./images/.
  • img_format: The format of the images (e.g., jpg, png). The default is *.jpg.
  • resize: If set to True, the images are resized. The default is False.
  • img_width and img_height: The dimensions to resize the images to. The defaults are 224 for both.
  • gray_scale: If set to True, the images are converted to grayscale. The default is False.
  • output: The path to save the processed images. The default is ./results/all_images.npz.

Return:

The function returns two things:

  • A 4D numpy array of the images. The dimensions are (number of images, image height, image width, number of channels).
  • A list of labels corresponding to each image.

Main Function

The main function uses an argument parser to get the user’s preferences and then calls the get_images() function. It then prints the dimensions of the resulting numpy array.

Example Usage

The typical usage from command line would look like this:

python image_preprocessing.py --dir ./images/ --img_format *.jpg --resize True --img_width 128 --img_height 128 --gray_scale True --output ./results/resized_grayscale_images.npz

This command will read JPG images from the ./images/ directory, resize them to 128x128, convert them to grayscale, and save the result in the ./results/resized_grayscale_images.npz file.

In case you want to use it inside another Python script, here is how you would do it:

import argparse
from image_preprocessing import get_images

options = argparse.Namespace(
    dir='./images/',
    img_format='*.jpg',
    resize=True,
    img_width=128,
    img_height=128,
    gray_scale=True,
    output='./results/resized_grayscale_images.npz'
)

_images, labels = get_images(options)

Note that this script also creates a frames_labels.txt file in the same directory, which contains the labels of each image, in the same order as in the returned list.

This module is handy for preprocessing image data for machine learning tasks. You can adjust the parameters to suit your specific needs.


* This tutorial has been made with the help of ChatGPT 4.0.