Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleBlobDetector in opencv does not detect anything

Tags:

c++

opencv

blob

I'm using OpenCV 3.1 to do some blob detection using SimpleBlobDetector but I'm having no luck and no tutorial has been able to solve this. My environment is XCode on x64.

I'm starting out with this image: enter image description here

Then I'm turning it into greyscale: enter image description here

Finally I turn it into a binary image and doing the blob detection on this: enter image description here

I've included "iostream" and "opencv2/opencv.hpp".

using namespace cv;
using namespace std;

Mat img_rgb;
Mat img_gray;
Mat img_keypoints;    
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create();

vector<KeyPoint> keypoints;

img_rgb = imread("summertriangle.jpg");

//Convert to greyscale
cvtColor(img_rgb, img_gray, CV_RGB2GRAY);

imshow("Grey Scale", img_gray);

// Start by creating the matrix that will allocate the new image
Mat img_bw(img_gray.size(), img_gray.type());

// Apply threshhold to convert into binary image and save to new matrix
threshold(img_gray, img_bw, 100, 255, THRESH_BINARY);

// Extract cordinates of blobs at their centroids, save to keypoints variable.
detector->detect(img_bw, keypoints);
cout << "The size of keypoints vector is: " << keypoints.size();

The keypoints vector is always empty. Nothing I've tried works.

like image 990
Carlos Granados Avatar asked Dec 02 '22 15:12

Carlos Granados


1 Answers

So I solved this, did not read the fine print on the docs. Thanks Dai for the heads up on the Params, made me give the docs a closer look.

Default values of parameters are tuned to extract dark circular blobs.

I had to simply do this when creating the SimpleBlobDetector object:

SimpleBlobDetector::Params params;

params.filterByArea = true;
params.minArea = 1;
params.maxArea = 1000;
params.filterByColor = true;
params.blobColor = 255;

Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);

This did it.

like image 167
Carlos Granados Avatar answered Dec 21 '22 06:12

Carlos Granados