Sharpening

Description

sharpen sharpens grayscale and RGB images using the unsharp masking (USM) technique.

The idea is to compute a blurred (low frequency) version of the image, subtract it from the original to obtain a high frequency "mask" that highlights edges, and then add that mask back to the original, amplified by a chosen amount. An optional threshold can be used to avoid sharpening noise that would otherwise be mistaken for an edge.

RGB images are converted to the Lab color space, sharpening is applied only to the L* (lightness) channel, leaving a* and b* untouched, and the result is converted back to RGB.

Algorithm

For each pixel I(u, v):

  • Compute I'(u, v), a smoothed version of I obtained by convolving the image with a Gaussian kernel of the given sigma.

  • Compute the local contrast I(u, v) - I'(u, v).

  • If threshold is greater than zero, discard (zero out) mask values whose absolute local contrast is below threshold * max(|local contrast|), so that only sufficiently strong edges get sharpened.

  • Apply the (optionally thresholded) mask back onto the original pixel, scaled by amount:

    I(u, v) = I(u, v) + amount * (I(u, v) - I'(u, v))

Reference: Principles of Digital Image Processing - Fundamental Techniques, Wilhelm Burger, Mark J. Burge.

Parameters

Parameter Description

src_view

Source image view. Must be grayscale or RGB.

dst_view

Destination image view. Must use the same color space as src_view and have the same dimensions.

sigma

Standard deviation of the Gaussian kernel used to compute the blurred image. Must be greater than 0. Larger values sharpen coarser (lower frequency) detail.

amount

Sharpening weight applied to the high frequency mask.

threshold

Minimum local contrast, in the range [0, 1], required for a pixel to be sharpened. Defaults to 0.0, which sharpens every pixel.

Demo

#include <boost/gil.hpp>
#include <boost/gil/extension/io/png.hpp>
#include <boost/gil/image_processing/sharpening.hpp>

using namespace boost::gil;

gray8_image_t img_in;
read_image("input.png", img_in, png_tag{});
gray8_image_t img_out(img_in.dimensions());

sharpen(view(img_in), view(img_out), 1, 3);
write_view("sharpened.png", view(img_out), png_tag{});

See also the full example at example/sharpening.cpp.