Get startedGet started for free

Filters

1. Filters

So far, we have only considered the images as a whole. However, you can combine intensity and spatial information by employing convolutional filters.

2. Filters

Two common examples of filtering are smoothing and sharpening. Smoothing emphasizes large intensity patterns in an image by reducing variability between neighboring pixels. Essentially, it suppresses noise by blurring the image. Image sharpening is the opposite: sharp changes are enhanced, exaggerating the differences between pixels. Let's look more closely at how this is accomplished.

3. Convolution with a sharpening filter

Here we have a five by five input array, where all the values are one except for the center value of 2. To get a sharpened value for the center pixel, we would first define a set of filter weights, also called a kernel. Then, we would select a window of input data of the same size as our kernel. In this case, the filter will highly weight the center pixel and down-weight the adjacent pixels.

4. Convolution with a sharpening filter

To perform convolution, we multiply these two matrices element-wise, and then we sum them up. In the top-left corner, we have an input value of 1 times a weight of 0, an input value of 1 times a weight of negative 1, and so on. We then sum all these products to get a new, sharpened value for the center pixel of our input image. In this case, it's been increased from two to six.

5. Convolution with a sharpening filter

Let's see how this works on a full-size image. On the left is a random 2D array; on the right, we have an empty output image that we will create. In between is the sharpening kernel.

6. Convolution with a sharpening filter

Starting from the top-left corner of the input image, we select the values surrounding the origin pixel. We then multiply each element in this selection by the kernel weights and add them together to get the filtered value. We take one step over to the next element, multiply the input window by the kernel, and repeat the process for each pixel in the image. This results, in this case, in a sharpened image.

7. Image convolution

We can apply custom filters using the convolve() function. First, we import packages and load the foot x-ray. Next, we create the kernel. In this case, let's average the center pixel with its neighbors to smooth out variability between pixels. After filtering, the major patterns will remain, but subtle variation between pixels will be dampened. Next, we call ndimage dot convolve() and pass in the image and weights. This produces a smoothed output image of the same size as our input.

8. Filtering functions

Filtering can also employ functions other than convolutional kernels, such as the mean, median, and maximum. SciPy has several of these functions built-in. Filter kernels do not have to be three by 3; they can be as large as you want. Here, we apply a ten by ten median filter to the foot image. You can see it does quite a nice job of smoothing out the variations in intensity.

9. Gaussian filtering

Finally, the Gaussian filter is useful for smoothing data across larger areas. It blurs activation based on a Gaussian, or normal, distribution around the filtered pixel. Basically, the filter weights dissipate in a circular pattern as you get further from the center. The width of the distribution is controlled by the sigma parameter. Applying a Gaussian filter can be a great way to reduce noise, but with very large sigma values, you'll lose a lot of detail in your image.

10. Let's practice!

With that crash course on filtering complete, let's try it out!