问题描述
我有一个TYPE_INT_BGR类型的BufferedImage。我需要与另一个BufferedImage进行逐像素比较,以计算两个图像之间的距离。我有一些有效的方法,但是速度很慢。我从参考图像中获得一个像素,将其分解为RGB字节:
I have a BufferedImage of type TYPE_INT_BGR. I need to do a pixel-by-pixel comparison to another BufferedImage to compute the "distance" between the two images. I have something that works, but is slow. I get a pixel from the "reference" image, break it up into RGB bytes with:
int pixel = referenceImage.getRGB(col, row);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
我将r / g / b值与候选图像的相应像素进行比较,然后求和
I compare the r/g/b values to the corresponding pixel of the candidate image, and sum up the squares the differences.
有没有更快的方法来进行这种比较?窥探JRE源,我看到BufferedImage.getRGB()实际上是对栅格中的RGB组成的值进行或运算,这对我来说是浪费的,因为我只是将其再次分解为字节。
Is there a faster way to do this kind of comparison? Peeking into the JRE source, I see that BufferedImage.getRGB() is actually ORing the constituent RGB values from the raster together, which is wasteful for my purposes, since I'm just breaking it down into bytes again.
我将直接尝试执行此操作,但我想知道是否没有更好的方法可以通过我可能错过的Java或第三方API来实现。
I'm going to try doing that directly, but I wonder if there is not a better way of doing this, either via a Java or 3rd party API that I might have missed.
推荐答案
从VolatileImage读取数据不会更快。使VolatileImages更快的原因是它们使用加速(VRAM)内存而不是系统(RAM)内存来绘制图像。但是,进行读取时,可能需要通过另一条总线访问内存,并且这些操作比系统内存要慢。
Reading data from a VolatileImage will not be faster. What makes VolatileImages "faster" is that they use accelerated (VRAM) memory instead of system (RAM) memory for drawing images. However, when doing a read, it may need to access memory over another bus and is slower than system memory for those operations.
比较BufferedImages最快的方法是使用整数比较的方式与您在帖子中讨论的方式相同,但是正如您提到的,出于性能原因,不能使用getRGB。您可以将一批像素放入一个数组中,但是总的来说,您应该只看一下Raster和DataBuffer即可获得性能。
The fastest way to compare BufferedImages would be to use integer comparisons the way you discuss in your post, but as you mention you can't use getRGB for performance reasons. You can get a batch of pixels into an array, but overall you should probably just peek into the Raster and DataBuffer for performance.
这篇关于比较两个BufferedImages之间的像素值的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!