【问题标题】:Combining greyscale images through averaging通过平均组合灰度图像
【发布时间】:2013-11-14 09:23:53
【问题描述】:

我有一个使用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());

提前致谢

【问题讨论】:

    标签: java image algorithm image-processing colors


    【解决方案1】:

    getRGB() 返回一个像素的 int 表示,包括 alpha 通道。由于一个 int 是 4 字节(32 位),这个 int 将是这样的:

    01010101 11111111 10101010 11111111
      Alpha     Red     Green    Blue
    

    当您添加三个值时,这也使用了 alpha 通道,所以我想这会让您得到奇怪的结果。此外,以这种方式添加整数可能会导致其中一个通道“溢出”。例如,如果一个像素的蓝色值为 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());
    

    【讨论】:

    • 嗯,这很有道理,谢谢。当我使用你发布的代码时,我得到一个错误,说 int 不能被取消引用,只能通过像以前那样做才能使它工作,对此有什么想法吗?感谢您的帮助
    • 非常感谢,我说我很感激。图像肯定是组合的,但是输出图像只是灰色的。我能想到的唯一原因是它没有超过 255,所以当除以 3 时,R、G、B 值通常相同,导致灰色?
    • @user2517280 这可能取决于输入图像。我用这些图像对其进行了测试:imgur.com/a/tQjQX 并且工作正常。
    • 当使用三个灰度图像作为输入时,我只收到一个平均灰度图像作为输出,虽然它在组合它们方面看起来不错,我希望为它们着色并希望得到平均值值,然后将其分配给红色、绿色和蓝色值,但这似乎不起作用,我认为理论上可以?
    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多