# The Kernel in the Guided Image Filter

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

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.

### Reference

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

• TBH I am confused how to apply it if you can explain how to apply this filter using kernel approach please explain it to me – Tab Jul 18 '17 at 6:03
• Did you solve this? – David Aug 19 '18 at 16:55
• – Royi Nov 11 '18 at 18:03

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):

• 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. – Tab Jul 19 '17 at 12:20
• 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. – Royi Jul 19 '17 at 16:37
• 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... – Royi Jul 19 '17 at 20:29
• 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. – Tab Jul 25 '17 at 5:54
• 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. – Royi Jul 25 '17 at 6:22