简体   繁体   English

通过平均组合灰度图像

[英]Combining greyscale images through averaging

I have a program which uses getRed() , getGreen() and getBlue() which works fine. 我有一个程序使用getRed()getGreen()getBlue()工作正常。 Im looking to see if I can find some other algorithms which also color greyscale images, whether it be more or less accurate it doesnt matter, its just something im looking into to compare the methods and their accuracy. 我想看看我是否能找到一些其他算法也可以为灰度图像着色,无论它或多或少都准确无关紧要,它只是我正在研究比较方法和它们的准确性。 I decided to use a similar method, just by using getRed() for image 1, 2 and 3 (as opposed to just 1) and dividing by 3 for each colour, in theory achieving the average red, green and blue values across the 3 images and creating a new image from it. 我决定使用类似的方法,只需使用getRed()用于图像1,2和3(而不是仅1)并且每种颜色除以3,理论上可以实现3的平均红色,绿色和蓝色值图像并从中创建新图像。

The resulting image is quite strange; 由此产生的图像非常奇怪; the colors arent uniform ie random colours for each pixel, you can barely make out any characteristics of the image as its so grainy, I suppose is the best way to describe it. 颜色不均匀,即每个像素的随机颜色,你几乎无法弄清楚图像的任何特征,因为它是如此颗粒状,我想这是描述它的最佳方式。 It looks like a normal photograph but with colour noise everywhere. 它看起来像一张普通的照片,但到处都是色彩噪音。 Just wondering if anyone knew why this is happening? 只是想知道是否有人知道为什么会这样? It wasnt my intentions and was expecting much more normal, quite similar to the original method but maybe more subdued/brighter colors for each. 这不是我的意图,并且期望更加正常,与原始方法非常相似,但每种方法可能更柔和/更明亮。

Anyone know why this is happening? 任何人都知道为什么会这样吗? It doesnt seem right. 它看起来不对。 Are there any other methods I could use to colour the images apart from the standard getRGB() ways? 除了标准的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());

Thanks in advance 提前致谢

getRGB() returns the int representations of a pixel, including the alpha channel. getRGB()返回像素的int表示,包括alpha通道。 Since a int is 4-byte (32 bits), this int will be like this: 由于int是4字节(32位),因此int将如下所示:

01010101 11111111 10101010 11111111
  Alpha     Red     Green    Blue

When you add the three values, this is using the alpha channel too, so I guess this is making you get strange results. 当你添加三个值时,这也是使用alpha通道,所以我想这会让你得到奇怪的结果。 Also, adding ints this way can cause "overflow" in one of the channels. 此外,以这种方式添加int可能会导致其中一个通道出现“溢出”。 For example, if one pixel has a blue value of 255 and another one has a value of 2, the sum will have a value 1 for green and value 1 for blue. 例如,如果一个像素的蓝色值为255而另一个像素的值为2,则总和的值为1表示绿色,值1表示蓝色。

To extract each color channel from the int you can do this. 要从int中提取每个颜色通道,您可以执行此操作。

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

(This is what Color class does internally inside getRed(), getBlue() and getGreen() http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/awt/Color.java#Color.getRed%28%29 ) (这就是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

Then, combine the colors like this: 然后,组合这样的颜色:

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

Or use new Color(image1.getRGB(x, y)).getRed() each time for each image. 或者每次为每个图像使用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