From 59339644de501ce539a9d35b25ead22a7e7f5518 Mon Sep 17 00:00:00 2001 From: Alberto Griggio Date: Wed, 17 Oct 2018 15:38:35 +0200 Subject: [PATCH] dehaze: do not consider out-of-gamut pixels when estimating the ambient light --- rtengine/ipdehaze.cc | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/rtengine/ipdehaze.cc b/rtengine/ipdehaze.cc index 1c5512c2a..37bd53704 100644 --- a/rtengine/ipdehaze.cc +++ b/rtengine/ipdehaze.cc @@ -58,8 +58,7 @@ namespace { #endif -int get_dark_channel(const array2D &R, const array2D &G, const array2D &B, array2D &dst, - int patchsize, float *ambient, bool multithread) +int get_dark_channel(const array2D &R, const array2D &G, const array2D &B, array2D &dst, int patchsize, float *ambient, bool clip, bool multithread) { const int W = R.width(); const int H = R.height(); @@ -89,7 +88,9 @@ int get_dark_channel(const array2D &R, const array2D &G, const arr } val = min(val, yval); } - val = LIM01(val); + if (clip) { + val = LIM01(val); + } for (int yy = y; yy < pH; ++yy) { std::fill(dst[yy]+x, dst[yy]+pW, val); } @@ -120,7 +121,9 @@ float estimate_ambient_light(const array2D &R, const array2D &G, c std::priority_queue p; for (int y = 0; y < H; y += patchsize) { for (int x = 0; x < W; x += patchsize) { - p.push(dark[y][x]); + if (!OOG(dark[y][x], 1.f)) { + p.push(dark[y][x]); + } } } darklim = get_percentile(p, 0.95); @@ -131,7 +134,7 @@ float estimate_ambient_light(const array2D &R, const array2D &G, c for (int y = 0; y < H; y += patchsize) { for (int x = 0; x < W; x += patchsize) { - if (dark[y][x] >= darklim) { + if (dark[y][x] >= darklim && !OOG(dark[y][x], 1.f)) { patches.push_back(std::make_pair(x, y)); } } @@ -245,7 +248,7 @@ void ImProcFunctions::dehaze(Imagefloat *img) extract_channels(img, R, G, B, patchsize, 1e-1, multiThread); patchsize = max(max(W, H) / 600, 2); - npatches = get_dark_channel(R, G, B, dark, patchsize, nullptr, multiThread); + npatches = get_dark_channel(R, G, B, dark, patchsize, nullptr, false, multiThread); DEBUG_DUMP(dark); max_t = estimate_ambient_light(R, G, B, dark, patchsize, npatches, ambient); @@ -256,7 +259,7 @@ void ImProcFunctions::dehaze(Imagefloat *img) << std::endl; } - get_dark_channel(R, G, B, dark, patchsize, ambient, multiThread); + get_dark_channel(R, G, B, dark, patchsize, ambient, true, multiThread); } if (min(ambient[0], ambient[1], ambient[2]) < 0.01f) { @@ -289,6 +292,10 @@ void ImProcFunctions::dehaze(Imagefloat *img) DEBUG_DUMP(t); + if (options.rtSettings.verbose) { + std::cout << "dehaze: max distance is " << max_t << std::endl; + } + float depth = -float(params->dehaze.depth) / 100.f; const float t0 = max(1e-3f, std::exp(depth * max_t)); const float teps = 1e-3f;