C#中人脸检测的图像侵蚀

时间:2010-05-16 19:01:32

标签: c# image-processing computer-vision

我正在尝试在C#中实现面部检测。我目前有一张黑色+白色的照片,上面有一张脸(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;
        }
    }
}

据我了解,为了侵蚀图像(并消除噪点),我们需要检查每个像素以查看它周围的像素是否为黑色,如果是,那么它是一个边框像素,我们不需要保持它,我相信我的代码确实如此,所以它超出了我为什么它不起作用。

非常感谢任何帮助或指示

谢谢, 克里斯

3 个答案:

答案 0 :(得分:2)

一些跳出来的bugaboos。图像格式为24bpp,但您正在读取字节。如果它是一个纯黑色+白色图像但是左边的像素在x - 3处可能会有效。如果将x加3,也是明智的。

索引行是错误的,你乘以w,你应该乘以步幅。

答案 1 :(得分:1)

你应该看看AForge.net图书馆(http://code.google.com/p/aforge/)。图像有许多不同的过滤器。在那里,您还可以找到如何直接修改图像的示例

答案 2 :(得分:0)

为什么不使用openCV?扩张是一种直接的功能,通常对所有图像都是最佳的..