原文:Three algorithms for converting color to grayscalehtml
如何轉換成彩***像灰度?若是每一個彩色像素由三重(R,G,B)(紅,綠,藍)的強度描述,
java
如何講(R,G,B)映射到一個單獨的數字做爲的灰度值?在GIMP圖像處理軟件有三種算法。web
lightness方法:是取最突觸顏色和最不突出顏色的平均值: (max(R, G, B) + min(R, G, B)) / 2.算法
average方法:最簡單取R,G,B的平均值:(R+G+B)/3 .ide
luminosity方法:是平均方法的一個更復雜的版本。它也是平均值,但它經過加權平均來解釋人類感知。咱們對綠色比其餘顏色更敏感,因此綠色加權最大。其計算公式爲亮度爲0.21 R +0.72 G +0.07 B.工具
下面向日葵圖片示例來自GIM文檔:this
lightness方法傾向於下降對比度。luminosity方法效果最好,若是你使用GIMP改變一個圖片從RGB到灰度圖片經過Image->ModeMenu,該方法是默認使用的方法。google
然而,一些圖像看起來更好地利用其餘算法之一,有時三種方法產生很是類似的結果。spa
更多關於顏色和灰度htm
附:
1. 調色板:https://www.google.com/design/spec/style/color.html#color-color-palette
2. GIMP(GNU Image Manipulation Program):開源圖片處理工具 開源免費跨平臺。
3. jscience 開源庫提供的灰階計算加權值常量文檔
4. Java封裝GIMP和Jscience提供的灰階計算方法,代碼示例:
/** * Compute method about grayscale from * <p/> * gimp website * http://docs.gimp.org/2.6/en/gimp-tool-desaturate.html * http://www.gimp.org/ * <p/> * http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/ * http://www.johndcook.com/blog/2009/08/24/more-on-colors-and-grayscale/ */ public static class GrayScaleUtil { interface GrayScaleCompute { int grayScale(int r, int g, int b); } public enum GrayScale { Lightness(new GrayScaleCompute() { @Override public int grayScale(int r, int g, int b) { return GrayScaleUtil.lightness(r, g, b); } }), Average(new GrayScaleCompute() { @Override public int grayScale(int r, int g, int b) { return GrayScaleUtil.average(r, g, b); } }), Luminosity(new GrayScaleCompute() { @Override public int grayScale(int r, int g, int b) { return GrayScaleUtil.luminosity(r, g, b); } }), BT709(new GrayScaleCompute() { @Override public int grayScale(int r, int g, int b) { return GrayScaleUtil.BT709(r, g, b); } }), RMY(new GrayScaleCompute() { @Override public int grayScale(int r, int g, int b) { return GrayScaleUtil.RMY(r, g, b); } }), Y(new GrayScaleCompute() { @Override public int grayScale(int r, int g, int b) { return GrayScaleUtil.Y(r, g, b); } }); private GrayScaleCompute gc; GrayScale(GrayScaleCompute gc) { this.gc = gc; } public int grayScale(int r, int g, int b) { return this.gc.grayScale(r, g, b); } } //Lightness = (max(r,g,b)+min(r,g,b))/2 public static int lightness(int r, int g, int b) { return (Math.max(Math.max(r, g), b) + Math.min(Math.min(r, g), b)) / 2; } // Average Brightness = (r+g+b)/3 public static int average(int r, int g, int b) { return (r + g + b) / 3; } //Luminosity =(0.21*r+0.72*g+0.07*b) public static int luminosity(int r, int g, int b) { return (int) (0.21 * r + 0.72 * g + 0.07 * b); } /** * Magic number about grayscale from http://jscience.org/experimental/javadoc/org/jscience/computing/ai/vision/GreyscaleFilter.html */ //BT709 Greyscale: Red: 0.2125 Green: 0.7154 Blue: 0.0721 public static int BT709(int r, int g, int b) { return (int) (0.2125 * r + 0.7154 * g + 0.0721 * b); } //RMY Greyscale: Red: 0.5 Green: 0.419 Blue: 0.081 public static int RMY(int r, int g, int b) { return (int) (0.5 * r + 0.419 * g + 0.081 * b); } //Y-Greyscale (YIQ/NTSC): Red: 0.299 Green: 0.587 Blue: 0.114 public static int Y(int r, int g, int b) { return (int) (0.299 * r + 0.587 * g + 0.114 * b); } }