Blob Detection OpenCV Python

opencv blob detection python

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.

If You have any kind of problem in OpenCV basic operations and OpenCV image manipulation. Read the blog about those functions as it will be used here in 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.

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.

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

# 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)

Python OpenCV Projects

Lanes Detection OpenCV Python

Self Driving Cars are the latest technology and it uses lanes detection for autonomous cars. Lane detection also uses Hough Transformation line detection in OpenCV. Learn How you can create a lanes detection program using OpenCV Python.

Background Subtraction OpenCV Python

How to remove background from a certain image or video. Learn Background subtraction OpenCV.

Motion Detection OpenCV Python

CCTV Cameras use Motion Detection to detect any kind of motion in the video frame. This helps in the security of the home and to detect any kind of small movement in the image from the previous image received in the form of the video source.

Mouse Events OpenCV Python

Learn How you can bind mouse events with the window in OpenCV.

Live Sketch OpenCV Python

This OpenCV live sketch program/app will create a real-time sketch of a person/object. It uses image manipulation techniques to Detect Edges in the video frame and image thresholding for making it look like a sketch.

Line Detection OpenCV Python

Learn how to detect lines in OpenCV using Hough lines transformation and probabilistic Hough lines.

Blob Detection OpenCV GitHub

Get full code on github – divshekhar

OpenCV Blob Detection Python Documentation

Learn more about the functions used for blob detection in official OpenCV Documentation.

Setting up the Blob Detector in OpenCV Python?

detector = cv.SimpleBlobDetector()

How to find key points from blob detector in OpenCV Python?

keypoints = detector.detect(img)

How to Draw on Detected Blobs in OpenCV Python?

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

What is Blob in OpenCV Python?

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

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *