【问题标题】:Removing image overlay using mask image使用蒙版图像删除图像覆盖
【发布时间】:2013-11-22 10:49:13
【问题描述】:

我正在编写一个程序,该程序使用掩码(覆盖图像)从 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);
                    }
                }
            }

【问题讨论】:

    标签: c# image graphics lockbits


    【解决方案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;
    }
    

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

    image3 = ExtractBaseImage((Bitmap)image1, (Bitmap)image2);
    

    【讨论】:

    • 很好的答案。谢谢。我自己也做过类似的事情,但结果很奇怪。我一定在某个地方犯了错误。最后我累了。再次感谢
    猜你喜欢
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2020-05-13
    • 2013-06-10
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多