revamped and simplified dehaze -- now it's finally usable

This commit is contained in:
Alberto Griggio
2018-10-16 23:20:11 +02:00
parent 7c10f92ace
commit 4d0ddd56e5
8 changed files with 87 additions and 237 deletions

View File

@@ -729,7 +729,6 @@ HISTORY_MSG_493;L*a*b* Adjustments
HISTORY_MSG_CLAMPOOG;Clip out-of-gamut colors
HISTORY_MSG_COLORTONING_LABGRID_VALUE;CT - Color correction
HISTORY_MSG_DEHAZE_DEPTH;Dehaze - Depth
HISTORY_MSG_DEHAZE_DETAIL;Dehaze - Detail
HISTORY_MSG_DEHAZE_ENABLED;Haze Removal
HISTORY_MSG_DEHAZE_SHOW_DEPTH_MAP;Dehaze - Show depth map
HISTORY_MSG_DEHAZE_STRENGTH;Dehaze - Strength
@@ -1512,7 +1511,6 @@ TP_DEFRINGE_LABEL;Defringe
TP_DEFRINGE_RADIUS;Radius
TP_DEFRINGE_THRESHOLD;Threshold
TP_DEHAZE_DEPTH;Depth
TP_DEHAZE_DETAIL;Detail
TP_DEHAZE_LABEL;Haze Removal
TP_DEHAZE_SHOW_DEPTH_MAP;Show Depth Map
TP_DEHAZE_STRENGTH;Strength

View File

@@ -70,10 +70,10 @@ int get_dark_channel(const array2D<float> &R, const array2D<float> &G, const arr
#pragma omp parallel for if (multithread)
#endif
for (int y = 0; y < H; y += patchsize) {
int pH = std::min(y+patchsize, H);
int pH = min(y+patchsize, H);
for (int x = 0; x < W; x += patchsize, ++npatches) {
float val = RT_INFINITY_F;
int pW = std::min(x+patchsize, W);
int pW = min(x+patchsize, W);
for (int yy = y; yy < pH; ++yy) {
float yval = RT_INFINITY_F;
for (int xx = x; xx < pW; ++xx) {
@@ -93,34 +93,6 @@ int get_dark_channel(const array2D<float> &R, const array2D<float> &G, const arr
for (int yy = y; yy < pH; ++yy) {
std::fill(dst[yy]+x, dst[yy]+pW, val);
}
float val2 = RT_INFINITY_F;
for (int yy = y; yy < pH; ++yy) {
for (int xx = x; xx < pW; ++xx) {
float r = R[yy][xx];
float g = G[yy][xx];
float b = B[yy][xx];
if (ambient) {
r /= ambient[0];
g /= ambient[1];
b /= ambient[2];
}
float l = min(r, g, b);
if (l >= 2.f * val) {
val2 = min(val2, l);
dst[yy][xx] = -1;
}
}
}
if (val2 < RT_INFINITY_F) {
val2 = LIM01(val2);
for (int yy = y; yy < pH; ++yy) {
for (int xx = x; xx < pW; ++xx) {
if (dst[yy][xx] < 0.f) {
dst[yy][xx] = val2;
}
}
}
}
}
}
@@ -128,7 +100,7 @@ int get_dark_channel(const array2D<float> &R, const array2D<float> &G, const arr
}
int estimate_ambient_light(const array2D<float> &R, const array2D<float> &G, const array2D<float> &B, const array2D<float> &dark, const array2D<float> &Y, int patchsize, int npatches, float ambient[3])
float estimate_ambient_light(const array2D<float> &R, const array2D<float> &G, const array2D<float> &B, const array2D<float> &dark, int patchsize, int npatches, float ambient[3])
{
const int W = R.width();
const int H = R.height();
@@ -143,7 +115,7 @@ int estimate_ambient_light(const array2D<float> &R, const array2D<float> &G, con
return q.top();
};
float lim = RT_INFINITY_F;
float darklim = RT_INFINITY_F;
{
std::priority_queue<float> p;
for (int y = 0; y < H; y += patchsize) {
@@ -151,7 +123,7 @@ int estimate_ambient_light(const array2D<float> &R, const array2D<float> &G, con
p.push(dark[y][x]);
}
}
lim = get_percentile(p, 0.95);
darklim = get_percentile(p, 0.95);
}
std::vector<std::pair<int, int>> patches;
@@ -159,7 +131,7 @@ int estimate_ambient_light(const array2D<float> &R, const array2D<float> &G, con
for (int y = 0; y < H; y += patchsize) {
for (int x = 0; x < W; x += patchsize) {
if (dark[y][x] >= lim) {
if (dark[y][x] >= darklim) {
patches.push_back(std::make_pair(x, y));
}
}
@@ -170,35 +142,36 @@ int estimate_ambient_light(const array2D<float> &R, const array2D<float> &G, con
<< " patches" << std::endl;
}
float bright_lim = RT_INFINITY_F;
{
std::priority_queue<float> l;
for (auto &p : patches) {
const int pW = std::min(p.first+patchsize, W);
const int pH = std::min(p.second+patchsize, H);
const int pW = min(p.first+patchsize, W);
const int pH = min(p.second+patchsize, H);
for (int y = p.second; y < pH; ++y) {
for (int x = p.first; x < pW; ++x) {
l.push(Y[y][x]);
l.push(R[y][x] + G[y][x] + B[y][x]);
}
}
}
lim = get_percentile(l, 0.95);
bright_lim = get_percentile(l, 0.95);
}
double rr = 0, gg = 0, bb = 0;
int n = 0;
for (auto &p : patches) {
const int pW = std::min(p.first+patchsize, W);
const int pH = std::min(p.second+patchsize, H);
const int pW = min(p.first+patchsize, W);
const int pH = min(p.second+patchsize, H);
for (int y = p.second; y < pH; ++y) {
for (int x = p.first; x < pW; ++x) {
if (Y[y][x] >= lim) {
float r = R[y][x];
float g = G[y][x];
float b = B[y][x];
if (r + g + b >= bright_lim) {
rr += r;
gg += g;
bb += b;
@@ -211,65 +184,12 @@ int estimate_ambient_light(const array2D<float> &R, const array2D<float> &G, con
ambient[1] = gg / n;
ambient[2] = bb / n;
return n;
// taken from darktable
return darklim > 0 ? -1.125f * std::log(darklim) : std::log(std::numeric_limits<float>::max()) / 2;
}
void get_luminance(Imagefloat *img, array2D<float> &Y, TMatrix ws, bool multithread)
{
const int W = img->getWidth();
const int H = img->getHeight();
#ifdef _OPENMP
#pragma omp parallel for if (multithread)
#endif
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
Y[y][x] = Color::rgbLuminance(img->r(y, x), img->g(y, x), img->b(y, x), ws);
}
}
}
void apply_contrast(array2D<float> &dark, float ambient, int contrast, double scale, bool multithread)
{
if (contrast) {
const int W = dark.width();
const int H = dark.height();
float avg = ambient * 0.25f;
float c = contrast * 0.3f;
std::vector<double> pts = {
DCT_NURBS,
0, //black point. Value in [0 ; 1] range
0, //black point. Value in [0 ; 1] range
avg - avg * (0.6 - c / 250.0), //toe point
avg - avg * (0.6 + c / 250.0), //value at toe point
avg + (1 - avg) * (0.6 - c / 250.0), //shoulder point
avg + (1 - avg) * (0.6 + c / 250.0), //value at shoulder point
1., // white point
1. // value at white point
};
const DiagonalCurve curve(pts, CURVES_MIN_POLY_POINTS / scale);
#ifdef _OPENMP
#pragma omp parallel for if (multithread)
#endif
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
dark[y][x] = curve.getVal(dark[y][x]);
}
}
}
}
void extract_channels(Imagefloat *img, const array2D<float> &Y, array2D<float> &r, array2D<float> &g, array2D<float> &b, int radius, float epsilon, bool multithread)
void extract_channels(Imagefloat *img, array2D<float> &r, array2D<float> &g, array2D<float> &b, int radius, float epsilon, bool multithread)
{
const int W = img->getWidth();
const int H = img->getHeight();
@@ -285,11 +205,12 @@ void extract_channels(Imagefloat *img, const array2D<float> &Y, array2D<float> &
}
}
guidedFilter(Y, r, r, radius, epsilon, multithread);
guidedFilter(Y, g, g, radius, epsilon, multithread);
guidedFilter(Y, b, b, radius, epsilon, multithread);
guidedFilter(r, r, r, radius, epsilon, multithread, radius / 2);
guidedFilter(g, g, g, radius, epsilon, multithread, radius / 2);
guidedFilter(b, b, b, radius, epsilon, multithread, radius / 2);
}
} // namespace
@@ -309,31 +230,33 @@ void ImProcFunctions::dehaze(Imagefloat *img)
std::cout << "dehaze: strength = " << strength << std::endl;
}
TMatrix ws = ICCStore::getInstance()->workingSpaceMatrix(params->icm.workingProfile);
array2D<float> Y(W, H);
get_luminance(img, Y, ws, multiThread);
array2D<float> dark(W, H);
int patchsize = max(int(5 / scale), 2);
int npatches = 0;
float ambient[3];
array2D<float> &t_tilde = dark;
float max_t = 0.f;
{
array2D<float> R(W, H);
array2D<float> G(W, H);
array2D<float> B(W, H);
int patchsize = max(int(20 / scale), 2);
extract_channels(img, Y, R, G, B, patchsize, 1e-1, multiThread);
extract_channels(img, R, G, B, patchsize, 1e-1, multiThread);
array2D<float> dark(W, H);
patchsize = std::max(W / (200 + params->dehaze.detail * (SGN(params->dehaze.detail) > 0 ? 4 : 1)), 2);
int npatches = get_dark_channel(R, G, B, dark, patchsize, nullptr, multiThread);
patchsize = max(max(W, H) / 600, 2);
npatches = get_dark_channel(R, G, B, dark, patchsize, nullptr, multiThread);
DEBUG_DUMP(dark);
float ambient[3];
int n = estimate_ambient_light(R, G, B, dark, Y, patchsize, npatches, ambient);
float ambient_Y = Color::rgbLuminance(ambient[0], ambient[1], ambient[2], ws);
max_t = estimate_ambient_light(R, G, B, dark, patchsize, npatches, ambient);
if (options.rtSettings.verbose) {
std::cout << "dehaze: ambient light is "
<< ambient[0] << ", " << ambient[1] << ", " << ambient[2]
<< " (average of " << n << ")"
<< std::endl;
std::cout << " ambient luminance is " << ambient_Y << std::endl;
}
get_dark_channel(R, G, B, dark, patchsize, ambient, multiThread);
}
if (min(ambient[0], ambient[1], ambient[2]) < 0.01f) {
@@ -344,12 +267,8 @@ void ImProcFunctions::dehaze(Imagefloat *img)
return; // probably no haze at all
}
array2D<float> &t_tilde = dark;
get_dark_channel(R, G, B, dark, patchsize, ambient, multiThread);
apply_contrast(dark, ambient_Y, params->dehaze.depth, scale, multiThread);
DEBUG_DUMP(t_tilde);
if (!params->dehaze.showDepthMap) {
#ifdef _OPENMP
#pragma omp parallel for if (multiThread)
#endif
@@ -358,45 +277,31 @@ void ImProcFunctions::dehaze(Imagefloat *img)
dark[y][x] = 1.f - strength * dark[y][x];
}
}
}
float mult = 2.f;
if (params->dehaze.detail > 0) {
mult -= (params->dehaze.detail / 100.f) * 1.9f;
} else {
mult -= params->dehaze.detail / 10.f;
}
const int radius = max(int(patchsize * mult), 1);
const float epsilon = 2.5e-4;
const int radius = patchsize * 4;
const float epsilon = 1e-7;
array2D<float> &t = t_tilde;
if (!params->dehaze.showDepthMap)
guidedFilter(Y, t_tilde, t, radius, epsilon, multiThread);
{
array2D<float> guideB(W, H, img->b.ptrs, ARRAY2D_BYREFERENCE);
guidedFilter(guideB, t_tilde, t, radius, epsilon, multiThread, patchsize);
}
DEBUG_DUMP(t);
if (params->dehaze.showDepthMap) {
#ifdef _OPENMP
#pragma omp parallel for if (multiThread)
#endif
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
img->r(y, x) = img->g(y, x) = img->b(y, x) = t[y][x] * 65535.f;
}
}
return;
}
const float t0 = 0.1;
const float teps = 1e-3;
float depth = -float(params->dehaze.depth) / 100.f;
const float t0 = max(1e-3f, std::exp(depth * max_t));
const float teps = 1e-3f;
#ifdef _OPENMP
#pragma omp parallel for if (multiThread)
#endif
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
// ensure that the transmission is such that to avoid clipping...
float rgb[3] = { img->r(y, x), img->g(y, x), img->b(y, x) };
// ... t >= tl to avoid negative values
float tl = 1.f - min(rgb[0]/ambient[0], rgb[1]/ambient[1], rgb[2]/ambient[2]);
// ... t >= tu to avoid values > 1
float tu = t0 - teps;
for (int c = 0; c < 3; ++c) {
if (ambient[c] < 1) {
@@ -404,6 +309,9 @@ void ImProcFunctions::dehaze(Imagefloat *img)
}
}
float mt = max(t[y][x], t0, tl + teps, tu + teps);
if (params->dehaze.showDepthMap) {
img->r(y, x) = img->g(y, x) = img->b(y, x) = 1.f - mt;
} else {
float r = (rgb[0] - ambient[0]) / mt + ambient[0];
float g = (rgb[1] - ambient[1]) / mt + ambient[1];
float b = (rgb[2] - ambient[2]) / mt + ambient[2];
@@ -413,36 +321,10 @@ void ImProcFunctions::dehaze(Imagefloat *img)
img->b(y, x) = b;
}
}
float oldmed;
findMinMaxPercentile(Y, Y.width() * Y.height(), 0.5, oldmed, 0.5, oldmed, multiThread);
get_luminance(img, Y, ws, multiThread);
float newmed;
findMinMaxPercentile(Y, Y.width() * Y.height(), 0.5, newmed, 0.5, newmed, multiThread);
if (newmed > 1e-5f) {
const float f1 = oldmed / newmed;
const float f = f1 * 65535.f;
#ifdef _OPENMP
#pragma omp parallel for if (multiThread)
#endif
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
float r = img->r(y, x);
float g = img->g(y, x);
float b = img->b(y, x);
float h, s, l;
Color::rgb2hslfloat(r * f, g * f, b * f, h, s, l);
s = LIM01(s / f1);
Color::hsl2rgbfloat(h, s, l, img->r(y, x), img->g(y, x), img->b(y, x));
}
}
} else {
img->normalizeFloatTo65535();
}
}
} // namespace rtengine

