简体   繁体   English

如何将图像转换为0-255灰度图像

[英]How to convert an image to a 0-255 grey scale image

I want to convert an image to a gray scale image where pixel intensities are between 0-255 . 我想将图像转换为灰度图像,其中像素强度0-255之间

I was able to convert images to a gray scale images with the following Java method. 我能够使用以下Java方法将图像转换为灰度图像。

public void ConvertToGrayScale(BufferedImage bufImage, int ImgWidth, int ImgHeight) {

    for (int w = 0; w < ImgWidth; w++) {
        for (int h = 0; h < ImgHeight; h++) {

            Color color = new Color(bufImage.getRGB(w, h));
            int ColAvgVal = ((color.getRed() + color.getGreen() + color.getBlue()) / 3);
            Color avg = new Color(ColAvgVal, ColAvgVal, ColAvgVal);

            bufImage.setRGB(w, h, avg.getRGB());

            System.out.println(avg.getRGB());
        }
    }
}

"System.out.println(avg.getRGB());" “的System.out.println(avg.getRGB());” is used to see the pixel intensities but the all the grey levels are minus values and not between 0-255 . 用于查看像素强度,但所有灰度级都是负值,而不是0-255之间

Am I doing it wrong ? 我做错了吗? How would I convert an image to a gray scale image where pixel intensities are between 0-255 . 如何将图像转换为像素强度在0-255之间的灰度图像。

Thanks 谢谢

color.getRGB() does not return a value from 0..255, it returns an integer composited of your red, green and blue values, including the Alpha value. color.getRGB()不返回0..255中的值,它返回由红色,绿色和蓝色值组成的整数,包括Alpha值。 Presumably, this alpha value is 0xFF , which makes any combined color end up as 0xFFrrggbb , or, as you got, a huge negative number when written in decimals. 据推测,这个alpha值是0xFF ,这使得任何组合颜色最终都为0xFFrrggbb ,或者,当你得到时,用小数写成一个巨大的负数。

To see the "gray" level assigned, just check ColAvgVal . 要查看指定的“灰色”级别,只需选中ColAvgVal

Note that a better formula to convert between RGB and grayscale is to use the PAL/NTSC conversion : 请注意,更好的RGB和灰度转换公式是使用PAL / NTSC转换

gray = 0.299 * red + 0.587 * green + 0.114 * blue

because "full blue" should be darker in grayscale than "full red" and "full green". 因为“全蓝”的灰度应该比“全红”和“全绿”更暗。


Note: if you use this formula directly, watch out for floating point rounding errors . 注意:如果直接使用此公式,请注意浮点舍入错误 In theory, it should not return a value outside of 0..255 for gray ; 从理论上讲,它不应该返回0..255之外的gray值; in practice, it will . 在实践中, 它会 So test and clamp the result. 所以测试并钳制结果。

Another option which does not require testing-and-clamping per pixel, is to use an integer-only version: 另一个不需要每像素测试和钳位的选项是使用仅整数版本:

gray = (299 * red + 587 * green + 114 * blue)/1000;

which should work with only a very small rounding error. 这应该只用很小的舍入误差。

You can check this . 你可以检查一下 I hope it can help you. 我希望它可以帮到你。

You can check some differents methods like: 你可以检查一些不同的方法,如:

// The average grayscale method
private static BufferedImage avg(BufferedImage original) {

int alpha, red, green, blue;
int newPixel;

BufferedImage avg_gray = new BufferedImage(original.getWidth(), original.getHeight(), original.getType());
int[] avgLUT = new int[766];
for(int i=0; i<avgLUT.length; i++)
    avgLUT[i] = (int) (i / 3);

for(int i=0; i<original.getWidth(); i++) {
    for(int j=0; j<original.getHeight(); j++) {

        // Get pixels by R, G, B
        alpha = new Color(original.getRGB(i, j)).getAlpha();
        red = new Color(original.getRGB(i, j)).getRed();
        green = new Color(original.getRGB(i, j)).getGreen();
        blue = new Color(original.getRGB(i, j)).getBlue();

        newPixel = red + green + blue;
        newPixel = avgLUT[newPixel];
        // Return back to original format
        newPixel = colorToRGB(alpha, newPixel, newPixel, newPixel);

        // Write pixels into image
        avg_gray.setRGB(i, j, newPixel);

    }
}

return avg_gray;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM