Skip to content

Dehazing

Fact Value
Also called Haze removal, Dark channel prior, Defogging
Status Implemented and tested
Family Restoration and reconstruction
Engine Science engine
Type Nonlinear, spatial, dark channel prior haze removal
Output Image
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/artifacts.py, dehaze
Operator code engine/src/pixelnexus_engine/filters/artifacts.py, dehaze_representation
Packages SciPy

Overview

Dehazing removes haze and fog from outdoor photos. It uses the dark channel prior of He, Sun and Tang: in a clear photo, almost every small patch holds a pixel that is dark in at least one color. Haze lifts those dark pixels toward the color of the air, so their brightness tells how thick the haze is. The filter turns that into a transmission map, refines it with a guided filter, and inverts the haze model. It runs in the PixelNexus engine on your computer.

Four panels: colored squares and red disks under a pale haze that thickens toward the top; the dehazed result with strong colors back; the transmission map, dark at the top and light at the bottom, with the squares faintly visible; and the scene without haze. Four panels: colored squares and red disks under a pale haze that thickens toward the top; the dehazed result with strong colors back; the transmission map, dark at the top and light at the bottom, with the squares faintly visible; and the scene without haze.
Colored squares under haze that thickens toward the top, the result with the defaults, the transmission it found, and the scene without haze. The top rows come out a little too saturated, since the filter found a slightly dark air light.

How it works

  1. Compute the dark channel: the darkest color of each pixel, then the darkest value in a square patch around it.
  2. Take the air light A from the brightest 0.1 percent of the dark channel.
  3. Compute a raw transmission from the dark channel of the image divided by A.
  4. Refine the transmission with the guided filter, guided by the luma, so it follows object edges.
  5. Remove the haze with the model, keeping the transmission at t0 or more.

Mathematical foundation

\[ I(x) = J(x)\, t(x) + A\, \big(1 - t(x)\big), \qquad \tilde{t}(x) = 1 - \omega \min_{y \in \Omega(x)} \min_c \frac{I_c(y)}{A_c}, \qquad J(x) = \frac{I(x) - A}{\max\big(t(x), t_0\big)} + A, \qquad \omega = 0.95,\ t_0 = 0.1 \]

The formula shows the default parameter values.

where

  • \(I\): The hazy image, values from 0 to 1
  • \(J\): The scene radiance, the haze-free image
  • \(A\): The air light, one color for the whole image
  • \(t\): The transmission, from 0 (all haze) to 1 (no haze)
  • \(\Omega(x)\): The square patch around pixel x
  • \(\omega\): The share of the haze that is removed
  • \(t_0\): The lowest transmission used in the division

Haze mixes the scene radiance J with the air light A. The transmission t is the share of J that reaches the camera. It falls with distance. He, Sun and Tang observed that in most patches of a clear outdoor photo, at least one color channel is close to 0, in shadows, dark objects or saturated colors. That is the dark channel prior. So the dark channel of the hazy image, divided by A, measures 1 - t. The air light A is taken from the brightest 0.1 percent of the dark channel. It is the pixel there with the highest intensity, here the mean of red, green and blue. The raw transmission t tilde is blocky, since it comes from patches. The first paper refined it with soft matting. Its authors later showed that the guided filter does the same job much faster. Here the guide is the luma. omega keeps a little haze, and t0 keeps the division stable.

The haze model says that each pixel mixes the true color of the scene with the color of the air, in proportions set by the transmission t. Far objects get less of their own light, so t falls with distance. If the scene were known, t would follow. He, Sun and Tang found a statistical shortcut. In outdoor photos, shadows, dark objects and strong colors make the minimum over colors and a small patch close to 0. So in a hazy photo, that minimum, divided by the air light, is about 1 - t. The patches make t blocky. The first paper smoothed it with soft matting, and the same authors later replaced that with the much faster guided filter. The prior fails on large white or sky areas, which have no dark pixel, and the filter then removes too much haze there.

Kernel or operator

The Inspector asks the engine for the stages with the current settings. They are the dark channel, the air light, the raw transmission, the refinement and the recovery.

Algorithm

  1. Compute the dark channel, the minimum over red, green and blue and over a square patch.
  2. Pick the air light A among the brightest 0.1 percent of the dark channel.
  3. Compute the raw transmission from the dark channel of the image divided by A.
  4. Refine the transmission with the guided filter, guided by the luma.
  5. Recover J from the haze model, with the transmission kept at t0 or more.

Parameters

Parameter Default Values Meaning
Output output Dehazed image Dehazed image, Transmission, Dark channel The dehazed image, the refined transmission t from 0 (all haze) to 1 (no haze), or the dark channel of the input.
Patch size patch 15 3 to 51 px The side of the square patch of the minimum filter in the dark channel. The paper uses 15.
Haze kept away omega 0.95 0.5 to 1 The share of the haze that is removed. The paper uses 0.95, so a little haze stays for depth.
Guided filter radius radius 30 1 to 100 px The radius of the box windows of the guided filter that refines the transmission.
Guided filter eps eps 0.001 1e-05 to 0.1 The regularization of the guided filter, for values from 0 to 1.
Lowest transmission t0 0.1 0.01 to 0.5 The transmission is never taken below this value, as in the paper, so thick haze does not blow up noise.
Border mode border Reflect Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.

Implementation

dehaze in engine/src/pixelnexus_engine/filters/artifacts.py calls dehaze_parts, which uses dark_channel, atmospheric_light and guided_filter. The minimum filter and the box means come from scipy.ndimage. The filter works on all three channels at once, on values from 0 to 1. There is no live preview. The result arrives when you click Apply.

Worked example

Omega PSNR in dB
0.80 12.43
0.90 13.42
0.95 14.10
1.00 14.91

Colored squares of 160 by 160 pixels, each with one channel near 0, as the dark channel prior expects, under haze made with the haze model: air light (0.85, 0.88, 0.9) and t = exp(-1.6 depth), with depth 1 at the top and 0 at the bottom. The hazy image has 9.63 dB against the scene. The filter found the air light (0.74, 0.79, 0.73), a little darker than the true one, since even the top row keeps 20 percent of the scene. The refined transmission ranges from 0.16 to 0.94, against 0.20 to 1.00 in the model. All rows use the other defaults.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. The minimum filter and the box means cost the same for any patch size or radius.

Artifacts and limitations

Limitations

  • Bright white objects and sky break the prior, so they can turn too dark or blue.
  • Assumes one air light color for the whole image.
  • Strengthens noise in areas of thick haze.

Artifacts

  • Halos along strong depth edges when the guided filter radius is large.
  • Too much saturation in the far parts of the scene.

Tests

The guided filter must match cv2.ximgproc.guidedFilter of OpenCV, and the dark channel a brute-force minimum. The test scene is made with the haze model, with a black pixel in every patch and a strip of pure air light as sky. On it, the air light must be found exactly. Away from the sky, the transmission and the scene must be recovered. The transmission output must be gray, keep alpha and show the sky as thick haze. The engine API tests run jobs and check the stages of the representation.

References

  • He, K., Sun, J., and Tang, X. Single image haze removal using dark channel prior. IEEE Transactions on Pattern Analysis and Machine Intelligence 33(12), 2341 to 2353, 2011. DOI 10.1109/TPAMI.2010.168.
  • He, K., Sun, J., and Tang, X. Guided image filtering. IEEE Transactions on Pattern Analysis and Machine Intelligence 35(6), 1397 to 1409, 2013. DOI 10.1109/TPAMI.2012.213.