【问题标题】:Image Erosion for face detection in C#C#中用于人脸检测的图像侵蚀
【发布时间】:2010-05-16 19:01:32
【问题描述】:

我正在尝试在 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;
        }
    }
}

据我了解,为了腐蚀图像(并去除噪声),我们需要检查每个像素,看看它周围的像素是否是黑色的,如果是,那么它是一个边界像素,我们不需要保留它,我相信我的代码会这样做,所以我无法理解为什么它不起作用。

任何帮助或指点将不胜感激

谢谢, 克里斯

【问题讨论】:

    标签: c# image-processing computer-vision


    【解决方案1】:

    几个跳出来的臭虫。图像格式为 24bpp,但您正在读取字节。如果它是纯黑白图像,但左像素将在 x - 3 处,这可能会起作用。将 x 索引为 3 也是明智的。

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

    【讨论】:

    • 非常感谢汉斯的回复!我已经更新了我的方法以包含您建议的修复但无济于事:(在图像上运行该方法后没有任何变化。我错过了什么吗?再次感谢!克里斯
    • 我不知道。如果您需要帮助调试此问题,请更新问题中的代码,发布指向示例图像的链接并说明您如何衡量成功。
    • 我已经为该方法创建了一个解决方案,并在上面发布了该代码。该解决方案仅包含 1 个带有图片框 (pbInputImage) 和按钮 (btErodeImage) 的表单。我已将图片框的图像设置为:img192.imageshack.us/img192/5821/blackscale.png 我期望的结果将是相当多的随机白点,其中一些在大部分白色周围变为黑色,类似于: img232.imageshack.us/img232/2917/erosionresult.png我希望这是你所期待的?非常感谢,克里斯
    • 好吧,您修复了索引,但您也将像素格式更改为 1bpp。现在索引又错了。使用打开了 一个 像素的示例图像对此进行调试。它应该被过滤。请注意位图是颠倒的。将 x 和 y 索引设置为该像素以检查您的逻辑。
    • 我终于明白了这一点,我对 bmp 上的 x 坐标和内存中 bmp 的位置感到困惑。现在,代码在内存中查找时使用 x * 3(对于 24bpp 图像),但在测试子句中仅使用 x 进行比较。非常感谢你的帮助汉斯:)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      你为什么不使用openCV?扩张是那里的直接功能,通常对所有图像都更优化..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-01
        • 1970-01-01
        • 2011-11-14
        • 1970-01-01
        • 1970-01-01
        • 2010-11-10
        • 2019-02-20
        • 2021-06-26
        相关资源
        最近更新 更多