protected override unsafe void ProcessFilter(UnmanagedImage image)
{
// use blob counter to build objects map and filter them
blobCounter.ProcessImage(image);
int[] objectsMap = blobCounter.ObjectLabels;
// get image width and height
int width = image.Width;
int height = image.Height;
// do the job
byte* ptr = (byte*)image.ImageData.ToPointer();
if (image.PixelFormat == PixelFormat.Format8bppIndexed)
{
int offset = image.Stride - width;
for (int y = 0, p = 0; y < height; y++)
{
for (int x = 0; x < width; x++, ptr++, p++)
{
if (objectsMap[p] == 0)
*ptr = 0;
}
ptr += offset;
}
}
else
{
int pixelSize = Bitmap.GetPixelFormatSize(image.PixelFormat) / 8;
int offset = image.Stride - width * pixelSize;
for (int y = 0, p = 0; y < height; y++)
{
for (int x = 0; x < width; x++, ptr += pixelSize, p++)
{
if (objectsMap[p] == 0)
ptr[RGB.R] = ptr[RGB.G] = ptr[RGB.B] = 0;
}
ptr += offset;
}
}
}
}