Useful Image Filters for Image Processing
- Yanfeng Liu
- Sep 15, 2016
- 3 min read
This is different from your Instagram filters. We are talking about a matrix that we created by ourselves and then applied to the image directly. And the purpose is not to make it beautiful for posting on Facebook. Instead we want to make it easier for ourselves and the computer to spot certain features in an image. Today we are gonna introduce you to a few useful image filters: Gaussian Blur, Median Filter, and Edge Detector.
The first two have something in common: both Gaussian Blur and Median filter will blur the image and smooth out some details. The third one does the opposite. It will highlight the edges in the image and make everything else go away. First, let's look at Gaussian Blur.
Gauss has always been special in many fields. You can hardly learn anything in the STEM field without coming across the name of Gauss once or twice. In Physics there is Gauss Law for flux; in Electromagnetism class there is the Gaussian Beam; and don't even mention all the concepts in Math with "Gauss" in it. Today we are look at Gaussian Blur. Remember the beautiful blurry background in some of the phone apps? They are probably created with Gaussian Blur.
The idea of blurring an image is to smooth out the details and smear the colors around, but how do we do that on computers? To generate a blurred image from a sharp one, we need to make change each pixel's value to the average of its neighbors, and there are different ways of doing it. For example, the simplest one is the take the rectangular average, and the operator kernel looks like this:

We essentially are just taking 1/81 of each of its surrounding pixels, including itself, and then add them up together to get the value of that pixel. We do this for every pixel in the image, and then we end up with a blurred image. The operation is called 2D convolution. To learn more about convolution, you can google it or find the related topic in an online math course. Here we focus on the MATLAB operation.


However, if we examine the blurred image carefully, we will notice that the blur is not very smooth. Edges can be observed easily in some areas, because of the structure of our kernel. We can compare the results from two filters. It is clear that the one on the top (average) has a sharper edge than the one on the bottom (Gaussian).


A smoother way to do it is through Gaussian filter. The way to do it in MATLAB is:


A Gaussian filter has a smoother effect because it assigns different weights to the pixel values. A view of the filter in 2D is provided below:

Next, let's look at median filter. A median filter replaces a pixel's value with the median value of its neighbor pixels. The effect is interesting: it smooths the image and makes it look like a cartoon drawing:

Compare the original image and the processed version:


Note that median filter by default can only handle grayscale images. There are ways to do it on colored images, but its slower. (https://www.mathworks.com/matlabcentral/fileexchange/25825-hybrid-median-filtering)
The third filter is edge detector. This is especially useful when we want the outline of objects. To get edges, we subtract the pixels values on one side of the image with the pixel values on the other side of the image. If they have very different values, then it means there is an edge between them. Common edge detection methods include Sobel, Canny, Prewitt, Roberts, etc. We use Sobel to show an example:


Comments