Image Erosion for face detection in C#
Posted
by Chris Dobinson
on Stack Overflow
See other posts from Stack Overflow
or by Chris Dobinson
Published on 2010-05-16T19:01:32Z
Indexed on
2010/05/16
19:40 UTC
Read the original article
Hit count: 822
Hi,
I'm trying to implement face detection in C#. I currently have a black + white outline of a photo with a face within it (Here). However i'm now trying to remove the noise and then dilate the image in order to improve reliability when i implement the detection.
The method I have so far is here:
unsafe public Image Process(Image input)
{
Bitmap bmp = (Bitmap)input;
Bitmap bmpSrc = (Bitmap)input;
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
int stride2 = bmData.Stride * 2;
IntPtr Scan0 = bmData.Scan0;
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - bmp.Width * 3;
int nWidth = bmp.Width - 2;
int nHeight = bmp.Height - 2;
var w = bmp.Width;
var h = bmp.Height;
var rp = p;
var empty = CompareEmptyColor;
byte c, cm;
int i = 0;
// Erode every pixel
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++, i++)
{
// Middle pixel
cm = p[y * w + x];
if (cm == empty) { continue; }
// Row 0
// Left pixel
if (x - 2 > 0 && y - 2 > 0)
{
c = p[(y - 2) * w + (x - 2)];
if (c == empty) { continue; }
}
// Middle left pixel
if (x - 1 > 0 && y - 2 > 0)
{
c = p[(y - 2) * w + (x - 1)];
if (c == empty) { continue; }
}
if (y - 2 > 0)
{
c = p[(y - 2) * w + x];
if (c == empty) { continue; }
}
if (x + 1 < w && y - 2 > 0)
{
c = p[(y - 2) * w + (x + 1)];
if (c == empty) { continue; }
}
if (x + 2 < w && y - 2 > 0)
{
c = p[(y - 2) * w + (x + 2)];
if (c == empty) { continue; }
}
// Row 1
// Left pixel
if (x - 2 > 0 && y - 1 > 0)
{
c = p[(y - 1) * w + (x - 2)];
if (c == empty) { continue; }
}
if (x - 1 > 0 && y - 1 > 0)
{
c = p[(y - 1) * w + (x - 1)];
if (c == empty) { continue; }
}
if (y - 1 > 0)
{
c = p[(y - 1) * w + x];
if (c == empty) { continue; }
}
if (x + 1 < w && y - 1 > 0)
{
c = p[(y - 1) * w + (x + 1)];
if (c == empty) { continue; }
}
if (x + 2 < w && y - 1 > 0)
{
c = p[(y - 1) * w + (x + 2)];
if (c == empty) { continue; }
}
// Row 2
if (x - 2 > 0)
{
c = p[y * w + (x - 2)];
if (c == empty) { continue; }
}
if (x - 1 > 0)
{
c = p[y * w + (x - 1)];
if (c == empty) { continue; }
}
if (x + 1 < w)
{
c = p[y * w + (x + 1)];
if (c == empty) { continue; }
}
if (x + 2 < w)
{
c = p[y * w + (x + 2)];
if (c == empty) { continue; }
}
// Row 3
if (x - 2 > 0 && y + 1 < h)
{
c = p[(y + 1) * w + (x - 2)];
if (c == empty) { continue; }
}
if (x - 1 > 0 && y + 1 < h)
{
c = p[(y + 1) * w + (x - 1)];
if (c == empty) { continue; }
}
if (y + 1 < h)
{
c = p[(y + 1) * w + x];
if (c == empty) { continue; }
}
if (x + 1 < w && y + 1 < h)
{
c = p[(y + 1) * w + (x + 1)];
if (c == empty) { continue; }
}
if (x + 2 < w && y + 1 < h)
{
c = p[(y + 1) * w + (x + 2)];
if (c == empty) { continue; }
}
// Row 4
if (x - 2 > 0 && y + 2 < h)
{
c = p[(y + 2) * w + (x - 2)];
if (c == empty) { continue; }
}
if (x - 1 > 0 && y + 2 < h)
{
c = p[(y + 2) * w + (x - 1)];
if (c == empty) { continue; }
}
if (y + 2 < h)
{
c = p[(y + 2) * w + x];
if (c == empty) { continue; }
}
if (x + 1 < w && y + 2 < h)
{
c = p[(y + 2) * w + (x + 1)];
if (c == empty) { continue; }
}
if (x + 2 < w && y + 2 < h)
{
c = p[(y + 2) * w + (x + 2)];
if (c == empty) { continue; }
}
// If all neighboring pixels are processed
// it's clear that the current pixel is not a boundary pixel.
rp[i] = cm;
}
}
bmpSrc.UnlockBits(bmData);
return bmpSrc;
}
As I understand it, in order to erode the image (and remove the noise), we need to check each pixel to see if it's surrounding pixels are black, and if so, then it is a border pixel and we need not keep it, which i believe my code does, so it is beyond me why it doesn't work.
Any help or pointers would be greatly appreciated
Thanks, Chris
© Stack Overflow or respective owner