2014-02-22 23 views
0

我已經編寫了平滑圖像的代碼,它使用3x3平均過濾器。但是似乎它不工作 畫面輸出幾乎是黑色的3x3 C#中的平均過濾器#

public void Smooth(Bitmap bmpInput){ 

     Bitmap temp; 

     Color c; 
     float sum = 0; 
     for (int i = 0; i < bmpInput.Width; i++) 
     { 
      for (int j = 0; j < bmpInput.Height; j++) 
      { 
       c = bmpInput.GetPixel(i, j); 
       byte gray = (byte)(.333 * c.R + .333 * c.G + .333 * c.B); 
       bmpInput.SetPixel(i, j, Color.FromArgb(gray, gray, gray)); 
      } 
     } 
     temp = bmpInput; 
     for (int i = 0; i <= bmpInput.Width - 3; i++) 
      for (int j = 0; j <= bmpInput.Height - 3; j++) 
      { 
       for (int x = i; x <= i + 2; x++) 
        for (int y = j; y <= j + 2; y++) 
        { 
         c = bmpInput.GetPixel(x,y); 
         sum = sum + c.R ; 
        } 
       int color = (int)Math.Round(sum/9,10); 
       temp.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color)); 
       sum = 0; 
      } 
     bmpInput = temp; 
    } 
+4

你能詳細說明它是如何工作的嗎? –

回答

1

變量temp仍然是指完全相同的位圖。您必須將temp分配給新的位圖圖像並使用它。或者,您可以將新像素值存儲在臨時陣列中,然後將此陣列的內容傳回圖像。

+0

非常感謝:D – NguyenDuc