1

I want to find the area of a bunch of blobs which I found with simpleblobdetector, and I know that a way to do that is to use connectedComponentsWithStats. I'm not quite sure how to use this function; could someone please give me an example or a link to one?

Here's my code so far:

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/opencv.hpp>
#include <vector>

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::vector;
using namespace cv;

using std::string;

int main() {
    string filename = "Grassland2.jpg";
    Mat im = imread(filename, IMREAD_GRAYSCALE);
    if (!im.data) {
        cout << "File not found" << endl;
        return -1;
    }
    Mat labelled;
    int total_pixels = im.rows*im.cols;
    int blob_area = -1;
    SimpleBlobDetector::Params params;
    params.minThreshold = 10;
    params.maxThreshold = 200;

    // Filter by Area.
    params.filterByArea = true;
    params.minArea = 400;

    // 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
    vector<KeyPoint> keypoints;
    // Set up detector with params
    Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);

    // Detect blobs
    detector->detect(im, 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(im, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
    //Is this correct? Also, where do I find the centroids?
    connectedComponentsWithStats(im, labelled, CC_STAT_AREA, centroids, 8, CV_32S)
    //I want to find out the area and set it to blob_area
    blob_area = CC_STAT_AREA;
    // Show blobs
    imshow("keypoints", im_with_keypoints);
    waitKey(0);
    return 0;
}
  • 2
    Does [this](http://stackoverflow.com/questions/29108270/opencv-2-4-10-bwlabel-connected-components/30265609#30265609) help? – beaker Jul 01 '16 at 17:27
  • Yes, thank you! I just wanted to know if connected components with stats figures out the blobs itself without me having to use simpleblobdetector. Please put your comment as an answer so I can accept it. – IgnisImperatrix Jul 02 '16 at 20:04
  • A question to compound your answer, if I want to find the area, do I simply access stats[0][4] and then so on? – IgnisImperatrix Jul 02 '16 at 20:15
  • Sorry, what I meant to write is what if I want to access the area of an unknown amount of components? Then I would simply cycle through the number of labels and use for example stats.at(i,CC_STAT_AREA), excluding 0 because that is the background, right? – IgnisImperatrix Jul 02 '16 at 20:24
  • Sorry for all the comments, I did a test and connected components doesn't seems to try to find its own blobs. In order to have it label the blobs I found using blob-detector, I would want to isolate the blobs and put them on an image without anything else but a blank background, right? – IgnisImperatrix Jul 02 '16 at 20:29
  • `connectedComponents` and `connectedComponentsWithStats` detect the components in a binary image *without* any other blob detector. If you look at the code in my answer, I generate the image and immediately call `connectedComponentsWithStats` to get the components. (Note that component `0` is the background.) If your image is not binary, you may need to do some thresholding first. – beaker Jul 02 '16 at 23:59

0 Answers0