我有这个高斯函数。这没有产生预期的结果。因此,图像无法正确模糊。在下面的代码中,我采用了内核大小(5)和 sigma 值(1),这与我在下面的链接中采用的相同。如果有人能指出问题,那将非常有帮助。
这是一个网站高斯内核链接的链接
public class KernelDemo {
public double[][] Calculate(int lenght, double weight) {
double[][] Kernel = new double[lenght][lenght];
double sumTotal = 0;
int kernelRadius = lenght / 2;
double distance = 0;
double calculatedEuler = 1.0 / (2.0 * Math.PI * Math.pow(weight, 2));
for (int filterY = -kernelRadius; filterY <= kernelRadius; filterY++) {
for (int filterX = -kernelRadius; filterX <= kernelRadius; filterX++) {
distance = ((filterX * filterX) + (filterY * filterY)) / (2 * (weight * weight));
Kernel[filterY + kernelRadius][filterX + kernelRadius] = calculatedEuler * Math.exp(-distance);
sumTotal += Kernel[filterY + kernelRadius][filterX + kernelRadius];
}
}
for (int y = 0; y < lenght; y++) {
for (int x = 0; x < lenght; x++) {
Kernel[y][x] = Kernel[y][x] * (1.0 / sumTotal);
}
}
return Kernel;
}
public static void main(String args[]) {
KernelDemo kd = new KernelDemo();
double terms[][];
terms = kd.Calculate(5,1);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(terms[i][j]);
}
System.out.println();
}
}
}