Search code examples
c++opencvblobs

How to get extra information of blobs with SimpleBlobDetector?


@robot_sherrick answered me this question, this is a follow-up question for his answer.

cv::SimpleBlobDetector in Opencv 2.4 looks very exciting but I am not sure I can make it work for more detailed data extraction.

I have the following concerns:

  • if this only returns center of the blob, I can't have an entire, labelled Mat, can I?
  • how can I access the features of the detected blobs like area, convexity, color and so on?
  • can I display an exact segmentation with this? (like with say, waterfall)

Solution

  • So the code should look something like this:

    cv::Mat inputImg = imread(image_file_name, CV_LOAD_IMAGE_COLOR);   // Read a file
    cv::SimpleBlobDetector::Params params; 
    params.minDistBetweenBlobs = 10.0;  // minimum 10 pixels between blobs
    params.filterByArea = true;         // filter my blobs by area of blob
    params.minArea = 20.0;              // min 20 pixels squared
    params.maxArea = 500.0;             // max 500 pixels squared
    SimpleBlobDetector myBlobDetector(params);
    std::vector<cv::KeyPoint> myBlobs;
    myBlobDetector.detect(inputImg, myBlobs);
    

    If you then want to have these keypoints highlighted on your image:

    cv::Mat blobImg;    
    cv::drawKeypoints(inputImg, myBlobs, blobImg);
    cv::imshow("Blobs", blobImg);
    

    To access the info in the keypoints, you then just access each element like so:

    for(std::vector<cv::KeyPoint>::iterator blobIterator = myBlobs.begin(); blobIterator != myBlobs.end(); blobIterator++){
       std::cout << "size of blob is: " << blobIterator->size << std::endl;
       std::cout << "point is at: " << blobIterator->pt.x << " " << blobIterator->pt.y << std::endl;
    } 
    

    Note: this has not been compiled and may have typos.