IMAGE PREPROCESSING
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:
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 :
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.
Python Implementation :
Why do we normalize?
- Helps numerical stability.
- Provides a consistent input scale.
- 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.
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)
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:
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 :
- Rotation: Rotating the image by a certain angle.
- Flipping: Flipping the image horizontally or vertically.
- Zooming: Zooming in or zooming out.
- Shifting: Moving the image position.
- Cropping: Removing unwanted regions.
- Brightness adjustment: Changing image brightness.
Python Implementation :
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.
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 :
- Noise reduction using Gaussian blur.
- Gradient calculation.
- Non-maximum suppression.
- Double thresholding.
- Edge tracking by hysteresis.
Python Implementation :
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.
Python Implementation :
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 :
- Input image.
- Gradient calculation.
- Gradient magnitude and orientation calculation.
- Divide image into cells.
- Calculate histogram of orientations.
- Block normalization.
- 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 :
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.
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.
B. Gradient Orientation
Gradient orientation represents the direction of the intensity change.
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.
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.
How does the histogram work?
- Calculate gradient magnitude for each pixel.
- Calculate gradient orientation for each pixel.
- Assign its magnitude to the corresponding orientation bins.
- 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
Other normalization method : L2-Hys
L2-Hys performs :
- L2 normalization.
- Clipping large normalized values.
- 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.
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.







