| 1 | initial version |
Mat erase_mask = Mat::zeros(labels.rows, labels.cols, CV_8U);
for(int i = 1; i<num_components; ++i) //Iterate over labels
{
Mat mask = labels == i; //Keep only the label in question
int area = findNonZero(mask); //Find the area of the label
if(area < area_threshold) //Your condition
bitwise_or(erase_mask, mask, erase_mask); //Save the area to erase
}
labels.setTo(0, erase_mask); //Actually erase them
Or you can use connectedComponentsWithStats and it will give you an area value. That would be faster, especially if you used the bounding box as an ROI. More complicated though, so I just gave you this.