需要更快的方法来获取缓冲图像的每个像素的RGB值 [英] Need Faster way to get RGB value for each Pixel of a Buffered Image

查看:112
本文介绍了需要更快的方法来获取缓冲图像的每个像素的RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取 BufferedImage 的每个像素的RGB值的最快方法是什么?

What is the fastest way to get the RGB value of each pixel of a BufferedImage?

现在我使用两个获取循环的RGB值,如下面的代码所示,但由于嵌套循环总共运行479999次,所以获取这些值需要很长时间。我的形象。如果我使用16位图像,这个数字会更高!

Right now I am getting the RGB values using two for loops as shown in the code below, but it took too long to get those values as the nested loop runs a total of 479999 times for my image. If I use a 16-bit image this number would be even higher!

我需要更快的方法来获取像素值。

I need a faster way to get the pixel values.

以下是我目前正在尝试使用的代码:

Here is the code I am currently trying to work with:

BufferedImage bi=ImageIO.read(new File("C:\\images\\Sunset.jpg"));

int countloop=0;  

for (int x = 0; x <bi.getWidth(); x++) {
    for (int y = 0; y < bi.getHeight(); y++) {
        Color c = new Color(bi.getRGB(x, y));
        System.out.println("red=="+c.getRed()+" green=="+c.getGreen()+"    blue=="+c.getBlue()+"  countloop="+countloop++);                                                                                                                                                  
    }
}


推荐答案

我不知道这是否有帮助,我还没有测试过,但你可以这样得到rgb值:

I don't know if this might help and I haven't tested it yet but you can get the rgb values this way:

BufferedImage bi=ImageIO.read(new File("C:\\images\\Sunset.jpg"));
int[] pixel;

for (int y = 0; y < bi.getHeight(); y++) {
    for (int x = 0; x < bi.getWidth(); x++) {
        pixel = bi.getRaster().getPixel(x, y, new int[3]);
        System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (bi.getWidth() * y + x));
    }
}

如您所见,您无需初始化循环中的新颜色。我也按照onemasse的建议反转了宽度/高度循环,以便从我已有的数据中检索计数器。

As you can see you don't have to initialize a new Color inside the loop. I also inverted the width/height loops as suggested by onemasse to retrieve the counter from data I already have.

这篇关于需要更快的方法来获取缓冲图像的每个像素的RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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