Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a binary image which contain few blobs.

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

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.

thank you.

share|improve this question
    
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) –  Eddy_Em Jun 7 '13 at 18:38
    
Could you provide an example image? –  by0 Jun 17 '13 at 1:32

1 Answer 1

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.