简体   繁体   中英

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 .

I was able to convert images to a gray scale images with the following Java method.

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());" is used to see the pixel intensities but the all the grey levels are minus values and not between 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 .

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. 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.

To see the "gray" level assigned, just check ColAvgVal .

Note that a better formula to convert between RGB and grayscale is to use the PAL/NTSC conversion :

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 ; 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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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