如何计算JAVA中特定像素的灰度值?有没有配方?

时间:2015-01-19 12:02:11

标签: java

public void setGraphicalDefaults(MouseEvent e)
{
    x = e.getX();
    y = e.getY();

    Color c = new Color(img.getRGB(x,y));

    int red = (c.getRed());
    int green =(c.getGreen());
    int blue = (c.getBlue());
    System.out.println(red + " "+ green + " "+ blue);

}

从上面的代码我可以得到RGB值但是如何获得'Gray Scale'值?

1 个答案:

答案 0 :(得分:3)

有各种算法可将彩色图像转换为灰度。一个非常简单的解决方案是平均三个通道以获得灰度值:

int gray = (red  + green + blue) / 3

有关详细信息,请查看此文章:

http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/