繁体   English   中英

通过平均组合灰度图像

[英]Combining greyscale images through averaging

我有一个程序使用getRed()getGreen()getBlue()工作正常。 我想看看我是否能找到一些其他算法也可以为灰度图像着色,无论它或多或少都准确无关紧要,它只是我正在研究比较方法和它们的准确性。 我决定使用类似的方法,只需使用getRed()用于图像1,2和3(而不是仅1)并且每种颜色除以3,理论上可以实现3的平均红色,绿色和蓝色值图像并从中创建新图像。

由此产生的图像非常奇怪; 颜色不均匀,即每个像素的随机颜色,你几乎无法弄清楚图像的任何特征,因为它是如此颗粒状,我想这是描述它的最佳方式。 它看起来像一张普通的照片,但到处都是色彩噪音。 只是想知道是否有人知道为什么会这样? 这不是我的意图,并且期望更加正常,与原始方法非常相似,但每种方法可能更柔和/更明亮。

任何人都知道为什么会这样吗? 它看起来不对。 除了标准的getRGB()方式之外,还有其他方法可以用来为图像着色吗?

BufferedImage combinedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
        for(int x = 0; x < combinedImage.getWidth(); x++)
            for(int y = 0; y < combinedImage.getHeight(); y++)
                combinedImage.setRGB(x, y, new Color(
                new Color((image1.getRGB(x, y) + image2.getRGB(x, y) + image3.getRGB(x, y))/3).getRed(), 
                new Color((image1.getRGB(x, y) + image2.getRGB(x, y) + image3.getRGB(x, y))/3).getGreen(), 
                new Color((image1.getRGB(x, y) + image2.getRGB(x, y) + image3.getRGB(x, y))/3).getBlue()).
                getRGB());

提前致谢

getRGB()返回像素的int表示,包括alpha通道。 由于int是4字节(32位),因此int将如下所示:

01010101 11111111 10101010 11111111
  Alpha     Red     Green    Blue

当你添加三个值时,这也是使用alpha通道,所以我想这会让你得到奇怪的结果。 此外,以这种方式添加int可能会导致其中一个通道出现“溢出”。 例如,如果一个像素的蓝色值为255而另一个像素的值为2,则总和的值为1表示绿色,值1表示蓝色。

要从int中提取每个颜色通道,您可以执行此操作。

red = (rgb >> 16) & 0xFF;
green = (rgb >>8 ) & 0xFF;
blue = (rgb) & 0xFF;

(这就是Color类在getRed(),getBlue()和getGreen()内部的作用http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/ awt / Color.java#Color.getRed%28%29

然后,组合这样的颜色:

combinedImage.setRGB(x, y, new Color(
((image1.getRGB(x, y) >> 16 & 0xFF) + (image1.getRGB(x, y) >> 16 & 0xFF) + (image1.getRGB(x, y) >> 16 & 0xFF))/3, 
((image1.getRGB(x, y) >> 8 & 0xFF) + (image1.getRGB(x, y) >> 8 & 0xFF) + (image1.getRGB(x, y) >> 8 & 0xFF))/3, 
((image1.getRGB(x, y) & 0xFF) + (image1.getRGB(x, y) & 0xFF) + (image1.getRGB(x, y) & 0xFF))/3).
getRGB());

或者每次为每个图像使用new Color(image1.getRGB(x, y)).getRed()

combinedImage.setRGB(x, y, new Color(
    (new Color(image1.getRGB(x, y)).getRed() + new Color(image2.getRGB(x, y)).getRed() + new Color(image3.getRGB(x, y)).getRed())/3, 
    (new Color(image1.getRGB(x, y)).getGreen() + new Color(image2.getRGB(x, y)).getGreen() + new Color(image3.getRGB(x, y)).getGreen())/3, 
    (new Color(image1.getRGB(x, y)).getBlue() + new Color(image2.getRGB(x, y)).getBlue() + new Color(image3.getRGB(x, y)).getBlue())/3).
    getRGB());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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