I am trying to implement the Gaussian blur. I have already computed the mask using the 2d function provided on wikipedia. I currently have a 2d matrix. I understand that in order to improve the running time, one can avoid using standard convolution methods due to the seperability of the Gaussian. In other words, as this This answer says, "In the Gaussian blur case it breaks down to two one dimensional operations".
This page has been helpful, however, I do not understand how to obtain a 1d mask from the existing 2d mask. For instance, this page converts the 2d mask in figure 3 to the 1d mask in figure 4. How does one do this conversion?
[EDIT]
Is is sufficient to compute a 1d mask to begin with, and apply it the x direction and then the y?
[EDIT 2]
I have figured out that yes, one can simply generate a 1d mask and use it on both x and y directions. The problem, however, is the quality of the resulting image when I apply my Gaussian filter.
I assume there are manipulations that need to be done when one computes the dot product of the mask and the selected section of the original image. What could cause the resultant image to be like this?
[EDIT]
In addition to @Jeremie Miserez's answer, this page was extremely helpful. It also has code if one needs to see how this is done.
This article on separable convolution should clear things up a bit.
I just recently had to do this, so here goes: As an example, we use a kernel based on a 2-dimensional Gaussian distribution with a standard deviation of 0.85. We'll want a 3x3 kernel (Matlab code).
Note that the sum of all entries is 1, i.e. the brightness of an image will not change if you apply this as a filter:
Also note that the rank is 1, thus the kernel is actually separable (two vectors
h1
andh2
that will result inh
when multiplied:h1*h2=h
)Great, we can proceed. Note that if the rank is greater than 1, you could still get an approximation, but you might need to use a different technique (see links at the end).
Without going into the details, we do singular value decomposition using the
svd
function. This is a standard function, computesU*S*V'=h
and is available in many math libraries.We now know that
U*S*V'=h
(V'
is the transpose ofV
). Now, for a rank 1 matrix,S
should just have 1 singular value, the rest should be 0 (see discussion at the end of this answer for more on that).So what we need now is
(h1)*(h2)=h
. We can splitS
in two different values so thats1*s2=S
. Thus we get:U*s1*s2*V'=h
, and then(U*s1)*(s2*V')=h
.You can choose how to split
S
however you want, but using the square root will divideS
equally betweenh1
andh2
:Note that we don't need the extra rows/columns with zeros, so we can do it simpler like this:
Note the minus signs cancel each other out, so you could also just remove them from
h1
andh2
if you like:Which is (almost) the same as what we had before:
Note that machine precision
eps
is just:so the error is miniscule and in this case due to imprecision. If you have any error much larger than this, you most likely simply ignored the remaining singular values and calculated a rank-1 approximation of
h
. In that case, you might want look into other options to get better approximations, e.g. this implementation or this implementation.