4. Filtering in Images - Smoothing Filters
Filtering is a technique for enhancing and modifying an image by emphasizing on specific features and/or removing other features. In image processing filtering techniques are used to smoothen or sharpen an image and for edge enhancement. A filter is defined by a kernel, a small array applied to each pixel and its neighbors within an image. In most applications the size of the kernel is set to an odd number (3, 5, 7, etc..) with center of kernel aligned with image’s current pixel in every dimension. The kernel is convoluted with the image to get a filtered image and can be applied either in spatial or frequency domain.
In this project we learn some of the most commonly used filters for smoothing images. Smoothing in images is achieved by convolving the image with a low-pass filter kernel. This technique is useful for removing noise by removing the high frequency noisy content in images. When this filter is applied it eventually blurs the images. In this project we discuss four different types of smoothing techniques.
Averaging/Mean filter
Averaging filtering is a technique to smoothen images by reducing the intensity variation between neighboring pixels which is performed by convolving the image with normalized box filter. In simple terms, it averages all the pixel under kernal area and replaces with average value. The main disadvantage of this filter is that it blurs the edges too much as it interpolates new values for edge pixels and if the image contains or output requires sharp edges then this is not best choice. Implementing averaging filter is done by cv2.blur() function or cv2.boxFilter() function. Here we specify the size of the kernel i.e., width and height of the kernel. A 3x3 normalized box filter would look like this:
$$K = \frac{1}{9} \begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix}$$
import cv2
im = cv2.imread('test_image.png')
cv2.imshow(im)
blur_im = cv2.blur(im, (5,5))
cv2.imshow(blur_im)
cv2.waitKey(0)
Check a sample demo below with a kernel of 5x5 size:
The output looks like This
For different kernel sizes the output looks like this
Median filter
Median filter is a widely used nonlinear method of filtering images to remove noises. It is very effective in removing noises while preserving edges. Mostly it is used to remove salt and pepper noise. The median filter takes the median of all the pixels within the kernel area and replaces the value to the center pixel. The filter is then moved pixel by pixel to compute median value of neighboring pixels. The median is calculated by first sorting all the values of pixels from the window and replacing with the median pixel value. Here we use cv.medianBlue() function to perform median filtering. Its kernel size should be positive odd number.
import cv2
im = cv2.imread('test_image.png')
cv2.imshow(im)
filtered_im = cv.medianBlur(im,5)
cv2.imshow(filtered_im)
cv2.waitKey(0)
In this demo, I added a 50% noise to our original image and applied median blurring. Check the result:
Gaussian filter
In this method, instead of a box filter, a Gaussian kernel is used. It is done with the function, cv.GaussianBlur(). We should specify the width and height of the kernel which should be positive and odd. We also should specify the standard deviation in the X and Y directions, sigmaX and sigmaY respectively. If only sigmaX is specified, sigmaY is taken as the same as sigmaX. If both are given as zeros, they are calculated from the kernel size. Gaussian blurring is highly effective in removing Gaussian noise from an image.
If you want, you can create a Gaussian kernel with the function, cv.getGaussianKernel().
The above code can be modified for Gaussian blurring: