用Java实现Matlab的rgb2gray [英] Implementing Matlab's rgb2gray in Java

查看:184
本文介绍了用Java实现Matlab的rgb2gray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据在Java中实现Matlab的rgb2gray http://www.mathworks.com/help/toolbox/images/ref/rgb2gray.html .我有以下代码:

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

现在,我确实获得了灰度图像,但是与从Matlab获得的图像相比,它太暗了. AFAIK,将图像转换为灰度的最简单方法是拥有TYPE_BYTE_GRAY类型的BufferedImage,然后仅复制TYPE_INT_(A)RGB的BufferedImage的像素.但是即使是这种方法,其图像也比Matlab的要暗,尽管灰度足够好.我还研究了使用RescaleOp的方法.但是,无论如何我都没有在RescaleOp中设置每个像素的灰度.

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.

作为一项附加测试,我打印了Matlab Java nad产生的图像矩阵.在Java中,我得到的像6316128 6250335 6118749 6118749 6250335 6447714在Matlab中,我得到的只有116 117 119 120 119 115(两个矩阵的前六个数字).

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

如何获得类似于Matlab的输出?

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

推荐答案

Java中的运算符优先级指定类型转换高于乘法.您将浮点常量转换为0,所以我完全不了解您是如何获得灰度结果的.易于修复:

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

我也将0.2989替换为0.2990,因为它似乎是文档中的错字.

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

这篇关于用Java实现Matlab的rgb2gray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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