dehaze: improved use of the guided filter for less halos
This commit is contained in:
@@ -58,28 +58,28 @@ namespace {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
int get_dark_channel(const Imagefloat &src, array2D<float> &dst,
|
int get_dark_channel(const array2D<float> &R, const array2D<float> &G, const array2D<float> &B, array2D<float> &dst,
|
||||||
int patchsize, float *ambient, bool multithread)
|
int patchsize, float *ambient, bool multithread)
|
||||||
{
|
{
|
||||||
const int w = src.getWidth();
|
const int W = R.width();
|
||||||
const int h = src.getHeight();
|
const int H = R.height();
|
||||||
|
|
||||||
int npatches = 0;
|
int npatches = 0;
|
||||||
|
|
||||||
#ifdef _OPENMP
|
#ifdef _OPENMP
|
||||||
#pragma omp parallel for if (multithread)
|
#pragma omp parallel for if (multithread)
|
||||||
#endif
|
#endif
|
||||||
for (int y = 0; y < src.getHeight(); y += patchsize) {
|
for (int y = 0; y < H; y += patchsize) {
|
||||||
int pH = std::min(y+patchsize, h);
|
int pH = std::min(y+patchsize, H);
|
||||||
for (int x = 0; x < src.getWidth(); x += patchsize, ++npatches) {
|
for (int x = 0; x < W; x += patchsize, ++npatches) {
|
||||||
float val = RT_INFINITY_F;
|
float val = RT_INFINITY_F;
|
||||||
int pW = std::min(x+patchsize, w);
|
int pW = std::min(x+patchsize, W);
|
||||||
for (int yy = y; yy < pH; ++yy) {
|
for (int yy = y; yy < pH; ++yy) {
|
||||||
float yval = RT_INFINITY_F;
|
float yval = RT_INFINITY_F;
|
||||||
for (int xx = x; xx < pW; ++xx) {
|
for (int xx = x; xx < pW; ++xx) {
|
||||||
float r = src.r(yy, xx);
|
float r = R[yy][xx];
|
||||||
float g = src.g(yy, xx);
|
float g = G[yy][xx];
|
||||||
float b = src.b(yy, xx);
|
float b = B[yy][xx];
|
||||||
if (ambient) {
|
if (ambient) {
|
||||||
r /= ambient[0];
|
r /= ambient[0];
|
||||||
g /= ambient[1];
|
g /= ambient[1];
|
||||||
@@ -89,14 +89,16 @@ int get_dark_channel(const Imagefloat &src, array2D<float> &dst,
|
|||||||
}
|
}
|
||||||
val = min(val, yval);
|
val = min(val, yval);
|
||||||
}
|
}
|
||||||
|
val = LIM01(val);
|
||||||
for (int yy = y; yy < pH; ++yy) {
|
for (int yy = y; yy < pH; ++yy) {
|
||||||
std::fill(dst[yy]+x, dst[yy]+pW, val);
|
std::fill(dst[yy]+x, dst[yy]+pW, val);
|
||||||
}
|
}
|
||||||
|
float val2 = RT_INFINITY_F;
|
||||||
for (int yy = y; yy < pH; ++yy) {
|
for (int yy = y; yy < pH; ++yy) {
|
||||||
for (int xx = x; xx < pW; ++xx) {
|
for (int xx = x; xx < pW; ++xx) {
|
||||||
float r = src.r(yy, xx);
|
float r = R[yy][xx];
|
||||||
float g = src.g(yy, xx);
|
float g = G[yy][xx];
|
||||||
float b = src.b(yy, xx);
|
float b = B[yy][xx];
|
||||||
if (ambient) {
|
if (ambient) {
|
||||||
r /= ambient[0];
|
r /= ambient[0];
|
||||||
g /= ambient[1];
|
g /= ambient[1];
|
||||||
@@ -104,7 +106,18 @@ int get_dark_channel(const Imagefloat &src, array2D<float> &dst,
|
|||||||
}
|
}
|
||||||
float l = min(r, g, b);
|
float l = min(r, g, b);
|
||||||
if (l >= 2.f * val) {
|
if (l >= 2.f * val) {
|
||||||
dst[yy][xx] = l;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,10 +128,10 @@ int get_dark_channel(const Imagefloat &src, array2D<float> &dst,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int estimate_ambient_light(const Imagefloat *img, const array2D<float> &dark, const array2D<float> &Y, int patchsize, int npatches, float ambient[3])
|
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])
|
||||||
{
|
{
|
||||||
const int W = img->getWidth();
|
const int W = R.width();
|
||||||
const int H = img->getHeight();
|
const int H = R.height();
|
||||||
|
|
||||||
const auto get_percentile =
|
const auto get_percentile =
|
||||||
[](std::priority_queue<float> &q, float prcnt) -> float
|
[](std::priority_queue<float> &q, float prcnt) -> float
|
||||||
@@ -183,9 +196,9 @@ int estimate_ambient_light(const Imagefloat *img, const array2D<float> &dark, co
|
|||||||
for (int y = p.second; y < pH; ++y) {
|
for (int y = p.second; y < pH; ++y) {
|
||||||
for (int x = p.first; x < pW; ++x) {
|
for (int x = p.first; x < pW; ++x) {
|
||||||
if (Y[y][x] >= lim) {
|
if (Y[y][x] >= lim) {
|
||||||
float r = img->r(y, x);
|
float r = R[y][x];
|
||||||
float g = img->g(y, x);
|
float g = G[y][x];
|
||||||
float b = img->b(y, x);
|
float b = B[y][x];
|
||||||
rr += r;
|
rr += r;
|
||||||
gg += g;
|
gg += g;
|
||||||
bb += b;
|
bb += b;
|
||||||
@@ -218,41 +231,25 @@ void get_luminance(Imagefloat *img, array2D<float> &Y, TMatrix ws, bool multithr
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void apply_contrast(array2D<float> &dark, int contrast, double scale, bool multithread)
|
void apply_contrast(array2D<float> &dark, float ambient, int contrast, double scale, bool multithread)
|
||||||
{
|
{
|
||||||
if (contrast) {
|
if (contrast) {
|
||||||
const int W = dark.width();
|
const int W = dark.width();
|
||||||
const int H = dark.height();
|
const int H = dark.height();
|
||||||
|
|
||||||
double tot = 0.0;
|
float avg = ambient * 0.25f;
|
||||||
#ifdef _OPENMP
|
float c = contrast * 0.3f;
|
||||||
#pragma omp parallel for if (multithread)
|
|
||||||
#endif
|
|
||||||
for (int y = 0; y < H; ++y) {
|
|
||||||
double ytot = 0.0;
|
|
||||||
for (int x = 0; x < W; ++x) {
|
|
||||||
ytot += dark[y][x];
|
|
||||||
}
|
|
||||||
#ifdef _OPENMP
|
|
||||||
#pragma omp critical
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
tot += ytot;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float avg = tot / (W * H);
|
|
||||||
|
|
||||||
std::vector<double> pts = {
|
std::vector<double> pts = {
|
||||||
DCT_NURBS,
|
DCT_NURBS,
|
||||||
0, //black point. Value in [0 ; 1] range
|
0, //black point. Value in [0 ; 1] range
|
||||||
0, //black point. Value in [0 ; 1] range
|
0, //black point. Value in [0 ; 1] range
|
||||||
|
|
||||||
avg - avg * (0.6 - contrast / 250.0), //toe point
|
avg - avg * (0.6 - c / 250.0), //toe point
|
||||||
avg - avg * (0.6 + contrast / 250.0), //value at toe point
|
avg - avg * (0.6 + c / 250.0), //value at toe point
|
||||||
|
|
||||||
avg + (1 - avg) * (0.6 - contrast / 250.0), //shoulder point
|
avg + (1 - avg) * (0.6 - c / 250.0), //shoulder point
|
||||||
avg + (1 - avg) * (0.6 + contrast / 250.0), //value at shoulder point
|
avg + (1 - avg) * (0.6 + c / 250.0), //value at shoulder point
|
||||||
|
|
||||||
1., // white point
|
1., // white point
|
||||||
1. // value at white point
|
1. // value at white point
|
||||||
@@ -271,6 +268,28 @@ void apply_contrast(array2D<float> &dark, int contrast, double scale, bool multi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void extract_channels(Imagefloat *img, const array2D<float> &Y, 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();
|
||||||
|
|
||||||
|
#ifdef _OPENMP
|
||||||
|
#pragma omp parallel for if (multithread)
|
||||||
|
#endif
|
||||||
|
for (int y = 0; y < H; ++y) {
|
||||||
|
for (int x = 0; x < W; ++x) {
|
||||||
|
r[y][x] = img->r(y, x);
|
||||||
|
g[y][x] = img->g(y, x);
|
||||||
|
b[y][x] = img->b(y, x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guidedFilter(Y, r, r, radius, epsilon, multithread);
|
||||||
|
guidedFilter(Y, g, g, radius, epsilon, multithread);
|
||||||
|
guidedFilter(Y, b, b, radius, epsilon, multithread);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
@@ -289,18 +308,24 @@ void ImProcFunctions::dehaze(Imagefloat *img)
|
|||||||
if (options.rtSettings.verbose) {
|
if (options.rtSettings.verbose) {
|
||||||
std::cout << "dehaze: strength = " << strength << std::endl;
|
std::cout << "dehaze: strength = " << strength << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
array2D<float> dark(W, H);
|
|
||||||
const int patchsize = std::max(W / (200 + max(params->dehaze.detail, 0)), 2);
|
|
||||||
int npatches = get_dark_channel(*img, dark, patchsize, nullptr, multiThread);
|
|
||||||
DEBUG_DUMP(dark);
|
|
||||||
|
|
||||||
TMatrix ws = ICCStore::getInstance()->workingSpaceMatrix(params->icm.workingProfile);
|
TMatrix ws = ICCStore::getInstance()->workingSpaceMatrix(params->icm.workingProfile);
|
||||||
array2D<float> Y(W, H);
|
array2D<float> Y(W, H);
|
||||||
get_luminance(img, Y, ws, multiThread);
|
get_luminance(img, Y, ws, multiThread);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
DEBUG_DUMP(dark);
|
||||||
|
|
||||||
float ambient[3];
|
float ambient[3];
|
||||||
int n = estimate_ambient_light(img, dark, Y, patchsize, npatches, ambient);
|
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);
|
float ambient_Y = Color::rgbLuminance(ambient[0], ambient[1], ambient[2], ws);
|
||||||
|
|
||||||
if (options.rtSettings.verbose) {
|
if (options.rtSettings.verbose) {
|
||||||
@@ -320,8 +345,8 @@ void ImProcFunctions::dehaze(Imagefloat *img)
|
|||||||
}
|
}
|
||||||
|
|
||||||
array2D<float> &t_tilde = dark;
|
array2D<float> &t_tilde = dark;
|
||||||
get_dark_channel(*img, dark, patchsize, ambient, multiThread);
|
get_dark_channel(R, G, B, dark, patchsize, ambient, multiThread);
|
||||||
apply_contrast(dark, params->dehaze.depth, scale, multiThread);
|
apply_contrast(dark, ambient_Y, params->dehaze.depth, scale, multiThread);
|
||||||
DEBUG_DUMP(t_tilde);
|
DEBUG_DUMP(t_tilde);
|
||||||
|
|
||||||
if (!params->dehaze.showDepthMap) {
|
if (!params->dehaze.showDepthMap) {
|
||||||
@@ -344,7 +369,8 @@ void ImProcFunctions::dehaze(Imagefloat *img)
|
|||||||
const int radius = max(int(patchsize * mult), 1);
|
const int radius = max(int(patchsize * mult), 1);
|
||||||
const float epsilon = 2.5e-4;
|
const float epsilon = 2.5e-4;
|
||||||
array2D<float> &t = t_tilde;
|
array2D<float> &t = t_tilde;
|
||||||
|
|
||||||
|
if (!params->dehaze.showDepthMap)
|
||||||
guidedFilter(Y, t_tilde, t, radius, epsilon, multiThread);
|
guidedFilter(Y, t_tilde, t, radius, epsilon, multiThread);
|
||||||
|
|
||||||
DEBUG_DUMP(t);
|
DEBUG_DUMP(t);
|
||||||
@@ -362,16 +388,26 @@ void ImProcFunctions::dehaze(Imagefloat *img)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const float t0 = 0.01;
|
const float t0 = 0.1;
|
||||||
|
const float teps = 1e-3;
|
||||||
#ifdef _OPENMP
|
#ifdef _OPENMP
|
||||||
#pragma omp parallel for if (multiThread)
|
#pragma omp parallel for if (multiThread)
|
||||||
#endif
|
#endif
|
||||||
for (int y = 0; y < H; ++y) {
|
for (int y = 0; y < H; ++y) {
|
||||||
for (int x = 0; x < W; ++x) {
|
for (int x = 0; x < W; ++x) {
|
||||||
float mt = std::max(t[y][x], t0);
|
float rgb[3] = { img->r(y, x), img->g(y, x), img->b(y, x) };
|
||||||
float r = (img->r(y, x) - ambient[0]) / mt + ambient[0];
|
float tl = 1.f - min(rgb[0]/ambient[0], rgb[1]/ambient[1], rgb[2]/ambient[2]);
|
||||||
float g = (img->g(y, x) - ambient[1]) / mt + ambient[1];
|
float tu = t0 - teps;
|
||||||
float b = (img->b(y, x) - ambient[2]) / mt + ambient[2];
|
for (int c = 0; c < 3; ++c) {
|
||||||
|
if (ambient[c] < 1) {
|
||||||
|
tu = max(tu, (rgb[c] - ambient[c])/(1.f - ambient[c]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
float mt = max(t[y][x], t0, tl + teps, tu + teps);
|
||||||
|
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];
|
||||||
|
|
||||||
img->r(y, x) = r;
|
img->r(y, x) = r;
|
||||||
img->g(y, x) = g;
|
img->g(y, x) = g;
|
||||||
img->b(y, x) = b;
|
img->b(y, x) = b;
|
||||||
@@ -388,7 +424,7 @@ void ImProcFunctions::dehaze(Imagefloat *img)
|
|||||||
|
|
||||||
if (newmed > 1e-5f) {
|
if (newmed > 1e-5f) {
|
||||||
const float f1 = oldmed / newmed;
|
const float f1 = oldmed / newmed;
|
||||||
const float f = /* f1 * */ 65535.f;
|
const float f = f1 * 65535.f;
|
||||||
#ifdef _OPENMP
|
#ifdef _OPENMP
|
||||||
#pragma omp parallel for if (multiThread)
|
#pragma omp parallel for if (multiThread)
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user