2013-11-22 25 views
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); 
        } 
       } 
      } 

enter image description here

回答

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); 
+1

偉大的答案。謝謝。我自己也做過類似的事情,但結果非常奇怪。我一定在某個地方犯了一個錯誤。最後我累了。再次感謝 –