View File

@@ -2378,8 +2378,7 @@ DehazeParams::DehazeParams() :
enabled(false),
strength(50),
showDepthMap(false),
depth(0),
detail(0)
depth(25)
{
}
@@ -2389,8 +2388,7 @@ bool DehazeParams::operator ==(const DehazeParams& other) const
enabled == other.enabled
&& strength == other.strength
&& showDepthMap == other.showDepthMap
&& depth == other.depth
&& detail == other.detail;
&& depth == other.depth;
}
bool DehazeParams::operator !=(const DehazeParams& other) const
@@ -3070,7 +3068,6 @@ int ProcParams::save(const Glib::ustring& fname, const Glib::ustring& fname2, bo
saveToKeyfile(!pedited || pedited->dehaze.strength, "Dehaze", "Strength", dehaze.strength, keyFile);
saveToKeyfile(!pedited || pedited->dehaze.showDepthMap, "Dehaze", "ShowDepthMap", dehaze.showDepthMap, keyFile);
saveToKeyfile(!pedited || pedited->dehaze.depth, "Dehaze", "Depth", dehaze.depth, keyFile);
saveToKeyfile(!pedited || pedited->dehaze.detail, "Dehaze", "Detail", dehaze.detail, keyFile);
// Directional pyramid denoising
saveToKeyfile(!pedited || pedited->dirpyrDenoise.enabled, "Directional Pyramid Denoising", "Enabled", dirpyrDenoise.enabled, keyFile);
@@ -4658,7 +4655,6 @@ int ProcParams::load(const Glib::ustring& fname, ParamsEdited* pedited)
assignFromKeyfile(keyFile, "Dehaze", "Strength", pedited, dehaze.strength, pedited->dehaze.strength);
assignFromKeyfile(keyFile, "Dehaze", "ShowDepthMap", pedited, dehaze.showDepthMap, pedited->dehaze.showDepthMap);
assignFromKeyfile(keyFile, "Dehaze", "Depth", pedited, dehaze.depth, pedited->dehaze.depth);
assignFromKeyfile(keyFile, "Dehaze", "Detail", pedited, dehaze.detail, pedited->dehaze.detail);
}
if (keyFile.has_group("Film Simulation")) {

View File

@@ -1232,7 +1232,6 @@ struct DehazeParams {
int strength;
bool showDepthMap;
int depth;
int detail;
DehazeParams();

View File

@@ -32,27 +32,21 @@ Dehaze::Dehaze(): FoldableToolPanel(this, "dehaze", M("TP_DEHAZE_LABEL"), false,
EvDehazeStrength = m->newEvent(HDR, "HISTORY_MSG_DEHAZE_STRENGTH");
EvDehazeShowDepthMap = m->newEvent(HDR, "HISTORY_MSG_DEHAZE_SHOW_DEPTH_MAP");
EvDehazeDepth = m->newEvent(HDR, "HISTORY_MSG_DEHAZE_DEPTH");
EvDehazeDetail = m->newEvent(HDR, "HISTORY_MSG_DEHAZE_DETAIL");
strength = Gtk::manage(new Adjuster(M("TP_DEHAZE_STRENGTH"), 0., 100., 1., 50.));
strength->setAdjusterListener(this);
strength->show();
depth = Gtk::manage(new Adjuster(M("TP_DEHAZE_DEPTH"), -100., 100., 1., 0.));
depth = Gtk::manage(new Adjuster(M("TP_DEHAZE_DEPTH"), 0., 100., 1., 25.));
depth->setAdjusterListener(this);
depth->show();
detail = Gtk::manage(new Adjuster(M("TP_DEHAZE_DETAIL"), -100, 100, 1, 0));
detail->setAdjusterListener(this);
detail->show();
showDepthMap = Gtk::manage(new Gtk::CheckButton(M("TP_DEHAZE_SHOW_DEPTH_MAP")));
showDepthMap->signal_toggled().connect(sigc::mem_fun(*this, &Dehaze::showDepthMapChanged));
showDepthMap->show();
pack_start(*strength);
pack_start(*depth);
pack_start(*detail);
pack_start(*showDepthMap);
}
@@ -64,7 +58,6 @@ void Dehaze::read(const ProcParams *pp, const ParamsEdited *pedited)
if (pedited) {
strength->setEditedState(pedited->dehaze.strength ? Edited : UnEdited);
depth->setEditedState(pedited->dehaze.depth ? Edited : UnEdited);
detail->setEditedState(pedited->dehaze.detail ? Edited : UnEdited);
set_inconsistent(multiImage && !pedited->dehaze.enabled);
showDepthMap->set_inconsistent(!pedited->dehaze.showDepthMap);
}
@@ -72,7 +65,6 @@ void Dehaze::read(const ProcParams *pp, const ParamsEdited *pedited)
setEnabled(pp->dehaze.enabled);
strength->setValue(pp->dehaze.strength);
depth->setValue(pp->dehaze.depth);
detail->setValue(pp->dehaze.detail);
showDepthMap->set_active(pp->dehaze.showDepthMap);
enableListener();
@@ -83,14 +75,12 @@ void Dehaze::write(ProcParams *pp, ParamsEdited *pedited)
{
pp->dehaze.strength = strength->getValue();
pp->dehaze.depth = depth->getValue();
pp->dehaze.detail = detail->getValue();
pp->dehaze.enabled = getEnabled();
pp->dehaze.showDepthMap = showDepthMap->get_active();
if (pedited) {
pedited->dehaze.strength = strength->getEditedState();
pedited->dehaze.depth = depth->getEditedState();
pedited->dehaze.detail = detail->getEditedState();
pedited->dehaze.enabled = !get_inconsistent();
pedited->dehaze.showDepthMap = !showDepthMap->get_inconsistent();
}
@@ -100,16 +90,13 @@ void Dehaze::setDefaults(const ProcParams *defParams, const ParamsEdited *pedite
{
strength->setDefault(defParams->dehaze.strength);
depth->setDefault(defParams->dehaze.depth);
detail->setDefault(defParams->dehaze.detail);
if (pedited) {
strength->setDefaultEditedState(pedited->dehaze.strength ? Edited : UnEdited);
depth->setDefaultEditedState(pedited->dehaze.depth ? Edited : UnEdited);
detail->setDefaultEditedState(pedited->dehaze.detail ? Edited : UnEdited);
} else {
strength->setDefaultEditedState(Irrelevant);
depth->setDefaultEditedState(Irrelevant);
detail->setDefaultEditedState(Irrelevant);
}
}
@@ -121,8 +108,6 @@ void Dehaze::adjusterChanged(Adjuster* a, double newval)
listener->panelChanged(EvDehazeStrength, a->getTextValue());
} else if (a == depth) {
listener->panelChanged(EvDehazeDepth, a->getTextValue());
} else if (a == detail) {
listener->panelChanged(EvDehazeDetail, a->getTextValue());
}
}
}
@@ -156,7 +141,6 @@ void Dehaze::setBatchMode(bool batchMode)
strength->showEditedCB();
depth->showEditedCB();
detail->showEditedCB();
}

