Fahad Al-Harbi
Fahad Al-Harbi

Reputation: 21

How can I use Blob Detector

I'm trying to use SimpleBlobDetector in OpenCV 3 to detect blobs of heat in thermal images, for example people. Any simple code or example will be appreciated.

i tried`

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

#include <iostream>

#include "opencv2\features2d.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
if (argc != 2)
{
    cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
    return 0;
}

Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);   // Read the file

if (!image.data)                              // Check for invalid input
{
    cout << "Could not open or find the image" << std::endl;
    return 0;
}



// Set up the detector with default parameters.
//SimpleBlobDetector detector;
// Setup SimpleBlobDetector parameters.
SimpleBlobDetector::Params params;

// Change thresholds
params.minThreshold = 50;
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;

// Detect blobs.
std::vector<KeyPoint> keypoints;
cv::Ptr<cv::SimpleBlobDetector> detector = cv::SimpleBlobDetector::create(params);
//detector->detect(img, keypoints);
detector->detect(image, keypoints);
//params.detect(image, keypoints);

// 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(image, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

// Show blobs
imshow("keypoints", im_with_keypoints);
waitKey(0);

//namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.
//imshow("Display window", image);                   // Show our image inside it.

//waitKey(0);                                          // Wait for a keystroke in the window
//return 0;

} ` but it just returns the gray image unchanged.

Upvotes: 2

Views: 11535

Answers (1)

TomJ
TomJ

Reputation: 424

To detect blobs with OpenCV you need to:

  1. Instantiate a SimpleBlobDetector type
  2. Declare a vector of type KeyPoints
  3. Call SimpleBlobDetector::detect()

There is a brilliant tutorial online here (where I nicked the code from): https://www.learnopencv.com/blob-detection-using-opencv-python-c/

using namespace cv;
Mat im = imread( "blob.jpg", IMREAD_GRAYSCALE );

SimpleBlobDetector detector;

std::vector<KeyPoint> keypoints;
detector.detect( im, keypoints);

drawKeypoints( im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

imshow("keypoints", im_with_keypoints );// Show blobs
waitKey(0);

You can also adjust parameters to select blobs with specific attributes, it's all listed in the tutorial. I'd suggest having a play, to get a feel for how it works.

Upvotes: 3

Related Questions