Blob Detection using OpenCV
We offer you a brighter future with placement-ready courses - Start Now!!
In this article, we’ll understand what is a blob in image processing and how we can perform blob detection using the OpenCV functions. In image processing and computer vision applications, blob detected is a method to detect regions with different properties as compared to the rest of the image, such as increased brightness or color compared to the neighboring regions.
Defining blob and blob detection in image processing
The full form of a blob is a Binary Large object and it refers to a lump of pixels connected together to form a recognizable shape. The term large in blob refers to large objects in the image while the small binary objects are noise in the image. A blob is any large object in the image that differs from its surrounding on the basis of brightness or color.
A blob is generalized as a group of values of pixels that form an object distinguishable from its background. In image processing, blob analysis finds numerous uses such as analysing the features of an object in the image such as the number, area, position or the direction of the lumps.
Blob detection is carried out by referring to modules that detect points or regions of contrasting brightness of color in the image as compared to the surrounding region. It is executed after the color detection and reduction of noise in the image to find the required object.
Blob extraction: Blobs extraction is the process of separating the objects in the binary image. Blob is a group of pixels that are connected and we can check the connectivity of pixels by determining the pixels in its neighbourhood. This is also termed as connectivity of pixels. There are 8-connectivity and 4-connectivity available in OpenCV for blob extraction.
Blob representation: Blob representation is the process of converting blob into numbers that represent it. Once we have extracted the blobs from an image, we need to represent each blob. Each blob is denoted according to its characteristics and by comparing the features of each blob.
Blob classification: Blob classification is the step of segregating the extracted blobs on the basis of the filters applied.
Working of SimpleBlobDetector()
1. Thresholding: The first step to applying the function on any image is to convert the input image to a binary image through thresholding. The threshold function has the minimum value set as the minThreshold and the value for the function is incremented in steps.
The thresholdStep parameter sets the value for incrementation. The value of the minThreshold parameter is increased till it reaches the maxThreshold value. The minThreshold determines the first value for the threshold function and consecutive values are given by the addition of the thresholdStep parameter which keeps being incremented.
2. Grouping: The second step in the working of SimpleBlobDetector function is the grouping of binary pixels. The connected pixels are grouped together and labelled as one object. Components of the binary images are extracted by grouping connected components by making use of the findContours function. The center is identified for each of the extracted groups of pixels and the value for the centers is computed.
3. Merging: After the computation of the centers for each pixel group, the minimum distance between the centers of each group is calculated. Centers which are close to each other are merged and constitute a single blob. The groups are merged on the basis of the minimum distance between the group centers. The blobs that are located at closer distances to each other than the value computed through the minDistBetweenBlobs function are merged into a single blob.
4. Center and radius calculation: From the final merged groups formed, we compute the final centers for each blob. Taking the final computed centers, the radius for each blob formed is calculated and is returned.
Filtering blobs in an image
The SimpleBlobDetector function provides the option of setting filters for blob detection. The filters available for blob detection are:
1. By color: When the filter is set to color, the intensity value of the binary image at the center of the blob is compared with the blob color defined through the filter. The filterbyColor has to be set to 1. The value for blobColor should be equal to 0 if we have to select darker blobs in the image and the blobColor has to be set to 255 if we have to select light blobs in the image.
2. By size: The blobs can be filtered in the binary image on the basis of size. The filterByArea parameter has to be set to 1. We define the value for the area by using the minArea and maxArea functions.
3. By circularity: This filter measures the resemblance of a blob to a circle and filters the blobs which have greater circularity as compared to others. The filterByCircularity function computes the circularity for any blob and is set to 1.
The formula for circularity for a given blob is:
( 4*π*Area)/(perimeter*perimeter)
We need to define the minCircularity and maxCircularity values to filter any blobs on the basis of their circularity. The circularity value for a circle is 1. Similarly, we compute the circularity value for blobs for filtering.
4. Convexity: The formula for convexity for a given blob is:
Area of the Blob/ Area of convex hull
The filterByConvexity filter categorizes the blobs on the basis of their convexity and it is set to 1. Computation of convexity for any blob is on the basis of the values of the function minConvexity and maxConvexity. The minConvexity for any blob cannot be less than 0 and the maxConvexity for any blob cannot be greater than 1.
5. By Inertia Ratio: This filter measures the ratio of the minimum value for inertia to the maximum value of the inertia. In simple words, the filter measures the elongation of any blob. To filter the blobs on the basis of their inertia ratio, the filterByInertia calculates the inertia ratio and is set to 1. The minimum value for minInteriaRatio is 0 and the maximum value for maxInertiaRatio is 1.
Syntax
cv2.SimpleBlobDetector(src, minThreshold, maxThreshold, thresholdStep)
Parameters
- src: Input image or source image
- minThreshold: Minimum value for the threshold function
- maxThreshold: Maximum value for the threshold function
- thresholdStep: We increment the value of this parameter and add to the minThreshold parameter till the value of minThreshold is equal to the maxThreshold parameter
Implementation
#Importing OpenCV
import cv2
from google.colab.patches import cv2_imshow
#Importing numpy
import numpy as np
# Importing matplotlib.pyplot
import matplotlib.pyplot as plt
# Reading the image
img = cv2.imread(r'/content/paint.png')
# Initializing parameter setting using cv2.SimpleBlobDetector function
params = cv2.SimpleBlobDetector_Params()
# Filter by area (value for area here defines the pixel value)
params.filterByArea = True
params.minArea = 100
# Filter by circularity
params.filterByCircularity = True
params.minCircularity = 0.75
# Filter by convexity
params.filterByConvexity = True
params.minConvexity = 0.2
# Filter by inertia ratio
params.filterByInertia = True
params.minInertiaRatio = 0.01
# Creating a blob detector using the defined parameters
detector = cv2.SimpleBlobDetector_create(params)
# Detecting the blobs in the image
keypoints = detector.detect(img)
# Drawing the blobs that have been filtered with green on the image
blank = np.zeros((1, 1))
blobs = cv2.drawKeypoints(img, keypoints, blank, (0, 255, 0),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Setting the grid size
plt.figure(figsize=(20,20))
# Displaying the image
plt.subplot(121)
plt.title('Original')
plt.imshow(img, cmap='gray')
plt.subplot(122)
plt.title('Blobs')
plt.imshow(blobs)
plt.show()
Background Subtraction
Background subtraction in image processing is a method of removing the background from an image. Essentially, the foreground of the image is extracted and the background and foreground of the image are separated. Background separation is mainly used to generate a foreground mask. The background separation technique finds its application in detecting moving objects from a sequence of frames obtained from static cameras.
Implementation
#Importing OpenCV
import cv2
#Importing numpy
import numpy as np
# Importing matplotlib.pyplot
import matplotlib.pyplot as plt
# Reading the image
img = cv2.imread(r'/content/paint.png')
# Converting to grayscale using cv2.cvtColor
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Applying gaussian blur
img_blur = cv2.GaussianBlur(gray, (5, 5), 0)
# Difference image
difference = cv2.absdiff(gray, img_blur)
# Thresholding the difference image
difference_img = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY)
# Setting the grid size
plt.figure(figsize=(10,10))
# Displaying the image
plt.subplot(131)
plt.title('Original')
plt.imshow(img, cmap='gray')
plt.subplot(132)
plt.title('Blur')
plt.imshow(img_blur, cmap='gray')
plt.subplot(133)
plt.title('Difference')
plt.imshow(difference, cmap='gray')
Background subtraction using Subtractor MOG2
The subtractor MOG2 method available in OpenCV is more efficient than the manual background subtraction method. The Subtractor MOG2 has the advantage of being able to deal with frame history.
Syntax:
createBackgroundSubtractorMOG2 cv2.createBackgroundSubtractorMOG2 (history, varTheshold, detectShadow)
Parameters:
- history: This parameter refers to the number of the last frame (by default it is set to 120.)
- varThreshold: This value is utilized while evaluating the difference so we can extract the foreground and remove the background. A lower threshold value ensures that greater variation in the image will be observed while it will also be more sensitive to noise.
- detectShadow: It is the algorithm’s function that, if enabled, can eliminate the shadow.
Implementation
# Importing OpenCV
import cv2
# Reading the video from URL
video = cv2.VideoCapture(r"C:\Users\tushi\Downloads\PythonGeeks\pexels-mart-production-8447658.mp4")
# Initializing the background subtractor
background = cv2.createBackgroundSubtractorMOG2()
while(video.isOpened()):
ret, frame = video.read()
# Resizing the video
frame = cv2.resize(frame, None, None, fx=0.2, fy=0.2)
# Creating and applying the mask on each frame
mask = background.apply(frame)
cv2.imshow('frame', mask)
# Using waitKey to display each frame of the video for 1 ms
key = cv2.waitKey(1)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()
Conclusion
In this article, we thoroughly understood what blobs are in image processing. We learned about blob detection in computer vision and also understood the working of the function. We talked in detail about the SimpleBlobDetector function in OpenCV and learned about the filters for blob detection. Then e also saw the implementation of the SimpleBlobDetector function in OpenCV. We wrapped our discussion by understanding how to implement background subtraction manually and by using the subtractor MOG2 method.
