c# - C# 中用于人脸检测的图像腐 eclipse

标签 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;
        }
    }
}

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

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

谢谢, 克里斯

最佳答案

一些跳出来的bugaboos。图像格式为 24bpp 但您正在读取字节。如果它是纯黑+白图像但左侧像素将在 x - 3 处,这可能有点工作。用 3 索引 x 也是明智的。

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

关于c# - C# 中用于人脸检测的图像腐 eclipse ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2845053/

相关文章:

machine-learning - 为什么 DQN 会为所有观察的 Action 空间 (2) 中的所有 Action 提供相似的值

c# - 取消异步 Web 服务调用

c# - SignalR 测试 - 如何在新版本的 SignalR for ASP.NET Core 2 中模拟组

c# - 服务在 StackOverflowException 上继续

c# - 在 asp.net core mvc 中创建实体列表

python - 如何使用 opencv 从皮肤图像中去除毛发?

python - 如何使用 cv2.im 显示大尺寸图像?

opencv - 静止图像中的虹膜检测

python - Python 中的 cv::Rect 功能?

opencv - 定向梯度对象检测的直方图