1 | #include <iostream> |
---|
2 | #include <iomanip> |
---|
3 | #include <math.h> |
---|
4 | #include <ATrous/atrous.hh> |
---|
5 | #include <Utils/utils.hh> |
---|
6 | |
---|
7 | using std::endl; |
---|
8 | using std::setw; |
---|
9 | |
---|
10 | void atrous1DReconstruct(long &xdim, float *&input,float *&output, Param &par) |
---|
11 | { |
---|
12 | extern Filter reconFilter; |
---|
13 | const float SNR_THRESH=par.getAtrousCut(); |
---|
14 | const int MIN_SCALE=par.getMinScale(); |
---|
15 | |
---|
16 | bool flagBlank=par.getFlagBlankPix(); |
---|
17 | float blankPixValue = par.getBlankPixVal(); |
---|
18 | int numScales = reconFilter.getNumScales(xdim); |
---|
19 | double *sigmaFactors = new double[numScales+1]; |
---|
20 | for(int i=0;i<=numScales;i++){ |
---|
21 | if(i<=reconFilter.maxFactor(1)) sigmaFactors[i] = reconFilter.sigmaFactor(1,i); |
---|
22 | else sigmaFactors[i] = sigmaFactors[i-1] / sqrt(2.); |
---|
23 | } |
---|
24 | |
---|
25 | float mean,sigma,originalSigma,originalMean,oldsigma,newsigma; |
---|
26 | bool *isGood = new bool[xdim]; |
---|
27 | for(int pos=0;pos<xdim;pos++) //isGood[pos] = (!flagBlank) || (input[pos]!=blankPixValue); |
---|
28 | isGood[pos] = !par.isBlank(input[pos]); |
---|
29 | |
---|
30 | float *coeffs = new float[xdim]; |
---|
31 | float *wavelet = new float[xdim]; |
---|
32 | |
---|
33 | for(int pos=0;pos<xdim;pos++) output[pos]=0.; |
---|
34 | |
---|
35 | int filterHW = reconFilter.width()/2; |
---|
36 | double *filter = new double[reconFilter.width()]; |
---|
37 | for(int i=0;i<reconFilter.width();i++) filter[i] = reconFilter.coeff(i); |
---|
38 | |
---|
39 | |
---|
40 | // No trimming done in 1D case. |
---|
41 | |
---|
42 | int iteration=0; |
---|
43 | newsigma = 1.e9; |
---|
44 | do{ |
---|
45 | if(par.isVerbose()) std::cout << "Iteration #"<<++iteration<<": "; |
---|
46 | // first, get the value of oldsigma and set it to the previous newsigma value |
---|
47 | oldsigma = newsigma; |
---|
48 | // all other times round, we are transforming the residual array |
---|
49 | for(int i=0;i<xdim;i++) coeffs[i] = input[i] - output[i]; |
---|
50 | |
---|
51 | float *array = new float[xdim]; |
---|
52 | int goodSize=0; |
---|
53 | for(int i=0;i<xdim;i++) if(isGood[i]) array[goodSize++] = input[i]; |
---|
54 | findMedianStats(array,goodSize,originalMean,originalSigma); |
---|
55 | originalSigma /= correctionFactor; // correct from MADFM to sigma estimator. |
---|
56 | delete [] array; |
---|
57 | |
---|
58 | int spacing = 1; |
---|
59 | for(int scale = 1; scale<=numScales; scale++){ |
---|
60 | |
---|
61 | if(par.isVerbose()) { |
---|
62 | std::cout << "\b\b\b\b\b\b\b\b\b\b\b\bScale "; |
---|
63 | std::cout << setw(2)<<scale<<" /"<<setw(2)<<numScales<<std::flush; |
---|
64 | } |
---|
65 | |
---|
66 | for(int xpos = 0; xpos<xdim; xpos++){ |
---|
67 | // loops over each pixel in the image |
---|
68 | int pos = xpos; |
---|
69 | |
---|
70 | wavelet[pos] = coeffs[pos]; |
---|
71 | |
---|
72 | if(!isGood[pos] ) wavelet[pos] = 0.; |
---|
73 | else{ |
---|
74 | |
---|
75 | for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){ |
---|
76 | int x = xpos + spacing*xoffset; |
---|
77 | //if(x<0) x = -x; // boundary conditions are |
---|
78 | //if(x>=xdim) x = 2*(xdim-1) - x; // reflection. |
---|
79 | // if(x<xLim1) x = 2*xLim1 - x; // boundary conditions are |
---|
80 | // if(x>xLim2) x = 2*xLim2 - x; // reflection. |
---|
81 | |
---|
82 | while((x<0)||(x>=xdim)){ |
---|
83 | if(x<0) x = 0 - x; |
---|
84 | else if(x>=xdim) x = 2*(xdim-1) - x; |
---|
85 | } |
---|
86 | |
---|
87 | int filterpos = (xoffset+filterHW); |
---|
88 | int oldpos = x; |
---|
89 | |
---|
90 | if(isGood[oldpos]) |
---|
91 | wavelet[pos] -= filter[filterpos]*coeffs[oldpos]; |
---|
92 | |
---|
93 | } //-> end of xoffset loop |
---|
94 | } //-> end of else{ ( from if(!isGood[pos]) ) |
---|
95 | |
---|
96 | } //-> end of xpos loop |
---|
97 | |
---|
98 | for(int pos=0;pos<xdim;pos++) coeffs[pos] = coeffs[pos] - wavelet[pos]; |
---|
99 | |
---|
100 | // Have found wavelet coeffs for this scale -- now threshold |
---|
101 | if(scale>=MIN_SCALE){ |
---|
102 | array = new float[xdim]; |
---|
103 | goodSize=0; |
---|
104 | for(int pos=0;pos<xdim;pos++) if(isGood[pos]) array[goodSize++] = wavelet[pos]; |
---|
105 | findMedianStats(array,goodSize,mean,sigma); |
---|
106 | delete [] array; |
---|
107 | |
---|
108 | for(int pos=0;pos<xdim;pos++){ |
---|
109 | // preserve the Blank pixel values in the output. |
---|
110 | if(!isGood[pos]) output[pos] = blankPixValue; |
---|
111 | else if(fabs(wavelet[pos])>(mean+SNR_THRESH*originalSigma*sigmaFactors[scale])) |
---|
112 | output[pos] += wavelet[pos]; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | spacing *= 2; |
---|
117 | |
---|
118 | } //-> end of scale loop |
---|
119 | |
---|
120 | for(int pos=0;pos<xdim;pos++) if(isGood[pos]) output[pos] += coeffs[pos]; |
---|
121 | |
---|
122 | array = new float[xdim]; |
---|
123 | goodSize=0; |
---|
124 | for(int i=0;i<xdim;i++) if(isGood[i]) array[goodSize++] = input[i] - output[i]; |
---|
125 | findNormalStats(array,goodSize,mean,newsigma); |
---|
126 | delete [] array; |
---|
127 | |
---|
128 | if(par.isVerbose()) std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; |
---|
129 | |
---|
130 | } while( (iteration==1) || |
---|
131 | (fabsf(oldsigma-newsigma)/newsigma > reconTolerance) ); |
---|
132 | |
---|
133 | if(par.isVerbose()) std::cout << "Completed "<<iteration<<" iterations. "; |
---|
134 | |
---|
135 | delete [] coeffs; |
---|
136 | delete [] wavelet; |
---|
137 | delete [] isGood; |
---|
138 | delete [] filter; |
---|
139 | delete [] sigmaFactors; |
---|
140 | } |
---|