Sitemap

IMAGE PREPROCESSING

8 min read8 hours ago

--

Press enter or click to view image in full size

Definition :

Image preprocessing is the process of converting raw images into a suitable format before feeding them into a Machine Learning or Deep Learning model.

Why is image preprocessing important?

Image preprocessing helps to:

  • Improve data quality.
  • Maintain consistency.
  • Reduce unwanted noise.
  • Make model training more effective.
  • Improve model performance.

Important Image Preprocessing Techniques

1.1 Image Resizing :

Definition :

Image resizing means changing all images into a fixed size.

Why do we resize images?

  • Maintain consistent input dimensions.
  • Reduce computational cost.
  • Make batch processing easier.
  • Ensure compatibility with deep learning models.

Python Implementation:

Press enter or click to view image in full size
Press enter or click to view image in full size

Important : OpenCV uses (width, height) in cv2.resize().

1.2 Color Space Conversion :

Definition :

An image can be represented using different color spaces depending on the application.

Color space conversion means converting an image from one color representation to another.

A. RGB (Red, Green, Blue)

  • RGB stands for Red, Green, and Blue.
  • Each pixel contains three values.
  • Each value represents the intensity of one color channel.

Example:

RGB = (255, 0, 0) This represents pure red.

B. Grayscale

Grayscale converts a color image into shades of gray and reduces each pixel to a single intensity value.

  • Pixel values range from 0 to 255.
  • 0 represents black.
  • 255 represents white.

C. HSV (Hue, Saturation, Value)

HSV converts a color image into three components:

  • H = Hue (color type).
  • S = Saturation (color intensity).
  • V = Value (brightness).

HSV is useful in color-based object detection.

Python Implementation :

Press enter or click to view image in full size
Press enter or click to view image in full size

Note : OpenCV reads images in BGR format by default, not RGB.

1.3 Image Normalization :

Definition :

Image normalization means converting pixel values into a small numerical range.

Normally, an 8-bit image contains pixel values between 0 and 255.

We can normalize these values between 0 and 1.

Press enter or click to view image in full size
Press enter or click to view image in full size

Python Implementation :

Press enter or click to view image in full size

Why do we normalize?

  1. Helps numerical stability.
  2. Provides a consistent input scale.
  3. Helps many neural networks train more effectively.

1.4 Noise Removal (Image Denoising) :

Definition :

Noise removal means reducing unwanted random variations in pixel intensity.

Noise can make it difficult for a model to identify important image features.

Examples of noise:

  • Gaussian noise.
  • Salt-and-pepper noise.
  • Speckle noise.

Noise removal can make it easier for a model to identify important features.

Important noise removal techniques :

A. Gaussian Blur

Gaussian blur uses a Gaussian filter to smooth the image.

It reduces Gaussian noise and small details.

Press enter or click to view image in full size

B. Histogram Equalization :

Definition :

Histogram equalization is a contrast enhancement technique that redistributes pixel intensity values to improve image contrast.

It is useful when an image has poor contrast or appears too dark.

Why do we use histogram equalization?

  • Improve image contrast.
  • Enhance visibility of details.
  • Make dark regions more visible.C. CLAHE(Contrast Limited Adaptive Histogram Equalization)
Press enter or click to view image in full size

C. CLAHE(Contrast Limited Adaptive Histogram Equalization) :

Definition:

  • CLAHE improves local contrast in different regions of an image while limiting excessive amplification of noise.
  • Unlike normal histogram equalization, CLAHE works on small image regions called tiles.

Python Implementation:

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

1.5 Image Augmentation :

Definition :

Image augmentation means creating modified versions of existing training images.

It increases the diversity of training data and helps reduce overfitting.

Common techniques :

  1. Rotation: Rotating the image by a certain angle.
  2. Flipping: Flipping the image horizontally or vertically.
  3. Zooming: Zooming in or zooming out.
  4. Shifting: Moving the image position.
  5. Cropping: Removing unwanted regions.
  6. Brightness adjustment: Changing image brightness.

Python Implementation :

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

Note : Apply augmentation mainly to training data. Do not blindly augment validation and test data.

1.6 Edge Detection :

Definition:

Edge detection identifies boundaries and sharp intensity changes in an image.

It helps identify:

  • Object boundaries.
  • Shapes.
  • Lines.
  • Structural information.

Popular techniques:

A. Sobel Operator

Sobel calculates horizontal and vertical image intensity gradients.

It uses two filters:

  • G_x — Horizontal gradient.
  • G_y — Vertical gradient.
Press enter or click to view image in full size

B. Canny Edge Detection

Get Bhargav’s stories in your inbox

Join Medium for free to get updates from this writer.

