How OpenCV’s blobFromImage works?


OpenCV's blobFromImage is a vital function in computer vision that allows the detection and extraction of blobs from an image. Understanding how this function works is crucial for various image processing tasks.

In this article, we delve into the inner workings of OpenCV's blobFromImage, exploring its parameters and their impact on blob extraction and gain comprehensive insights into this powerful tool for blob detection in computer vision applications.

What is OpenCV’s blobFromImage?

OpenCV's blobFromImage is a function that allows the detection and extraction of blobs from an image. Blobs are regions in an image that share common properties such as brightness or color. Blob detection is a critical step in many computer vision applications, including object tracking, motion analysis, and feature extraction.

The syntax for blobFromImage is as follows −

cv2.dnn.blobFromImage(image, scalefactor=None, size=None, mean=None, swapRB=None, crop=None, ddepth=None)
  • image − The input image to extract the blobs from.

  • scalefactor − A multiplier applied to the image data before blob extraction.

  • size − The desired spatial size of the output blobs.

  • mean − The mean values used for subtracting from the input image data.

  • swapRB − A boolean value indicating whether to swap the Red and Blue channels of the input image.

  • crop − A boolean value indicating whether to center crop the input image before blob extraction.

  • ddepth − The depth of the output blobs.

The image parameter is the only mandatory argument for the function, while the rest are optional. The scalefactor parameter is useful for scaling the image data before blob extraction, while size is used to specify the spatial size of the output blobs. mean is a set of values used for normalization of the input data, and swapRB is useful for changing the order of the color channels. The crop parameter specifies whether to center crop the input image before blob extraction, and ddepth is the depth of the output blobs.

How OpenCV’s blobFromImage works?

OpenCV's blobFromImage is a function used for blob detection and extraction from an input image. This function takes in an image as input and outputs a blob, which is a region of the image that shares common properties such as brightness or color.

Follow the steps given below to explore how blobFromImage works −

  • Import the required libraries and load an image as shown below −

import cv2
import numpy as np
# Load the input image
img = cv2.imread('example_image.jpg')
  • Define the parameters for blobFromImage. These parameters are used to fine-tune the blob extraction process. In this example, we will set the scale factor to 1.0, which means that the image data will not be scaled. We will also set the spatial size of the output blob to 224 x 224. Additionally, we will use mean subtraction to normalize the input image data, and we will swap the red and blue channels of the input image.

# Define the parameters for blobFromImage
params = {
   'scalefactor': 1.0,
   'size': (224, 224),
   'mean': (104.0, 177.0, 123.0),
   'swapRB': True
}
  • Use the blobFromImage function to extract the blob from the input image. We pass the image and the defined parameters to the function as arguments. The function returns a blob, which is a 4-dimensional array containing the extracted region of the image.

# Extract the blob from the input image using blobFromImage
blob = cv2.dnn.blobFromImage(img, **params)
  • Display the original image and the extracted blob using OpenCV's imshow function and reshape the blob to a 3-dimensional array to match the number of channels expected by cv2.imshow(). Then, we convert the blob image from the BGR color space to the RGB color space using cv2.cvtColor(). Finally, we display the extracted blob using cv2.imshow().

# Display the original image
cv2.imshow('Original Image', img)

# Reshape the blob to a 3-dimensional array
blob_reshaped = blob.reshape(blob.shape[2], blob.shape[3], blob.shape[1])
# Convert the blob to a valid image format
blob_image = cv2.cvtColor(blob_reshaped, cv2.COLOR_BGR2RGB)

# Display the extracted blob
cv2.imshow('Extracted Blob', blob_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • Below is the full code with the output −

Example

import cv2
import numpy as np

# Load the input image
img = cv2.imread('sample2.jpg')
# Define the parameters for blobFromImage
params = {
   'scalefactor': 1.0,
   'size': (224, 224),
   'mean': (104.0, 177.0, 123.0),
   'swapRB': True
}
# Extract the blob from the input image using blobFromImage
blob = cv2.dnn.blobFromImage(img, **params)

# Display the original image
cv2.imshow('Original Image', img)

# Reshape the blob to a 3-dimensional array
blob_reshaped = blob.reshape(blob.shape[2], blob.shape[3], blob.shape[1])

# Convert the blob to a valid image format
blob_image = cv2.cvtColor(blob_reshaped, cv2.COLOR_BGR2RGB)

# Display the extracted blob
cv2.imshow('Extracted Blob', blob_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

In the above example, we have demonstrated how to use OpenCV's blobFromImage function for blob detection and extraction. The function's parameters can be fine-tuned to adjust the blob extraction process to suit various image-processing tasks.

Note − The above example assumes that the input image is in the BGR format. If the image is in a different format, the mean values used for normalization may need to be adjusted accordingly.

Conclusion

In conclusion, the blobFromImage() function in OpenCV is a valuable tool in readying images for deep learning models. It enables the extraction of image features and their conversion into a format compatible with neural networks. By comprehending the functioning and syntax of blobFromImage(), we can enhance our deep learning workflows and attain improved outcomes.

Updated on: 24-Jul-2023

530 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements