Implementing Matlab's rgb2gray in Java

2019-06-23 23:28发布

I'm trying to implement Matlab's rgb2gray in Java according to http://www.mathworks.com/help/toolbox/images/ref/rgb2gray.html . I have the following code:

public BufferedImage convert(BufferedImage bi){
    int heightLimit = bi.getHeight();
    int widthLimit = bi.getWidth();
    BufferedImage converted = new BufferedImage(widthLimit, heightLimit,
        BufferedImage.TYPE_BYTE_GRAY);

    for(int height = 0; height < heightLimit; height++){
        for(int width = 0; width < widthLimit; width++){
            // Remove the alpha component
            Color c = new Color(bi.getRGB(width, height) & 0x00ffffff);
            // Normalize
            int newRed = (int) 0.2989f * c.getRed();
            int newGreen = (int) 0.5870f * c.getGreen();
            int newBlue = (int) 0.1140f * c.getBlue();
            int roOffset = newRed + newGreen + newBlue;
            converted.setRGB(width, height, roOffset);
        }
    }

    return converted;
}

Now, I do get a grayscale image but it is too dark compared to what I get from Matlab. AFAIK, the easiest way to turn an image to grayscale is have a BufferedImage of type TYPE_BYTE_GRAY and then just copy the pixels of a BufferedImage of TYPE_INT_(A)RGB. But even this method gives an image that is darker than Matlab's though grayscale decently enough. I've also looked into using RescaleOp. However, I don't find anyway in RescaleOp to set the grayness per pixel.

As an added test, I print out the image matrices produced by Java nad by Matlab. In Java, I get figures like 6316128 6250335 6118749 6118749 6250335 6447714 while in Matlab, I only get something like 116 117 119 120 119 115 (first six figures of both matrices).

How do I get an output similar to Matlab's?

1条回答
戒情不戒烟
2楼-- · 2019-06-23 23:38

The operator precedence in Java specifies that type-casting is higher than multiplication. You're casting your floating-point constants to 0, so I don't understand how you're getting a grayscale result at all. Easy to fix:

        int newRed = (int) (0.2989f * c.getRed());
        int newGreen = (int) (0.5870f * c.getGreen());
        int newBlue = (int) (0.1140f * c.getBlue());

I would also replace 0.2989 with 0.2990 as it appears to be a typo in the documentation.

查看更多
登录 后发表回答