Sort blobs generated by cvBlobsLib by areas.

cvBlobsLib is an useful OpenCV 3rd party library to do blob analysis. However the blobs extracted are sorted based on their y coordinates and the author provided no interface to sort the blobs by other criteria such as area. To do so, you need to make some internal change. Add the following member function that sort the blobs based on their areas to the class CBlobResult.

    // sort blobs
    void SortBlobs()
    {
        struct myclass {
            bool operator() (CBlob* b1,CBlob* b2)
            {
                return (b1->GetArea() < b2->GetArea());
            }
        } blobComp;

        std::sort(m_blobs.begin(),m_blobs.end(),blobComp);
    }

If you want to sort the blobs based on their x coordinates, change the 7th line of above code to

return (b1->GetBoundingBox().x < b2->GetBoundingBox().x);

.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.