Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OpenCV SimpleBlobDetector

Tags:

opencv

Instead of any additional blob detection library, how do I use the cv::SimpleBlobDetector class and its function detectblobs()?

like image 394
user1036908 Avatar asked Nov 10 '11 08:11

user1036908


People also ask

What is SimpleBlobDetector?

simpleBlobDetector implements a simple algorithm for extracting blobs an Image object. A blob is a region in an image that differs in properties (e.g. brightness, color) from surrounding regions.

What is blob detection in OpenCV?

Blob stands for Binary Large Object and refers to the connected pixel in the binary image. The term "Large" focuses on the object of a specific size, and that other "small" binary objects are usually noise.

How do you detect an ellipse in OpenCV?

To identify circles, ellipses, or in general, any shape in which the pixels are connected we use the SimpleBlobDetector() function of OpenCV.

What is a blob in image?

A blob refers to a lump. Blob analysis is image processing's most basic method for analyzing the shape features of an object, such as the presence, number, area, position, length, and direction of lumps.


1 Answers

Python: Reads image blob.jpg and performs blob detection with different parameters.

#!/usr/bin/python  # Standard imports import cv2 import numpy as np;  # Read image im = cv2.imread("blob.jpg")  # Setup SimpleBlobDetector parameters. params = cv2.SimpleBlobDetector_Params()  # Change thresholds params.minThreshold = 10 params.maxThreshold = 200   # Filter by Area. params.filterByArea = True params.minArea = 1500  # Filter by Circularity params.filterByCircularity = True params.minCircularity = 0.1  # Filter by Convexity params.filterByConvexity = True params.minConvexity = 0.87  # Filter by Inertia params.filterByInertia = True params.minInertiaRatio = 0.01  # Create a detector with the parameters # OLD: detector = cv2.SimpleBlobDetector(params) detector = cv2.SimpleBlobDetector_create(params)   # Detect blobs. keypoints = detector.detect(im)  # Draw detected blobs as red circles. # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures # the size of the circle corresponds to the size of blob  im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)  # Show blobs cv2.imshow("Keypoints", im_with_keypoints) cv2.waitKey(0) 

C++: Reads image blob.jpg and performs blob detection with different parameters.

#include "opencv2/opencv.hpp"  using namespace cv; using namespace std;  int main(int argc, char** argv) {     // Read image #if CV_MAJOR_VERSION < 3   // If you are using OpenCV 2     Mat im = imread("blob.jpg", CV_LOAD_IMAGE_GRAYSCALE); #else     Mat im = imread("blob.jpg", IMREAD_GRAYSCALE); #endif      // Setup SimpleBlobDetector parameters.     SimpleBlobDetector::Params params;      // Change thresholds     params.minThreshold = 10;     params.maxThreshold = 200;      // Filter by Area.     params.filterByArea = true;     params.minArea = 1500;      // Filter by Circularity     params.filterByCircularity = true;     params.minCircularity = 0.1;      // Filter by Convexity     params.filterByConvexity = true;     params.minConvexity = 0.87;      // Filter by Inertia     params.filterByInertia = true;     params.minInertiaRatio = 0.01;      // Storage for blobs     std::vector<KeyPoint> keypoints;  #if CV_MAJOR_VERSION < 3   // If you are using OpenCV 2      // Set up detector with params     SimpleBlobDetector detector(params);      // Detect blobs     detector.detect(im, keypoints); #else       // Set up detector with params     Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);      // Detect blobs     detector->detect(im, keypoints); #endif       // Draw detected blobs as red circles.     // DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures     // the size of the circle corresponds to the size of blob      Mat im_with_keypoints;     drawKeypoints(im, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);      // Show blobs     imshow("keypoints", im_with_keypoints);     waitKey(0); } 

The answer has been copied from this tutorial I wrote at LearnOpenCV.com explaining various parameters of SimpleBlobDetector. You can find additional details about the parameters in the tutorial.

like image 139
Satya Mallick Avatar answered Sep 18 '22 12:09

Satya Mallick