简体   繁体   中英

Wrong brightness converting image to grayscale in Java

I'm converting a image to gray scale in Java with the following code:

BufferedImage originalImage = ImageIO.read(new File("/home/david/input.bmp"));
BufferedImage grayImage = new BufferedImage(originalImage.getWidth()
                                          , originalImage.getHeight()
                                          , BufferedImage.TYPE_BYTE_GRAY);

ColorSpace gray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp colorConvert = new ColorConvertOp(gray, null);
colorConvert.filter(originalImage, grayImage);

ImageIO.write(grayImage, "bmp", new File("/home/david/output_java.bmp"));

That seems to work, but the problem is that the output image is very different from the gray scale image generated by gimp (see examples below).

  1. Can I control someway how is the image generated?
  2. How I can make the result more similar to the gimp result?

Original image:

彩色原图

Gray scale image generated in Java:

ColorConvertOp生成的灰度图像

Gray scale image generated in Gimp ( Image -> Mode -> Grayscale ):

Gimp中生成的灰度图像

BTW: I have a bunch of images coming from ffmpeg (with gray option) and they are like Gimp images so because of that I want my image in that way.

Find out the conversion formula used by Gimp. It probably takes some human color perception into account, while the Java implementation is mathematical (R+G+B)/ 3 .

Finally I've wrote GrayscaleFilter class implementing BufferedImageOp interface.

I've followed this really good guide about Java image processing.

This is the relevant code fragment:

public class GrayscaleFilter extends AbstractFilter
{
    public final static double[] METHOD_AVERAGE = {1.0/3.0, 1.0/3.0, 1.0/3.0};
    public final static double[] METHOD_GIMP_LUMINOSITY = {0.21, 0.71, 0.07};

    public GrayscaleFilter(final double[] rgb)
    {
        this(rgb[0], rgb[1], rgb[2]);
    }

    public BufferedImage filter(BufferedImage src, BufferedImage dest)
    {
        if (src.getType() == BufferedImage.TYPE_BYTE_GRAY)
        {
            dest = src;
            return dest;
        }

        if (dest == null)
            dest = createCompatibleDestImage(src, null);

        final int width = src.getWidth();
        final int height = src.getHeight();

        int[] inPixels = new int[width * height];
        GraphicsUtilities.getPixels(src, 0, 0, width, height, inPixels);
        byte[] outPixels = doFilter(inPixels);
        GraphicsUtilities.setPixels(dest, 0, 0, width, height, outPixels);
        return dest;
    }

    private byte[] doFilter(int[] inputPixels)
    {
        int red, green, blue;
        int i = 0;
        byte[] outPixels = new byte[inputPixels.length];

        for(int pixel : inputPixels)
        {
            // Obtengo valores originales
            red   = (pixel >> 16) & 0xFF;
            green = (pixel >> 8) & 0xFF;
            blue  = pixel & 0xFF;

            // Calculo valores nuevos
            outPixels[i++] = (byte)(
                 red   * red_part   +
                 green * green_part +
                 blue  * blue_part
            );
        }
        return outPixels;
    }

    public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM)
    {
        return new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_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