File Exchange

image thumbnail

Guided Filter

version 1.0 (2.54 KB) by

An 2D edge-preserving filter derived from a local linear model. Related to bilateral filter.



View License

This is a novel type of explicit image filter - guided filter. Derived from a local linear model, the guided filter
generates the filtering output by considering the content of a guidance
image, which can be the input image itself or another different image.

The guided filter can perform as an edge-preserving smoothing operator
like the popular bilateral filter, but has better behavior near the edges. It also has a theoretical connection with the matting Laplacian matrix, so is a more generic concept than a smoothing operator and can better utilize the structures in the guidance image. Moreover, the
guided filter has a fast and on-approximate linear-time algorithm, whose
computational complexity is independent of the filtering kernel size. The guided filter is both effective and efficient in a great variety of computer vision and computer graphics applications including
noise reduction, detail smoothing/enhancement, HDR compression, image matting/feathering, haze removal, and joint upsampling.

There are two files: 'guided_filter.m' contains the guided filter function; 'guided.m' is an example script demonstrating the use of the filter.

Comments and Ratings (12)

pavithra p



Rachel (view profile)

@Liu: thanks for the comment. It sounds like your input is a color image with three channels. This guided filter code only deals with gray-level images. To get around the problem, you can:

1) apply rgb2gray on the input image to convert it to a gray-level image then apply the guided filter.

2) apply guided filter individually to each of the three channels and combine the filtered the results at the end.


Liu (view profile)

Thanks for the code.
However, when I run this code, there is an error

"Error using .*
Matrix dimensions must agree.

Error in guided_filter (line 92)
filtered = mua .* input + mub;

Error in guided (line 14)
filtered = guided_filter(input, guide, .01, win_size);"

the dimension of mua is 384*1544, while the dimension of the input is 384*512*3, should I reshape the mua or the input? Thanks.


Rachel (view profile)

@Phithak: Hi Phithak, thanks for downloading guided filter. I tried to reproduce the error and couldn't get the same error message. When I replaced the input and the guide image with their UINT8 version, I got the following error:
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.

Error in guided_filter (line 43)
mup = mup + circshift(paddedp, [i, j]);
This is what you would expect when you use operations for doubles on UINT8 type.

Can you double check your code and make sure that the file 'guided_filter.m' is actually in your MATLAB path when you run your script? The error you got sounds more like MATLAB can't find the file.

One way to test is to apply double() to your input and guide image and see if the the same error happens. If it still does, then the file 'guided_filter.m' is probably not in the current directory.

If you are still having problems, please send your code to me. I'm happy to take a look.

Another thing is: if you have MATLAB 2014a and up, you should find the imguidedfilter function from Mathworks. I have tested this command and it produces very similar results to my implementation. If you choose to use imguidedfilter, there won't be any problem with finding the external files.

phithak deang

i'm using Matlab R2012b. when i try to run the code i received
Undefined function 'guided_filter'
for input arguments of type

why does it happen? can u explain more about this problem? Thank you

Stefan Karlsson

Stefan Karlsson (view profile)

Its always nice to see useful implementations. As pointed out, this function is already implemented in the imageprocessing toolbox, but for 2014 versions and onward.

A question: how does this code perform when compared to the one found here?:


Aasd (view profile)

good job ,this code doing smooth well


Rachel (view profile)

@Royi: this code is simply using the input image (noisy image) as the guidance image, which the original authors used in their example (Fig. 1 in their original paper). If you compare the results from this code and the new MATLAB 2014a function 'imguidedfilter', you'll notice the results look almost identical. I suggest you have another look at the original paper before posting.

Royi Avital

Pay attention that you make it a linear combination of the Input Image while the article states that the output is a linear combination of the Guidance Image. Hence the code is wrong.

Guided filter is offered in the Image Processing Toolbox, as function imguidedfilter, since R2014a.


jun (view profile)

Another codes from Kaiming He,author of the paper 'Guided Filter', are here:


jun (view profile)

The code doing smooth well @ epsilon in range[0.05,0.1]. And what about HDR compression?

MATLAB Release
MATLAB 7.13 (R2011b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video