Canny is a popular multi-stage edge detection algorithm.

It generally performs :

  1. Noise reduction using Gaussian blur.
  2. Gradient calculation.
  3. Non-maximum suppression.
  4. Double thresholding.
  5. Edge tracking by hysteresis.

Python Implementation :

Press enter or click to view image in full size
Press enter or click to view image in full size

Why use Canny?

  • Detects object boundaries.
  • Identifies important shapes.
  • Helps in image segmentation and object detection.

2. Feature Extraction

Definition :

Feature extraction is the process of identifying useful information from an image and converting it into numerical features that a Machine Learning model can understand.

Examples of image features :

  • Edges.
  • Corners.
  • Shapes.
  • Textures.
  • Patterns.

2.1 Popular Feature Extraction Techniques

A. Flattening :

Definition :

Flattening is a technique used to convert multidimensional image data into a one-dimensional array.

Press enter or click to view image in full size

Python Implementation :

Press enter or click to view image in full size

Important : Flattening preserves pixel values but does not explicitly extract meaningful shapes, edges, or patterns. It can also produce a very large feature vector.

B. HOG Technique — Histogram of Orianted Gradients :

Definition :

HOG identifies edges, lines, and shapes present in an image and converts them into numerical features that a Machine Learning model can understand.

HOG is commonly used in traditional computer vision applications, especially pedestrian detection.

2.2 Working Process of HOG

HOG follows these steps :

  1. Input image.
  2. Gradient calculation.
  3. Gradient magnitude and orientation calculation.
  4. Divide image into cells.
  5. Calculate histogram of orientations.
  6. Block normalization.
  7. Generate final HOG feature vector.

Step 1 : Input Image

Read the input image and convert it into grayscale.

Why grayscale?

  • Reduces the number of channels.
  • Simplifies gradient calculation.
  • Reduces computational complexity.

Example :

Press enter or click to view image in full size

Step 2 : Gradient Calculation

Definition :

Gradient calculation identifies how quickly pixel intensity changes in horizontal and vertical directions.

We calculate two gradients :

  • G_x = Horizontal gradient.
  • G_y = Vertical gradient.
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

Step 3 : Calculate Gradient Magnitude and Orientation

After calculating horizontal and vertical gradients, we calculate two important values.

A. Gradient Magnitude

Gradient magnitude represents the strength of the intensity change.

Press enter or click to view image in full size

B. Gradient Orientation

Gradient orientation represents the direction of the intensity change.

Press enter or click to view image in full size

Important : HOG commonly uses unsigned orientations between 0° and 180°. Some implementations support signed orientations between 0° and 360°.

Step 4 : Divide Image into Cells

Definition :

Now divide the image into small regions called cells.

Each cell contains a fixed number of pixels.

Press enter or click to view image in full size

Step 5 : Calculate Histogram of Oriented Gradients

For every cell, we calculate a histogram based on gradient orientations.

A commonly used HOG configuration has:

  • Orientation range = 0° to 180°.
  • Number of orientation bins = 9.
Press enter or click to view image in full size

How does the histogram work?

  1. Calculate gradient magnitude for each pixel.
  2. Calculate gradient orientation for each pixel.
  3. Assign its magnitude to the corresponding orientation bins.
  4. If an angle lies between two bins, its magnitude is distributed between neighboring bins using interpolation.

Example :

Suppose a pixel has :

  • Gradient magnitude = 100.
  • Gradient orientation = 35°.

The angle lies between :

  • 20°–40°.
  • 40°–60°.

Its magnitude contributes to the corresponding neighboring bins through interpolation.

The histogram represents the distribution of edge directions in that cell.

Step 6 : Block Normalization

Definition :

Block normalization helps reduce the effects of changes in image contrast and illumination.

Block :

A block contains multiple neighboring cells.

A common HOG configuration :

  • Cell size = 8 × 8 pixels.
  • Block size = 2 × 2 cells.
  • Block stride = 1 cell.

Common technique : L2 Normalization

Press enter or click to view image in full size

Other normalization method : L2-Hys

L2-Hys performs :

  1. L2 normalization.
  2. Clipping large normalized values.
  3. Another L2 normalization.

It is commonly used in HOG descriptors.

Step 7 : Final HOG Feature Vector

After calculating histograms and normalizing blocks, we concatenate all block descriptors into one long feature vector.

Let’s calculate the feature vector size for a standard pedestrian detection window.

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

Therefore, a 64 × 128 image produces a HOG feature vector containing 3,780 values with these settings.

2.3 Python Implementation of HOG

We can use skimage.feature.hog() to extract HOG features.

Press enter or click to view image in full size
Press enter or click to view image in full size

--

--