I am trying to understand how to calculate kernel matrix for guided image filter. Following is the formula for kernel calculation.

enter image description here where k is window withing pixels i and j belong i assumed that window k is a 3x3 matrix and take following I matrix to calculate kernel. omega is total number of pixels in window. I am confused about index i and j so if I take a matrix 3x 3 that is as $$ I= \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{matrix} $$ now mean is 5 and variance is 6.67 taking epsilon as 0.002 and omega is 9 I calculated value for kernel for the first pixel so taking i = pixel(1,1) and all rest will be j including i== j case. then my output came as (I used excel sheet to do this I could have used MATLAB)

$$ W= \begin{matrix} 0.111 & 0.111 & 0.111\\ 0.111 & 0.111 & 0.111 \\ 0.111 & 0.111 & 0.111 \\ \end{matrix} $$

when I changed values in matrix I output was still same so I doubt i understood something really wrong. Is there anyone who can explain me how to calculate W in this formula. Most confusing term for me is that sum of k:(i,j) over window k. If anyone can explain me I guess I will be able to correct my calculation.


The paper - Guided Image Filtering (Available on Scribd).
Please check section 3.3 for kernel formula.


The Guided Image Filter is a filter which assumes Piece Wise Linear model of the image.
Basically, it tries to estimate the best Linear Estimator per patch / pixel of the image.

Since it is Linear its coefficients are based on the first and second moments of the local patch.

You can see the code in my Q42415 GitHub Repository which also includes an implementation in Kernel Form in ApplyLocalLinearKernel().
Here are some results (Output of Q42415):

enter image description here

enter image description here

  • $\begingroup$ Thanks for answer but the code is based on first algorithm in the paper that I already implemented. I want to implement it with kernel formula so that I can have different shapes of kernel. In box filter it is fixed to square or cube shape in case of 3d. I implemented 2d and 3d of box filter version but for circle and sphere I am not able to understand how to calculate kernel matrix. $\endgroup$ – Tab Jul 19 '17 at 12:20
  • $\begingroup$ No problem I can create its Kernel Form. Do you understand its speed will be significantly lower, right? You can use the attached filter in non box cases as well. Just calculate the first and second moments in any neighborhood form you'd like. $\endgroup$ – Royi Jul 19 '17 at 16:37
  • $\begingroup$ I added in the repository a file called ApplyLocalLinearKernel.m which implements the filter in a Kernel Form. More in depth derivation is given at Guided Image Filtering. Pay attention that thought they claim the weights sum up to 1 they don't and normalization is required. Enjoy... $\endgroup$ – Royi Jul 19 '17 at 20:29
  • $\begingroup$ Thanks for the help though I have tried this and it is not generating similar result with same parameters for algorithm one and kernel one. That is where I doubted my calculations were wrong and so I need to understand it with a single 3x3 matrix input for kernel. $\endgroup$ – Tab Jul 25 '17 at 5:54
  • $\begingroup$ It gets very similar results and does what the paper states. I think the paper might be wrong on saying the kernel sums to 1. I'm not sure about the derivatives. $\endgroup$ – Royi Jul 25 '17 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.