.Net unsharp mask filter

January 20, 2010 Photoshop Image Library C# Snippet Open source

If you're familiar with Photoshop or any other photo manipulation tools you've probably come across a filter called unsharp mask. It works by making a gaussian blur copy of the image and then comparing it to the original image and extruding the differences, which appears around the edges, resulting in a very sharp and crisp image.

This kind of filter is truely awesome if you're creating thumbnails as they useally becomes somewhat blurry. Applying an unsharp mask makes a very nice thumbnail.

And if you're looking to do this with C# code, you've come to the right place. Because this filter was introduced in version 1.1 of the .Net Image Library.



As with most of the filters I've tried to keep as close to the Photoshop filter parameters as possible. So if you're working with an Art Director that wants an unsharp mask with the amount of 32% and a radius of 1.4 pixels, you can just answer "No problem" and add one more line of code to your project.

How to implement

So lets start with the code. But first, head over to C# Image Library and download either the latest source code package or binary version or install through nuget.org.

Add a reference to Kaliko.ImageLibrary.dll or the NuGet package ImageLibrary to your the source code project.

To make this more readable, I'm assuming that you're also have these two lines in your code:

using Kaliko.ImageLibrary;
using Kaliko.ImageLibrary.Filters;

We're assuming the above, that you want the equality to an amount of 32% and a radius of 1.4 pixels in Photoshop. The first parameter passed to UnsharpMaskFilter is the radius, the second the amount.

// Load image from file
KalikoImage image = new KalikoImage("c:\\images\\garden.jpg");

// Create a thumbnail of 128x128 pixels
KalikoImage thumb = image.GetThumbnailImage(128, 128, ThumbnailMethod.Crop);

// Apply unsharpen filter (radius = 1.4, amount = 32%, threshold = 0)
thumb.ApplyFilter(new UnsharpMaskFilter(1.4f, 0.32f, 0));

// Save the thumbnail as JPG in quality 99 (very high)
thumb.SaveJpg("c:\\images\\garden-thumbnail.jpg", 99);

That was pretty easy, wasn't it? :)

Use the faster unsharp mask filter

The core .Net Image Library was written to be compatible with medium trust environments (such as many web hosting providers). But if you can run in a full trust web environment or are developing a Windows application you should definitely check out the package of optimized filters and use them instead. On a Intel i7-2600k the fast filter is about 5 times faster as the compatible filter!

Use the faster filters by adding the ImageLibrary.FastFilters NuGet package to your project (make sure to select pre-release in the NuGet manager to get the latest version). All optimized filters are prefixed with Fast. So instead of UnsharpMaskFilter you use FastUnsharpMaskFilter.

using Kaliko.ImageLibrary;
using Kaliko.ImageLibrary.FastFilters;

// ...

// Load image from file
KalikoImage image = new KalikoImage("c:\\images\\garden.jpg");

// Create a thumbnail of 128x128 pixels
KalikoImage thumb = image.GetThumbnailImage(128, 128, ThumbnailMethod.Crop);

// Apply unsharpen filter (radius = 1.4, amount = 32%, threshold = 0)
thumb.ApplyFilter(new FastUnsharpMaskFilter(1.4f, 0.32f, 0));

// Save the thumbnail as JPG in quality 99 (very high)
thumb.SaveJpg("c:\\images\\garden-thumbnail.jpg", 99);

.NET Image Library also includes easy-to-use filters for:

All these filters are also available as optimized multi-threaded versions.

Related posts: