In this blog post, we implement a classical spatial filter – spatial filtering deals with performing operations, such as image sharpening, by working in a neighborhood of every pixel in an image – called Highboost Filtering.
A process that has been used for many years by the printing and publishing industry to sharpen images consists of subtracting an unsharp (smoothed) version of an image from the original image. This process, called unsharp masking, consists of the following steps:
- Blur the original image.
- Subtract the blurred image from the original (the resulting difference is called the mask.)
- Add the mask to the original.
Letting denote the blurred image, unsharp masking is expressed in equation form as follows. First we obtain the mask:
gmask(x, y) = f(x, y) – (x, y)
Then we add a weighted portion of the mask back to the original image:
g(x, y) = f(x,y) + k * gmask(x, y)
where we included a weight, k (k >= 0), for generality. When k = 1, we have unsharp masking, as defined above. When k > 1, the process is referred to as highboost filtering. Choosing k < 1 de-emphasizes the contribution of the unsharp mask.
In the above discussion, we could obtain the blurred image using a Gaussian smoothing filter of size m x n with standard deviation σ. Recall that a 2-D Gaussian function has a bell shape, and that the standard deviation controls the “tightness” of the bell.
In order to run this example on your machine, suppose you have a working version of Linux Ubuntu. First, make sure you have the latest version of Octave by running the following commands:
$ sudo add-apt-repository ppa:picaso/octave
$ sudo apt-get update ; sudo apt-get install octave
There are many packages that provide specialized functionality for GNU Octave. The Octave-forge Image package provides functions for processing images. You may download it here. Then, it can be installed from the Octave prompt with the command:
$ pkg install image-1.0.0.tar.gz
But don’t forget to activate it using:
$ pkg load image
In this example, we created two m files for our two functions; one for the Gaussian smoothing and the second for the unsharp masking. The GaussianFilter takes three parameters; file_path, mask_size, and mask_sigma. Then it returns the resulting blurred image.
function [result] = GaussianFilter(file_path, mask_size = 9, mask_sigma = 3) im = imread(file_path); gauss = fspecial('gaussian', mask_size, mask_sigma); im_blur = imfilter(im, gauss); figure, imshow(im), title('Source'); figure, imshow(im_blur), title('Blurred Image'); result = im_blur; endfunction
The UnsharpMasking function takes two parameters; image_path, and weight_k. Then, it returns the resulting sharpened image.
function [out_image] = UnsharpMasking (image_path, weight_k) input_image = imread(image_path); blured_image = GaussianFilter(image_path, 5, 3); unsharp_mask = input_image - blured_image; out_image = input_image + weight_k * unsharp_mask; figure, imshow(unsharp_mask), title('Unsharp Mask'); figure, imshow(out_image), title('Result'); endfunction
In order to run the code, you will have to add the m files path to Octave workspace by executing the following command:
Then, we finally call our unsharp masking function as follows:
$ UnsharpMasking(‘Mona-Lisa.jpg’, 1);
However, note that the image file is located in the current path as well, otherwise you may have to include the images path in the same way as we did for the m files. Here are the screen-shots for the original and the resulting images:
We also note that the unsharp mask is very similar to what we would obtain using a second-order derivative:
For the sake of completeness, here is the code for the second derivative image sharpening – the Laplacian:
function [output_image] = LaplacianFilter (image_path) input_image = imread("Mona-Lisa.jpg"); laplacian_mask = fspecial("laplacian", 0); edges = imfilter(input_image, laplacian_mask); output_image = input_image + edges; figure, imshow(input_image), title('Source'); figure, imshow(edges), title('Edges'); figure, imshow(output_image), title('Sharpened'); endfunction
1. Digital Image Processing (3rd Edition) by Rafael C. Gonzalez (Author), Richard E. Woods.
2. How do you install the latest version of GNU Octave? http://askubuntu.com/questions/194151/how-do-you-install-the-latest-version-of-gnu-octave
3. Installing and Removing Packages: https://www.gnu.org/software/octave/doc/interpreter/Installing-and-Removing-Packages.html
4. GNU Octave Packages: http://octave.sourceforge.net/packages.php
5. Manipulating the Load Path: https://www.gnu.org/software/octave/doc/interpreter/Manipulating-the-Load-Path.html
6. Source code for this example: https://github.com/thecortex/Octave