can anyone tell me how to generate a 2d gaussian filter kernel using the gaussian filter equation? how does the x and y value vary?
Asked
Active
Viewed 3,665 times
0
-
Interesting math/signal processing question, but not a coding question, so it's off-topic here. – Ben Voigt Dec 18 '12 at 17:46
-
There have been some discussions on this on stackoverflow, but the answers didnt seem to be clear. http://stackoverflow.com/questions/8204645/implementing-gaussian-blur-how-to-calculate-convolution-matrix-kernel http://stackoverflow.com/questions/1696113/how-do-i-gaussian-blur-an-image-without-using-any-in-built-gaussian-functions – Abhishek Thakur Dec 18 '12 at 17:47
-
Abishek, I find those answers clearer than your question. And those questions weren't very good either. If you have a question about one of those answers, add a link in your question, and be specific about what you aren't understanding. – Ben Voigt Dec 18 '12 at 17:56
1 Answers
2
To generate the kernel is quite simple. If your problem is in applying the kernel, you need to update the question.
The kernel is simply a square matrix of values, generally an odd number size so that there's a clearly defined center. To fill it, the x
and y
values go from -(n-1)/2
to (n-1)/2
where n
is the size of the matrix.
double half_n = (n - 1) / 2.0;
for (i = 0; i < n; ++i)
{
double x = i - half_n;
for (j = 0; j < n; ++j)
{
double y = j - half_n;
kernel[i][j] = // use formula with x and y here
}
}

Mark Ransom
- 299,747
- 42
- 398
- 622
-
-
@Mark Your result image will be darkened or brightened. To fix this the values in the matrix need to be normalized. This can be done by dividing each value by the sum of all values in the matrix. – Igor Apr 10 '16 at 10:15
-
@Igor good point, I should have mentioned it. There are a few formulas that already sum to 1 and don't need scaling. – Mark Ransom Apr 10 '16 at 15:58