Wavelets replace edge-preserving blur in first stage filtering. Much improved performance for about the same time cost; memory still an issue.

This commit is contained in:
Emil Martinec
2012-02-08 23:47:31 -06:00
parent 163098df71
commit c492509175
10 changed files with 1442 additions and 719 deletions

View File

@@ -1,4 +1,9 @@
#include "EdgePreserveLab.h"
#include "boxblur.h"
#include <cstdlib>
#define MAX(a,b) ((a)<(b)?(b):(a))
#define MIN(a,b) ((a)>(b)?(b):(a))
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -50,28 +55,33 @@ float *EdgePreserveLab::CreateBlur(float *Source, float LScale, float abScale, f
unsigned int x, y, i;
unsigned int w1 = w - 1, h1 = h - 1;
float eps = 0.02f;
float eps = 0.0001f;
float scL = powf(100.0f,LScale);
float scab = powf(200.0f,abScale);
float * var = new float[w*h];
boxvar(g, var, 1, 1, w, h);
for(y = 0; y != h1; y++){
float *rg = &g[w*y];
for(x = 0; x != w1; x++){
//Estimate the central difference gradient in the center of a four pixel square. (gx, gy) is actually 2*gradient.
float gx = (fabs((rg[x + 1] - rg[x]) + (rg[x + w + 1] - rg[x + w])));
/*float gx = (fabs((rg[x + 1] - rg[x]) + (rg[x + w + 1] - rg[x + w])));
float gy = (fabs((rg[x + w] - rg[x]) + (rg[x + w + 1] - rg[x + 1])));
float Lave = 0;//0.25*((rg[x + 1] + rg[x]) + (rg[x + w + 1] + rg[x + w]));
//TODO: combine this with gx, gy if not needing separate quantities
float hx = (fabs((rg[x + 1 + n] - rg[x + n]) + (rg[x + w + 1 + n] - rg[x + w + n])) + \
fabs((rg[x + 1 + 2*n] - rg[x + 2*n]) + (rg[x + w + 1 + 2*n] - rg[x + w + 2*n])));
float hy = (fabs((rg[x + w + n] - rg[x + n]) + (rg[x + w + 1 + n] - rg[x + 1 + n])) + \
fabs((rg[x + w + 2*n] - rg[x + 2*n]) + (rg[x + w + 1 + 2*n] - rg[x + 1 + 2*n])));
*/
//float gradtot = (gx+gy+hx+hy);
//gradhisto[MAX(0,MIN(32767,(int)gradtot))] ++;
//Apply power to the magnitude of the gradient to get the edge stopping function.
a[x + w*y] = scL*expf(-100.0f*(gx + gy + hx + hy)/(EdgeStoppingLuma));
//a[x + w*y] = LScale*100.0f*expf(-100.0f*SQR(gx + gy)/SQR(EdgeStoppingLuma));///(0.1+rg[x]);
//a[x + w*y] = scL*expf(-100.0f*(gx + gy + hx + hy)/(EdgeStoppingLuma));
//a[x + w*y] = scL*expf(-var[y*w+x]/SQR(0.02*EdgeStoppingLuma));///(0.1+rg[x]);
a[x + w*y] = scL*expf(-50.0f*sqrt(var[y*w+x])/(EdgeStoppingLuma+eps));///(0.1+rg[x]);
//b[x + w*y] = (scab)*expf(-20.0f*(gx + gy + Lave*(hx + hy))/(EdgeStoppingChroma));
//b[x + w*y] = (scab)*expf(-400.0f*SQR(gx + gy + Lave*(hx + hy))/SQR(EdgeStoppingChroma));;

File diff suppressed because it is too large Load Diff

View File

@@ -21,19 +21,28 @@
#include "cplx_wavelet_dec.h"
namespace rtengine {
cplx_wavelet_decomposition::~cplx_wavelet_decomposition()
{
for(int i = 0; i < lvltot; i++) {
for (int j=0; j<4; j++) {
delete dual_tree_coeffs[i][j];
cplx_wavelet_decomposition::~cplx_wavelet_decomposition()
{
for(int i = 0; i < lvltot; i++) {
for (int j=0; j<4; j++) {
delete dual_tree[i][j];
}
}
}
delete[] first_lev_anal;
delete[] first_lev_synth;
delete[] wavfilt_anal;
delete[] wavfilt_synth;
}
delete[] first_lev_anal;
delete[] first_lev_synth;
delete[] wavfilt_anal;
delete[] wavfilt_synth;
}
wavelet_decomposition::~wavelet_decomposition()
{
for(int i = 0; i < lvltot; i++) {
delete wavelet_decomp[i];
}
delete[] wavfilt_anal;
delete[] wavfilt_synth;
}
};

View File

@@ -35,11 +35,19 @@ namespace rtengine {
template <typename A, typename B>
void copy_out(A ** a, B * b, size_t datalen)
{
{// for complex wavelet decomposition
for (size_t j=0; j<datalen; j++) {
b[j] = static_cast<B> (0.25*(a[0][j]+a[1][j]+a[2][j]+a[3][j]));
}
}
template <typename A, typename B>
void copy_out(A * a, B * b, size_t datalen)
{// for standard wavelet decomposition
for (size_t j=0; j<datalen; j++) {
b[j] = static_cast<B> (a[j]);
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -70,7 +78,7 @@ private:
float *testfilt_anal;
float *testfilt_synth;
cplx_wavelet_level<internal_type> * dual_tree_coeffs[maxlevels][4];//m_c in old code
wavelet_level<internal_type> * dual_tree[maxlevels][4];
public:
@@ -78,11 +86,35 @@ public:
cplx_wavelet_decomposition(E * src, int width, int height, int maxlvl);
~cplx_wavelet_decomposition();
internal_type ** level_coeffs(int level, int branch) const
{
return dual_tree[level][branch]->subbands();
}
int level_W(int level, int branch) const
{
return dual_tree[level][branch]->width();
}
int level_H(int level, int branch) const
{
return dual_tree[level][branch]->height();
}
int level_pad(int level, int branch) const
{
return dual_tree[level][branch]->padding();
}
int maxlevel() const
{
return lvltot;
}
template<typename E>
void reconstruct(E * dst);
};
@@ -98,20 +130,34 @@ public:
//initialize wavelet filters
first_lev_len = Kingsbury_len;
first_lev_offset = Kingsbury_offset;
first_lev_len = FSFarras_len;
first_lev_offset = FSFarras_offset;
first_lev_anal = new float[4*first_lev_len];
first_lev_synth = new float[4*first_lev_len];
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
for (int i=0; i<first_lev_len; i++) {
first_lev_anal[first_lev_len*(2*n+m)+i] = Kingsbury_anal[n][m][i];
first_lev_synth[first_lev_len*(2*n+m)+i] = Kingsbury_anal[n][m][first_lev_len-1-i];
first_lev_anal[first_lev_len*(2*n+m)+i] = FSFarras_anal[n][m][i]/sqrt(2);
first_lev_synth[first_lev_len*(2*n+m)+i] = FSFarras_anal[n][m][first_lev_len-1-i]/sqrt(2);
}
}
}
/*first_lev_len = AntonB_len;
first_lev_offset = AntonB_offset;
first_lev_anal = new float[4*first_lev_len];
first_lev_synth = new float[4*first_lev_len];
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
for (int i=0; i<first_lev_len; i++) {
first_lev_anal[first_lev_len*(2*n+m)+i] = AntonB_anal[n][m][i];
first_lev_synth[first_lev_len*(2*n+m)+i] = 2*AntonB_synth[n][m][i];
}
}
}*/
wavfilt_len = Kingsbury_len;
wavfilt_offset = Kingsbury_offset;
wavfilt_anal = new float[4*wavfilt_len];
@@ -120,8 +166,8 @@ public:
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
for (int i=0; i<wavfilt_len; i++) {
wavfilt_anal[wavfilt_len*(2*n+m)+i] = Kingsbury_anal[n][m][i];
wavfilt_synth[wavfilt_len*(2*n+m)+i] = Kingsbury_anal[n][m][first_lev_len-1-i];
wavfilt_anal[wavfilt_len*(2*n+m)+i] = Kingsbury_anal[n][m][i]/sqrt(2);
wavfilt_synth[wavfilt_len*(2*n+m)+i] = Kingsbury_anal[n][m][first_lev_len-1-i]/sqrt(2);
}
}
}
@@ -130,51 +176,67 @@ public:
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// data structure is dual_tree_coeffs[scale][2*n+m=2*(Re/Im)+dir][channel={lo,hi1,hi2,hi3}][pixel_array]
// after coefficient rotation, data structure is:
// dual_tree[scale][2*n+m=2*(Re/Im)+dir][channel={lo,hi1,hi2,hi3}][pixel_array]
//srand((unsigned)time(0));
//for (int i=0; i<m_w*m_h; i++ )
// src[i] = (float)rand()/(float)RAND_MAX;
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
float padding = 0;//pow(2, maxlvl);//must be a multiple of two
dual_tree_coeffs[0][2*n+m] = new cplx_wavelet_level<internal_type>(src, padding, m_w, m_h, first_lev_anal+first_lev_len*2*n, \
lvltot=0;
float padding = 0;//1<<(maxlvl-1);
dual_tree[0][2*n+m] = new wavelet_level<internal_type>(src, lvltot, padding, m_w, m_h, first_lev_anal+first_lev_len*2*n, \
first_lev_anal+first_lev_len*2*m, first_lev_len, first_lev_offset);
lvltot=1;
while(lvltot < maxlvl) {
dual_tree_coeffs[lvltot][2*n+m] = new cplx_wavelet_level<internal_type>(dual_tree_coeffs[lvltot-1][2*n+m]->lopass()/*lopass*/, 0/*no padding*/, \
dual_tree_coeffs[lvltot-1][2*n+m]->width(), \
dual_tree_coeffs[lvltot-1][2*n+m]->height(), \
wavfilt_anal+wavfilt_len*2*n, wavfilt_anal+wavfilt_len*2*m, wavfilt_len, wavfilt_offset);
lvltot++;
dual_tree[lvltot][2*n+m] = new wavelet_level<internal_type>(dual_tree[lvltot-1][2*n+m]->lopass()/*lopass*/, lvltot, 0/*no padding*/, \
dual_tree[lvltot-1][2*n+m]->width(), \
dual_tree[lvltot-1][2*n+m]->height(), \
wavfilt_anal+wavfilt_len*2*n, wavfilt_anal+wavfilt_len*2*m, \
wavfilt_len, wavfilt_offset);
}
}
}
//rotate detail coefficients
float coeffave[5][4][3];
float root2 = sqrt(2);
for (int lvl=0; lvl<lvltot; lvl++) {
int Wlvl = dual_tree_coeffs[lvl][0]->width();
int Hlvl = dual_tree_coeffs[lvl][0]->height();
for (int i=0; i<Wlvl*Hlvl; i++) {//pixel
for (int m=1; m<4; m++) {//detail coefficients only
float wavtmp = (dual_tree_coeffs[lvl][0]->wavcoeffs[m][i] + dual_tree_coeffs[lvl][3]->wavcoeffs[m][i])/root2;
dual_tree_coeffs[lvl][3]->wavcoeffs[m][i] = (dual_tree_coeffs[lvl][0]->wavcoeffs[m][i] - dual_tree_coeffs[lvl][3]->wavcoeffs[m][i])/root2;
dual_tree_coeffs[lvl][0]->wavcoeffs[m][i] = wavtmp;
int Wlvl = dual_tree[lvl][0]->width();
int Hlvl = dual_tree[lvl][0]->height();
for (int n=0; n<4; n++)
for (int m=1; m<4; m++)
coeffave[lvl][n][m-1]=0;
for (int m=1; m<4; m++) {//detail coefficients only
for (int i=0; i<Wlvl*Hlvl; i++) {//pixel
wavtmp = (dual_tree_coeffs[lvl][1]->wavcoeffs[m][i] + dual_tree_coeffs[lvl][2]->wavcoeffs[m][i])/root2;
dual_tree_coeffs[lvl][2]->wavcoeffs[m][i] = (dual_tree_coeffs[lvl][1]->wavcoeffs[m][i] - dual_tree_coeffs[lvl][2]->wavcoeffs[m][i])/root2;
dual_tree_coeffs[lvl][1]->wavcoeffs[m][i] = wavtmp;
float wavtmp = (dual_tree[lvl][0]->wavcoeffs[m][i] + dual_tree[lvl][3]->wavcoeffs[m][i])/root2;
dual_tree[lvl][3]->wavcoeffs[m][i] = (dual_tree[lvl][0]->wavcoeffs[m][i] - dual_tree[lvl][3]->wavcoeffs[m][i])/root2;
dual_tree[lvl][0]->wavcoeffs[m][i] = wavtmp;
wavtmp = (dual_tree[lvl][1]->wavcoeffs[m][i] + dual_tree[lvl][2]->wavcoeffs[m][i])/root2;
dual_tree[lvl][2]->wavcoeffs[m][i] = (dual_tree[lvl][1]->wavcoeffs[m][i] - dual_tree[lvl][2]->wavcoeffs[m][i])/root2;
dual_tree[lvl][1]->wavcoeffs[m][i] = wavtmp;
for (int n=0; n<4; n++) coeffave[lvl][n][m-1] += fabs(dual_tree[lvl][n]->wavcoeffs[m][i]);
}
}
for (int n=0; n<4; n++)
for (int i=0; i<3; i++)
coeffave[lvl][n][i] /= Wlvl*Hlvl;
}
}
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
/* function y=reconstruct(w,J,Fsf,sf) */
template<typename E>
void cplx_wavelet_decomposition::reconstruct(E * dst) {
@@ -183,22 +245,21 @@ public:
//rotate detail coefficients
float root2 = sqrt(2);
for (int lvl=0; lvl<lvltot; lvl++) {
int Wlvl = dual_tree_coeffs[lvl][0]->width();
int Hlvl = dual_tree_coeffs[lvl][0]->height();
int Wlvl = dual_tree[lvl][0]->width();
int Hlvl = dual_tree[lvl][0]->height();
for (int i=0; i<Wlvl*Hlvl; i++) {//pixel
for (int m=1; m<4; m++) {//detail coefficients only
float wavtmp = (dual_tree_coeffs[lvl][0]->wavcoeffs[m][i] + dual_tree_coeffs[lvl][3]->wavcoeffs[m][i])/root2;
dual_tree_coeffs[lvl][3]->wavcoeffs[m][i] = (dual_tree_coeffs[lvl][0]->wavcoeffs[m][i] - dual_tree_coeffs[lvl][3]->wavcoeffs[m][i])/root2;
dual_tree_coeffs[lvl][0]->wavcoeffs[m][i] = wavtmp;
float wavtmp = (dual_tree[lvl][0]->wavcoeffs[m][i] + dual_tree[lvl][3]->wavcoeffs[m][i])/root2;
dual_tree[lvl][3]->wavcoeffs[m][i] = (dual_tree[lvl][0]->wavcoeffs[m][i] - dual_tree[lvl][3]->wavcoeffs[m][i])/root2;
dual_tree[lvl][0]->wavcoeffs[m][i] = wavtmp;
wavtmp = (dual_tree_coeffs[lvl][1]->wavcoeffs[m][i] + dual_tree_coeffs[lvl][2]->wavcoeffs[m][i])/root2;
dual_tree_coeffs[lvl][2]->wavcoeffs[m][i] = (dual_tree_coeffs[lvl][1]->wavcoeffs[m][i] - dual_tree_coeffs[lvl][2]->wavcoeffs[m][i])/root2;
dual_tree_coeffs[lvl][1]->wavcoeffs[m][i] = wavtmp;
wavtmp = (dual_tree[lvl][1]->wavcoeffs[m][i] + dual_tree[lvl][2]->wavcoeffs[m][i])/root2;
dual_tree[lvl][2]->wavcoeffs[m][i] = (dual_tree[lvl][1]->wavcoeffs[m][i] - dual_tree[lvl][2]->wavcoeffs[m][i])/root2;
dual_tree[lvl][1]->wavcoeffs[m][i] = wavtmp;
}
}
}
//y = ConstantArray[0, {vsizetmp, hsizetmp}];
internal_type ** tmp = new internal_type *[4];
for (int i=0; i<4; i++) {
tmp[i] = new internal_type[m_w*m_h];
@@ -206,12 +267,14 @@ public:
for (int n=0; n<2; n++) {
for (int m=0; m<2; m++) {
int skip=1<<(lvltot-1);
for (int lvl=lvltot-1; lvl>0; lvl--) {
dual_tree_coeffs[lvl][2*n+m]->reconstruct_level(dual_tree_coeffs[lvl-1][2*n+m]->wavcoeffs[0], wavfilt_synth+wavfilt_len*2*n, \
wavfilt_synth+wavfilt_len*2*m, wavfilt_len, wavfilt_offset);
dual_tree[lvl][2*n+m]->reconstruct_level(dual_tree[lvl-1][2*n+m]->wavcoeffs[0], wavfilt_synth+wavfilt_len*2*n, \
wavfilt_synth+wavfilt_len*2*m, wavfilt_len, wavfilt_offset, skip);
skip /=2;
}
dual_tree_coeffs[0][2*n+m]->reconstruct_level(tmp[2*n+m], first_lev_synth+first_lev_len*2*n,
first_lev_synth+first_lev_len*2*m, first_lev_len, first_lev_offset);
dual_tree[0][2*n+m]->reconstruct_level(tmp[2*n+m], first_lev_synth+first_lev_len*2*n,
first_lev_synth+first_lev_len*2*m, first_lev_len, first_lev_offset, skip);
}
}
@@ -226,9 +289,156 @@ public:
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
class wavelet_decomposition
{
public:
typedef float internal_type;
private:
static const int maxlevels = 8;//should be greater than any conceivable order of decimation
int lvltot;
size_t m_w, m_h;//dimensions
size_t m_w1, m_h1;
int wavfilt_len, wavfilt_offset;
float *wavfilt_anal;
float *wavfilt_synth;
int testfilt_len, testfilt_offset;
float *testfilt_anal;
float *testfilt_synth;
wavelet_level<internal_type> * wavelet_decomp[maxlevels];
public:
template<typename E>
wavelet_decomposition(E * src, int width, int height, int maxlvl);
~wavelet_decomposition();
internal_type ** level_coeffs(int level) const
{
return wavelet_decomp[level]->subbands();
}
int level_W(int level) const
{
return wavelet_decomp[level]->width();
}
int level_H(int level) const
{
return wavelet_decomp[level]->height();
}
int level_pad(int level) const
{
return wavelet_decomp[level]->padding();
}
int maxlevel() const
{
return lvltot;
}
template<typename E>
void reconstruct(E * dst);
};
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename E>
wavelet_decomposition::wavelet_decomposition(E * src, int width, int height, int maxlvl)
: lvltot(0), m_w(width), m_h(height), m_w1(0), m_h1(0)
{
m_w1 = width;
m_h1 = height;
//initialize wavelet filters
wavfilt_len = Haar_len;
wavfilt_offset = Haar_offset;
wavfilt_anal = new float[2*wavfilt_len];
wavfilt_synth = new float[2*wavfilt_len];
for (int n=0; n<2; n++) {
for (int i=0; i<wavfilt_len; i++) {
wavfilt_anal[wavfilt_len*(n)+i] = Haar_anal[n][i];
wavfilt_synth[wavfilt_len*(n)+i] = Haar_anal[n][wavfilt_len-1-i];
//n=0 lopass, n=1 hipass
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// after coefficient rotation, data structure is:
// wavelet_decomp[scale][channel={lo,hi1,hi2,hi3}][pixel_array]
//srand((unsigned)time(0));
//for (int i=0; i<m_w*m_h; i++ )
// src[i] = (float)rand()/(float)RAND_MAX;
int padding = 0;//pow(2, maxlvl);//must be a multiple of two
lvltot=0;
wavelet_decomp[lvltot] = new wavelet_level<internal_type>(src, lvltot/*level*/, padding/*padding*/, m_w, m_h, \
wavfilt_anal, wavfilt_anal, wavfilt_len, wavfilt_offset);
while(lvltot < maxlvl) {
lvltot++;
wavelet_decomp[lvltot] = new wavelet_level<internal_type>(wavelet_decomp[lvltot-1]->lopass()/*lopass*/, lvltot/*level*/, 0/*no padding*/, \
wavelet_decomp[lvltot-1]->width(), wavelet_decomp[lvltot-1]->height(), \
wavfilt_anal, wavfilt_anal, wavfilt_len, wavfilt_offset);
}
}
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
template<typename E>
void wavelet_decomposition::reconstruct(E * dst) {
// data structure is wavcoeffs[scale][channel={lo,hi1,hi2,hi3}][pixel_array]
int skip=1<<(lvltot-1);
for (int lvl=lvltot-1; lvl>0; lvl--) {
wavelet_decomp[lvl]->reconstruct_level(wavelet_decomp[lvl-1]->wavcoeffs[0], wavfilt_synth, wavfilt_synth, wavfilt_len, wavfilt_offset, skip);
skip /=2;
}
internal_type * tmp = new internal_type[m_w*m_h];
wavelet_decomp[0]->reconstruct_level(tmp, wavfilt_synth, wavfilt_synth, wavfilt_len, wavfilt_offset, skip);
copy_out(tmp,dst,m_w*m_h);
delete[] tmp;
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
};

View File

@@ -32,11 +32,10 @@ namespace rtengine {
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*CplxWavelet AntonB = {
12,//length of filter
6,//offset
/*const int AntonB_len = 12;//length of filter
const int AntonB_offset = 6;//offset
{//analysis filter
const float AntonB_anal[2][2][12] = {//analysis filter
{{0, -0.08838834764832, 0.08838834764832, 0.69587998903400, 0.69587998903400,
0.08838834764832, -0.08838834764832, 0.01122679215254, 0.01122679215254, 0},
{0, 0, 0, 0.04563588155712, -0.02877176311425, -0.29563588155712 ,
@@ -44,9 +43,9 @@ namespace rtengine {
{{0 , 0 , 0.02674875741081, -0.01686411844287, -0.07822326652899, 0.26686411844288,
0.60294901823636, 0.26686411844287, -0.07822326652899, -0.01686411844287, 0.02674875741081, 0},
{0 , 0 , 0, 0 , 0.04563588155712, -0.02877176311425,
-0.29563588155712 , 0.55754352622850, -0.29563588155713, -0.02877176311425, 0.04563588155712 , 0}} },
-0.29563588155712 , 0.55754352622850, -0.29563588155713, -0.02877176311425, 0.04563588155712 , 0}} };
{//synthesis filter
const float AntonB_synth[2][2][12] = {//synthesis filter
{{0 , 0 , 0, -0.04563588155712, -0.02877176311425, 0.29563588155712,
0.55754352622850, 0.29563588155713, -0.02877176311425, -0.04563588155712, 0, 0},
{0, 0.02674875741081, 0.01686411844287, -0.07822326652899, -0.26686411844288 , 0.60294901823636,
@@ -54,8 +53,8 @@ namespace rtengine {
{{0 , 0, -0.04563588155712, -0.02877176311425, 0.29563588155712 , 0.55754352622850 ,
0.29563588155713, -0.02877176311425, -0.04563588155712, 0, 0 , 0},
{0.02674875741081 , 0.01686411844287, -0.07822326652899, -0.26686411844288 , 0.60294901823636, -0.26686411844287,
-0.07822326652899, 0.01686411844287 , 0.02674875741081 , 0 , 0, 0}} }
};*/
-0.07822326652899, 0.01686411844287 , 0.02674875741081 , 0 , 0, 0}} };*/
/* for (int i=0; i<4; i++)
for (int n=0; n<12; n++) {
@@ -66,7 +65,7 @@ namespace rtengine {
const int FSFarras_len=10;//length of filter
const int FSFarras_offset=5;//offset
const int FSFarras_offset=4;//offset
const float FSFarras_anal[2][2][10] = {//analysis filter
{{0, -0.08838834764832, 0.08838834764832, 0.69587998903400, 0.69587998903400, 0.08838834764832, -0.08838834764832, 0.01122679215254 , 0.01122679215254, 0},
@@ -92,7 +91,7 @@ const float FSFarras_anal[2][2][10] = {//analysis filter
% Image Proc.(ICIP),2000 */
const int Kingsbury_len=10;//length of filter
const int Kingsbury_offset=5;//offset
const int Kingsbury_offset=4;//offset
const float Kingsbury_anal[2][2][10] = {//analysis filter
{{0.03516384000000, 0, -0.08832942000000, 0.23389032000000, 0.76027237000000, 0.58751830000000, 0, -0.11430184000000 , 0, 0},
@@ -103,6 +102,15 @@ const float Kingsbury_anal[2][2][10] = {//analysis filter
//synthesis filter is the reverse (see cplx_wavelet_dec.h)
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
const int Haar_len=2;//length of filter
const int Haar_offset=1;//offset
const float Haar_anal[2][2] = {{0.5,0.5}, {0.5,-0.5}};//analysis filter
//synthesis filter is the reverse (see cplx_wavelet_dec.h)
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
};

View File

@@ -25,17 +25,19 @@
#include <algorithm>
#include "array2D.h"
#include "gauss.h"
namespace rtengine {
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) > (b) ? (b) : (a))
#define SQR(x) ((x)*(x))
//////////////////////////////////////////////////////////////////////////////
template<typename T>
class cplx_wavelet_level
class wavelet_level
{
// full size
size_t m_w, m_h;
@@ -46,6 +48,12 @@ namespace rtengine {
// size of padded border
size_t m_pad;
// level of decomposition
int lvl;
// spacing of filter taps
size_t skip;
// array of pointers to lines of coeffs
// actually is a single contiguous data array pointed by m_coeffs[0]
//T ** m_coeffs;
@@ -67,32 +75,32 @@ namespace rtengine {
//void dwt_2d(size_t w, size_t h);
//void idwt_2d(size_t w, size_t h, int alpha);
void AnalysisFilter (T * src, T * dstLo, T * dstHi, float *filterLo, float *filterHi,
void AnalysisFilter (T * srcbuffer, T * dstLo, T * dstHi, float *filterLo, float *filterHi,
int taps, int offset, int pitch, int srclen);
void SynthesisFilter (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi,
float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen);
void imp_nr (T* src, int width, int height, double thresh);
public:
T ** wavcoeffs;
template<typename E>
cplx_wavelet_level(E * src, int padding, size_t w, size_t h, float *filterV, float *filterH, int len, int offset)
: m_w(w), m_h(h), m_w2((w+1+2*padding)/2), m_h2((h+1+2*padding)/2), m_pad(padding), wavcoeffs(NULL)
wavelet_level(E * src, int level, int padding, size_t w, size_t h, float *filterV, float *filterH, int len, int offset)
: m_w(w), m_h(h), m_w2(w), m_h2(h), m_pad(padding), wavcoeffs(NULL), lvl(level), skip(1<<level)
{
//m_coeffs = create(w, h);
//m_weights_rows = create(w + 4, h);
//m_weights_cols = create(h + 4, w);
//decompose_level(src, w, h, wavcoeffs, float **filterV, float **filterH, int len, int offset);
m_w2 = (w+2*skip*padding);
m_h2 = (h+2*skip*padding);
m_pad= skip*padding;
wavcoeffs = create((m_w2)*(m_h2));
decompose_level(src, filterV, filterH, len, offset);
decompose_level(src, filterV, filterH, len, offset, skip);
}
~cplx_wavelet_level()
~wavelet_level()
{
destroy(wavcoeffs);
}
@@ -117,11 +125,16 @@ namespace rtengine {
return m_h2;
}
template<typename E>
void decompose_level(E *src, float *filterV, float *filterH, int len, int offset);
size_t padding() const
{
return m_pad/skip;
}
template<typename E>
void reconstruct_level(E *dst, float *filterV, float *filterH, int len, int offset);
void decompose_level(E *src, float *filterV, float *filterH, int len, int offset, int skip);
template<typename E>
void reconstruct_level(E *dst, float *filterV, float *filterH, int len, int offset, int skip);
};
@@ -130,7 +143,7 @@ namespace rtengine {
template<typename T>
T ** cplx_wavelet_level<T>::create(size_t n)
T ** wavelet_level<T>::create(size_t n)
{
T * data = new T[4*n];
T ** subbands = new T*[4];
@@ -145,7 +158,7 @@ namespace rtengine {
template<typename T>
void cplx_wavelet_level<T>::destroy(T ** subbands)
void wavelet_level<T>::destroy(T ** subbands)
{
if(subbands)
{
@@ -157,22 +170,52 @@ namespace rtengine {
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename T> template<typename E>
void cplx_wavelet_level<T>::loadbuffer(E * src, E * dst, int pitch, int srclen)
void wavelet_level<T>::loadbuffer(E * src, E * dst, int pitch, int srclen)
{
E * tmp = dst + m_pad;
memset(dst, 0, (srclen+2*m_pad)*sizeof(E));
for(size_t i = 0, j = 0; i<srclen; i++, j += pitch)
memset(dst, 0, (MAX(m_w2,m_h2))*sizeof(E));
/*int cosetlen = (srclen+1)/skip;
//create buffer with 'skip' rows and 'cosetlen' columns from src data
//'skip' is the spacing of taps on the wavelet filter to be applied to src rows/columns
//therefore there are 'skip' cosets of the row/column data, each of length 'cosetlen'
//'pitch' is 1 for rows, W for columns
for (size_t i = 0, j = 0; i<srclen; i++, j += pitch)
{
int coset = i%skip;
int indx = i/skip;
tmp[coset*cosetlen + indx] = src[j];
}
//even up last row/column if srclen is not a multiple of 'skip'
for (size_t i=srclen; i<srclen+(srclen%skip); i++) {
tmp[i] = tmp[i-skip];
}
// extend each coset mirror-like by padding amount 'm_pad'
for (size_t coset=0; coset<skip*cosetlen; coset+=cosetlen) {
for (size_t i=1; i<=MIN(cosetlen-1,m_pad); i++) {
tmp[coset-i] = tmp[coset+i];
tmp[coset+cosetlen+i-1] = tmp[coset+cosetlen-i-1];
}
}*/
//create padded buffer from src data
for (size_t i = 0, j = 0; i<srclen; i++, j += pitch)
{
tmp[i] = src[j];
}
// extend mirror-like
// extend each coset mirror-like by padding amount 'm_pad' and to a multiple of 'skip'
for (size_t i=1; i<=MIN(srclen-1,m_pad); i++) {
tmp[-i] = tmp[i];
tmp[srclen+i-1] = tmp[srclen-i-1];
}
for (size_t i=0; i<srclen%skip; i++) {
tmp[srclen+m_pad+i] = tmp[srclen+m_pad-i-2];
}
}
@@ -181,7 +224,7 @@ namespace rtengine {
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename T>
void cplx_wavelet_level<T>::AnalysisFilter (T * src, T * dstLo, T * dstHi, float *filterLo, float *filterHi,
void wavelet_level<T>::AnalysisFilter (T * srcbuffer, T * dstLo, T * dstHi, float *filterLo, float *filterHi,
int taps, int offset, int pitch, int srclen) {
/* Basic convolution code
@@ -195,26 +238,49 @@ namespace rtengine {
* treatment of mirror BC's is implemented.
*
*/
// calculate coefficients
for(int i = 0; i < (srclen); i+=2) {
//input data is 'skip' rows and cosetlen=srclen/skip columns (which includes padding at either and)
/*int cosetlen = srclen/skip;
for (size_t coset=0; coset<srclen; coset+=cosetlen) {
for (size_t i = 0; i < (cosetlen); i++) {
float lo=0,hi=0;
if (i>taps && i<cosetlen-taps) {//bulk
for (int j=0, l=-offset; j<taps; j++, l++) {
lo += filterLo[j] * src[i-l];//lopass channel
hi += filterHi[j] * src[i-l];//hipass channel
}
} else {//boundary
for (int j=0; j<taps; j++) {
int arg = MAX(0,MIN(i+(offset-j),srclen-1));//clamped BC's
lo += filterLo[j] * src[arg];//lopass channel
hi += filterHi[j] * src[arg];//hipass channel
}
}
dstLo[(pitch*(coset+i))] = lo;
dstHi[(pitch*(coset+i))] = hi;
}
}*/
for (size_t i = 0; i < (srclen); i++) {
float lo=0,hi=0;
if (i>taps && i<srclen-taps) {//bulk
for (int j=0; j<taps; j++) {
lo += filterLo[j] * src[(i+offset-j)];//lopass channel
hi += filterHi[j] * src[(i+offset-j)];//hipass channel
if (i>skip*taps && i<srclen-skip*taps) {//bulk
for (int j=0, l=-skip*offset; j<taps; j++, l+=skip) {
lo += filterLo[j] * srcbuffer[i-l];//lopass channel
hi += filterHi[j] * srcbuffer[i-l];//hipass channel
}
} else {//boundary
for (int j=0; j<taps; j++) {
int arg = MAX(0,MIN(i+offset-j,srclen-1));//clamped BC's
lo += filterLo[j] * src[arg];//lopass channel
hi += filterHi[j] * src[arg];//hipass channel
int arg = MAX(0,MIN(i+skip*(offset-j),srclen-1));//clamped BC's
lo += filterLo[j] * srcbuffer[arg];//lopass channel
hi += filterHi[j] * srcbuffer[arg];//hipass channel
}
}
dstLo[(pitch*(i/2))] = lo;
dstHi[(pitch*(i/2))] = hi;
dstLo[(pitch*(i))] = lo;
dstHi[(pitch*(i))] = hi;
}
}
@@ -222,8 +288,8 @@ namespace rtengine {
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename T>
void cplx_wavelet_level<T>::SynthesisFilter (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi,
float *filterLo, float *filterHi, int taps, int offset, int pitch, int dstlen) {
void wavelet_level<T>::SynthesisFilter (T * srcLo, T * srcHi, T * dst, T *bufferLo, T *bufferHi, float *filterLo,
float *filterHi, int taps, int offset, int pitch, int dstlen) {
/* Basic convolution code
* Applies an FIR filter 'filter' with 'len' taps,
@@ -239,34 +305,66 @@ namespace rtengine {
// calculate coefficients
int srclen=(dstlen+1+2*m_pad)/2;
for (int i=0; i<srclen; i++) {
bufferLo[i]=srcLo[i*pitch];
bufferHi[i]=srcHi[i*pitch];
// load into buffer
/*
int srclen=(dstlen+(dstlen%skip)+2*m_pad); //length of row/col in src (coarser level)
int cosetlen = srclen/skip; //length of coset (skip is spacing of taps in filter)
for (size_t i=0, j=0; i<srclen; i++, j+=pitch) {
int indx = (i%skip)*cosetlen + i/skip;
bufferLo[indx]=srcLo[j];
bufferHi[indx]=srcHi[j];
}
int shift=taps-offset-1;
for(int i = m_pad; i < (dstlen-m_pad); i++) {
if (bufferLo[i]!=0) {
float xxx=bufferLo[i];
for (size_t coset=0; coset<srclen; coset+=cosetlen) {
for (size_t i = m_pad; i < (cosetlen-m_pad); i++) {
float tot=0;
if (i>taps && i<(cosetlen-taps)) {//bulk
for (int j=0, l=-shift; j<taps; j++, l++) {
tot += (filterLo[j] * bufferLo[i-l] + filterHi[j] * bufferHi[i-l]);
}
} else {//boundary
if (coset+i-m_pad == srclen) return;
for (int j=0, l=-shift; j<taps; j++, l++) {
int arg = MAX(0,MIN((i-l),srclen-1));//clamped BC's
tot += (filterLo[j] * bufferLo[arg] + filterHi[j] * bufferHi[arg]);
}
}
dst[pitch*(coset+i-m_pad)] = tot;
}
}*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// load into buffer
int srclen = (dstlen==m_w ? m_w2 : m_h2);//length of row/col in src (coarser level)
for (size_t i=0, j=0; i<srclen; i++, j+=pitch) {
bufferLo[i]=srcLo[j];
bufferHi[i]=srcHi[j];
}
int shift=(taps-offset-1);
for(size_t i = m_pad; i < (dstlen+m_pad); i++) {
float tot=0;
int i_src = (i+shift)/2;
int begin = (i+shift)%2;
if (i>taps && i<(srclen-taps)) {//bulk
for (int j=begin, l=0; j<taps; j+=2, l++) {
tot += (filterLo[j] * bufferLo[i_src-l] + filterHi[j] * bufferHi[i_src-l]);
if (i>skip*taps && i<(srclen-skip*taps)) {//bulk
for (int j=0, l=-skip*shift; j<taps; j++, l+=skip) {
tot += (filterLo[j] * bufferLo[i-l] + filterHi[j] * bufferHi[i-l]);
}
} else {//boundary
for (int j=begin, l=0; j<taps; j+=2, l++) {
int arg = MAX(0,MIN((i_src-l),srclen-1));//clamped BC's
for (int j=0, l=-skip*shift; j<taps; j++, l+=skip) {
int arg = MAX(0,MIN((i-l),srclen-1));//clamped BC's
tot += (filterLo[j] * bufferLo[arg] + filterHi[j] * bufferHi[arg]);
}
}
dst[pitch*(i-m_pad)] = tot;
if (tot<0.0f || tot>65535.0f) {
float xxx=tot;
float yyy=1.0f;
}
}
}
@@ -274,26 +372,28 @@ namespace rtengine {
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template<typename T> template<typename E>
void cplx_wavelet_level<T>::decompose_level(E *src, float *filterV, float *filterH, int taps, int offset) {
void wavelet_level<T>::decompose_level(E *src, float *filterV, float *filterH, int taps, int offset, int skip) {
T *tmpLo = new T[m_w*m_h2];
T *tmpHi = new T[m_w*m_h2];
T *buffer = new T[MAX(m_w,m_h)+2*m_pad];
T *buffer = new T[MAX(m_w2,m_h2)];
/* filter along columns */
for (int j=0; j<m_w; j++) {
loadbuffer(src+j, buffer, m_w/*pitch*/, m_h/*srclen*/);//pad a column of data and load it to buffer
AnalysisFilter (buffer, tmpLo+j, tmpHi+j, filterV, filterV+taps, taps, offset, m_w/*output_pitch*/, m_h+2*m_pad/*srclen*/);
AnalysisFilter (buffer, tmpLo+j, tmpHi+j, filterV, filterV+taps, taps, offset, m_w/*output_pitch*/, m_h/*srclen*/);
}
/* filter along rows */
for (int i=0; i<m_h2; i++) {
loadbuffer(tmpLo+i*m_w, buffer, 1/*pitch*/, m_w/*srclen*/);//pad a row of data and load it to buffer
AnalysisFilter (buffer, wavcoeffs[0]+i*m_w2, wavcoeffs[1]+i*m_w2, filterH, filterH+taps, taps, offset, 1/*output_pitch*/, m_w+2*m_pad/*srclen*/);
AnalysisFilter (buffer, wavcoeffs[0]+i*m_w2, wavcoeffs[1]+i*m_w2, filterH, filterH+taps, taps, offset, 1/*output_pitch*/, m_w/*srclen*/);
loadbuffer(tmpHi+i*m_w, buffer, 1/*pitch*/, m_w/*srclen*/);
AnalysisFilter (buffer, wavcoeffs[2]+i*m_w2, wavcoeffs[3]+i*m_w2, filterH, filterH+taps, taps, offset, 1/*output_pitch*/, m_w+2*m_pad/*srclen*/);
AnalysisFilter (buffer, wavcoeffs[2]+i*m_w2, wavcoeffs[3]+i*m_w2, filterH, filterH+taps, taps, offset, 1/*output_pitch*/, m_w/*srclen*/);
}
//imp_nr (wavcoeffs[0], m_w2, m_h2, 50.0f/20.0f);
delete[] tmpLo;
delete[] tmpHi;
@@ -303,24 +403,21 @@ namespace rtengine {
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
template<typename T> template<typename E>
void cplx_wavelet_level<T>::reconstruct_level(E *dst, float *filterV, float *filterH, int taps, int offset) {
void wavelet_level<T>::reconstruct_level(E *dst, float *filterV, float *filterH, int taps, int offset, int skip) {
//int hfw = (W+1)/2;
//int hfh = (H+1)/2;
T *tmpLo = new T[m_w*m_h2];
T *tmpHi = new T[m_w*m_h2];
int buflen = MAX(m_w,m_h);
int buflen = MAX(m_w2,m_h2);
float *bufferLo = new float[buflen];
float *bufferHi = new float[buflen];
/* filter along rows */
for (int i=0; i<m_h2; i++) {
SynthesisFilter (wavcoeffs[0]+i*m_w2, wavcoeffs[1]+i*m_w2, tmpLo+i*m_w, bufferLo, bufferHi, \
SynthesisFilter (wavcoeffs[0]+i*m_w2, wavcoeffs[1]+i*m_w2, tmpLo+i*m_w, bufferLo, bufferHi,
filterH, filterH+taps, taps, offset, 1/*pitch*/, m_w/*dstlen*/);
SynthesisFilter (wavcoeffs[2]+i*m_w2, wavcoeffs[3]+i*m_w2, tmpHi+i*m_w, bufferLo, bufferHi, \
SynthesisFilter (wavcoeffs[2]+i*m_w2, wavcoeffs[3]+i*m_w2, tmpHi+i*m_w, bufferLo, bufferHi,
filterH, filterH+taps, taps, offset, 1/*pitch*/, m_w/*dstlen*/);
}
@@ -341,6 +438,83 @@ namespace rtengine {
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
template<typename T>
void wavelet_level<T>::imp_nr (T* src, int width, int height, double thresh) {
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// impulse noise removal
// local variables
float hpfabs, hfnbrave;
const float eps = 0.01;
// buffer for the lowpass image
float * lpf = new float[width*height];
// buffer for the highpass image
float * impish = new float[width*height];
//The cleaning algorithm starts here
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// modified bilateral filter for lowpass image, omitting input pixel; or Gaussian blur
/*
static float eps = 1.0;
float wtdsum[3], dirwt, norm;
int i1, j1;
AlignedBuffer<double>* buffer = new AlignedBuffer<double> (MAX(width,height));
gaussHorizontal<float> (src, lpf, buffer, width, height, MAX(2.0,thresh-1.0), false);
gaussVertical<float> (lpf, lpf, buffer, width, height, MAX(2.0,thresh-1.0), false);
delete buffer;
*/
boxblur(src, lpf, 2, 2, width, height);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
float impthr = MAX(1.0,5.5-thresh);
for (int i=0; i < height; i++)
for (int j=0; j < width; j++) {
hpfabs = fabs(src[i*width+j]-lpf[i*width+j]);
//block average of high pass data
for (int i1=MAX(0,i-2), hfnbrave=0; i1<=MIN(i+2,height-1); i1++ )
for (int j1=MAX(0,j-2); j1<=MIN(j+2,width-1); j1++ ) {
hfnbrave += fabs(src[i1*width+j1]-lpf[i1*width+j1]);
}
hfnbrave = (hfnbrave-hpfabs)/24;
hpfabs>(hfnbrave*impthr) ? impish[i*width+j]=1 : impish[i*width+j]=0;
}//now impulsive values have been identified
for (int i=0; i < height; i++)
for (int j=0; j < width; j++) {
if (!impish[i*width+j]) continue;
float norm=0.0;
float wtdsum=0.0;
for (int i1=MAX(0,i-2), hfnbrave=0; i1<=MIN(i+2,height-1); i1++ )
for (int j1=MAX(0,j-2); j1<=MIN(j+2,width-1); j1++ ) {
if (i1==i && j1==j) continue;
if (impish[i1*width+j1]) continue;
float dirwt = 1/(SQR(src[i1*width+j1]-src[i*width+j])+eps);//use more sophisticated rangefn???
wtdsum += dirwt*src[i1*width+j1];
norm += dirwt;
}
//wtdsum /= norm;
if (norm) {
src[i*width+j]=wtdsum/norm;//low pass filter
}
}//now impulsive values have been corrected
delete [] lpf;
delete [] impish;
}
};

View File

@@ -233,9 +233,11 @@ template<class T> void gaussDerivH (T** src, T** dst, AlignedBuffer<double>* buf
double* temp = buffer->data;
for (int j=1; j<W-1; j++)
temp[j] = (0.5 * (src[i][j+1] - src[i][j-1]) );
dst[i][0] = 0.5*(src[i][1]-src[i][0]);
memcpy (dst[i]+1, temp+1, (W-2)*sizeof(T));
dst[i][W-1] = 0.5*(src[i][W-1]-src[i][W-2]);
dst[i][0] = (src[i][1]-src[i][0]);
//memcpy (dst[i]+1, temp+1, (W-2)*sizeof(T));
for (int j=1; j<W-1; j++)
dst[i][j] = temp[j];
dst[i][W-1] = (src[i][W-1]-src[i][W-2]);
}
return;
}
@@ -273,7 +275,7 @@ template<class T> void gaussDerivH (T** src, T** dst, AlignedBuffer<double>* buf
for (int i=0; i<H; i++) {
double* temp2 = buffer->data;
double src0 = 0.5*(src[i][1]-src[i][0]);
double src0 = (src[i][1]-src[i][0]);
temp2[0] = B * src0 + b1*src0 + b2*src0 + b3*src0;
temp2[1] = B * 0.5*(src[i][2]-src[i][0]) + b1*temp2[0] + b2*src0 + b3*src0;
@@ -282,7 +284,7 @@ template<class T> void gaussDerivH (T** src, T** dst, AlignedBuffer<double>* buf
for (int j=3; j<W-1; j++)
temp2[j] = B * 0.5*(src[i][j+1]-src[i][j-1]) + b1*temp2[j-1] + b2*temp2[j-2] + b3*temp2[j-3];
double srcWm1 = 0.5*(src[i][W-1]-src[i][W-2]);
double srcWm1 = (src[i][W-1]-src[i][W-2]);
temp2[W-1] = B * srcWm1 + b1*temp2[W-2] + b2*temp2[W-3] + b3*temp2[W-4];
@@ -310,14 +312,14 @@ template<class T> void gaussDerivV (T** src, T** dst, AlignedBuffer<double>* buf
#ifdef _OPENMP
#pragma omp for
#endif
for (int i=0; i<W; i++) {
for (int j=0; j<W; j++) {
double* temp = buffer->data;
for (int j = 1; j<H-1; j++)
temp[j] = (0.5 * (src[j+1][i] - src[j-1][i]) );
dst[0][i] = 0.5*(src[1][i]-src[0][i]);
for (int j=1; j<H-1; j++)
dst[j][i] = temp[j];
dst[H-1][i] = 0.5*(src[H-1][i]-src[H-2][i]);
for (int i = 1; i<H-1; i++)
temp[i] = (0.5 * (src[i+1][j] - src[i-1][j]) );
dst[0][j] = (src[1][j]-src[0][j]);
for (int i=1; i<H-1; i++)
dst[i][j] = temp[i];
dst[H-1][j] = (src[H-1][j]-src[H-2][j]);
}
return;
}

View File

@@ -36,6 +36,7 @@
#include "color.h"
#include "calc_distort.h"
#include "cplx_wavelet_dec.h"
#include "boxblur.h"
#ifdef _OPENMP
#include <omp.h>
@@ -558,10 +559,68 @@ void ImProcFunctions::colorCurve (LabImage* lold, LabImage* lnew) {
void ImProcFunctions::impulsedenoise (LabImage* lab) {
if (params->impulseDenoise.enabled && lab->W>=8 && lab->H>=8)
{
//impulse_nr (lab, (float)params->impulseDenoise.thresh/10.0 );//20 is normal
//impulse_nr (lab, (float)params->impulseDenoise.thresh/20.0 );
{ cplx_wavelet_decomposition Ldecomp(lab->data, lab->W, lab->H, 1 /*maxlvl*/);
Ldecomp.reconstruct(lab->data);}
for (int i=0; i<lab->W*lab->H; i++) {
lab->data[i] *= lab->data[i]/32768.0f;
}
wavelet_decomposition Ldecomp(lab->data, lab->W, lab->H, 5 );//last arg is num levels
//WaveletDenoise(Ldecomp, SQR((float)params->impulseDenoise.thresh*25.0f));
WaveletDenoise(Ldecomp, SQR((float)params->impulseDenoise.thresh/25.0f));
LabImage* labtmp = new LabImage (lab->W,lab->H);
int lvl = (params->impulseDenoise.thresh>>4)&7;
int branch = (params->impulseDenoise.thresh>>2)&1;//2*re_im + dir
int subband = params->impulseDenoise.thresh&3;//orientation for detail subbands
float noisevar = SQR((float)params->defringe.threshold * 10.0f);
/*for (int i=0; i<lab->W*lab->H; i++) {
//float recoeff = Ldecomp.level_coeffs(lvl,branch)[subband][i]/(2<<lvl);
//float imcoeff = Ldecomp.level_coeffs(lvl,branch+2)[subband][i]/(2<<lvl);
//float shrink = (SQR(recoeff)+SQR(imcoeff))/(SQR(recoeff)+SQR(imcoeff)+noisevar);
//lab->data[i] = sqrt(SQR(recoeff)+SQR(imcoeff)) * (subband != 0 ? 2*shrink : 0.707);
lab->data[i] = Ldecomp.level_coeffs(lvl,branch)[subband][i] + (subband != 0 ? 10000 : 0);
}*/
//for (int i=0; i<lab->W*lab->H; i++) {
// Ldecomp.level_coeffs(4)[0][i] = 0;
//}
Ldecomp.reconstruct(labtmp->data);
//double radius = (int)(params->impulseDenoise.thresh/10) ;
//boxvar(lab->data, lab->data, radius, radius, lab->W, lab->H);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*int w=lab->W;
int h=lab->H;
for(int y = 0; y != h-1; y++){
float *rg = &lab->data[w*y];
for(int x = 0; x != w-1; x++){
float gx = (fabs((rg[x + 1] - rg[x]) + (rg[x + w + 1] - rg[x + w])));
float gy = (fabs((rg[x + w] - rg[x]) + (rg[x + w + 1] - rg[x + 1])));
lab->data[w*y+x] = gx+gy;//sqrt(lab->data[i]/32768.0f)*32768.0f;
}
}*/
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//double radius = (double)params->impulseDenoise.thresh/40.0 ;
//AlignedBuffer<double>* buffer = new AlignedBuffer<double> (MAX(lab->W,lab->H));
//gaussDerivH<float> (lab->L, lab->L, buffer, lab->W, lab->H, radius, false/*multiThread*/);
//gaussVertical<float>(lab->L, lab->L, buffer, lab->W, lab->H, radius, false/*multiThread*/);
//delete buffer;
//impulse_nr (labtmp, 50.0f/20.0f);
for (int i=0; i<lab->W*lab->H; i++) {
//lab->data[i] = 4*(labtmp->data[i]-lab->data[i])+10000;
lab->data[i] = sqrt(MAX(0,labtmp->data[i]/32768.0f))*32768.0f;
}
delete labtmp;
impulse_nr (lab, 50.0f/20.0f);
}
}
void ImProcFunctions::defringe (LabImage* lab) {
@@ -646,7 +705,6 @@ fclose(f);*/
void ImProcFunctions::getAutoExp (LUTu & histogram, int histcompr, double defgain, double clip, \
double& expcomp, int& bright, int& contr, int& black, int& hlcompr, int& hlcomprthresh) {
double corr = 1;//pow(2.0, defgain);//defgain may be redundant legacy of superceded code???
float scale = 65536.0;
float midgray=0.15;//0.18445f;//middle gray in linear gamma = 0.18445*65535
@@ -677,15 +735,13 @@ fclose(f);*/
octile[count] += histogram[i];
if (octile[count]>sum/8 || (count==7 && octile[count]>sum/16)) {
octile[count]=log(1+i)/log(2);
count++;// = MIN(count+1,7);
count++;
}
}
if (i<ave) {
//lodev += SQR(ave-i)*histogram[i];
lodev += (log(ave+1)-log(i+1))*histogram[i];
losum += histogram[i];
} else {
//hidev += SQR(i-ave)*histogram[i];
hidev += (log(i+1)-log(ave+1))*histogram[i];
hisum += histogram[i];
}
@@ -701,7 +757,7 @@ fclose(f);*/
for (int i=1; i<6; i++) {
ospread += (octile[i+1]-octile[i])/MAX(0.5,(i>2 ? (octile[i+1]-octile[3]) : (octile[3]-octile[i])));
}
ospread /= 5;
ospread /= 5;//average width of octiles
// compute clipping points based on the original histograms (linear, without exp comp.)
int clipped = 0;
@@ -743,13 +799,9 @@ fclose(f);*/
//sets the mean or median at middle gray, and the amount that sets the estimated top
//of the histogram at or near clipping.
float expcomp1 = log(/*(median/ave)*//*(hidev/lodev)*/midgray*scale/(ave-shc+midgray*shc))/log(2);
float expcomp1 = log(midgray*scale/(ave-shc+midgray*shc))/log(2);
float expcomp2 = 0.5*( (15.5f-histcompr-(2*octile[7]-octile[6])) + log(scale/rawmax)/log(2) );
/*expcomp = (expcomp1*fabs(expcomp2)+expcomp2*fabs(expcomp1))/(fabs(expcomp1)+fabs(expcomp2));
if (expcomp<0) {
MIN(0.0f,MAX(expcomp1,expcomp2));
}*/
expcomp = 0.5 * (expcomp1 + expcomp2);
float gain = exp(expcomp*log(2));
@@ -765,7 +817,7 @@ fclose(f);*/
float shoulder = ((scale/MAX(1,gain))*(hlcomprthresh/200.0))+0.1;
//this is a series approximation of the actual formula for comp,
//which is a transcendental equation
float comp = (gain*((float)whiteclip)/scale - 1)*2;//*(1-shoulder/scale);
float comp = (gain*((float)whiteclip)/scale - 1)*2;
hlcompr=(int)(100*comp/(MAX(0,expcomp) + 1.0));
hlcompr = MAX(0,MIN(100,hlcompr));
@@ -778,52 +830,12 @@ fclose(f);*/
bright = (midgray-midtmp)*15.0/(0.10833-0.0833*midtmp);
}
bright = 0.25*/*(median/ave)*(hidev/lodev)*/MAX(0,bright);
bright = 0.25*MAX(0,bright);
//compute contrast that spreads the average spacing of octiles
contr = 50.0*(1.1-ospread);
contr = MAX(0,MIN(100,contr));
//diagnostics
//printf ("**************** AUTO LEVELS ****************\n");
//printf ("gain1= %f gain2= %f gain= %f\n",expcomp1,expcomp2,gain);
//printf ("median: %i average: %f median/average: %f\n",median,ave, median/ave);
//printf ("average: %f\n",ave);
//printf ("median/average: %f\n",median/ave);
//printf ("lodev: %f hidev: %f hidev/lodev: %f\n",lodev,hidev,hidev/lodev);
//printf ("lodev: %f\n",lodev);
//printf ("hidev: %f\n",hidev);
//printf ("rawmax= %d whiteclip= %d gain= %f\n",rawmax,whiteclip,gain);
//printf ("octiles: %f %f %f %f %f %f %f %f\n",octile[0],octile[1],octile[2],octile[3],octile[4],octile[5],octile[6],octile[7]);
//printf ("ospread= %f\n",ospread);
/*
// %%%%%%%%%% LEGACY AUTOEXPOSURE CODE %%%%%%%%%%%%%
// black point selection is based on the linear result (yielding better visual results)
black = (int)(shc * corr);
// compute the white point of the exp. compensated gamma corrected image
double whiteclipg = (int)(CurveFactory::gamma2 (whiteclip * corr / 65536.0) * 65536.0);
// compute average intensity of the exp compensated, gamma corrected image
double gavg = 0;
for (int i=0; i<65536>>histcompr; i++)
gavg += histogram[i] * CurveFactory::gamma2((int)(corr*(i<<histcompr)<65535 ? corr*(i<<histcompr) : 65535)) / sum;
if (black < gavg) {
int maxwhiteclip = (gavg - black) * 4 / 3 + black; // dont let whiteclip be such large that the histogram average goes above 3/4
//double mavg = 65536.0 / (whiteclipg-black) * (gavg - black);
if (whiteclipg < maxwhiteclip)
whiteclipg = maxwhiteclip;
}
whiteclipg = CurveFactory::igamma2 ((float)(whiteclipg/65535.0)) * 65535.0; //need to inverse gamma transform to get correct exposure compensation parameter
black = (int)((65535*black)/whiteclipg);
expcomp = log(65535.0 / (whiteclipg)) / log(2.0);
if (expcomp<0.0) expcomp = 0.0;*/
if (expcomp<-5.0) expcomp = -5.0;
if (expcomp>10.0) expcomp = 10.0;

View File

@@ -29,6 +29,7 @@
#include "labimage.h"
#include "LUT.h"
#include <fftw3.h>
#include "cplx_wavelet_dec.h"
namespace rtengine {
@@ -144,7 +145,7 @@ namespace rtengine {
//void L_denoise(Imagefloat * src, LabImage * dst, const procparams::DirPyrDenoiseParams & dnparams);//Emil's FT denoise
//void tile_denoise (fftwf_complex ** fLblox, int vblproc, int hblproc, \
int blkrad, int numblox_H, int numblox_W, float noisevar );
void RGB_InputTransf(Imagefloat * src, LabImage * dst, LabImage * blur, const procparams::DirPyrDenoiseParams & dnparams, const procparams::DefringeParams & defringe);
void RGB_InputTransf(Imagefloat * src, LabImage * dst, const procparams::DirPyrDenoiseParams & dnparams, const procparams::DefringeParams & defringe);
void RGB_OutputTransf(LabImage * src, Imagefloat * dst, const procparams::DirPyrDenoiseParams & dnparams);
void output_tile_row (float *Lbloxrow, float ** Lhipassdn, float ** tilemask, int height, int width, int top, int blkrad );
void RGB_denoise(Imagefloat * src, Imagefloat * dst, const procparams::DirPyrDenoiseParams & dnparams, const procparams::DefringeParams & defringe);
@@ -157,6 +158,17 @@ namespace rtengine {
void dirpyr_ab(LabImage * data_fine, LabImage * data_coarse, const procparams::DirPyrDenoiseParams & dnparams);
void dirpyr_ablevel(LabImage * data_fine, LabImage * data_coarse, int width, int height, \
LUTf &rangefn_L, LUTf &rangefn_ab, int level, int scale);
void ImStats(float* src, float* dst, int H, int W, int box );
void WaveletDenoise(cplx_wavelet_decomposition &DualTreeCoeffs, float noisevar );
void WaveletDenoise(wavelet_decomposition &WaveletCoeffs, float noisevar );
void BiShrink(float * ReCoeffs, float * ImCoeffs, float * ReParents, float * ImParents, \
int W, int H, int level, int padding, float noisevar);
void BiShrink(float ** WavCoeffs, float ** WavParents, int W, int H, int level, int padding, float noisevar);
void FirstStageWiener(float* ReCoeffs, float* ImCoeffs, float* wiener1, int W, int H, int rad, float noisevar);
void SecondStageWiener(float* ReCoeffs, float* ImCoeffs, float* wiener1, int W, int H, int rad, float noisevar);
void QCoeffs (float* srcre, float* srcim, float* wiener1, float* dst, int rad, int W, int H);
float UniversalThresh(float * HH_Coeffs, int datalen);
// pyramid equalizer
void dirpyr_equalizer (float ** src, float ** dst, int srcwidth, int srcheight, const double * mult );//Emil's directional pyramid equalizer

View File

@@ -37,9 +37,7 @@ void ImProcFunctions::impulse_nr (LabImage* lab, double thresh) {
int width = lab->W;
int height = lab->H;
float hpfabs, hfnbrave;
// buffer for the lowpass image
float ** lpf = new float *[height];
// buffer for the highpass image
@@ -77,10 +75,10 @@ void ImProcFunctions::impulse_nr (LabImage* lab, double thresh) {
for (int i=0; i < height; i++)
for (int j=0; j < width; j++) {
hpfabs = fabs(lab->L[i][j]-lpf[i][j]);
float hfnbrave = 0;
float hpfabs = fabs(lab->L[i][j]-lpf[i][j]);
//block average of high pass data
for (i1=MAX(0,i-2), hfnbrave=0; i1<=MIN(i+2,height-1); i1++ )
for (i1=MAX(0,i-2); i1<=MIN(i+2,height-1); i1++ )
for (j1=MAX(0,j-2); j1<=MIN(j+2,width-1); j1++ ) {
hfnbrave += fabs(lab->L[i1][j1]-lpf[i1][j1]);
}
@@ -94,7 +92,7 @@ void ImProcFunctions::impulse_nr (LabImage* lab, double thresh) {
if (!impish[i][j]) continue;
norm=0.0;
wtdsum[0]=wtdsum[1]=wtdsum[2]=0.0;
for (i1=MAX(0,i-2), hfnbrave=0; i1<=MIN(i+2,height-1); i1++ )
for (i1=MAX(0,i-2); i1<=MIN(i+2,height-1); i1++ )
for (j1=MAX(0,j-2); j1<=MIN(j+2,width-1); j1++ ) {
if (i1==i && j1==j) continue;
if (impish[i1][j1]) continue;