1. Feature detection
Filters aren't just useful for blurring and smoothing. They can also be used as detectors for features of interest, such as edges.
2. Edges: sharp changes in intensity
If we want to construct a filter kernel that will emphasize edges, what should it look like?
Recall that when we perform convolution, it creates a new image that reflects what the filter looks like: a smoothing filter itself has a smooth gradient, whereas a sharpening filter has a sudden change in intensity.
An edge is a change in intensity along an axis. Sharp edges, for example, between the skull and background in this MRI image, have very high contrast. The filter should reflect this.
3. Edge detection
Let's see if we can make this work. We start by loading the foot x-ray.
Next, we construct our kernel: to look for areas that have a change in intensity from top to bottom. We can weight the top row to positive 1 and the bottom row to negative 1. Essentially, this filter calculates the difference between the top and bottom rows, returning values far from 0 when there is a sudden change in intensity.
Then, we convolve the image with the filter using SciPy.
Plotting the image, it's clear that our detector has done a fine job of highlighting some edges. But note two things:
First, this is a horizontal edge detector because it is looking for differences between the top and bottom values at each point. If you look at the vertical edges in the filtered image, you'll see that they have relatively low values compared to the top and bottom of the foot.
Second, there are both positive and negative values. This happens because some edges have high-intensity values on top and low values on bottom, whereas others have the opposite. The direction of this difference determines whether the convolution yields a positive or negative value.
4. Sobel filters
There are many possible configurations for edge detectors. A very common one is the Sobel operator, which provides an extra weight to the center pixels of the kernel. The filter can be rotated to make it sensitive to either horizontal or vertical edges.
5. Sobel filters
Implementing the Sobel filter is just like implementing other filters: call ndimage dot sobel(), then pass in the image and the orientation of the filter.
6. Sobel filter magnitude
To remedy the fact that we have multiple edge maps with positive and negative values, we can create a composite edge map.
Recall the Pythagorean Theorem - when you have two perpendicular vectors, you can calculate their distance by taking the root of their squares.
This is useful in our situation: if we apply the Sobel filter along the first and second axes, we can then use these as input to the Pythagorean Theorem to get a composite, positively weighted edge image.
This nicely highlights intensity changes in our image, and we can use these features for masking or object detection in later analysis steps.
7. Let's practice!
Now that you've learned the basics of edge detection give it a try yourself.