Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How OpenCV’s blobFromImage works?
OpenCV's blobFromImage is a preprocessing function designed to prepare images for deep neural networks. Despite its name suggesting blob detection, it actually converts images into 4-dimensional arrays (blobs) that can be fed into DNN models for inference.
In this article, we'll explore how blobFromImage works, its parameters, and demonstrate its usage with practical examples.
What is OpenCV's blobFromImage?
blobFromImage is a function in OpenCV's DNN module that preprocesses images for deep learning models. It performs several operations including resizing, normalization, channel swapping, and mean subtraction to convert images into the standardized format required by neural networks.
Syntax
cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(0,0), mean=(0,0,0), swapRB=False, crop=False, ddepth=CV_32F)
Parameters
image ? Input image (required parameter)
scalefactor ? Multiplier for image values (default: 1.0). Commonly 1/255.0 for normalization
size ? Spatial size for output image (default: original size)
mean ? Scalar with mean values to subtract from channels (default: (0,0,0))
swapRB ? Flag to swap R and B channels (default: False)
crop ? Flag to crop image after resize (default: False)
ddepth ? Depth of output blob (default: CV_32F)
Basic Example
Let's create a simple example demonstrating blobFromImage functionality ?
import cv2
import numpy as np
# Create a sample image (3x3 RGB)
image = np.array([[[100, 150, 200],
[120, 170, 220],
[140, 190, 240]]], dtype=np.uint8)
print("Original image shape:", image.shape)
print("Original image:\n", image)
# Convert to blob with basic parameters
blob = cv2.dnn.blobFromImage(image, scalefactor=1.0/255.0, size=(2, 2))
print("\nBlob shape:", blob.shape)
print("Blob values:\n", blob)
Original image shape: (1, 3, 3) Original image: [[[100 150 200] [120 170 220] [140 190 240]]] Blob shape: (1, 3, 2, 2) Blob values: [[[[0.39215687 0.47058824] [0.54901963 0.74509805]] [[0.58823532 0.66666669] [0.74509805 1. ]] [[0.78431374 0.86274511] [0.94117647 1. ]]]]
Real Image Example
Here's how to use blobFromImage for preprocessing images for neural networks ?
import cv2
import numpy as np
# Create a sample colored image
height, width = 100, 100
image = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
print("Original image shape:", image.shape)
# Preprocess for neural network
blob = cv2.dnn.blobFromImage(
image,
scalefactor=1.0/255.0, # Normalize to [0,1]
size=(224, 224), # Resize to 224x224
mean=(0.485, 0.456, 0.406), # ImageNet mean
swapRB=True, # Convert BGR to RGB
crop=False
)
print("Blob shape:", blob.shape)
print("Blob data type:", blob.dtype)
print("Blob value range: [{:.3f}, {:.3f}]".format(blob.min(), blob.max()))
Understanding the Output
The blobFromImage function returns a 4D array with dimensions:
N ? Number of images (batch size, always 1 for single image)
C ? Number of channels (3 for RGB/BGR images)
H ? Height of the processed image
W ? Width of the processed image
Common Use Cases
| Parameter | Typical Value | Purpose |
|---|---|---|
| scalefactor | 1/255.0 | Normalize pixel values to [0,1] |
| size | (224,224), (416,416) | Match model input requirements |
| mean | ImageNet means | Dataset normalization |
| swapRB | True | Convert OpenCV BGR to RGB |
Conclusion
blobFromImage is essential for preprocessing images before feeding them to deep learning models. It handles resizing, normalization, and format conversion in a single function call, making it invaluable for computer vision applications.
