Skip to content

Advanced enhancement, local contrast, and edge detection

Copy page

Enhancement is not a single “make it better” switch. Each variant shows the generated output first, followed by the corresponding binding code.

sigma: 2 reduces high-frequency detail. The reference output was generated with govips v2.20.0 / libvips 8.17.3 in the locked Docker Runner.

Night architecture photo softened with Gaussian blur (Sigma 4.0)
WEBP1938 × 1292, 95,724 bytes, 3 bands

Generated with govips v2.20.0 / libvips 8.17.3.

image, err := vips.NewImageFromFile("input.jpg")
if err != nil { return err }
defer image.Close()
if err := image.GaussianBlur(2); err != nil { return err }
data, _, err := image.ExportWebp(vips.NewWebpExportParams())
if err != nil { return err }
return os.WriteFile("blur.webp", data, 0o644)

The example uses sigma: 2 and explicit mask parameters. Sharpening can also amplify noise and compression artefacts.

Night architecture photo sharpened on the LAB lightness channel
WEBP1938 × 1292, 621,814 bytes, 3 bands

Generated with govips v2.20.0 / libvips 8.17.3.

if err := image.Sharpen(2, 2, 3); err != nil { return err }
data, _, err := image.ExportWebp(vips.NewWebpExportParams())
if err != nil { return err }
return os.WriteFile("sharpen.webp", data, 0o644)

1.5 is this example’s exponent, not a universal default.

Night architecture photo after gamma and linear contrast adjustment
WEBP1938 × 1292, 493,340 bytes, 3 bands

Generated with govips v2.20.0 / libvips 8.17.3.

if err := image.Gamma(1.5); err != nil { return err }
data, _, err := image.ExportWebp(vips.NewWebpExportParams())
if err != nil { return err }
return os.WriteFile("gamma.webp", data, 0o644)

govips and the CLI use Sobel directly; sharp and pyvips execute the same Sobel kernel with convolve() / conv(). All four outputs were generated, but convolution borders and encoding differences still need comparison in the target use case.

Single-band grayscale Sobel edge detection output of the night architecture photo
WEBP1938 × 1292, 945,172 bytes, 3 bands

Generated with govips v2.20.0 / libvips 8.17.3.

if err := image.Sobel(); err != nil { return err }
data, _, err := image.ExportWebp(vips.NewWebpExportParams())
if err != nil { return err }
return os.WriteFile("edge.webp", data, 0o644)

Local contrast (CLAHE) and histogram normalization across bindings

Section titled “Local contrast (CLAHE) and histogram normalization across bindings”

sharp exposes normalise(), clahe({ width, height, maxSlope }), and arbitrary convolve(). Because CLAHE maps to vips_hist_local in libvips, pyvips (image.hist_local(w, h, max_slope=...)) and the vips CLI (vips hist_local) also support CLAHE and histogram equalization (hist_equal / hist_norm) natively. govips v2.19.0 exports global histogram methods (HistogramFind(), HistogramCumulative(), and HistogramNormalise()), but does not wrap internal vipsGenHistLocal on ImageRef, so internal generated functions should not be treated as a public application API.