Digital Image Processing

# Highboost Filtering using GNU Octave

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:

1. Blur the original image.
2. Subtract the blurred image from the original (the resulting difference is called the mask.)

Letting $\hbar\$ denote the blurred image, unsharp masking is expressed in equation form as follows. First we obtain the mask:

gmask(x, y) = f(x, y) – $\hbar\$(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 $\hbar\$ 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 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:

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_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)

blured_image = GaussianFilter(image_path, 5, 3);
out_image = input_image + weight_k * 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:
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)
output_image = input_image + edges;

figure, imshow(input_image), title('Source');
figure, imshow(edges), title('Edges');
figure, imshow(output_image), title('Sharpened');
endfunction
```

References:
1. Digital Image Processing (3rd Edition) by Rafael C. Gonzalez (Author), Richard E. Woods.