Hello everyone, I'm trying to write a program that will return a Guassian Kernel for image blur processing and here are the results I'm getting from my function with KernelSize = 5 and Sigma = 1.4
0.012146124201989834 0.026109944200732167 0.033697319240713085 0.026109944200732167 0.012146124201989834
0.026109944200732167 0.056127302407599576 0.07243752084678488 0.056127302407599576 0.026109944200732167
0.033697319240713085 0.07243752084678488 0.0934873796057929 0.07243752084678488 0.033697319240713085
0.026109944200732167 0.056127302407599576 0.07243752084678488 0.056127302407599576 0.026109944200732167
0.012146124201989834 0.026109944200732167 0.033697319240713085 0.026109944200732167 0.012146124201989834
But when I compare results from online it says
0.012841 0.026743 0.03415 0.026743 0.012841
0.026743 0.055697 0.071122 0.055697 0.026743
0.03415 0.071122 0.090818 0.071122 0.03415
0.026743 0.055697 0.071122 0.055697 0.026743
0.012841 0.026743 0.03415 0.026743 0.012841
My formula is correct, here is what I use to calculate kernel
public static double[][] Kernel(int size, double sigma) {
double[][] ret = new double[size][size];
double gausSum = 0;
double twoPiSigmaSquared = 2 * Math.PI * Math.pow(sigma, 2);
double expression1 = (1 / twoPiSigmaSquared);
double denominator = 2 * Math.pow(sigma, 2);
for (int x = 0; x < size; x++) {
int gx = Math.abs((size/2) - x);
for (int y = 0; y < size; y++) {
int gy = Math.abs((size/2) - y);
double numerator = Math.pow(gx, 2) + Math.pow(gy, 2);
double expression2 = Math.pow(Math.E, -1 * (numerator/denominator));
double gaussian = expression1 * expression2;
gausSum += gaussian;
ret[x][y] = gaussian;
}
}
double multiplier = (1 / gausSum);
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
ret[x][y] *= multiplier;
}
}
return ret;
}
Any thoughts would be greatly appreciated. My only guess is that double precission in java is better than the online site im referencing my calculations wtih.