Image Preprocessing

Description

The Image Preprocessing module manages and applies a sequence of preprocessing steps to image datasets using the ImagePreprocessor class.

Key Definitions:

  1. Preprocessing Step: A discrete action applied to an image dataset, such as

    noise reduction or normalization. Each step is implemented as a subclass of the abstract class StepBase.

  2. Pipeline: An ordered list of preprocessing steps applied sequentially to an image dataset.

Main Class

class ImagePreprocessor

Manages and processes a pipeline of image preprocessing steps.

The ImagePreprocessor class encapsulates a sequence of preprocessing operations defined as steps. Each step is a discrete preprocessing action, such as noise reduction or normalization, applied in sequence to an input dataset of images.

__init__(raise_step_process_exception: bool = True)

Initializes the ImagePreprocessor with an empty pipeline.

Parameters:

raise_step_process_exception (bool) – Determines whether exceptions during step processing are raised or logged.

set_default_datatype(datatype: tf.DType)

Sets the default datatype for pipeline steps.

Parameters:

datatype (tf.dtypes.DType) – The default output datatype for the pipeline steps.

set_pipe(pipeline: list[StepBase])

Sets the preprocessing pipeline with a deep copy of the provided steps.

Parameters:

pipeline (list[StepBase]) – List of preprocessing steps to be set in the pipeline.

pipe_append(step: StepBase)

Appends a new step to the pipeline.

Parameters:

step (StepBase) – The preprocessing step to be appended.

pipe_pop() StepBase

Removes and returns the last step from the pipeline.

Returns:

(StepBase) - The last step that was removed from the pipeline.

pipe_clear()

Clears all steps from the pipeline.

process(image_dataset: tf.data.Dataset) tf.data.Dataset

Applies each preprocessing step to the provided dataset.

If _raise_step_process_exception is True, exceptions occurring in a step will be caught and logged, and the process will return None. Otherwise, the process continues without exception handling.

Parameters:

image_dataset (tf.data.Dataset) – The TensorFlow dataset to be processed.

Returns:

(tf.data.Dataset) - The processed dataset after applying all the steps in the pipeline.

save_pipe_to_json(json_path: str)

Serializes the preprocessing pipeline to the specified JSON file.

Parameters:

json_path (str) – File path where the pipeline configuration will be saved.

load_pipe_from_json(json_path: str)

Loads and reconstructs a preprocessing pipeline from a JSON file.

Parameters:

json_path (str) – File path from which the pipeline configuration will be loaded.

load_randomized_pipe_from_json(json_path: str)

Loads and reconstructs a preprocessing pipeline from a JSON file with randomized parameters.

Parameters:

json_path (str) – File path from which the pipeline configuration will be loaded.

get_pipe_code_representation() str

Generates a text representation of the pipeline’s configuration.

Returns:

(str) A string representation of the pipeline in a code-like format.

Available Preprocessing Steps

The following preprocessing steps are available in the module:

Category

Preprocessing Steps

Histogram Equalization

AdaptiveHistogramEqualizer, GlobalHistogramEqualizer

Blurring & Smoothing

GaussianBlurFilter, MedianBlurFilter, BilateralFilter, AverageBlurFilter

Thresholding

OtsuThresholding, AdaptiveThresholding, BinaryThresholding, TruncatedThresholding, ThresholdToZero

Noise Reduction & Injection

NonLocalMeanDenoiser, GaussianNoiseInjector

Color Space Conversion

RGBToGrayscale, GrayscaleToRGB

Geometric Transformations

Rotator, Mirrorer, ShapeResizer, SquareShapePadder

Morphological Operations

ErosionFilter, DilationFilter, DilateErodeSequencer

Normalization

MinMaxNormalizer, StandardNormalizer, MeanNormalizer, LocalContrastNormalizer

Random Augmentations

RandomColorJitterer, RandomSharpening, RandomRotator, RandomFlipper, RandomCropper, RandomPerspectiveTransformer, RandomElasticTransformer

Miscellaneous

TypeCaster, Clipper, Scaler