打开CV C ++ connectedComponentsWithStats和SimpleBlobDetector

时间:2016-07-01 05:22:09

标签: c++ opencv connected-components

我想找到我在simpleblobdetector找到的一堆blob的区域,我知道这样做的方法是使用connectedComponentsWithStats。我不太清楚如何使用这个功能;有人可以给我一个例子或一个链接吗?

到目前为止,这是我的代码:

#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;
}

0 个答案:

没有答案