DEV Community


Blob Detection OpenCV Python

divshekhar profile image Divyanshu Shekhar ・2 min read

What is blob? How to detect blobs in an image? In this blog, we are going to learn about blob detection in OpenCV Python.

Blob can be described as groups of connected pixels that all share a common property.

Let’s Code Blob Detection in OpenCV Python!

Blob Detection OpenCV Algorithm

  1. Read Image using OpenCV imread() function.
  2. Create or Set up the Simple Blob Detector.
  3. Input image in the created detector.
  4. Obtain key points on the image.
  5. Draw shapes on the Key points found on the image.
  6. If You have any kind of problem in OpenCV basic operations and 7. OpenCV image manipulation. Read the blog about those functions as it will be used herein blob detection.

Import packages

Import Computer Vision Python package for image manipulation.

NumPy for working with image matrix and matrix manipulation.

Matplotlib for data visualization and for displaying two or more images in the same window. This package is also used for scientific purposes.

Read Image

Img = cv.imread(‘./img.jpg’, cv.IMREAD_GRAYSCALE)

The image is read and converted to Grayscale.

Blob Detector OpenCV

detector = cv.SimpleBlobDetector()

This step will create the object/instance of Simple Blob Detector. We will use this created instance in blob detection.

OpenCV Blob Detection Keypoints

keypoints = detector.detect(img)

The detect() function from the detector instance takes the grayscale image as an argument and finds the key points for blob detection.

NumPy Black Screen

blank = np.zeros((1,1))

This will create a black screen on which the shapes will be drawn.

Color Blob Detection OpenCV Python

blobs = cv.drawKeypoints(img, keypoints, blank, (0,255,255), cv.DRAW_MATCHES_FLAGS_DEFAULT)

This will draw the shapes on the keypoints detected by the detector on the Grayscale image.

cv.DRAW_MATCHES_FLAGS_DEFAULT – This method draws detected blobs as red circles and ensures that the size of the circle corresponds to the size of the blob.

Read more... Blob Detection OpenCV Python


Editor guide