Code covered by the BSD License  

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

» Watch video

Highlights from
Bilateral Filtering

4.6 | 28 ratings Rate this file 235 Downloads (last 30 days) File Size: 118 KB File ID: #12191 Version: 1.0
image thumbnail

Bilateral Filtering


Douglas Lanman (view profile)


Implements bilateral filtering for grayscale and color images.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Bilateral filtering was proposed by Tomasi and Manduchi in 1998 as a non-iterative method for edge-preserving smoothing. For a complete description see the following publication:

C. Tomasi and R. Manduchi. Bilateral Filtering for Gray and Color Images. In Proceedings of the IEEE International Conference on Computer Vision, 1998.

This package contains a basic implementation (bfilter2.m) of their method for both grayscale and color images. In order to demonstrate the utility of bilateral filtering, the main function is used to implement an automatic image abstraction routine (cartoon.m) inspired by:

Holger Winnemoller, Sven C. Olsen, and Bruce Gooch. Real-Time Video Abstraction. In Proceedings of ACM SIGGRAPH, 2006.

A demonstration program (runDemo.m) summarizes the typical use of all included files.

A gallery of image and video abstraction results is available at:


This file inspired Retrogressive "Classic Game" Image Processing.

MATLAB release MATLAB 7.3 (R2006b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (38)
14 Jun 2016 SOHYUN PARK

Good! very nice

23 May 2016 Ankit Rohilla  
26 Aug 2015 tanu sidhu

i have some problm when i use this code

Comment only
27 Jul 2015 tanu sidhu  
13 Jan 2015 devi

devi (view profile)

how to implement bilateral filter coding for contrast reduction

Comment only
07 Aug 2014 sweety

sweety (view profile)

i need to know the implementation of bilateral filtering

Comment only
08 Feb 2014 Supreeth Mannava

Great examples of bilateral filtering. Good way to illustrate how to use MATLAB's image processing tools and functions

27 Jul 2013 ashkan abbasi  
16 Jul 2013 Elad  
16 Jul 2013 Elad

Simple and nice review on bilater filtering, using this file can be found here:

Comment only
28 Oct 2012 uit

uit (view profile)

Nice...Very Helpful, Thanks!!

24 Aug 2012 Artem

Artem (view profile)

thank you. works great!

Comment only
06 Jun 2012 Esmaeil  
02 Nov 2011 wei yang

First of all, thank you for the code but, while using with mat lab 6.1 , i am getting errors
??? Error: File: C:\bfilter2\Bilateral Filtering\bfilter2.m Line: 30 Column: 23
Expected a variable, function, or constant, found "|".

Error in ==> C:\bfilter2\Bilateral Filtering\runDemo.m
On line 30 ==> bflt_img1 = bfilter2(img1,w,sigma);

Tell me how to correct the error.

Comment only
31 Oct 2011 Siyu Zhu  
04 Jan 2011 xu

xu (view profile)

it is useful,thanks very much.

Comment only
21 Jun 2010 randomName  
12 Apr 2010 Alireza Saberi

Hello, thank for codes [which works nicely both in gray-level and color image]

Mr Douglas Lanman or Mr. Joseph Shtok,
I have understood the bilateral filter over 2D dimension / an considering Kernel which is a 2D square .

I have question. I'm apply bilateral filter over a sequence of images [video].
In one part of paper it applies 2D bilateral over a frame [which its code is here ]
but in other part of paper, it acquired 1-D bilateral filter for each pixel over frames. question is how?!!
I can't realize kernel here. but Mr. Joseph Shtok mentioned it's simple. Can you please explain?
Best Regards

23 Feb 2010 Alireza Saberi

sankiiiiiuuuu :)

Comment only
10 Feb 2010 Regis

Regis (view profile)

Hi everybody and thanks for the code. I'm a student in an engineering school in France and I'm working on image denoising. Why applying bilateral filtering twice on an image is better than once ?

Regis Melnotte

27 Jan 2010 B.M.E Guo

Great codes and easy to read except for low speed. I try to extend it to 3-D case, however, it's very slow. Could you convert it to the matrix or vector computation instead of loop pixel by pixel?

14 Dec 2009 Joseph Shtok

The code reduces easily and correctly to the one-dimensional case by slight modification of the bfilter2.m. Thank you!

Comment only
08 Feb 2009 Arun Kesavan

First of all, thank you for the code but, while using with mat lab 7.0 , i am getting errors

??? Error: File: Z:\Bilateral Filtering\bfilter2.m Line: 29 Column: 1
Function definitions are not permitted at the prompt or in scripts.

Error in ==> runDemo at 30
bflt_img1 = bfilter2(img1,w,sigma);

please tell me how to use your code?

17 Dec 2008 Hani Jamleh  
06 Aug 2008 Douglas Lanman


Thank you for your comments. I believe the code you referred to, specifically lines 67 and 114 in bfilter2.m, is correct as written. These lines implement the c(\xi,x) equation from Section 2.1 from the Tomasi and Manduchi paper. As they state, c is radially symmetric, with the d(\xi,x) term corresponding to the Euclidean distance between \xi and x. Expanding these expressions, the numerator of the exponent should be -(X.^2+Y.^2), not -(X-Y).^2. Your concern might come from how I've used the G matrix on these lines. This matrix is used to pre-compute c(\xi,x) for a w-by-w sized window. In this case, the origin is always the center pixel, so \sqrt((X.^2+Y.^2)) will represent the Euclidean distance of a pixel from the center of the window, where X and Y are pixel coordinates with respect to the center pixel, i.e. as given on lines 66 and 113. These geometric terms are identical for all windows, which is why I pre-computed their values. Unfortunately, the "intensity similarity" will vary for each w-by-w window. As a result, the H matrix, as given by lines 87 and 140, must be evaluated independently using a "sliding window" analysis.

Please send me an email if you have any further questions about my implementation.

Best regards,

Comment only
06 Aug 2008 Frederic Garcia

First of all, thank you for the code but, according to the Tomasi and Manduchi paper, I think that instead of:

G = exp(-(X.^2+Y.^2)/(2*sigma_d^2));

it should be:

G = exp(-(X-Y).^2/(2*sigma_d^2));

with respect to the GausSian distance weight in the bfilter2.m file.

Please, correct me if I'm wrong.


16 Jul 2008 DO Quoc Bao

Thanks alot :)

02 Jul 2008 Giljin Jang

Thanks for the hard work. I appreciate the code.

29 Apr 2008 Daniel Oldjira Fufa

Thanks for the code

18 Jan 2008 Seong Park


18 Dec 2007 sethu madhavi  
21 Nov 2007 kanch K  
27 Oct 2007 Zhi Zhang

Excellent method except the speed. But a lot of papers are coming out. You can go to the SIGGRAPH07 for some information.

10 Oct 2007 anup pandey  
30 Aug 2007 Yulia Monich  
21 Mar 2007 Tom Pinkiewicz

Great filter but rather slow.
Do you know if it can be vectorised or sped up in some other way?
If there is anyone out there that knows how to speed up this code please let me know.
Good work!

04 Dec 2006 Wenjing Jia

Very easy to understand and use! Comments precede each part. Run perfectly!

15 Nov 2006 Ido Omer  

Contact us