Blob Detection OpenCV Python

Share this blog with others!

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.

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 thesame window. This package is also used for scientific purposes.

Blob Detection OpenCV
Source | Divyanshu Shekhar- https://divyanshushekhar.com

Read the Image for Blob Detection

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 the blob detection.Shortcode

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.

Blob Detection Image Display

cv.imshow(‘Blobs’,blobs)

This step will show the image on which the blobs have been drawn.

Blob Detection OpenCV Python Code

Shortcode

# imports
import cv2
import numpy as np;

# Read image
img = cv.imread('./img.jpg', cv.IMREAD_GRAYSCALE)

# Set up the blob detector.
detector = cv2.SimpleBlobDetector()

# Detect blobs from the image.
keypoints = detector.detect(img)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS - This method draws detected blobs as red circles and ensures that the size of the circle corresponds to the size of the blob.
blobs = cv.drawKeypoints(img, keypoints, blank, (0,255,255), cv.DRAW_MATCHES_FLAGS_DEFAULT)

# Show keypoints
cv.imshow('Blobs',blobs)
cv2.waitKey(0)

0 0 votes
Article Rating

Share this blog with others!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top