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:
@@ -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));;
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include "LUT.h"
|
||||
#include "array2D.h"
|
||||
#include "iccmatrices.h"
|
||||
#include "boxblur.h"
|
||||
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
@@ -51,9 +52,9 @@
|
||||
//#define ULIM(x,y,z) ((y) < (z) ? LIM(x,y,z) : LIM(x,z,y))
|
||||
//#define CLIP(x) LIM(x,0,65535)
|
||||
|
||||
#define TS 256 // Tile size
|
||||
#define offset TS/2 // shift between tiles
|
||||
#define fTS ((TS/2+1)) // shift between Fourier tiles
|
||||
#define TS 128 // Tile size
|
||||
#define offset (TS/4) // shift between tiles
|
||||
#define fTS ((TS/2+1)) // second dimension of Fourier tiles
|
||||
//#define eps 0.01f/(TS*TS) //tolerance
|
||||
|
||||
namespace rtengine {
|
||||
@@ -80,7 +81,7 @@ namespace rtengine {
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
void ImProcFunctions::RGB_InputTransf(Imagefloat * src, LabImage * dst, LabImage * blur, const procparams::DirPyrDenoiseParams & dnparams, const procparams::DefringeParams & defringe) {
|
||||
void ImProcFunctions::RGB_InputTransf(Imagefloat * src, LabImage * dst, const procparams::DirPyrDenoiseParams & dnparams, const procparams::DefringeParams & defringe) {
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
// gamma transform input channel data
|
||||
@@ -92,9 +93,11 @@ void ImProcFunctions::RGB_InputTransf(Imagefloat * src, LabImage * dst, LabImage
|
||||
LUTf gamcurve(65536,0);
|
||||
|
||||
for (int i=0; i<65536; i++) {
|
||||
gamcurve[i] = (gamma((double)i/65535.0, gam, gamthresh, gamslope, 1.0, 0.0));// * 32768.0f;
|
||||
gamcurve[i] = (gamma((double)i/65535.0, gam, gamthresh, gamslope, 1.0, 0.0)) * 32768.0f;
|
||||
}
|
||||
|
||||
//srand((unsigned)time(0));//test with random data
|
||||
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for
|
||||
#endif
|
||||
@@ -105,95 +108,19 @@ void ImProcFunctions::RGB_InputTransf(Imagefloat * src, LabImage * dst, LabImage
|
||||
float Y = xyz_prophoto[1][0]*src->r[i][j] + xyz_prophoto[1][1]*src->g[i][j] + xyz_prophoto[1][2]*src->b[i][j];
|
||||
float Z = xyz_prophoto[2][0]*src->r[i][j] + xyz_prophoto[2][1]*src->g[i][j] + xyz_prophoto[2][2]*src->b[i][j];
|
||||
|
||||
X = gamcurve[X];
|
||||
Y = gamcurve[Y];
|
||||
Z = gamcurve[Z];
|
||||
X = X<65535.0f ? gamcurve[X] : (gamma((double)X/65535.0, gam, gamthresh, gamslope, 1.0, 0.0)*32768.0f);
|
||||
Y = Y<65535.0f ? gamcurve[Y] : (gamma((double)Y/65535.0, gam, gamthresh, gamslope, 1.0, 0.0)*32768.0f);
|
||||
Z = Z<65535.0f ? gamcurve[Z] : (gamma((double)Z/65535.0, gam, gamthresh, gamslope, 1.0, 0.0)*32768.0f);
|
||||
|
||||
dst->L[i][j] = Y;
|
||||
dst->a[i][j] = 0.2f*(X-Y);
|
||||
dst->b[i][j] = 0.2f*(Y-Z);
|
||||
|
||||
}
|
||||
|
||||
//pre-blur L channel to mitigate artifacts
|
||||
/*for (int i=1; i<src->height-1; i++)
|
||||
for (int j=1; j<src->width-1; j++) {
|
||||
dst->L[i][j] = 0.25*(dst->L[i][j]+0.5*(dst->L[i-1][j]+dst->L[i+1][j]+dst->L[i][j-1]+dst->L[i][j+1])
|
||||
+0.25*(dst->L[i-1][j-1]+dst->L[i-1][j+1]+dst->L[i+1][j-1]+dst->L[i+1][j+1]));
|
||||
|
||||
}*/
|
||||
|
||||
//cplx_wavelet_decomposition Ldecomp(dst->data, dst->W, dst->H, 2 /*maxlvl*/);
|
||||
//Ldecomp.reconstruct(dst->data);
|
||||
|
||||
impulse_nr (dst, 50.0f/20.0f);
|
||||
//PF_correct_RT(dst, dst, defringe.radius, defringe.threshold);
|
||||
|
||||
|
||||
//blur the image
|
||||
//dirpyr_ab(dst, blur, dnparams);//use dirpyr here if using it to blur all channels
|
||||
|
||||
EPDParams *p = (EPDParams *)(¶ms->edgePreservingDecompositionUI);
|
||||
|
||||
float EdgeStop = (pow(0.15f, 1.0f/dnparams.gamma)/0.15f) * (float)dnparams.Lamt/10.0f;//compensate edgestopping power according to gamma
|
||||
EdgePreserveLab epd = EdgePreserveLab(dst->W, dst->H);
|
||||
blur->data = epd.CreateIteratedBlur(dst->data /*Source*/, (float)p->Scale /*LScale*/, (float)p->EdgeStopping /*abScale*/, \
|
||||
EdgeStop /*(float)dnparams.Lamt/10.0f*/ /*EdgeStoppingLuma*/, (float)dnparams.chroma/25.0f/*p->EdgeStopping*/ /*EdgeStoppingChroma*/, \
|
||||
5/*Iterates*/, 0/*Reweightings*/, blur->data/*Blur*/);
|
||||
|
||||
//impulsedenoise (blur);
|
||||
|
||||
/*
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel
|
||||
#endif
|
||||
{
|
||||
AlignedBuffer<double>* buffer = new AlignedBuffer<double> (MAX(dst->W,dst->H));
|
||||
//gaussHorizontal<float> (src->L, tmp1->L, buffer, src->W, src->H, radius, multiThread);
|
||||
//gaussHorizontal<float> (src->b, tmp1->b, buffer, src->W, src->H, radius, multiThread);
|
||||
//gaussVertical<float> (tmp1->a, tmp1->a, buffer, src->W, src->H, radius, multiThread);
|
||||
//gaussVertical<float> (tmp1->b, tmp1->b, buffer, src->W, src->H, radius, multiThread);
|
||||
|
||||
gaussHorizontal<float> (dst->L, blur->L, buffer, dst->W, dst->H, (double)dnparams.Lamt/25.0f, multiThread);
|
||||
gaussVertical<float> (blur->L, blur->L, buffer, dst->W, dst->H, (double)dnparams.Lamt/25.0f, multiThread);
|
||||
//gaussHorizontal<float> (blur->L, blur->L, buffer, dst->W, dst->H, (double)dnparams.Lamt/25.0f, multiThread);
|
||||
//gaussVertical<float> (blur->L, blur->L, buffer, dst->W, dst->H, (double)dnparams.Lamt/25.0f, multiThread);
|
||||
|
||||
delete buffer;
|
||||
}
|
||||
*/
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
// gamma transform input channel data
|
||||
/*gam = dnparams.gamma/3.0f;
|
||||
gamthresh = 0.03;
|
||||
gamslope = exp(log((double)gamthresh)/gam)/gamthresh;
|
||||
gam3slope = exp(log((double)gamthresh)/3.0f)/gamthresh;
|
||||
|
||||
for (int i=0; i<65536; i++) {
|
||||
gamcurve[i] = (gamma((double)i/65535.0, gam, gamthresh, gamslope, 1.0, 0.0)) * 65535.0f;
|
||||
}*/
|
||||
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for
|
||||
#endif
|
||||
for (int i=0; i<src->height; i++) {
|
||||
for (int j=0; j<src->width; j++) {
|
||||
//float wt = expf(-100.0f*fabs(dst->L[i][j]-blur->L[i][j])/((float)dnparams.luma));
|
||||
//blur->L[i][j] = wt*dst->L[i][j] + (1-wt)*blur->L[i][j];
|
||||
//blur->L[i][j] = dst->L[i][j];
|
||||
blur->a[i][j] /*= 32768.0f*0.2f*(blur->a[i][j]-blur->L[i][j]);/*/*= 32768.0f;
|
||||
blur->b[i][j] /*= 32768.0f*0.2f*(blur->L[i][j]-blur->b[i][j]);/*/*= 32768.0f;
|
||||
blur->L[i][j] *= 32768.0f;//= gamcurve[32768.0f*blur->L[i][j]];
|
||||
|
||||
dst->L[i][j] *= 32768.0f;//= gamcurve[32768.0f*dst->L[i][j]];
|
||||
dst->a[i][j] *= 32768.0f;
|
||||
dst->b[i][j] *= 32768.0f;
|
||||
//Y = 0.05+0.1*((float)rand()/(float)RAND_MAX);//test with random data
|
||||
//dst->L[i][j] = gamcurve[65535.0f*Y];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//dirpyr_ab(blur, blur, dnparams);//use dirpyr here if using it to blur ab channels only
|
||||
//dirpyrLab_denoise(blur, blur, dnparams);//use dirpyr here if using it to blur ab channels only
|
||||
|
||||
}
|
||||
|
||||
@@ -243,9 +170,11 @@ void ImProcFunctions::RGB_OutputTransf(LabImage * src, Imagefloat * dst, const p
|
||||
X = (5.0f*(src->a[i][j])) + Y;
|
||||
Z = Y - (5.0f*(src->b[i][j]));
|
||||
|
||||
X = igamcurve[X];
|
||||
Y = igamcurve[Y];
|
||||
Z = igamcurve[Z];
|
||||
X = X<32768.0f ? igamcurve[X] : (gamma((float)X/32768.0f, igam, igamthresh, igamslope, 1.0, 0.0) * 65535.0f);
|
||||
Y = Y<32768.0f ? igamcurve[Y] : (gamma((float)Y/32768.0f, igam, igamthresh, igamslope, 1.0, 0.0) * 65535.0f);
|
||||
Z = Z<32768.0f ? igamcurve[Z] : (gamma((float)Z/32768.0f, igam, igamthresh, igamslope, 1.0, 0.0) * 65535.0f);
|
||||
|
||||
//Y = 65535.0f*(0.05+0.1*((float)rand()/(float)RAND_MAX));//test with random data
|
||||
|
||||
dst->r[i][j] = prophoto_xyz[0][0]*X + prophoto_xyz[0][1]*Y + prophoto_xyz[0][2]*Z;
|
||||
dst->g[i][j] = prophoto_xyz[1][0]*X + prophoto_xyz[1][1]*Y + prophoto_xyz[1][2]*Z;
|
||||
@@ -277,8 +206,8 @@ void ImProcFunctions::RGB_denoise(Imagefloat * src, Imagefloat * dst, /*int Roff
|
||||
//const short int hfh=(height+1), hfw=(width+1);
|
||||
|
||||
const int blkrad=1;
|
||||
float noisevar_L = SQR(dnparams.luma * TS * 40.0f);
|
||||
float noisevar_ab = SQR(dnparams.chroma * TS * 200.0f);
|
||||
float noisevar_L = SQR(dnparams.luma * TS * 10.0f);
|
||||
float noisevar_ab = SQR(dnparams.chroma * TS * 100.0f);
|
||||
|
||||
//int dxr=Roffset&1, dyr=(Roffset&2)/2, dxb=(1-dxr), dyb=(1-dyr);
|
||||
//int rdx, rdy, bdx, bdy;
|
||||
@@ -301,14 +230,17 @@ void ImProcFunctions::RGB_denoise(Imagefloat * src, Imagefloat * dst, /*int Roff
|
||||
for (int i=0; i<TS; i++) {
|
||||
float i1 = abs((i>TS/2 ? i-TS : i));
|
||||
float vmask = (i1<8 ? SQR(sin(M_PI*i1/16.0f)) : 1.0f);
|
||||
float vmask2 = (i1<32 ? SQR(sin(M_PI*i1/64.0f)) : 1.0f);
|
||||
for (int j=0; j<TS; j++) {
|
||||
float j1 = abs((j>TS/2 ? j-TS : j));
|
||||
//tilemask_in[i][j] = vmask * (j1<4 ? SQR(sin(M_PI*(float)j1/8.0f)) : 1.0f);
|
||||
tilemask_in[i][j] = (exp(-(SQR(i-TS/2-0.5f)+SQR(j-TS/2-0.5f))/(2*SQR(TS/4.0f))) * \
|
||||
vmask * (j1<8 ? SQR(sin(M_PI*(float)j1/16.0f)) : 1.0f));
|
||||
|
||||
tilemask_out[i][j] = (SQR(MAX(0.0f, sin(M_PI*(float)(i-4.0f)/(TS-9) ))) * \
|
||||
SQR(MAX(0.0f, sin(M_PI*(float)(j-4.0f)/(TS-9) ))));
|
||||
//tilemask_out[i][j] = (SQR(MAX(0.0f, sin(M_PI*(float)(i-8.0f)/(TS-17) ))) * \
|
||||
SQR(MAX(0.0f, sin(M_PI*(float)(j-8.0f)/(TS-17) ))));
|
||||
tilemask_out[i][j] = vmask2 * (j1<32 ? SQR(sin(M_PI*(float)j1/64.0f)) : 1.0f);
|
||||
|
||||
//tilemask_out[i][j] = exp(-(SQR(i-TS/2-0.5f)+SQR(j-TS/2-0.5f))/(SQR(TS/4.0f)));
|
||||
|
||||
}
|
||||
@@ -323,13 +255,42 @@ void ImProcFunctions::RGB_denoise(Imagefloat * src, Imagefloat * dst, /*int Roff
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
// transform RGB input to ersatz Lab
|
||||
|
||||
RGB_InputTransf(src, labin, labblur, dnparams, defringe);
|
||||
RGB_InputTransf(src, labin, dnparams, defringe);
|
||||
|
||||
wavelet_decomposition Ldecomp(labin->data, labin->W, labin->H, 5 );//last arg is num levels
|
||||
WaveletDenoise(Ldecomp, SQR((float)dnparams.Lamt/25.0f));
|
||||
Ldecomp.reconstruct(labblur->data);
|
||||
|
||||
|
||||
//impulse_nr (dst, 50.0f/20.0f);
|
||||
//PF_correct_RT(dst, dst, defringe.radius, defringe.threshold);
|
||||
|
||||
|
||||
int datalen = labin->W*labin->H;
|
||||
|
||||
for (int i=datalen; i<3*datalen; i++) {
|
||||
labblur->data[i]=labin->data[i];
|
||||
}
|
||||
/*
|
||||
wavelet_decomposition adecomp(labin->data+datalen, labin->W, labin->H, 5 );//last arg is num levels
|
||||
WaveletDenoise(adecomp, SQR((float)dnparams.chroma/5.0f));
|
||||
adecomp.reconstruct(labblur->data+datalen);
|
||||
|
||||
wavelet_decomposition bdecomp(labin->data+2*datalen, labin->W, labin->H, 5 );//last arg is num levels
|
||||
WaveletDenoise(bdecomp, SQR((float)dnparams.chroma/5.0f));
|
||||
bdecomp.reconstruct(labblur->data+2*datalen);
|
||||
*/
|
||||
|
||||
//dirpyr_ab(labin, labblur, dnparams);//use dirpyr here if using it to blur ab channels only
|
||||
//dirpyrLab_denoise(labin, labblur, dnparams);//use dirpyr here if using it to blur ab channels only
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
// initialize FT and output data structures
|
||||
LabImage * labdn = new LabImage(width,height);
|
||||
for (int i=0; i<3*labdn->W*labdn->H; i++) {
|
||||
labdn->data[i] = 0.0f;
|
||||
}
|
||||
float ** Lblox = new float *[8] ;
|
||||
float ** RLblox = new float *[8] ;
|
||||
float ** BLblox = new float *[8] ;
|
||||
@@ -364,13 +325,13 @@ void ImProcFunctions::RGB_denoise(Imagefloat * src, Imagefloat * dst, /*int Roff
|
||||
Lblox[0], NULL, 1, TS*TS, FFTW_ESTIMATE );
|
||||
|
||||
//for DCT:
|
||||
/*const fftw_r2r_kind fwdkind[2] = {FFTW_REDFT10, FFTW_REDFT10};
|
||||
const fftw_r2r_kind bwdkind[2] = {FFTW_REDFT01, FFTW_REDFT01};
|
||||
//const fftw_r2r_kind fwdkind[2] = {FFTW_REDFT10, FFTW_REDFT10};
|
||||
//const fftw_r2r_kind bwdkind[2] = {FFTW_REDFT01, FFTW_REDFT01};
|
||||
|
||||
plan_forward_blox = fftwf_plan_many_r2r(2, nfwd, numblox_W, Lblox[0], NULL, 1, TS*TS, \
|
||||
//plan_forward_blox = fftwf_plan_many_r2r(2, nfwd, numblox_W, Lblox[0], NULL, 1, TS*TS, \
|
||||
fLblox[0], NULL, 1, TS*TS, fwdkind, FFTW_ESTIMATE );
|
||||
plan_backward_blox = fftwf_plan_many_r2r(2, nfwd, numblox_W, fLblox[0], NULL, 1, TS*TS, \
|
||||
Lblox[0], NULL, 1, TS*TS, bwdkind, FFTW_ESTIMATE );*/
|
||||
//plan_backward_blox = fftwf_plan_many_r2r(2, nfwd, numblox_W, fLblox[0], NULL, 1, TS*TS, \
|
||||
Lblox[0], NULL, 1, TS*TS, bwdkind, FFTW_ESTIMATE );
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
@@ -479,6 +440,8 @@ void ImProcFunctions::RGB_denoise(Imagefloat * src, Imagefloat * dst, /*int Roff
|
||||
}
|
||||
//end of tile padding
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
Lblox[vblkmod][(indx + TS/2)*TS+TS/2]=32768.0f;//testing: locate block center
|
||||
|
||||
}//end of filling block row
|
||||
|
||||
//fftwf_print_plan (plan_forward_blox);
|
||||
@@ -577,34 +540,38 @@ void ImProcFunctions::RGB_denoise(Imagefloat * src, Imagefloat * dst, /*int Roff
|
||||
for (int i=0; i<height; i++) {
|
||||
for (int j=0; j<width; j++) {
|
||||
//may want to include masking threshold for large hipass data to preserve edges/detail
|
||||
//or compare original hpf to denoised; restore original based on their ratio
|
||||
float hporig = labin->L[i][j]-labblur->L[i][j];
|
||||
float hpdn = labdn->L[i][j]/totwt[i][j];//note that labdn initially stores the denoised hipass data
|
||||
float hgratio = 0;//MIN(1.0f,(abs(hpdn)+eps)/(abs(hporig)+eps));
|
||||
|
||||
labdn->L[i][j] = labblur->L[i][j] + (hgratio*hporig+(1-hgratio)*hpdn);
|
||||
labdn->L[i][j] = labblur->L[i][j] + hpdn;
|
||||
//labdn->L[i][j] = -(hporig)+0.25;
|
||||
|
||||
hporig = labin->a[i][j]-labblur->a[i][j];
|
||||
hpdn = labdn->a[i][j]/totwt[i][j];
|
||||
labdn->a[i][j] = labblur->a[i][j] ;//+ (hgratio*hporig+(1-hgratio)*hpdn);
|
||||
//hpdn = labdn->a[i][j]/totwt[i][j];
|
||||
//labdn->a[i][j] = labblur->a[i][j] ;//+ hpdn;
|
||||
//labdn->a[i][j] = (hporig);
|
||||
|
||||
|
||||
hporig = labin->b[i][j]-labblur->b[i][j];
|
||||
hpdn = labdn->b[i][j]/totwt[i][j];
|
||||
labdn->b[i][j] = labblur->b[i][j] ;//+ (hgratio*hporig+(1-hgratio)*hpdn);
|
||||
//hpdn = labdn->b[i][j]/totwt[i][j];
|
||||
//labdn->b[i][j] = labblur->b[i][j] ;//+ hpdn;
|
||||
//labdn->b[i][j] = (hporig);
|
||||
|
||||
|
||||
//labdn->a[i][j] = labin->a[i][j];
|
||||
//labdn->b[i][j] = labin->b[i][j];
|
||||
labdn->a[i][j] = labblur->a[i][j];
|
||||
labdn->b[i][j] = labblur->b[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
//dirpyr_ab(labdn, labdn, dnparams);//use dirpyr here if using it to blur ab channels only
|
||||
|
||||
dirpyrdenoise(labdn);//denoise ab channels using ImProcFns denoise (stripped to ab channels only)
|
||||
//dirpyrdenoise(labdn);//denoise ab channels using ImProcFns denoise (stripped to ab channels only)
|
||||
|
||||
//Wavelet denoise of ab channels
|
||||
//int numpix = labdn->W*labdn->H;
|
||||
//cplx_wavelet_decomposition adecomp(labdn->data+numpix, labdn->W, labdn->H, 5 );//last arg is num levels
|
||||
//WaveletDenoise(adecomp, SQR((float)dnparams.chroma*100.0f));
|
||||
//adecomp.reconstruct(labdn->data+numpix);
|
||||
//cplx_wavelet_decomposition bdecomp(labdn->data+2*numpix, labdn->W, labdn->H, 5 );//last arg is num levels
|
||||
//WaveletDenoise(bdecomp, SQR((float)dnparams.chroma*100.0f));
|
||||
//bdecomp.reconstruct(labdn->data+2*numpix);
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
// transform denoised "Lab" to output RGB
|
||||
@@ -633,7 +600,9 @@ void ImProcFunctions::RGBtile_denoise (fftwf_complex ** fLblox, fftwf_complex **
|
||||
{
|
||||
int vblprocmod=vblproc%8;
|
||||
|
||||
float eps = 0.01f/(TS*TS); //tolerance
|
||||
const float eps = 0.01f/(TS*TS); //tolerance
|
||||
const float cutoffsq = 8.0f;//frequency cutoff
|
||||
|
||||
|
||||
float RLblockvar=eps, BLblockvar=eps, Lblockvar=eps;
|
||||
|
||||
@@ -678,7 +647,7 @@ void ImProcFunctions::RGBtile_denoise (fftwf_complex ** fLblox, fftwf_complex **
|
||||
//float BLcoeffre = fBLblox[vblprocmod][(hblproc*TS+i)*fTS+j][0];
|
||||
//float BLcoeffim = fBLblox[vblprocmod][(hblproc*TS+i)*fTS+j][1];
|
||||
|
||||
double nbrave=0, nbrsqave=0, coeffsq;
|
||||
/*double nbrave=0, nbrsqave=0, coeffsq;
|
||||
int vblnbrmod, hblnbrmod;
|
||||
for (int k=0; k<9; k++) {
|
||||
vblnbrmod = (vblproc+(k/3)+7)%8;
|
||||
@@ -690,7 +659,7 @@ void ImProcFunctions::RGBtile_denoise (fftwf_complex ** fLblox, fftwf_complex **
|
||||
nbrave += sqrt(coeffsq);
|
||||
nbrsqave += coeffsq;
|
||||
}
|
||||
float nbrvar = (nbrsqave/9.0f)-SQR(nbrave/9.0f);
|
||||
float nbrvar = (nbrsqave/9.0f)-SQR(nbrave/9.0f);*/
|
||||
|
||||
float Lwsq = eps+SQR( Lcoeffre)+SQR( Lcoeffim); //for FT
|
||||
//float Lwsq = eps+SQR( Lcoeff); //for DCT
|
||||
@@ -699,13 +668,14 @@ void ImProcFunctions::RGBtile_denoise (fftwf_complex ** fLblox, fftwf_complex **
|
||||
//float BLwsq = eps+SQR(BLcoeffre)+SQR(BLcoeffim);
|
||||
|
||||
//wsqave += Lwsq;
|
||||
float Lfactor = (4*Lblockvar)/(eps+(Lwsq+nbrvar)+2*Lblockvar);
|
||||
//float Lfactor = (4*Lblockvar)/(eps+(Lwsq+nbrvar)+2*Lblockvar);
|
||||
//float Lfactor = expf(-Lwsq/(9*Lblockvar));
|
||||
//float Lfactor = 1;//(2* Lblockvar)/(eps+ Lwsq+ Lblockvar);
|
||||
float freqfactor = 1.0f-MAX((expf(-(SQR(i)+SQR(j))/cutoffsq)),(expf(-(SQR(TS-i)+SQR(j))/cutoffsq)));
|
||||
float Lfactor = 1;//freqfactor;//*(2* Lblockvar)/(eps+ Lwsq+ Lblockvar);
|
||||
//float RLfactor = 1;//(2*RLblockvar)/(eps+RLwsq+RLblockvar);
|
||||
//float BLfactor = 1;//(2*BLblockvar)/(eps+BLwsq+BLblockvar);
|
||||
Lwsq = MAX(0.0f, Lwsq-0.25*Lblockvar);
|
||||
float Lshrinkfactor = Lwsq/(Lwsq + noisevar_L * Lfactor);
|
||||
//Lwsq = MAX(0.0f, Lwsq-0.25*Lblockvar);
|
||||
float Lshrinkfactor = SQR(Lwsq/(Lwsq + noisevar_L * Lfactor*exp(-Lwsq/(3*noisevar_L))));
|
||||
//float RLshrinkfactor = RLwsq/(RLwsq+noisevar_ab*RLfactor);
|
||||
//float BLshrinkfactor = BLwsq/(BLwsq+noisevar_ab*BLfactor);
|
||||
|
||||
@@ -763,7 +733,7 @@ void ImProcFunctions::RGBoutput_tile_row (float *bloxrow_L, float *bloxrow_a, fl
|
||||
#undef TS
|
||||
#undef fTS
|
||||
#undef offset
|
||||
#undef eps
|
||||
//#undef eps
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
@@ -814,7 +784,9 @@ void ImProcFunctions::dirpyr_ab(LabImage * data_fine, LabImage * data_coarse, co
|
||||
}
|
||||
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
void ImProcFunctions::dirpyr_ablevel(LabImage * data_fine, LabImage * data_coarse, int width, int height, LUTf & rangefn_L, LUTf & rangefn_ab, int level, int scale)
|
||||
{
|
||||
@@ -897,6 +869,10 @@ void ImProcFunctions::dirpyr_ablevel(LabImage * data_fine, LabImage * data_coars
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
/*
|
||||
void ImProcFunctions::FixImpulse_ab(LabImage * src, LabImage * dst, double radius, int thresh) {
|
||||
|
||||
@@ -987,4 +963,316 @@ void ImProcFunctions::FixImpulse_ab(LabImage * src, LabImage * dst, double radiu
|
||||
*/
|
||||
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
void ImProcFunctions::WaveletDenoise(cplx_wavelet_decomposition &DualTreeCoeffs, float noisevar )
|
||||
{
|
||||
int maxlvl = DualTreeCoeffs.maxlevel();
|
||||
int rad_stage1[8] = {3,2,1,1,1,1,1,1};
|
||||
int rad_stage2[8] = {2,1,1,1,1,1,1,1};
|
||||
|
||||
for (int lvl=0; lvl<maxlvl-1; lvl++) {
|
||||
int Wlvl = DualTreeCoeffs.level_W(lvl,0);
|
||||
int Hlvl = DualTreeCoeffs.level_H(lvl,0);
|
||||
|
||||
array2D<float> wiener1(Wlvl,Hlvl);
|
||||
|
||||
for (int m=0; m<2; m++) {
|
||||
float ** ReCoeffs = DualTreeCoeffs.level_coeffs(lvl,0+m);
|
||||
float ** ImCoeffs = DualTreeCoeffs.level_coeffs(lvl,2+m);
|
||||
float ** ReParents = DualTreeCoeffs.level_coeffs(lvl+1,0+m);
|
||||
float ** ImParents = DualTreeCoeffs.level_coeffs(lvl+1,2+m);
|
||||
int ParentPadding = DualTreeCoeffs.level_pad(lvl+1,0+m);
|
||||
for (int dir=1; dir<4; dir++) {
|
||||
//FirstStageWiener (ReCoeffs[dir],ImCoeffs[dir],wiener1,Wlvl,Hlvl,rad_stage1[lvl], noisevar);
|
||||
//SecondStageWiener(ReCoeffs[dir],ImCoeffs[dir],wiener1,Wlvl,Hlvl,rad_stage2[lvl], noisevar);
|
||||
|
||||
BiShrink(ReCoeffs[dir], ImCoeffs[dir], ReParents[dir], ImParents[dir], Wlvl, Hlvl, lvl, ParentPadding, noisevar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
void ImProcFunctions::WaveletDenoise(wavelet_decomposition &WaveletCoeffs, float noisevar )
|
||||
{
|
||||
int maxlvl = WaveletCoeffs.maxlevel();
|
||||
int rad_stage1[8] = {3,2,1,1,1,1,1,1};
|
||||
int rad_stage2[8] = {2,1,1,1,1,1,1,1};
|
||||
|
||||
for (int lvl=0; lvl<maxlvl/*-1*/; lvl++) {
|
||||
int Wlvl = WaveletCoeffs.level_W(lvl);
|
||||
int Hlvl = WaveletCoeffs.level_H(lvl);
|
||||
|
||||
//array2D<float> wiener1(Wlvl,Hlvl);
|
||||
int ParentPadding;
|
||||
float ** WavParents;
|
||||
float ** WavCoeffs = WaveletCoeffs.level_coeffs(lvl);
|
||||
if (lvl<maxlvl-1) {
|
||||
WavParents = WaveletCoeffs.level_coeffs(lvl+1);
|
||||
ParentPadding = WaveletCoeffs.level_pad(lvl+1);
|
||||
} else {
|
||||
WavParents = WaveletCoeffs.level_coeffs(lvl);
|
||||
ParentPadding = 0;
|
||||
}
|
||||
|
||||
float threshsq = noisevar;//*SQR(UniversalThresh(WaveletCoeffs.level_coeffs(lvl)[3], Wlvl*Hlvl));
|
||||
|
||||
BiShrink(WavCoeffs, WavParents, Wlvl, Hlvl, lvl, ParentPadding, threshsq/*noisevar*/);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
void ImProcFunctions::BiShrink(float * ReCoeffs, float * ImCoeffs, float * ReParents, float * ImParents, int W, int H, int level, int pad, float noisevar)
|
||||
{
|
||||
//bivariate shrinkage of Sendur & Selesnick
|
||||
float * sigma = new float[W*H];
|
||||
int rad = 3*(1<<level);
|
||||
boxvar(ReCoeffs,sigma,rad,rad,W,H);//box blur detail coeffs to estimate local variance
|
||||
const float root3 = sqrt(3);
|
||||
const int Wpar = (W+2*pad);
|
||||
//const int Hpar = (H+1+2*pad)/2;
|
||||
const float eps = 0.01f;
|
||||
|
||||
for (int i=0; i<H; i++) {
|
||||
for (int j=0; j<W; j++) {
|
||||
|
||||
float thresh = root3 * noisevar/sqrt(MAX(sigma[i*W+j]-noisevar, eps));
|
||||
int parentloc = ((i)-pad)*Wpar+(j)-pad;
|
||||
int coeffloc = i*W+j;
|
||||
float mag = sqrt(SQR(ReCoeffs[coeffloc]) + SQR(ImCoeffs[coeffloc]) + SQR(ReParents[parentloc]) + SQR(ImParents[parentloc]));
|
||||
float shrinkfactor = MAX(0,mag-thresh);
|
||||
shrinkfactor /= (shrinkfactor+thresh+eps);
|
||||
//float shrinkfactor = mag/(mag+noisevar+eps);
|
||||
//float shrinkre = SQR(ReCoeffs[coeffloc])/(noisevar+ SQR(ReCoeffs[coeffloc]) +eps);
|
||||
//float shrinkim = SQR(ImCoeffs[coeffloc])/(noisevar+ SQR(ImCoeffs[coeffloc]) +eps);
|
||||
ReCoeffs[coeffloc] *= shrinkfactor;
|
||||
ImCoeffs[coeffloc] *= shrinkfactor;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delete[] sigma;
|
||||
}
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
void ImProcFunctions::BiShrink(float ** WavCoeffs, float ** WavParents, int W, int H, int level, int pad, float noisevar)
|
||||
{
|
||||
//bivariate shrinkage of Sendur & Selesnick
|
||||
float * sigma = new float[W*H];
|
||||
int rad = 256;//3*(1<<level);
|
||||
//boxdev(WavCoeffs[3],sigma,rad,rad,W,H);//box blur detail coeffs to estimate local variance
|
||||
const float root3 = sqrt(3);
|
||||
const int Wpar = (W+2*pad);
|
||||
//const int Hpar = (H+1+2*pad)/2;
|
||||
const float eps = 0.01f;
|
||||
printf("level=%d ",level);
|
||||
for (int dir=1; dir<4; dir++) {
|
||||
float mad = SQR(UniversalThresh(WavCoeffs[dir], W*H));//*6*/(level+1);
|
||||
printf(" dir=%d mad=%f ",dir,sqrt(mad));
|
||||
for (int i=0; i<H; i++) {
|
||||
for (int j=0; j<W; j++) {
|
||||
|
||||
//float thresh = root3 * noisevar/sqrt(MAX(sigma[i*W+j]-noisevar, eps));
|
||||
//int parentloc = ((i)-pad)*Wpar+(j)-pad;
|
||||
int coeffloc = i*W+j;
|
||||
//float mag = (SQR(WavCoeffs[dir][coeffloc]) + SQR(WavParents[dir][parentloc]) );
|
||||
//float shrinkfactor = MAX(0,mag-thresh);
|
||||
//shrinkfactor /= (shrinkfactor+thresh+eps);
|
||||
float mag = SQR(WavCoeffs[dir][coeffloc]);
|
||||
float shrinkfactor = mag/(mag+noisevar*mad*exp(-mag/(3*noisevar*mad))+eps);
|
||||
//float shrinkfactor = mag/(mag+noisevar*SQR(sigma[coeffloc])+eps);
|
||||
|
||||
//WavCoeffs[dir][coeffloc] *= shrinkfactor;
|
||||
sigma[coeffloc] = shrinkfactor;
|
||||
}
|
||||
}
|
||||
|
||||
boxblur(sigma, sigma, 1, 1, W, H);
|
||||
for (int i=0; i<W*H; i++) {
|
||||
float mag = SQR(WavCoeffs[dir][i]);
|
||||
float sf = mag/(mag+noisevar*mad+eps);
|
||||
//float sf1 = mag/(mag+4*noisevar*mad+eps);
|
||||
|
||||
WavCoeffs[dir][i] *= (SQR(sigma[i])+SQR(sf))/(sigma[i]+sf+eps);
|
||||
|
||||
//float wdn = WavCoeffs[dir][i]*sf;
|
||||
//float wdn1 = WavCoeffs[dir][i]*sf1;
|
||||
|
||||
//WavCoeffs[dir][i] = wdn-wdn1;
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
delete[] sigma;
|
||||
}
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
void ImProcFunctions::FirstStageWiener(float* ReCoeffs, float* ImCoeffs, float* wiener1, int W, int H, int rad, float noisevar) {
|
||||
|
||||
//wiener filter
|
||||
float * sigmare = new float[W*H];
|
||||
float * sigmaim = new float[W*H];
|
||||
|
||||
//box blur detail coeffs to estimate local variance
|
||||
boxvar(ReCoeffs,sigmare,rad,rad,W,H);
|
||||
boxvar(ImCoeffs,sigmaim,rad,rad,W,H);
|
||||
|
||||
const float eps = 0.01f;
|
||||
|
||||
for (int i=0; i<H; i++) {
|
||||
for (int j=0; j<W; j++) {
|
||||
//classic Wiener filter
|
||||
float var = sigmare[i*W+j]+sigmaim[i*W+j];
|
||||
wiener1[i*W+j] = MAX(var-noisevar, eps)/(var+eps);
|
||||
}
|
||||
}
|
||||
|
||||
delete[] sigmare;
|
||||
delete[] sigmaim;
|
||||
}
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
void ImProcFunctions::SecondStageWiener(float* ReCoeffs, float* ImCoeffs, float* wiener1, int W, int H, int rad, float noisevar) {
|
||||
|
||||
//wiener filter
|
||||
float * Q = new float[W*H];
|
||||
float * C = new float[W*H];
|
||||
|
||||
//compute matched filter and neighborhood wiener ave
|
||||
QCoeffs(ReCoeffs, ImCoeffs, wiener1, Q, rad, W, H);
|
||||
boxblur(wiener1, C, rad, rad, W, H);
|
||||
|
||||
const float eps = 0.01f;
|
||||
|
||||
for (int i=0; i<H; i++) {
|
||||
for (int j=0; j<W; j++) {
|
||||
//classic Wiener filter
|
||||
float wiener2 = MAX(C[i*W+j]*noisevar,Q[i*W+j]-C[i*W+j]*noisevar)/(Q[i*W+j]+(1-C[i*W+j])*noisevar+eps);
|
||||
ReCoeffs[i*W+j] *= wiener2;
|
||||
ImCoeffs[i*W+j] *= wiener2;
|
||||
}
|
||||
}
|
||||
|
||||
delete[] Q;
|
||||
delete[] C;
|
||||
}
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
void ImProcFunctions::QCoeffs (float* srcre, float* srcim, float* wiener1, float* dst, int rad, int W, int H) {
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//box blur image; box size is (2*rad+1)x(2*rad+1)
|
||||
|
||||
AlignedBuffer<float>* buffer = new AlignedBuffer<float> (W*H);
|
||||
float* temp = buffer->data;
|
||||
|
||||
if (rad==0) {
|
||||
for (int row=0; row<H; row++)
|
||||
for (int col=0; col<H; col++) {
|
||||
temp[row*H+col] = wiener1[row*W+col]*(SQR(srcre[row*W+col])+SQR(srcim[row*W+col]));
|
||||
}
|
||||
} else {
|
||||
//horizontal blur
|
||||
for (int row = 0; row < H; row++) {
|
||||
int len = rad + 1;
|
||||
temp[row*W+0] = wiener1[row*W+0]*(SQR(srcre[row*W+0])+SQR(srcim[row*W+0]))/len;
|
||||
for (int j=1; j<=rad; j++) {
|
||||
temp[row*W+0] += wiener1[row*W+j]*(SQR(srcre[row*W+j])+SQR(srcim[row*W+j]))/len;
|
||||
}
|
||||
for (int col=1; col<=rad; col++) {
|
||||
temp[row*W+col] = (temp[row*W+col-1]*len +
|
||||
wiener1[row*W+col+rad]*(SQR(srcre[row*W+col+rad])+SQR(srcim[row*W+col+rad])))/(len+1);
|
||||
len ++;
|
||||
}
|
||||
for (int col = rad+1; col < W-rad; col++) {
|
||||
temp[row*W+col] = temp[row*W+col-1] + (wiener1[row*W+col+rad]*(SQR(srcre[row*W+col+rad])+SQR(srcim[row*W+col+rad])) - \
|
||||
wiener1[row*W+col-rad-1]*(SQR(srcre[row*W+col-rad-1])+SQR(srcim[row*W+col-rad-1])))/len;
|
||||
}
|
||||
for (int col=W-rad; col<W; col++) {
|
||||
temp[row*W+col] = (temp[row*W+col-1]*len - wiener1[row*W+col-rad-1]*(SQR(srcre[row*W+col-rad-1])+SQR(srcim[row*W+col-rad-1])))/(len-1);
|
||||
len --;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rad==0) {
|
||||
for (int row=0; row<H; row++)
|
||||
for (int col=0; col<H; col++) {
|
||||
dst[row*W+col] = temp[row*W+col];
|
||||
}
|
||||
} else {
|
||||
//vertical blur
|
||||
for (int col = 0; col < W; col++) {
|
||||
int len = rad + 1;
|
||||
dst[0*W+col] = temp[0*W+col]/len;
|
||||
for (int i=1; i<=rad; i++) {
|
||||
dst[0*W+col] += temp[i*W+col]/len;
|
||||
}
|
||||
for (int row=1; row<=rad; row++) {
|
||||
dst[row*W+col] = (dst[(row-1)*W+col]*len + temp[(row+rad)*W+col])/(len+1);
|
||||
len ++;
|
||||
}
|
||||
for (int row = rad+1; row < H-rad; row++) {
|
||||
dst[row*W+col] = dst[(row-1)*W+col] + (temp[(row+rad)*W+col] - temp[(row-rad-1)*W+col])/len;
|
||||
}
|
||||
for (int row=H-rad; row<H; row++) {
|
||||
dst[row*W+col] = (dst[(row-1)*W+col]*len - temp[(row-rad-1)*W+col])/(len-1);
|
||||
len --;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete buffer;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
float ImProcFunctions::UniversalThresh(float * HH_Coeffs, int datalen) {
|
||||
|
||||
int * histo = new int[65536];
|
||||
//memset(histo, 0, 65536*sizeof(histo));
|
||||
for (int i=0; i<65536; i++) histo[i]=0;
|
||||
|
||||
//calculate histogram of absolute values of HH wavelet coeffs
|
||||
for (int i=0; i<datalen; i++) {
|
||||
histo[MAX(0,MIN(65535,abs((int)HH_Coeffs[i])))]++;
|
||||
}
|
||||
|
||||
//find median of luminance
|
||||
int median=0, count=0;
|
||||
while (count<datalen/2) {
|
||||
count += histo[median];
|
||||
median++;
|
||||
}
|
||||
|
||||
int count_ = count - histo[median-1];
|
||||
|
||||
delete[] histo;
|
||||
|
||||
return (( (median-1) + (datalen/2-count_)/((float)(count-count_)) )/0.6745);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
};
|
||||
|
||||
@@ -26,7 +26,7 @@ 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];
|
||||
delete dual_tree[i][j];
|
||||
}
|
||||
}
|
||||
delete[] first_lev_anal;
|
||||
@@ -35,5 +35,14 @@ cplx_wavelet_decomposition::~cplx_wavelet_decomposition()
|
||||
delete[] wavfilt_synth;
|
||||
}
|
||||
|
||||
wavelet_decomposition::~wavelet_decomposition()
|
||||
{
|
||||
for(int i = 0; i < lvltot; i++) {
|
||||
delete wavelet_decomp[i];
|
||||
}
|
||||
delete[] wavfilt_anal;
|
||||
delete[] wavfilt_synth;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -35,12 +35,20 @@ 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:
|
||||
|
||||
@@ -79,10 +87,34 @@ public:
|
||||
|
||||
~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,41 +176,59 @@ 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
|
||||
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
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -173,8 +237,6 @@ public:
|
||||
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
|
||||
|
||||
|
||||
/* 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;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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},
|
||||
@@ -102,6 +101,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)
|
||||
|
||||
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
|
||||
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
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
|
||||
@@ -196,25 +239,48 @@ namespace rtengine {
|
||||
*
|
||||
*/
|
||||
|
||||
// calculate coefficients
|
||||
//input data is 'skip' rows and cosetlen=srclen/skip columns (which includes padding at either and)
|
||||
|
||||
for(int i = 0; i < (srclen); i+=2) {
|
||||
/*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<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>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
|
||||
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*(i/2))] = lo;
|
||||
dstHi[(pitch*(i/2))] = hi;
|
||||
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>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+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))] = 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
|
||||
// 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)
|
||||
|
||||
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];
|
||||
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;
|
||||
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>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
|
||||
for (int j=begin, l=0; j<taps; j+=2, l++) {
|
||||
int arg = MAX(0,MIN((i_src-l),srclen-1));//clamped BC's
|
||||
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;
|
||||
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=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,27 +372,29 @@ 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;
|
||||
delete[] buffer;
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -38,8 +38,6 @@ 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;
|
||||
|
||||
Reference in New Issue
Block a user