9

I have a binary image which contain few blobs.

I want to remove blobs which are less than a certain area.

Can any one suggest me a way?

I am using Open-CV. i did dilation and erosion to get those blobs. so i need something different to remove the reaming blobs which are less than a certain area.

2
  • 1
    1. run conncomp labeling algorithm. 2. Throw out areas with size smaller than treshold. Another variant is N-times erosion and after - N-times dilation (but the higher N the worse restavration of blobs) Commented Jun 7, 2013 at 18:38
  • Could you provide an example image? Commented Jun 17, 2013 at 1:32

2 Answers 2

3

You can do something like this:

// your input binary image
// assuming that blob pixels have positive values, zero otherwise
Mat binary_image; 

// threashold specifying minimum area of a blob
double threshold = 100;

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
vector<int> small_blobs;
double contour_area;
Mat temp_image;

// find all contours in the binary image
binary_image.copyTo(temp_image);
findContours(temp_image, contours, hierarchy, CV_RETR_CCOMP,
                                                  CV_CHAIN_APPROX_SIMPLE);

// Find indices of contours whose area is less than `threshold` 
if ( !contours_all.empty()) {
    for (size_t i=0; i<contours.size(); ++i) {
        contour_area = contourArea(contours_all[i]) ;
        if ( contour_area < threshold)
            small_blobs.push_back(i);
    }
}

// fill-in all small contours with zeros
for (size_t i=0; i < small_blobs.size(); ++i) {
    drawContours(binary_image, contours, small_blobs[i], cv::Scalar(0), 
                                                 CV_FILLED, 8);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can somebody please explain me how did contours_all came in the if condition? and what it contains. I couldnt understand
0
            //src_gray is your image that we threshold
            threshold(src_gray, threshold_output, NULL, 255, THRESH_OTSU);
            /// Find contours
            findContours(threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
            /// Approximate contours
            vector<Rect> boundRect(contours.size());
            for (unsigned int i = 0; i < contours.size(); i++)
            {   //identify bounding box
                boundRect[i] = boundingRect(contours[i]);
            }
            for (unsigned int i = 0; i < contours.size(); i++)
            {

                if ((boundRect[i].area() < //enter your area here))
                {
                    src_gray(boundRect[i])=//set it to whatever value you want;
                }
            }

Well give this a try...

1 Comment

add more details here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.