问题描述
我正在寻找一种从图像中提取直方图数据的更快方法。
我目前正在使用这段代码,需要大约1200毫秒的6mpx JPEG图像:
I'm looking for a faster way to extract histogram data from an image.I'm currently using this piece of code that needs about 1200ms for a 6mpx JPEG image:
ImageReader imageReader = (ImageReader) iter.next();
imageReader.setInput(is);
BufferedImage image = imageReader.read(0);
int height = image.getHeight();
int width = image.getWidth();
Raster raster = image.getRaster();
int[][] bins = new int[3][256];
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
bins[0][raster.getSample(i, j, 0)]++;
bins[1][raster.getSample(i, j, 1)]++;
bins[2][raster.getSample(i, j, 2)]++;
}
您有什么建议吗?
推荐答案
你正在进行很多的 getSamples 方法调用,他们反过来做电话和电话等。
You're doing a lot of getSamples method calls and they're in turn doing calls and calls etc.
我经常使用图片和 获得速度的典型技巧是直接操作底层 int [ ] (在这种情况下,您的BufferedImage必须由int []支持。)
I work often with pictures and the typical trick to gain speed is to manipulate directly the underlying int[] (in this case your BufferedImage must be backed by an int[]).
访问 int [] 然而,做一个 getRGB 可能是巨大的。当我写巨大的时候,我的意思是两个数量级(尝试在OS X 10.4和 int [x] 上做 getRGB ,你会看到性能获得)。
The difference between accessing the int[] and doing, say, a getRGB can be gigantic. When I write gigantic, I mean by as much as two orders of magnitude (try doing a getRGB on OS X 10.4 vs int[x] and you'll see the perf gain).
此外,没有任何电话三次 getSamples 。我只是检索一个与你的ARGB像素相对应的int,然后使用bitshift来获取RGB波段(你每个R,G和B组件做一个直方图吗?)。
Also, there's no call three times getSamples. I'd simply retrieve one int corresponding to your ARGB pixel and then bitshift to get the RGB bands (you're doing one histogram per R, G and B component right?).
您可以通过以下操作访问像素数组:
You can gain access to the pixels array by doing something like this:
final int[] a = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
此外,您可以使用单个循环执行您想要的操作,循环遍历所有像素。
Also you can do what you want to do with a single loop, looping over all the pixels.
而不是:
for ( int x = 0; x < width; x++ ) {
for ( int y = 0; y < height; y++ ) {
....
你可以这样做:
for ( int p = 0; p < width*height; p++ ) {
现在,如果你想进入更奇怪的优化,不太可能证明你有效:
Now if you want to get into weirder optimizations, not as likely to prove effective you could:
-
使用循环展开(迭代超过600万像素是一种罕见的情况,它可能有所帮助)
use loop unrolling (iterating over 6 million pixels is one of the rare case where it may help)
反转循环:for(p = width * height - 1; p> = 0; p - )
invert the loop: for ( p = width*height - 1; p >= 0; p--)
这篇关于更快地从图像中提取直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!