2010-05-16 4 views
6

C#에서 얼굴 검출을 구현하려고합니다. 나는 현재 그 안에 얼굴이있는 검은 색 + 흰색 윤곽선이있다 (Here). 그러나 나는 지금 탐지를 구현할 때 안정성을 향상시키기 위해 노이즈를 제거한 다음 이미지를 확장하려고합니다.이미지 C#에서 얼굴 검출을위한 침식

지금까지이 방법은 여기에 있습니다 :

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

나는 이미지를 침식 (소음 제거)하기 위해, 그것을 알고있는 것처럼, 우리가 주변 픽셀 것 있는지 확인하기 위해 각 픽셀을 확인해야 검정이고, 그렇다면 경계 픽셀이며, 코드를 유지할 필요가 없습니다. 그래서 내 코드가 그렇다고 생각합니다. 그래서 그것은 왜 작동하지 않는 것입니까?

어떤 도움이나 포인터가 크게

감사 감사하겠습니다, 크리스

답변

2

뛰어 bugaboos의 커플. 이미지 형식은 24bpp이지만 바이트를 읽는 중입니다. 순수한 검정 + 흰색 이미지이지만 왼쪽 픽셀이 x - 3 일 경우 작업이 가능합니다. x를 3으로 인덱싱하는 것도 현명합니다.

행의 색인 생성이 잘못되어 w를 곱하면 스트라이드를 곱해야합니다.

+0

답장을 보내 주셔서 감사합니다. 한스! 귀하의 제안 된 수정 사항을 포함시키지 만 아무 소용이없는 방법을 업데이트했습니다 :(이미지에 메서드를 실행하면 아무런 변화가 없습니다.) 내가 놓친 것이 있습니까? 다시 한 번 감사합니다. Chris –

+0

저는 도움이 필요합니다. 이 디버깅, 질문의 코드 업데이트, 샘플 이미지에 대한 링크 게시 및 성공 측정 방법 설명 –

+0

위의 코드를 게시 한 솔루션에 대한 솔루션을 만들었습니다. (pbInputImage) 및 버튼 (btErodeImage)을 사용합니다. 그림 상자의 이미지를 다음과 같이 설정했습니다. http://img192.imageshack.us/img192/5821/blackscale.png 결과가 예상됩니다. 무작위로 흰색 점이 많고 흰색의 주위에있는 일부는 다음과 같이 검정색으로 변경됩니다. http://img232.imageshack.us/img232/2917/ erosionresult.png 나는 이것이 당신이 기대하고 있었던 것이기를 바란다. 다시 감사합니다. Chris –

1

AForge.net 라이브러리 (http://code.google.com/p/aforge/)를 살펴보십시오. 이미지에 사용할 수있는 다양한 필터가 있습니다. 여기에서 이미지를 직접 수정할 수있는 예를 찾을 수도 있습니다

0

왜 openCV를 사용하지 않습니까? Dilate는 직접 함수이며 일반적으로 모든 이미지에 더 적합합니다.