现在我有一张人物照片,背景是纯色的(注:这里的纯色是指单一颜色,因为通过相机拍照后,颜色RGB值变化很大,比如看起来是红色,GB的值就有很大的不同)。
我现在需要把照片里面的人物扣出来,然后替换背景。
大家有么好的思路。

解决方案 »

  1.   

     using (Bitmap bmp = new Bitmap(@""))
                {
                    Color pixelColor = bmp.GetPixel(1, 1);
                    //像素点颜色的 Alpha 值
                    byte alpha = pixelColor.A;
                    //颜色的 RED 分量值
                    byte red = pixelColor.R;
                    //颜色的 GREEN 分量值
                    byte green = pixelColor.G;
                    //颜色的 BLUE 分量值
                    byte blue = pixelColor.B;
                }
    分析图片的ARGB试试
      

  2.   

    给你写了一个,希望你满意:
    /// <summary>
    /// 获取经容差计算后的最终图,参考色彩点为源图坐标(1,1)
    /// </summary>
    /// <param name="SourceBitmap">源图</param>
    /// <param name="Tolerance">容差</param>
    /// <returns></returns>
    /// <res></res>
    private Bitmap GetFinalBitmap_Forli_007(Bitmap SourceBitmap, int Tolerance)
    {
    try {
    if (_tmpB != null)
    _tmpB.Dispose();
    //释放旧临时图内存
    Color SourceColor = SourceBitmap.GetPixel(1, 1);
    _tmpB = new Bitmap(SourceBitmap);
    System.Drawing.Imaging.BitmapData bmpDATA = new System.Drawing.Imaging.BitmapData();
    bmpDATA = _tmpB.LockBits(new Rectangle(0, 0, _tmpB.Width, _tmpB.Height), Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb);
    byte[] BTS = new byte[bmpDATA.Stride * bmpDATA.Height + 1];
    System.Runtime.InteropServices.Marshal.Copy(bmpDATA.Scan0, BTS, 0, BTS.Length - 1);
    for (int I = 0; I <= BTS.Length - 4; I += 4) {
    if (IsNearValue(BTS[I], BTS[I + 1], BTS[I + 2], SourceColor, Tolerance) == true) {
    BTS[I + 3] = 0;
    }
    }
    System.Runtime.InteropServices.Marshal.Copy(BTS, 0, bmpDATA.Scan0, BTS.Length - 1);
    _tmpB.UnlockBits(bmpDATA);
    return _tmpB;
    } catch {
    return null;
    }
    }
    private bool IsNearValue(int B, int G, int R, Color Cr, int Tol)
    {
    try {
    if (Math.Max(Cr.B, B) - Math.Min(Cr.B, B) <= Tol && Math.Max(Cr.G, G) - Math.Min(Cr.G, G) <= Tol && Math.Max(Cr.R, R) - Math.Min(Cr.R, R) <= Tol) {
    return true;
    } else {
    return false;
    }
    } catch {
    return false;
    }
    }
      

  3.   

    如果有这么方便的话,PS早就能实现了,现在PS还得手工去圈。
    楼主的头像是很好的例子,牙齿与耳朵很难处理的。