// Function to remove small blobs from the binary image
IplImage* remove_small_objects( IplImage* img_in, int size )
{
IplImage* img_out = cvCloneImage( img_in ); // return image
CvMemStorage* storage = cvCreateMemStorage( 0 ); // container of retrieved contours
CvSeq* contours = NULL;
CvScalar black = CV_RGB( 0, 0, 0 ); // black color
CvScalar white = CV_RGB( 255, 255, 255 ); // white color
double area;
// find contours in binary image
cvFindContours( img_in, storage, &contours, sizeof( CvContour ), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE );
while( contours ) // loop over all the contours
{
area = cvContourArea( contours, CV_WHOLE_SEQ );
if( fabs( area ) <= size ) // if the area of the contour is less than threshold remove it
{
// draws the contours into a new image
cvDrawContours( img_out, contours, black, black, -1, CV_FILLED, 8 ); // removes white dots
}
/* else
{
cvDrawContours( img_out, contours, white, white, -1, CV_FILLED, 8 ); // fills in holes
}*/
contours = contours->h_next; // jump to the next contour
}
cvReleaseMemStorage( &storage );
return img_out;
}
Wednesday, May 6, 2009
OpenCV#4: Remove small blobs from binary image
Subscribe to:
Post Comments (Atom)
Nice, exactly what I needed!
ReplyDeleteThanks this help me a lot!
ReplyDelete