C#图像处理之头发检测的方法


本文实例讲述了C#图像处理之头发检测的方法。分享给大家供大家参考。具体如下:

//发色检测(YCbCr颜色空间)
public Bitmap HairD(Bitmap a)
{
  Rectangle rect = new Rectangle(0, 0, a.Width, a.Height);
  System.Drawing.Imaging.BitmapData bmpData = a.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  int stride = bmpData.Stride;
  unsafe
  {
   byte* pIn = (byte*)bmpData.Scan0.ToPointer();
   byte* p;
   int R, G, B;
   double Cr, Cb,BrightV;
   for (int y = 0; y < a.Height; y++)
   {
     for (int x = 0; x < a.Width; x++)
     {
     p = pIn;
     R = p[2];
     G = p[1];
     B = p[0];
     Cb = 128 - 37.797 * R / 255 - 74.203 * G / 255 + 112 * B / 255;
     Cr = 128 + 112 * R / 255 - 93.768 * G / 255 - 18.214 * B / 255;
     BrightV = (R + G + B) / 3;
     if (!(Cb >= 115 && Cb <= 141 && Cr >= 115 && Cr <= 143 && (R < 35)))
     {
       pIn[0] = (byte)255;
       pIn[1] = (byte)255;
       pIn[2] = (byte)255;
     }
     pIn += 3;
     }
     pIn += stride - a.Width * 3;
   }
  }
  a.UnlockBits(bmpData);
  return a;
}
//定义最小值函数
public double Min(double a, double b, double c)
{
  double e = (a <= b ? a : b) <= c ? (a <= b ? a : b) : c;
  return e;
}
//定义最大值函数
public int Max(int a, int b, int c)
{
  int e = (a >= b ? a : b) <= c ? c : (a >= b ? a : b);
  return e;
}

结果图像效果:

希望本文所述对大家的C#程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3