protected override unsafe void ProcessFilter( UnmanagedImage image )
{
int width = image.Width;
int height = image.Height;
// 1 - invert the source image
Invert invertFilter = new Invert( );
UnmanagedImage invertedImage = invertFilter.Apply( image );
// 2 - use blob counter to find holes (they are white objects now on the inverted image)
BlobCounter blobCounter = new BlobCounter( );
blobCounter.ProcessImage( invertedImage );
Blob[] blobs = blobCounter.GetObjectsInformation( );
// 3 - check all blobs and determine which should be filtered
byte[] newObjectColors = new byte[blobs.Length + 1];
newObjectColors[0] = 255; // don't touch the objects, which have 0 ID
for ( int i = 0, n = blobs.Length; i < n; i++ )
{
Blob blob = blobs[i];
if ( ( blob.Rectangle.Left == 0 ) || ( blob.Rectangle.Top == 0 ) ||
( blob.Rectangle.Right == width ) || ( blob.Rectangle.Bottom == height ) )
{
newObjectColors[blob.ID] = 0;
}
else
{
if ( ( ( coupledSizeFiltering ) && ( blob.Rectangle.Width <= maxHoleWidth ) && ( blob.Rectangle.Height <= maxHoleHeight ) ) |
( ( !coupledSizeFiltering ) && ( ( blob.Rectangle.Width <= maxHoleWidth ) || ( blob.Rectangle.Height <= maxHoleHeight ) ) ) )
{
newObjectColors[blob.ID] = 255;
}
else
{
newObjectColors[blob.ID] = 0;
}
}
}
// 4 - process the source image image and fill holes
byte* ptr = (byte*) image.ImageData.ToPointer( );
int offset = image.Stride - width;
int[] objectLabels = blobCounter.ObjectLabels;
for ( int y = 0, i = 0; y < height; y++ )
{
for ( int x = 0; x < width; x++, i++, ptr++ )
{
*ptr = newObjectColors[objectLabels[i]];
}
ptr += offset;
}
}
}