The purpose of this article is to explore and illustrate the concept of Image Unsharp Masking. This article implements image convolution in the form of a 3×3 Gaussian Blur, 5×5 Gaussian Blur, 3×3 Mean filter and a 5×5 Mean filter.
Sample Source code
This article is accompanied by a sample source code Visual Studio project which is available for download here.
Using the Sample Application
The sample source code associated with this article includes a Windows Forms based sample application implementing the concepts explored throughout this article.
When using the Image Unsharp Mask sample application users can select a source/input image from the local file system by clicking the Load Image button. The dropdown combobox at the bottom of the screen allows the user to select an unsharp masking variation. On the right hand side of the screen users can specify the level/intensity of resulting image sharpness.
Clicking the Save Image button allows a user to save resulting images to the local file system. The image below is a screenshot of the Image Unsharp Mask sample application in action:
What is Image Unsharp Masking?
Unsharp masking (USM) is an image manipulation technique, often available in digital image processing software.
The "unsharp" of the name derives from the fact that the technique uses a blurred, or "unsharp", positive image to create a "mask" of the original image. The unsharped mask is then combined with the negative image, creating an image that is less blurry than the original. The resulting image, although clearer, probably loses accuracy with respect to the image’s subject. In the context of signal-processing, an unsharp mask is generally a linear or nonlinear filter that amplifies high-frequency components.
In this article we implement Image Unsharp Masking by first creating a blurred copy of a source/input image then subtracting the blurred image from the original image, which is known as the mask. Increased image sharpness is achieved by adding a factor of the mask to the original image.
Applying a Convolution Matrix filter
The sample source code provides the definition for the ConvolutionFilter extension method targeting the Bitmap class. This method is invoked when implementing image blurring. The definition of the ConvolutionFilter extension method as follows:
Subtracting and Adding Images
An important step required when implementing Image Unsharp Masking comes in the form of creating a mask by subtracting a blurred copy from the original image and then adding a factor of the mask to the original image. In order to achieve increased performance the sample source code combines the process of creating the mask and adding the mask to the original image.
The SubtractAddFactorImage extension method iterates every pixel that forms part of an image. In a single step the blurred pixel is subtracted from the original pixel, multiplied by a user specified factor and then added to the original pixel. The definition of the SubtractAddFactorImage extension method as follows:
The image blurring convolution filters implemented by the sample source code relies on static matrix/kernel values defined in the Matrix class. The variants of blurring implemented are: 3×3 Gaussian, 5×5 Gaussian, 3×3 Mean and 5×5 Mean. The definition of the Matrix class is detailed by the following code snippet:
Implementing Image Unsharpening
This article explores four variants of Image Unsharp Masking, relating to the four types of image blurring discussed in the previous section. The sample source code defines the following Image Unsharp Masking extension methods: UnsharpGaussian3x3, UnsharpGaussian5x5, UnsharpMean3x3 and UnsharpMean5x5. All four methods are defined as extension methods targeting the Bitmap class. When looking at the sample images in the following section you will notice the correlation between increased image blurring and enhanced image sharpness. The definition as follows:
The input/source image used in rendering the sample images shown in this article is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license and can be downloaded from Wikipedia:
The Original Image
Unsharp Gaussian 3×3
Unsharp Gaussian 5×5
Unsharp Mean 3×3
Unsharp Gaussian 5×5
Related Articles and Feedback
Feedback and questions are always encouraged. If you know of an alternative implementation or have ideas on a more efficient implementation please share in the comments section.
I’ve published a number of articles related to imaging and images of which you can find URL links here:
- C# How to: Image filtering by directly manipulating Pixel ARGB values
- C# How to: Image filtering implemented using a ColorMatrix
- C# How to: Blending Bitmap images using colour filters
- C# How to: Bitmap Colour Substitution implementing thresholds
- C# How to: Generating Icons from Images
- C# How to: Swapping Bitmap ARGB Colour Channels
- C# How to: Bitmap Pixel manipulation using LINQ Queries
- C# How to: Linq to Bitmaps – Partial Colour Inversion
- C# How to: Bitmap Colour Balance
- C# How to: Bi-tonal Bitmaps
- C# How to: Bitmap Colour Tint
- C# How to: Bitmap Colour Shading
- C# How to: Image Solarise
- C# How to: Image Contrast
- C# How to: Bitwise Bitmap Blending
- C# How to: Image Arithmetic
- C# How to: Image Convolution
- C# How to: Image Edge Detection
- C# How to: Difference Of Gaussians
- C# How to: Image Median Filter
- C# How to: Image Colour Average
- C# How to: Image Erosion and Dilation
- C# How to: Morphological Edge Detection
- C# How to: Boolean Edge Detection
- C# How to: Gradient Based Edge Detection
- C# How to: Sharpen Edge Detection
- C# How to: Image Cartoon Effect
- C# How to: Calculating Gaussian Kernels
- C# How to: Image Blur
- C# How to: Image Transform Rotate
- C# How to: Image Transform Shear
- C# How to: Compass Edge Detection
- C# How to: Oil Painting and Cartoon Filter
- C# How to: Stained Glass Image Filter