In a previous post, we looked at the mathematical aspects behind one of the basic filters used in image processing, the Gaussian blur. In this post I want to focus on the other basic algorithm that serves as a building block for many of the other post-processing techniques that are usually applied to all kinds of images: the sharpening filter.
It is easy to see sharpening as the opposite of blur. But this does not only hold true for the intuitive aspect but also for the mathematical aspect. While the blur filter is basically a low-pass filter, the sharpening filter is a high-pass filter, meaning that it will leave the high-frequency components more or less unchanged while eliminating or attenuating the low-frequency ones.
High-pass filters are closely related to a technique called edge detection. This is because edges in an image are usually the regions where the high-frequency components are located and thus in order to extract edges, special cases of sharpening filters can be applied.
The technique used to sharpen an image is very similar to the one used to blur it, as it was described in the post dedicated to Gaussian blur and it is based in the convolution of a given matrix (filter) with the image we are applying the filter to. What differs between both techniques is the shape of this filter.
While the filter used for blurring purposes is called Gaussian, the one used for sharpening is called Laplacian. Once again, these filters can be regarded as the opposite of each other. The following figure shows 3D representations of both filters, with the Gaussian on the left and the Laplacian on the right.
If you remember from the previous post, the way the Gaussian filter works at a given pixel is by combining information from the pixel itself and the surrounding pixels, assigning a different ‘weight’ to each of them. By emphasizing the center pixel and combining it with ‘smoothed’ versions of the surrounding pixels, a mixture of information is obtained that ends up producing the blurring effect.
The reason why the Laplacian filter works for sharpening images is because in image processing, sharpness is directly related to edges. This time, by giving more emphasis to the surrounding pixels, the edges of different features are enhanced. In fact, the sharpening filter is a two-step process: first, the edges of the original image are extracted and then, those edges, which correspond to detail information, are added the original image, thus giving a much sharper look.
In reality, once the filtered image is obtained, given the negative sign of the central pixel of the filter, it has to be subtracted from the original image in order to enhance the edges. To see how the process works, let’s take an image with easily recognizable edges on it. Take, for instance, this shot of the Brooklyn Bridge in New York City.
The whole image is full of edges, from the structure itself, to the wooden floor, the painted white lines and the supporting steel structure, so quite a rich output from the Laplacian filter is to be expected.
The following image is the the output of applying the Laplacian filter.
Notice how all the edges within the image are extracted quite neatly while the areas without changes are left out. Finally, as mentioned above, subtracting both images taking care of the image values (the filtering can give negative values that cannot be shown in an image) gives the sharpened result, which is shown in the next image.
If we compare the original image with the final one, it is noticeable how the edges are enhanced while the flat regions like the overcast sky remain unchanged. This is a consequence of the size of the filter applied. For this example, a 3×3 matrix was used and thus no change will be observable for flat regions with a length smaller than 3 pixels and this is the reason why choosing the smallest value for the filter radius in Photoshop will result in smoother images at the end.
If we take, for instance, a 9×9 filter with similar characteristics as the one used for the previous image, the result is a much sharper and noisier image, with a lot of structure on regions that should be rather smooth. The following image shows side by side the output of the Laplacian filter and the filtered image.
In general, when using sharpening filters, always look for the trade-off between getting sharp details where you want them and noise where you don’t. If the areas that you want to leave unaffected are large enough and easily discernible from the rest of the image, it might be easy to apply a layer mask, but if that is not the case, getting the desired results might be just too difficult so the easiest option might be to just reduce the size of your filter.
I hope you enjoyed this second post on post-processing filters and don’t hesitate to write me if you have any question.