Simple Face Detection for Windows Phone (2/5)

Simple Face Detection for Windows Phone (2/5)

Simple Face Detection for Windows Phone (2/5)

To explain simple face detection, I wrote 5 posts with each another technique in the face detection process. When you combine all 5 steps, you will be able to detect faces in pictures or video streams.

The steps described in these posts are:

This is the second post in which we will reduce the noise. This will make it easier to detect the areas that contain a face. From the last post, we see that there are a lot of white pixels, they have the color of skin but they are too small to be a face. When further filters are applied to the picture, it will take a lot of processing power to mark each small white area, so we need to get rid of those.

The easiest way to get rid of this noise, a shrink filter is a good solution. When the very small parts get even smaller, they will disappear. This is a very simple and effective solution, what we need to do for this, is just make each white pixel that touches a black pixel black. For small images running this filter once might be enough. When we have larger resolutions, it might be desirable to run it twice or more.

The code to do this is:

/// <summary>
/// This filter will shrink blobs in a black and white image
/// </summary>
/// <param name="pixels">The pixel array of the image</param>
/// <param name="width">The width of the image</param>
/// <param name="height">The height of the image</param>
/// <param name="connections">The number of black neighbours that is needed to make the pixel black</param>
public static void Shrink(int[] pixels, int width, int height, int connections)
{
    // A copy is made to prevent a chain reaction within the function
    int[] tmp = (int[])pixels.Clone();
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height - 1; y++)
            if (pixels[x + width * y] != 0)
            {
                byte change = 0;
				if (x < width && ((uint)tmp[x + 1 + width * y] == 0xFF000000 || (uint)tmp[x + 1 + width * y] == (uint)0))
                    change++;
				if (y < height && ((uint)tmp[x + width * (y + 1)] == 0xFF000000 || (uint)tmp[x + width * (y + 1)] == (uint)0))
                    change++;
				if (x > 0 && ((uint)tmp[x - 1 + width * y] == 0xFF000000 || (uint)tmp[x - 1 + width * y] == (uint)0))
                    change++;
				if (y > 0 && ((uint)tmp[x + width * (y - 1)] == 0xFF000000 || (uint)tmp[x + width * (y - 1)] == (uint)0))
                    change++;
                if (change >= connections)
                    pixels[x + width * y] = (int)(0xFF << 24);
            }
}
Black and white result from the YcBcR filter

Black and white result from the YcBcR filter

The black and white picture after shrinking the white parts

The black and white picture after shrinking the white parts

Download Download the whole demo project

4 thoughts on “Simple Face Detection for Windows Phone (2/5)”

Leave a Reply

Your email address will not be published. Required fields are marked *