views:

278

answers:

3

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:

           using System;
using System.Collections.Generic
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace ImageErosion
{
    public partial class Form1 : Form
    {
        public int CompareEmptyColor { get; set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void btErodeImage_Click(object sender, EventArgs e)
        {
            Image inputImage = pbInputImage.Image;

            Image result = Process(inputImage);

            pbInputImage.Image = result;
        }

        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.Format1bppIndexed);

            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 += 3, i++)
                {
                    // Middle pixel
                    cm = p[y * stride + x];
                    if (cm == empty) { continue; }

                    #region FirstRow
                    // Row 0
                    // Left pixel
                    if (x - 3 > 0 && y - 2 > 0)
                    {
                        c = p[(y - 2) * stride + (x - 3)];
                        if (c == empty) { continue; }
                    }
                    // Middle left pixel
                    if (x - 2 > 0 && y - 2 > 0)
                    {
                        c = p[(y - 2) * stride + (x - 2)];
                        if (c == empty) { continue; }
                    }
                    if (x - 1 > 0 && y - 2 > 0)
                    {
                        c = p[(y - 2) * stride + (x - 1)];
                        if (c == empty) { continue; }
                    }
                    if (y - 2 > 0)
                    {
                        c = p[(y - 2) * stride + x];
                        if (c == empty) { continue; }
                    }
                    if (x + 1 < w && y - 2 > 0)
                    {
                        c = p[(y - 2) * stride + (x + 1)];
                        if (c == empty) { continue; }
                    }
                    if (x + 2 < w && y - 2 > 0)
                    {
                        c = p[(y - 2) * stride + (x + 2)];
                        if (c == empty) { continue; }
                    }
                    if (x + 3 < w && y - 2 > 0)
                    {
                        c = p[(y - 2) * stride + (x + 3)];
                        if (c == empty) { continue; }
                    }
                    #endregion

                    #region SecondRow
                    // Row 1
                    // Left pixel 
                    if (x - 3 > 0 && y - 1 > 0)
                    {
                        c = p[(y - 1) * stride + (x - 3)];
                        if (c == empty) { continue; }
                    }
                    if (x - 2 > 0 && y - 1 > 0)
                    {
                        c = p[(y - 1) * stride + (x - 2)];
                        if (c == empty) { continue; }
                    }
                    if (x - 1 > 0 && y - 1 > 0)
                    {
                        c = p[(y - 1) * stride + (x - 1)];
                        if (c == empty) { continue; }
                    }
                    if (y - 1 > 0)
                    {
                        c = p[(y - 1) * stride + x];
                        if (c == empty) { continue; }
                    }
                    if (x + 1 < w && y - 1 > 0)
                    {
                        c = p[(y - 1) * stride + (x + 1)];
                        if (c == empty) { continue; }
                    }
                    if (x + 2 < w && y - 1 > 0)
                    {
                        c = p[(y - 1) * stride + (x + 2)];
                        if (c == empty) { continue; }
                    }
                    if (x + 3 < w && y - 1 > 0)
                    {
                        c = p[(y - 1) * stride + (x + 3)];
                        if (c == empty) { continue; }
                    }

                    #endregion

                    #region ThirdRow
                    // Row 2
                    if (x - 3 > 0)
                    {
                        c = p[y * stride + (x - 3)];
                        if (c == empty) { continue; }
                    }
                    if (x - 2 > 0)
                    {
                        c = p[y * stride + (x - 2)];
                        if (c == empty) { continue; }
                    }
                    if (x - 1 > 0)
                    {
                        c = p[y * stride + (x - 1)];
                        if (c == empty) { continue; }
                    }
                    if (x + 1 < w)
                    {
                        c = p[y * stride + (x + 1)];
                        if (c == empty) { continue; }
                    }
                    if (x + 2 < w)
                    {
                        c = p[y * stride + (x + 2)];
                        if (c == empty) { continue; }
                    }
                    if (x + 3 < w)
                    {
                        c = p[y * stride + (x + 3)];
                        if (c == empty) { continue; }
                    }
                    #endregion

                    #region FourthRow
                    // Row 3
                    if (x - 3 > 0 && y + 1 < h)
                    {
                        c = p[(y + 1) * stride + (x - 3)];
                        if (c == empty) { continue; }
                    }
                    if (x - 2 > 0 && y + 1 < h)
                    {
                        c = p[(y + 1) * stride + (x - 2)];
                        if (c == empty) { continue; }
                    }
                    if (x - 1 > 0 && y + 1 < h)
                    {
                        c = p[(y + 1) * stride + (x - 1)];
                        if (c == empty) { continue; }
                    }
                    if (y + 1 < h)
                    {
                        c = p[(y + 1) * stride + x];
                        if (c == empty) { continue; }
                    }
                    if (x + 1 < w && y + 1 < h)
                    {
                        c = p[(y + 1) * stride + (x + 1)];
                        if (c == empty) { continue; }
                    }
                    if (x + 2 < w && y + 1 < h)
                    {
                        c = p[(y + 1) * stride + (x + 2)];
                        if (c == empty) { continue; }
                    }
                    if (x + 3 < w && y + 1 < h)
                    {
                        c = p[(y + 1) * stride + (x + 3)];
                        if (c == empty) { continue; }
                    }
                    #endregion

                    #region FifthRow
                    // Row 4
                    if (x - 3 > 0 && y + 2 < h)
                    {
                        c = p[(y + 2) * stride + (x - 3)];
                        if (c == empty) { continue; }
                    }
                    if (x - 2 > 0 && y + 2 < h)
                    {
                        c = p[(y + 2) * stride + (x - 2)];
                        if (c == empty) { continue; }
                    }
                    if (x - 1 > 0 && y + 2 < h)
                    {
                        c = p[(y + 2) * stride + (x - 1)];
                        if (c == empty) { continue; }
                    }
                    if (y + 2 < h)
                    {
                        c = p[(y + 2) * stride + x];
                        if (c == empty) { continue; }
                    }
                    if (x + 1 < w && y + 2 < h)
                    {
                        c = p[(y + 2) * stride + (x + 1)];
                        if (c == empty) { continue; }
                    }
                    if (x + 2 < w && y + 2 < h)
                    {
                        c = p[(y + 2) * stride + (x + 2)];
                        if (c == empty) { continue; }
                    }
                    if (x + 3 < w && y + 2 < h)
                    {
                        c = p[(y + 2) * stride + (x + 3)];
                        if (c == empty) { continue; }
                    }
                    #endregion

                    // 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

+2  A: 

A couple of bugaboos that jump out. The image format is 24bpp but you are reading bytes. That could sorta work if it is a pure black+white image but the left pixel would be at x - 3. Indexing x by 3 would also be wise.

Indexing the row is wrong, you multiply by w, you should multiply by stride.

Hans Passant
Many thanks for the reply Hans! I've updated my method to include your suggested fixes but to no avail :( Upon running the method on the image it there are no changes. Have i missed something?Thanks again!Chris
Chris Dobinson
I dunno. If you want help debugging this, update the code in your question, post a link to a sample image and explain how you measure success.
Hans Passant
I've created a solution for the method, and posted that code above. The solution contains just 1 form with a picturebox (pbInputImage) and a button(btErodeImage). I've set the image of the picturebox to this: http://img192.imageshack.us/img192/5821/blackscale.pngThe results i would expect would be for quite alot of the random white dots, and some of those around the bulk of white, to change to black, similar to this: http://img232.imageshack.us/img232/2917/erosionresult.pngI hope this is what you were expecting?Many thanks again,Chris
Chris Dobinson
Well, you fixed the indexing, but you also changed the pixel format to 1bpp. Now the indexing is wrong again. Debug this with a sample image that has *one* pixel turned on. It should be filtered. Beware that bitmaps are upside-down. Set the x and y indexes to that one pixel to check your logic.
Hans Passant
I've finally got my head round this, i was getting confused with the co-ordinate of x on the bmp, and the positon of the bmp in memory. The code now works by using x * 3 (for 24bpp images) when looking in memory, but comparing by just x in test clauses.Thanks alot for your help hans :)
Chris Dobinson
+1  A: 

you should take a look at the AForge.net Library ( http://code.google.com/p/aforge/ ). There are many different filters available for images. There you can also find examples how you can modify images directly

A: 

Why dont you use openCV? Dilate is a direct function there and is generally more optimal for all images..

Ram Bhat