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 ofIobtained by convolving the image with a Gaussian kernel of the givensigma. -
Compute the local contrast
I(u, v) - I'(u, v). -
If
thresholdis greater than zero, discard (zero out) mask values whose absolute local contrast is belowthreshold * 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 |
|---|---|
|
Source image view. Must be grayscale or RGB. |
|
Destination image view. Must use the same color space as |
|
Standard deviation of the Gaussian kernel used to compute the blurred image.
Must be greater than |
|
Sharpening weight applied to the high frequency mask. |
|
Minimum local contrast, in the range |
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.