如何实现最快的高斯模糊算法?
我将在Java中实现它,因此排除了GPU解决方案。我的应用程序planetGenesis是跨平台的,所以我不想使用JNI。
如何实现最快的高斯模糊算法?
我将在Java中实现它,因此排除了GPU解决方案。我的应用程序planetGenesis是跨平台的,所以我不想使用JNI。
你应该利用高斯核是可分离的事实,也就是说你可以将二维卷积表示为两个一维卷积的组合。
如果滤波器很大,使用空间域中的卷积等效于频率(傅里叶)域中的乘法可能更有意义。这意味着您可以对图像和滤波器进行傅里叶变换,相乘得到(复杂的)结果,然后进行逆傅里叶变换。 FFT(快速傅里叶变换)的复杂度为O(n log n),而卷积的复杂度为O(n^2)。此外,如果您需要使用相同的滤波器模糊多个图像,则只需对滤波器进行一次FFT即可。
如果您决定使用FFT,则FFTW库是一个不错的选择。FFTW library
数学精英可能已经知道这个,但对于其他人来说..
由于高斯函数的一个美好数学特性,您可以通过先对图像的每行运行1D高斯模糊,然后对每列运行1D模糊,快速地使2D图像变得模糊。
我发现了Quasimondo : 孵化器 : 处理 : 快速高斯模糊。该方法包含许多近似值,例如使用整数和查找表而不是浮点数和浮点数除法。我不知道在现代Java代码中这会有多大的加速。
C#中的快速高斯模糊算法声称具有一些很酷的优化。
此外,David Everly的快速高斯模糊(PDF)具有快速的高斯模糊处理方法。
我将尝试各种方法,对它们进行基准测试,并在此处发布结果。
为了我的目的,我从互联网上复制并实现了基本的(独立处理X-Y轴)方法和David Everly的快速高斯模糊方法。它们的参数不同,因此我无法直接比较它们。但是后者在大模糊半径下经过的迭代次数要少得多。此外,后者是一种近似算法。
如果要使用更大的模糊半径,可以尝试应用三次盒状模糊。这将很好地近似高斯模糊,并且比真正的高斯模糊快得多。
@Override
public BufferedImage ProcessImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
int[] changedPixels = new int[pixels.length];
FastGaussianBlur(pixels, changedPixels, width, height, 12);
BufferedImage newImage = new BufferedImage(width, height, image.getType());
newImage.setRGB(0, 0, width, height, changedPixels, 0, width);
return newImage;
}
private void FastGaussianBlur(int[] source, int[] output, int width, int height, int radius) {
ArrayList<Integer> gaussianBoxes = CreateGausianBoxes(radius, 3);
BoxBlur(source, output, width, height, (gaussianBoxes.get(0) - 1) / 2);
BoxBlur(output, source, width, height, (gaussianBoxes.get(1) - 1) / 2);
BoxBlur(source, output, width, height, (gaussianBoxes.get(2) - 1) / 2);
}
private ArrayList<Integer> CreateGausianBoxes(double sigma, int n) {
double idealFilterWidth = Math.sqrt((12 * sigma * sigma / n) + 1);
int filterWidth = (int) Math.floor(idealFilterWidth);
if (filterWidth % 2 == 0) {
filterWidth--;
}
int filterWidthU = filterWidth + 2;
double mIdeal = (12 * sigma * sigma - n * filterWidth * filterWidth - 4 * n * filterWidth - 3 * n) / (-4 * filterWidth - 4);
double m = Math.round(mIdeal);
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < n; i++) {
result.add(i < m ? filterWidth : filterWidthU);
}
return result;
}
private void BoxBlur(int[] source, int[] output, int width, int height, int radius) {
System.arraycopy(source, 0, output, 0, source.length);
BoxBlurHorizantal(output, source, width, height, radius);
BoxBlurVertical(source, output, width, height, radius);
}
private void BoxBlurHorizontal(int[] sourcePixels, int[] outputPixels, int width, int height, int radius) {
int resultingColorPixel;
float iarr = 1f / (radius + radius);
for (int i = 0; i < height; i++) {
int outputIndex = i * width;
int li = outputIndex;
int sourceIndex = outputIndex + radius;
int fv = Byte.toUnsignedInt((byte) sourcePixels[outputIndex]);
int lv = Byte.toUnsignedInt((byte) sourcePixels[outputIndex + width - 1]);
float val = (radius) * fv;
for (int j = 0; j < radius; j++) {
val += Byte.toUnsignedInt((byte) (sourcePixels[outputIndex + j]));
}
for (int j = 0; j < radius; j++) {
val += Byte.toUnsignedInt((byte) sourcePixels[sourceIndex++]) - fv;
resultingColorPixel = Byte.toUnsignedInt(((Integer) Math.round(val * iarr)).byteValue());
outputPixels[outputIndex++] = (0xFF << 24) | (resultingColorPixel << 16) | (resultingColorPixel << 8) | (resultingColorPixel);
}
for (int j = (radius + 1); j < (width - radius); j++) {
val += Byte.toUnsignedInt((byte) sourcePixels[sourceIndex++]) - Byte.toUnsignedInt((byte) sourcePixels[li++]);
resultingColorPixel = Byte.toUnsignedInt(((Integer) Math.round(val * iarr)).byteValue());
outputPixels[outputIndex++] = (0xFF << 24) | (resultingColorPixel << 16) | (resultingColorPixel << 8) | (resultingColorPixel);
}
for (int j = (width - radius); j < width; j++) {
val += lv - Byte.toUnsignedInt((byte) sourcePixels[li++]);
resultingColorPixel = Byte.toUnsignedInt(((Integer) Math.round(val * iarr)).byteValue());
outputPixels[outputIndex++] = (0xFF << 24) | (resultingColorPixel << 16) | (resultingColorPixel << 8) | (resultingColorPixel);
}
}
}
private void BoxBlurVertical(int[] sourcePixels, int[] outputPixels, int width, int height, int radius) {
int resultingColorPixel;
float iarr = 1f / (radius + radius + 1);
for (int i = 0; i < width; i++) {
int outputIndex = i;
int li = outputIndex;
int sourceIndex = outputIndex + radius * width;
int fv = Byte.toUnsignedInt((byte) sourcePixels[outputIndex]);
int lv = Byte.toUnsignedInt((byte) sourcePixels[outputIndex + width * (height - 1)]);
float val = (radius + 1) * fv;
for (int j = 0; j < radius; j++) {
val += Byte.toUnsignedInt((byte) sourcePixels[outputIndex + j * width]);
}
for (int j = 0; j <= radius; j++) {
val += Byte.toUnsignedInt((byte) sourcePixels[sourceIndex]) - fv;
resultingColorPixel = Byte.toUnsignedInt(((Integer) Math.round(val * iarr)).byteValue());
outputPixels[outputIndex] = (0xFF << 24) | (resultingColorPixel << 16) | (resultingColorPixel << 8) | (resultingColorPixel);
sourceIndex += width;
outputIndex += width;
}
for (int j = radius + 1; j < (height - radius); j++) {
val += Byte.toUnsignedInt((byte) sourcePixels[sourceIndex]) - Byte.toUnsignedInt((byte) sourcePixels[li]);
resultingColorPixel = Byte.toUnsignedInt(((Integer) Math.round(val * iarr)).byteValue());
outputPixels[outputIndex] = (0xFF << 24) | (resultingColorPixel << 16) | (resultingColorPixel << 8) | (resultingColorPixel);
li += width;
sourceIndex += width;
outputIndex += width;
}
for (int j = (height - radius); j < height; j++) {
val += lv - Byte.toUnsignedInt((byte) sourcePixels[li]);
resultingColorPixel = Byte.toUnsignedInt(((Integer) Math.round(val * iarr)).byteValue());
outputPixels[outputIndex] = (0xFF << 24) | (resultingColorPixel << 16) | (resultingColorPixel << 8) | (resultingColorPixel);
li += width;
outputIndex += width;
}
}
}
在一维情况下:
使用几乎任何核进行重复模糊处理最终都会趋向于高斯核。这就是高斯分布的魅力所在,也是统计学家喜欢它的原因。因此,选择一个易于模糊处理的东西,并多次应用它。
例如,可以使用盒形核进行模糊处理。首先计算累积和:
y(i) = y(i-1) + x(i)
那么:
blurred(i) = y(i+radius) - y(i-radius)
多次重复。
或者您可以使用各种形式的IIR滤波器来前后反复,这些滤波器同样快速。
在二维或更高维度中:
依次在每个维度中进行模糊处理,如DarenW所说。
高斯系数
*输入像素值
的组合。当然,您需要离散化系数,但我只是想添加这个解决方案。如果您有IEEE订阅,您可以在Fast image blurring using Lookup Table for real time feature extraction中了解更多信息。如果你想使用更大的内核,我建议你考虑使用CUDA或其他GPU编程工具包。如果不行,你可以手动调整循环的汇编代码。