在Java中将图像转换为灰度的错误亮度 [英] Wrong brightness converting image to grayscale in Java

查看:150
本文介绍了在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"));

这似乎有效,但问题是输出图像与灰度图像非常不同由gimp生成(参见下面的示例)。

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. 我可以控制图像是如何生成的吗?

  2. 我如何才能使结果与gimp结果更相似?

原始图片:

在Java中生成的灰度图像:

Gray scale image generated in Java:

Gimp生成的灰度图像(图像 - >模式 - >灰度):

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

BTW:我有一堆来自ffmpeg的图像(带灰色选项),它们就像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.

推荐答案

最后我写了 GrayscaleFilter 类实现 BufferedImageOp 界面。

Finally I've wrote GrayscaleFilter class implementing BufferedImageOp interface.

我跟着这个非常好的指南。

这是相关的代码片段:

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

这篇关于在Java中将图像转换为灰度的错误亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