2010-10-12 59 views
0

我需要顏色平衡其中有18%灰色卡的圖像。用戶將該圖像加載到應用程序中,然後單擊灰色卡片。從這裏我需要一個算法來幫助顏色平衡圖像。我發現了一些文章,提到做矩陣變換,我試過了,但沒有成功(圖像洗掉或變成一種顏色或另一種顏色)。我現在的代碼是:如何在C中使用灰色卡進行顏色平衡#

 int sampleSize = 20; // The square around the user's click on the gray card 
     int rVal = 0, gVal = 0, bVal = 0; 
     int count = 0; 
     for (int x = 0; x < sampleSize - 1; x++) 
     { 
      for (int y = 0; y < sampleSize - 1; y++) 
      { 
       System.Drawing.Color c = grayCardArea.GetPixel(x, y); 
       if (c.R > 0) 
       { 
        rVal += c.R; 
        gVal += c.G; 
        bVal += c.B; 
        rs.Add(c.R); 
        count++; 
       } 
      } 
     } 
     grayCardGraphics.Dispose(); 

     int rAvg = 0, gAvg = 0, bAvg = 0; 
     rAvg = (int)Math.Round((decimal)rVal/(count)); 
     gAvg = (int)Math.Round((decimal)gVal/(count)); 
     bAvg = (int)Math.Round((decimal)bVal/(count)); 

     // 117 is a value I found online for the neutral gray color of the gray card 
     float rDiff = (117/(float)rAvg); 
     float gDiff = (117/(float)gAvg); 
     float bDiff = (117/(float)bAvg); 

     float[][] ptsArray = 
      { 
      new float[] {rDiff, 0, 0, 0, 0}, 
      new float[] {0, gDiff, 0, 0, 0}, 
      new float[] {0, 0, bDiff, 0, 0}, 
      new float[] {0, 0, 0, 1, 0}, 
      new float[] {0, 0, 0, .0f, 1} 
      }; 
     // Create a ColorMatrix 
     ColorMatrix clrMatrix = new ColorMatrix(ptsArray); 

     // Create ImageAttributes 
     ImageAttributes imgAttribs = new ImageAttributes(); 
     // Set color matrix 
     imgAttribs.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default); 
     // Draw image with ImageAttributes 
     outputImageGraphics.DrawImage(srcImage, new System.Drawing.Rectangle(0, 0, srcImage.Width, srcImage.Height), 
      0, 0, srcImage.Width, srcImage.Height, 
      GraphicsUnit.Pixel, imgAttribs); 

查看outputImage的保存副本顯示圖像的奇怪轉換。

任何幫助,非常感謝!

+0

這些伽瑪校正圖像?如果是這樣,你需要說明這一點。 – Gabe 2010-10-12 18:38:23

回答

2

我的公司Atalasoft有一個免費的.NET Imaging SDK,它有一個名爲LevelsCommand的類,我認爲它可以做你想做的事。

http://atalasoft.com/photofree

代碼是一樣的東西

AtalaImage img = new AtalaImage("filename"); 
LevelsCommand cmd = new LevelsCommand(/* ... */); // need to pass in leveling colors 
img = cmd.Apply(img).Image; 
img.Save("filename-new", new PngEncoder(), null); // or could be new JpegEncoder() or something else 

您應該使用的正確的文件名擴展顯示的格式。

1

你的第一個假設似乎是圖像被正確曝光,並使得讀取117,117,117的灰卡將解決問題。我的建議是單獨放置曝光並調整偏色。您可能會發現有用的不同顏色模型 - 例如HSL。灰卡的飽和度應始終爲零。

另外,我有一個示例灰色目標閱讀71,72,60。這有點溫暖。理由是更正確的讀數是67,67,67或(R + G + B)/ 3。因爲圖像有點曝光不足,所以我放棄了這種方式,但在不改變圖像密度的情況下實現了真正的中性。

我希望這可以幫助您獲得正確的顏色。

相關問題