0

マスク(オーバーレイ画像)を使用してpng画像からオーバーレイを削除するプログラムを書いています

画像1と2を持っている画像3を達成したい.

私はロックビットを使ってみたり、いろいろ試してみましたが、正しい計算ができないと思います

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。つまり、ブレンド カラー (2 番目の画像) と結果の色 (1 番目の画像) からベース カラー (3 番目の画像) を計算する方法を示しているだけです。 ):

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);
于 2013-11-22T13:37:55.530 に答える