An unsharp mask increases contrast around edges.
dotnet add package RecursiveAI.OneImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneImage; // input.png: soft gray edge, in the zip. Image.Load("input.png") .Sharpen(amount: 1.5, radius: 1.2, threshold: 0.02, linear: false) .Save("sharpened.png"); Console.WriteLine("Wrote sharpened.png");
Wrote sharpened.pngUnzip the example, then dotnet run.
The full program
input.png is a generated 128 × 64 image: a gray value of 80 on the left and 180 on the right, blurred with sigma 1 in stored values. The full program recreates that input before sharpening it.
using OneImage; // Two gray panels with a soft boundary. const int width = 128, height = 64; var pixels = new Rgba32[width * height]; for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) { byte value = (byte)(x < 64 ? 80 : 180); pixels[y * width + x] = new(value, value, value, 255); } Image input = Image.FromPixels<Rgba32>(pixels, width, height) .GaussianBlur(sigma: 1, linear: false); input.Save("input.png"); Image sharpened = input.Sharpen( amount: 1.5, radius: 1.2, threshold: 0.02, linear: false); sharpened.Save("sharpened.png"); var original = input.ToArray<Rgba32>(); var result = sharpened.ToArray<Rgba32>(); Console.WriteLine($"Size: {sharpened.Width}x{sharpened.Height}"); Console.WriteLine($"Edge before: {original[63].R},{original[64].R}"); Console.WriteLine($"Edge after: {result[63].R},{result[64].R}");
dotnet run prints:
Size: 128x64 Edge before: 110,150 Edge after: 99,161
Invalid sharpening parameters throw ArgumentOutOfRangeException; decoding bad input may fail when the lazy result is saved.
Variations
Sharpen in linear light with a higher threshold.
using OneImage; Image.Load("input.png") .Sharpen(amount: 1, radius: 1, threshold: 0.04, linear: true) .Save("linear.png"); Console.WriteLine("Wrote linear.png");
Wrote linear.pngRelated recipes
The short answer
Call Image.Sharpen() and save the returned image. This is unsharp masking: OneImage computes out = in + amount * (in - blur). The blur is Gaussian, and radius is its sigma in pixels. The method does not resize the image. It applies lazily to every frame, so saving or reading pixels actually computes the filter.
The example uses linear: false for conventional sharpening in stored values. That choice is explicit because OneImage defaults to linear-light filtering. Alpha is not sharpened. Keep a copy of the input and compare both edges and flat areas before choosing settings for a batch.
How can unsharp masking be used in image processing?
It emphasizes differences between a pixel and its blurred neighborhood. Where the image is flat, the difference is small. Around a transition, adding that difference makes the dark side darker and the light side lighter. The generated sample has a deliberately softened vertical gray edge so the change is easy to inspect.
Before sharpening, its two central samples are 110 and 150. After sharpening, they are 99 and 161. The dimensions remain 128 × 64. This shows increased edge contrast on this fixture, not recovery of a lost feature. Sharpening cannot reconstruct detail that is absent from the source. It also does not reverse motion blur simply by increasing the amount.
Use the saved output as a new image rather than assuming the source instance changes. OneImage’s edit pipeline is lazy and returns a result. Saving the original instance instead would skip the sharpening operation.
What are the best settings for Unsharp Mask?
There is no one setting for all images. amount scales the edge difference. radius decides the neighborhood size through the Gaussian sigma. threshold leaves differences below a fraction of full scale alone; 0.02 means a difference of two percent of full scale, not two pixel-value units. Raising it can leave small differences untouched, but it is not a dedicated noise-removal operation.
The example’s amount 1.5, radius 1.2 and threshold 0.02 are settings for a synthetic edge, not a universal photo preset. Use an image at its intended output dimensions when choosing them. Look for conspicuous outlines, clipped highlights and rough texture as well as apparent detail. A larger amount is not automatically a better result.
Working space changes the look. The pinned guide says linear-light sharpening gives stronger halos on the dark side of edges. Passing linear: false requests the conventional stored-value look. The variation uses linear light so you can compare the two choices on the same supplied input rather than accidentally relying on a default.
The default edge mode clamps to the nearest edge pixel. Mirror and constant edges are also supported; constant means transparent black and can darken opaque image edges. Wrap is not supported by these filters. On a crop with important content touching its border, this boundary decision can be as visible as changing the amount.
How do I unsharpen an image?
If you still have the original, return to it and choose gentler settings. Applying a blur to an already sharpened result is not an exact inverse, especially after clipping or saving to a lossy format. Keep the input unchanged and produce separate output files while experimenting. The full program follows that pattern by writing input.png and sharpened.png separately.