what is the fastest algorithm to compute the gradient of the Gaussian blur of a (grayscale) image, with a blur radius of around 100 pixels, where smoothness of the result is important to me, i.e. no edges or discontinuities? I run the code on the GPU, if that makes any difference. What I'm currently doing, is the following:
vec2 gradOfBlur(sampler2D image, vec2 coord) { vec2 grad = vec2(0, 0); base = radius * floor(coord + 0.5); for(int dx = -10; dx <= 10; dx++) { for(int dy = -10; dy <= 10; dy++) { vec2 pos = base + vec2(dx, dy); vec2 diff = (pos - coord); float bell = exp(-0.1 * dot(diff, diff)); float hgt = texture2D(texture, pos).a; grad.x += hgt * diff.x * bell; grad.y += hgt * diff.y * bell; sum += bell; } } return grad / sum; } I assumed the blur radius here to be 1, for simplicity, but the coordinates are not necessarily pixels. Also, it's OK if the gradient is scaled by some scalar, as I play around with a scalar in front of it anyway.
What I do is basically sampling the image at the 100 integer coordinates closest to my coordinate input, summing up the gradients (evaulated at the coordinate input) of the bell curves centered at those integer coordinates weighted by the image luminance, and dividing this sum of gradients by the sum of the values of the bell curves at my coordinate input.
The 0.1 parameter, which is the radius of the bell curves, is empirically estimated. I'm not sure if the division at the end is necessary, but it looks somehow reasonable. Using this method, my result is definitely very smooth, only when the coordinate input contains an integer value, there might be a very small step as new bell curves drop in and out of the sum.
What could I improve here?