Improvment to smooth blur
This commit is contained in:
@@ -28,20 +28,32 @@
|
||||
* available at https://arxiv.org/abs/1505.00996
|
||||
*/
|
||||
|
||||
#include "boxblur.h"
|
||||
#include "guidedfilter.h"
|
||||
#include "imagefloat.h"
|
||||
#include "rescale.h"
|
||||
#include "boxblur.h"
|
||||
#include "sleef.h"
|
||||
#include "rescale.h"
|
||||
#include "imagefloat.h"
|
||||
|
||||
#define BENCHMARK
|
||||
#include "StopWatch.h"
|
||||
namespace rtengine {
|
||||
|
||||
namespace rtengine
|
||||
{
|
||||
#if 0
|
||||
# define DEBUG_DUMP(arr) \
|
||||
do { \
|
||||
Imagefloat im(arr.width(), arr.height()); \
|
||||
const char *out = "/tmp/" #arr ".tif"; \
|
||||
for (int y = 0; y < im.getHeight(); ++y) { \
|
||||
for (int x = 0; x < im.getWidth(); ++x) { \
|
||||
im.r(y, x) = im.g(y, x) = im.b(y, x) = arr[y][x] * 65535.f; \
|
||||
} \
|
||||
} \
|
||||
im.saveTIFF(out, 16); \
|
||||
} while (false)
|
||||
#else
|
||||
# define DEBUG_DUMP(arr)
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
namespace {
|
||||
|
||||
int calculate_subsampling(int w, int h, int r)
|
||||
{
|
||||
@@ -64,6 +76,7 @@ int calculate_subsampling(int w, int h, int r)
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
void guidedFilter(const array2D<float> &guide, const array2D<float> &src, array2D<float> &dst, int r, float epsilon, bool multithread, int subsampling)
|
||||
{
|
||||
|
||||
@@ -77,12 +90,7 @@ void guidedFilter(const array2D<float> &guide, const array2D<float> &src, array2
|
||||
enum Op { MUL, DIVEPSILON, ADD, SUB, ADDMUL, SUBMUL };
|
||||
|
||||
const auto apply =
|
||||
#ifdef _OPENMP
|
||||
[multithread, epsilon](Op op, array2D<float> &res, const array2D<float> &a, const array2D<float> &b, const array2D<float> &c=array2D<float>()) -> void
|
||||
#else
|
||||
// removed multithread to fix clang warning on msys2 clang builds, which don't support OpenMp
|
||||
[epsilon](Op op, array2D<float> &res, const array2D<float> &a, const array2D<float> &b, const array2D<float> &c=array2D<float>()) -> void
|
||||
#endif
|
||||
[=](Op op, array2D<float> &res, const array2D<float> &a, const array2D<float> &b, const array2D<float> &c=array2D<float>()) -> void
|
||||
{
|
||||
const int w = res.width();
|
||||
const int h = res.height();
|
||||
@@ -134,7 +142,7 @@ void guidedFilter(const array2D<float> &guide, const array2D<float> &src, array2
|
||||
{
|
||||
if (d.width() == s.width() && d.height() == s.height()) {
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for if (multithread)
|
||||
# pragma omp parallel for if (multithread)
|
||||
#endif
|
||||
for (int y = 0; y < s.height(); ++y) {
|
||||
for (int x = 0; x < s.width(); ++x) {
|
||||
@@ -155,6 +163,7 @@ void guidedFilter(const array2D<float> &guide, const array2D<float> &src, array2
|
||||
[multithread](array2D<float> &d, array2D<float> &s, int rad) -> void
|
||||
{
|
||||
rad = LIM(rad, 0, (min(s.width(), s.height()) - 1) / 2 - 1);
|
||||
// boxblur(s, d, rad, s.width(), s.height(), multithread);
|
||||
boxblur(static_cast<float**>(s), static_cast<float**>(d), rad, s.width(), s.height(), multithread);
|
||||
};
|
||||
|
||||
@@ -164,39 +173,54 @@ void guidedFilter(const array2D<float> &guide, const array2D<float> &src, array2
|
||||
f_subsample(I1, I);
|
||||
f_subsample(p1, p);
|
||||
|
||||
DEBUG_DUMP(I);
|
||||
DEBUG_DUMP(p);
|
||||
DEBUG_DUMP(I1);
|
||||
DEBUG_DUMP(p1);
|
||||
|
||||
float r1 = float(r) / subsampling;
|
||||
|
||||
array2D<float> meanI(w, h);
|
||||
f_mean(meanI, I1, r1);
|
||||
DEBUG_DUMP(meanI);
|
||||
|
||||
array2D<float> meanp(w, h);
|
||||
f_mean(meanp, p1, r1);
|
||||
DEBUG_DUMP(meanp);
|
||||
|
||||
array2D<float> &corrIp = p1;
|
||||
apply(MUL, corrIp, I1, p1);
|
||||
f_mean(corrIp, corrIp, r1);
|
||||
DEBUG_DUMP(corrIp);
|
||||
|
||||
array2D<float> &corrI = I1;
|
||||
apply(MUL, corrI, I1, I1);
|
||||
f_mean(corrI, corrI, r1);
|
||||
DEBUG_DUMP(corrI);
|
||||
|
||||
array2D<float> &varI = corrI;
|
||||
apply(SUBMUL, varI, meanI, meanI, corrI);
|
||||
DEBUG_DUMP(varI);
|
||||
|
||||
array2D<float> &covIp = corrIp;
|
||||
apply(SUBMUL, covIp, meanI, meanp, corrIp);
|
||||
DEBUG_DUMP(covIp);
|
||||
|
||||
array2D<float> &a = varI;
|
||||
apply(DIVEPSILON, a, covIp, varI);
|
||||
DEBUG_DUMP(a);
|
||||
|
||||
array2D<float> &b = covIp;
|
||||
apply(SUBMUL, b, a, meanI, meanp);
|
||||
DEBUG_DUMP(b);
|
||||
|
||||
array2D<float> &meana = a;
|
||||
f_mean(meana, a, r1);
|
||||
DEBUG_DUMP(meana);
|
||||
|
||||
array2D<float> &meanb = b;
|
||||
f_mean(meanb, b, r1);
|
||||
DEBUG_DUMP(meanb);
|
||||
|
||||
// speedup by heckflosse67
|
||||
const int Ws = meana.width();
|
||||
@@ -218,7 +242,7 @@ void guidedFilter(const array2D<float> &guide, const array2D<float> &src, array2
|
||||
}
|
||||
|
||||
|
||||
void guidedFilterLog(float base, array2D<float> &chan, int r, float eps, bool multithread, int subsampling)
|
||||
void guidedFilterLog(const array2D<float> &guide, float base, array2D<float> &chan, int r, float eps, bool multithread, int subsampling)
|
||||
{
|
||||
#ifdef _OPENMP
|
||||
# pragma omp parallel for if (multithread)
|
||||
@@ -229,7 +253,7 @@ void guidedFilterLog(float base, array2D<float> &chan, int r, float eps, bool mu
|
||||
}
|
||||
}
|
||||
|
||||
guidedFilter(chan, chan, chan, r, eps, multithread, subsampling);
|
||||
guidedFilter(guide, chan, chan, r, eps, multithread, subsampling);
|
||||
|
||||
#ifdef _OPENMP
|
||||
# pragma omp parallel for if (multithread)
|
||||
@@ -242,4 +266,13 @@ void guidedFilterLog(float base, array2D<float> &chan, int r, float eps, bool mu
|
||||
}
|
||||
|
||||
|
||||
void guidedFilterLog(float base, array2D<float> &chan, int r, float eps, bool multithread, int subsampling)
|
||||
{
|
||||
guidedFilterLog(chan, base, chan, r, eps, multithread, subsampling);
|
||||
}
|
||||
|
||||
} // namespace rtengine
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user