Bilateral filter is a non-linear filter used for many applications in computational photography including image smoothing, denoising, tone mapping, relighting and image blur. Bilateral filter smooths an image while keeping the edges preserved. To calculate the filtered value, the filter uses (1) euclidean distance and (2) perceptual difference between the current pixel and its local neighborhood.The euclidean distance is calculated from the two-dimensional image coordinates of the center pixel and the pixels in the local neighborhood. The perceptual difference is the difference between the tristimulus values (RGB) of the center pixel and the pixels in the neighborhood. To be more perceptually meaningful colour difference convert the image into a perceptually uniform colour space (CIELab or CIELuv) and calculate the colour difference. In calculating colour difference, you can use the chromaticity (or any feature that is invariant to illuminant such as hue, chromaticity, normalised rgb values) of the image to smooth edges caused by the illumination while preserving the edges caused by the reflectance.
An intuitive explanation is that bilateral filter compares the colour difference in a local neighborhood and averages only the neighbouring pixels that have similar colour value as that of the center pixel. This process avoids smoothing any edges in the image.
Simple Implementation: Use a threshold value to decide whether to include a neighborhood pixel in the process of averaging. In particular calculate the colour difference of all the pixels in the local neighborhood of a pixel and average only the neighborhood pixels with colour difference smaller than the predefined threshold. This should result in a reasonably smoothed image with edges preserved. To improve the quality of the smoothed image vary the threshold, vary the size of the neighborhood and repeat the smoothing step a few times and find the best parameters (colour difference threshold, neighborhood and number of repeats) that gives the best result.
Better Implementation: Instead of using simple threshold use a kernel function such as a Gaussian function to weight the euclidean distance and perceptual difference. This should result in a better result. To further improve the results try different kernel functions and find the one gives the best result.
For more information :http://people.csail.mit.edu/sparis/bf_course/