2012-06-09 49 views
1

因此,我使用的是我在網上發現的某種變體將圖像轉換爲灰度。我希望能夠「淡化灰色」,即。逐漸淡化顏色,所以我可以從全色到部分有色到純灰色。ColorMatrix部分灰度

public static ColorMatrix ColorMatrixForGrayScale = new ColorMatrix(new[] { 
          new[] {.3f, .3f, .3f, 0, 0}, 
          new[] {.59f, .59f, .59f, 0, 0}, 
          new[] {.11f, .11f, .11f, 0, 0}, 
          new float[] {0, 0, 0, 1, 0}, 
          new float[] {0, 0, 0, 0, 1} }); 

    public static Bitmap MakeGrayscale(Image original, int percent = 100) 
    { 
     ColorMatrix matrix = GetMatrix(percent); // returns a variation of the above. 

     //create a blank bitmap the same size as original 
     var newBitmap = new Bitmap(original.Width, original.Height); 

     //get a graphics object from the new image 
     var g = System.Drawing.Graphics.FromImage(newBitmap); 

     //create some image attributes 
     var attributes = new ImageAttributes(); 

     //set the color matrix attribute 
     attributes.SetColorMatrix(matrix); 

     //draw the original image on the new image 
     //using the grayscale color matrix 
     g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 
      0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); 

     //dispose the Graphics object 
     g.Dispose(); 
     return newBitmap; 
    } 

所以問題是兩個部分。首先,我可以使用矩陣嗎?如果是的話,我有什麼不同,如果不是,那麼可行的方法是什麼?我正在考慮創建完整的灰階,然後將其與彩色圖像合併(如何)。

感謝

編輯:原始來源Switch on the code

+0

一個有趣的文章,你說的部分灰度是什麼意思?在區域被着色和其餘灰度(這就是所謂的colorkey)?不知道你的意思 – Sascha

+0

對不起,我沒有說清楚。想象一下,全是紅色的圖片。將其轉換爲灰度,並且全部是灰色的。部分轉換它,顏色會介於紅色和灰色之間。 – Ian

+0

我想減少飽和度,同時保持光線和色相一致? – Ian

回答