This tutorial explains simple blob detection using OpenCV.
What is a Blob ?
A Blob is a group of connected pixels in an image that share some common property ( E.g grayscale value ). In the image above, the dark connected regions are blobs, and the goal of blob detection is to identify and mark these regions.
OpenCV provides a convenient way to detect blobs and filter them based on different characteristics. Let’s start with the simplest example
How does Blob detection work ?
SimpleBlobDetector, as the name implies, is based on a rather simple algorithm described below. The algorithm is controlled by parameters ( shown in bold below ) and has the following steps. Scroll down to know how the parameters are set.
- Thresholding : Convert the source images to several binary images by thresholding the source image with thresholds starting at minThreshold. These thresholds are incremented by thresholdStepuntil maxThreshold. So the first threshold is minThreshold, the second is minThreshold + thresholdStep, the third is minThreshold + 2 x thresholdStep, and so on.
- Grouping : In each binary image, connected white pixels are grouped together. Let’s call these binary blobs.
- Merging : The centers of the binary blobs in the binary images are computed, and blobs located closer than minDistBetweenBlobs are merged.
- Center & Radius Calculation : The centers and radii of the new merged blobs are computed and returned.
Filtering Blobs by Color, Size and Shape
The parameters for SimpleBlobDetector can be set to filter the type of blobs we want.
- By Color : [ Note : This feature appears to be broken. I checked the code, and it appears to have a logical error ] First you need to set filterByColor = 1. Set blobColor = 0 to select darker blobs, and blobColor = 255 for lighter blobs.
- By Size : You can filter the blobs based on size by setting the parameters filterByArea = 1, and appropriate values for minArea and maxArea. E.g. setting minArea = 100 will filter out all the blobs that have less then 100 pixels.
- By Shape : Now shape has three different parameters.
- Circularity : This just measures how close to a circle the blob is. E.g. a regular hexagon has higher circularity than say a square. To filter by circularity, set filterByCircularity = 1. Then set appropriate values for minCircularity and maxCircularity. Circularity is defined as
This means that a circle has a circularity of 1, circularity of a square is 0.785, and so on.
- Convexity : A picture is worth a thousand words. Convexity is defined as the (Area of the Blob / Area of it’s convex hull). Now, Convex Hull of a shape is the tightest convex shape that completely encloses the shape. To filter by convexity, set filterByConvexity = 1, followed by setting 0 ≤ minConvexity ≤ 1 and maxConvexity ( ≤ 1)
- Inertia Ratio : Don’t let this scare you. Mathematicians often use confusing words to describe something very simple. All you have to know is that this measures how elongated a shape is. E.g. for a circle, this value is 1, for an ellipse it is between 0 and 1, and for a line it is 0. To filter by inertia ratio, set filterByInertia = 1, and set 0 ≤ minInertiaRatio ≤ 1 and maxInertiaRatio (≤ 1 ) appropriately.
How to set SimpleBlobDetector params ?
Setting parameters for SimpleBlobDetector is easy. Here is an example
Setting of params for SimpleBlobDetector in OpenCV 2 is slightly different from OpenCV 3. In the code below we use the macro CV_MAJOR_VERSION to detect the version of OpenCV. In OpenCV 3, the SimpleBlobDetector::create method is used to create a smart pointer. The usage is shown in the code below.
Download code & example image
If you liked this article and would like to get more OpenCV tips, tricks, examples and tutorials in your email, sign up using the blue top bar. Some of my material is exclusive for email subscribers. My emails are meant for beginners and intermediate level OpenCV programmers who want to learn more.
Click OpenCV blob detector to download code (C++, Python, and example image) from GitHub. The image included in the download link can be used to test various parameters, as shown below.