View File

@@ -28,13 +28,11 @@ class Dehaze: public ToolParamBlock, public AdjusterListener, public FoldableToo
private:
Adjuster *strength;
Adjuster *depth;
Adjuster *detail;
Gtk::CheckButton *showDepthMap;
rtengine::ProcEvent EvDehazeEnabled;
rtengine::ProcEvent EvDehazeStrength;
rtengine::ProcEvent EvDehazeDepth;
rtengine::ProcEvent EvDehazeDetail;
rtengine::ProcEvent EvDehazeShowDepthMap;
public:

View File

@@ -570,7 +570,6 @@ void ParamsEdited::set(bool v)
dehaze.strength = v;
dehaze.showDepthMap = v;
dehaze.depth = v;
dehaze.detail = v;
metadata.mode = v;
exif = v;
@@ -1128,7 +1127,6 @@ void ParamsEdited::initFrom(const std::vector<rtengine::procparams::ProcParams>&
dehaze.strength = dehaze.strength && p.dehaze.strength == other.dehaze.strength;
dehaze.showDepthMap = dehaze.showDepthMap && p.dehaze.showDepthMap == other.dehaze.showDepthMap;
dehaze.depth = dehaze.depth && p.dehaze.depth == other.dehaze.depth;
dehaze.detail = dehaze.detail && p.dehaze.detail == other.dehaze.detail;
metadata.mode = metadata.mode && p.metadata.mode == other.metadata.mode;
// How the hell can we handle that???
@@ -3135,10 +3133,6 @@ void ParamsEdited::combine(rtengine::procparams::ProcParams& toEdit, const rteng
toEdit.dehaze.depth = mods.dehaze.depth;
}
if (dehaze.detail) {
toEdit.dehaze.detail = mods.dehaze.detail;
}
if (dehaze.showDepthMap) {
toEdit.dehaze.showDepthMap = mods.dehaze.showDepthMap;
}

View File

@@ -731,7 +731,6 @@ public:
bool strength;
bool showDepthMap;
bool depth;
bool detail;
};