Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法



[函数名称]

肤色检测函数SkinDetectProcess(WriteableBitmap src)

[算法说明]

  这个算法是一篇学术论文算法的实现,论文名字为“基于韧脸检测和颜色分析的红眼自动消除”(作者金秋明,王朔中),主要是采集并统计肤色像素,得到肤色像素在RGB颜色空间中的分布范围,以此作为像素是否为肤色像素的判断标准及约束条件。具体内容大家可以在网络中搜索,由统计结果得到的肤色范围如下公式2-(45),2-(46)所示:

Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法_第1张图片

[函数代码]
        /// 
        /// Skin detection.
        /// 
        /// The source image.
        /// 
        public static WriteableBitmap SkinDetectProcess(WriteableBitmap src)////36肤色检测 
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                byte[] tempMask = (byte[])temp.Clone();
                int R, G, B, S;
                double r, g, b;
                for (int i = 0; i < temp.Length; i += 4)
                {
                    B = tempMask[i];
                    G = tempMask[i + 1];
                    R = tempMask[i + 2];
                    S = R + G + B;
                    r = (double)R / (double)(R + G + B + 1.0);
                    g = (double)G / (double)(R + G + B + 1.0);
                    b = (double)B / (double)(R + G + B + 1.0);
                    if (S != 0)
                    {
                        if ((r > (double)(95.0 / (double)S)) && (r < 1 - (double)(90.0 / (double)S)) && (g > (double)(50.0 / (double)S)) && ((r - g) > (double)(30.0 / (double)S)) && (r - g < 0.2))
                        {
                            temp[i] = (byte)B;
                            temp[i+1] = (byte)G;
                            temp[i+2] = (byte)R;
                        }
                        else
                        {
                            temp[i] = 0;
                            temp[i + 1] = 0;
                            temp[i + 2] = 0;
                        }
                    }
                    else
                    {
                        temp[i] = 0;
                        temp[i + 1] = 0;
                        temp[i + 2] = 0;
                    }
                }
                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return srcImage;
            }
            else
            {
                return null;
            }
        }
Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法_第2张图片

你可能感兴趣的:(Win8,Metro(C#),数字图像处理)