0

我正在编写一个程序,该程序使用掩码(覆盖图像)从 png 图像中删除覆盖

拥有图像 1 和 2 我想获得图像 3。

我尝试过使用 lockbits 并尝试了很多事情,但我认为我无法正确计算

rgbValues 是覆盖的字节数组,rgbValues2 是给定图像的字节数组。

for (int counter = 0; counter < rgbValues.Length; counter ++)
            {
                int x = (counter / 4) * 4;
                if (rgbValues[x + 3] != 0)
                {
                    if (rgbValues[x + 3] == rgbValues2[x + 3])
                    {
                        rgbValues2[counter] = 0;
                    }
                    else
                    {
                        float a1 = (float)rgbValues[counter];
                        float a2 = (float)rgbValues2[counter] ;
                        float b1 = (float)rgbValues[x + 3];
                        float b2 = (float)rgbValues2[x + 3];
                        rgbValues2[counter] = (byte)(2 * a2- a1);
                    }
                }
            }

在此处输入图像描述

4

1 回答 1

1

我已经用你的示例图像尝试过这个,尽管它们是由同一个大图像组成的并且看起来很有效。以下代码不是LockBits为了简单起见,它只是给你一个想法,即如何从混合颜色(在第二个图像中)和结果颜色(在第一个图像中)计算基色(在第三个图像中) ):

public Image ExtractBaseImage(Bitmap resultImage, Bitmap blendImage) {
    Bitmap bm = new Bitmap(resultImage.Width, resultImage.Height);
    for (int i = 0; i < resultImage.Width; i++) {
        for (int j = 0; j < resultImage.Height; j++) {
           Color resultColor = resultImage.GetPixel(i, j);
           Color blendColor = blendImage.GetPixel(i, j);
           if (blendColor.A == 0) bm.SetPixel(i, j, resultColor);
           else if(blendColor != resultColor){
              float opacity = blendColor.A / 255f;
              int r = Math.Max(0,Math.Min(255,(int) ((resultColor.R - (opacity) * blendColor.R) / (1-opacity))));
              int g = Math.Max(0,Math.Min(255,(int)((resultColor.G - (opacity) * blendColor.G) / (1-opacity))));
              int b = Math.Max(0,Math.Min(255,(int)((resultColor.B - (opacity) * blendColor.B) / (1-opacity))));                        
              bm.SetPixel(i,j,Color.FromArgb(r,g,b));
           }
        }
    }
    return bm;
}

用法:假设图像按照您对问题中发布的图像进行的编号,我们有image1, image2,image3变量:

image3 = ExtractBaseImage((Bitmap)image1, (Bitmap)image2);
于 2013-11-22T13:37:55.530 回答