1

simpleblobdetectorで見つけた一連のブロブの領域を見つけたいのですが、それを行う方法は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;
}
4

0 に答える 0