AForge.NET

Testimonials
Features
Samples
Tools
Documentation
License
Downloads
Sources
Discussions
Partners
Projects
Members
Donate

AForge.NET Framework
2.2.5 version is available!

Blobs Processing

AForge.NET framework provides set of routines aimed for blobs processing - searching for separate objects and their manipulation.

Below is the list of some blobs processing tools and the result of their application to the below source image.

Source image
Source image

Blob Counter Base
The routine is a central routine for most blobs processing routines implemented in the framework and allows to count blobs, filter them, extract them, get their dimension, etc. As an example, below source code searches for blobs bigger than 5x5 pixel, orders them by size and then extract image of the biggest blob:

// create an instance of blob counter algorithm
BlobCounterBase bc = new BlobCounter( );
// set filtering options
bc.FilterBlobs = true;
bc.MinWidth  = 5;
bc.MinHeight = 5;
// set ordering options
bc.ObjectsOrder = ObjectsOrder.Size;
// process binary image
bc.ProcessImage( image );
Blob[] blobs = bc.GetObjectsInformation( );
// extract the biggest blob
if ( blobs.Length > 0 )
{
    bc.ExtractBlobsImage( image, blobs[0], true );
}
Blob counting and extraction

Extract Biggest Blob
In the case if extraction of the biggest blob is the only required task, then code may be simplified by using a dedicated filter, which just extracts the biggest blob:

// create filter
ExtractBiggestBlob filter = new ExtractBiggestBlob( );
// apply the filter
Bitmap biggestBlobsImage = filter.Apply( image );
Extract Biggest Blob filter

Blobs Filtering
The filter allows to filter certain blobs by their size by removing blobs, which are smaller than specified limit and/or bigger than specified limit.

// create filter
BlobsFiltering filter = new BlobsFiltering( );
// configure filter
filter.CoupledSizeFiltering = true;
filter.MinWidth  = 70;
filter.MinHeight = 70;
// apply the filter
filter.ApplyInPlace( image );
Blobs Filtering filter

Connected Components Labeling
The filter performs labeling of objects in binary image. It colors each separate object using different color.

// create filter
ConnectedComponentsLabeling filter =
    new ConnectedComponentsLabeling( );
// apply the filter
Bitmap newImage = filter.Apply( image );
Connected Components Labeling filter

Finding convex hull
The BlobCounterBase class provides methods, which allow getting blobs edge points - left/right or top/bottom edge points. These edge points may be used with different algorithm for finding blob's convex hull, quadrilateral corners, etc.

// process image with blob counter
BlobCounter blobCounter = new BlobCounter( );
blobCounter.ProcessImage( image );
Blob[] blobs = blobCounter.GetObjectsInformation( );

// create convex hull searching algorithm
GrahamConvexHull hullFinder = new GrahamConvexHull( );

// lock image to draw on it
BitmapData data = image.LockBits(
    new Rectangle( 0, 0, image.Width, image.Height ),
        ImageLockMode.ReadWrite, image.PixelFormat );
  
// process each blob
foreach ( Blob blob in blobs )
{
    List<IntPoint> leftPoints, rightPoints, edgePoints;

    // get blob's edge points
    blobCounter.GetBlobsLeftAndRightEdges( blob,
        out leftPoints, out rightPoints );

    edgePoints.AddRange( leftPoints );
    edgePoints.AddRange( rightPoints );

    // blob's convex hull
    List<IntPoint> hull = hullFinder.FindHull( edgePoints );

    Drawing.Polygon( data, hull, Color.Red );
}

image.UnlockBits( data );
Objects' convex hulls