[3] | 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 | |
---|
[86] | 10 | void atrous3DReconstruct(long &xdim, long &ydim, long &zdim, float *&input, |
---|
| 11 | float *&output, Param &par) |
---|
[3] | 12 | { |
---|
[86] | 13 | /** |
---|
| 14 | * atrous3DReconstruct(xdim, ydim, zdim, input, output, par) |
---|
| 15 | * |
---|
| 16 | * A routine that uses the a trous wavelet method to reconstruct a |
---|
| 17 | * 3-dimensional image cube. |
---|
| 18 | * The Param object "par" contains all necessary info about the filter and |
---|
| 19 | * reconstruction parameters, although a Filter object has to be declared |
---|
| 20 | * elsewhere previously. |
---|
| 21 | * The input array is in "input", of dimensions "xdim"x"ydim"x"zdim", and |
---|
| 22 | * the reconstructed array is in "output". |
---|
| 23 | */ |
---|
| 24 | |
---|
[3] | 25 | extern Filter reconFilter; |
---|
| 26 | long size = xdim * ydim * zdim; |
---|
[52] | 27 | long spatialSize = xdim * ydim; |
---|
[3] | 28 | long mindim = xdim; |
---|
| 29 | if (ydim<mindim) mindim = ydim; |
---|
| 30 | if (zdim<mindim) mindim = zdim; |
---|
| 31 | int numScales = reconFilter.getNumScales(mindim); |
---|
[52] | 32 | |
---|
[3] | 33 | double *sigmaFactors = new double[numScales+1]; |
---|
| 34 | for(int i=0;i<=numScales;i++){ |
---|
| 35 | if(i<=reconFilter.maxFactor(3)) sigmaFactors[i] = reconFilter.sigmaFactor(3,i); |
---|
| 36 | else sigmaFactors[i] = sigmaFactors[i-1] / sqrt(8.); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | float mean,sigma,originalSigma,originalMean,oldsigma,newsigma; |
---|
| 40 | bool *isGood = new bool[size]; |
---|
| 41 | float blankPixValue = par.getBlankPixVal(); |
---|
[52] | 42 | for(int pos=0;pos<size;pos++){ |
---|
[3] | 43 | isGood[pos] = !par.isBlank(input[pos]); |
---|
[52] | 44 | } |
---|
| 45 | |
---|
[3] | 46 | float *array = new float[size]; |
---|
| 47 | int goodSize=0; |
---|
| 48 | for(int i=0;i<size;i++) if(isGood[i]) array[goodSize++] = input[i]; |
---|
| 49 | findMedianStats(array,goodSize,originalMean,originalSigma); |
---|
| 50 | originalSigma /= correctionFactor; // correct from MADFM to sigma estimator. |
---|
| 51 | delete [] array; |
---|
| 52 | |
---|
| 53 | float *coeffs = new float[size]; |
---|
| 54 | float *wavelet = new float[size]; |
---|
| 55 | |
---|
| 56 | for(int pos=0;pos<size;pos++) output[pos]=0.; |
---|
| 57 | |
---|
[52] | 58 | // Define the 3-D (separable) filter, using info from reconFilter |
---|
[3] | 59 | int filterwidth = reconFilter.width(); |
---|
| 60 | int filterHW = filterwidth/2; |
---|
| 61 | int fsize = filterwidth*filterwidth*filterwidth; |
---|
| 62 | double *filter = new double[fsize]; |
---|
| 63 | for(int i=0;i<filterwidth;i++){ |
---|
| 64 | for(int j=0;j<filterwidth;j++){ |
---|
| 65 | for(int k=0;k<filterwidth;k++){ |
---|
| 66 | filter[i +j*filterwidth + k*filterwidth*filterwidth] = |
---|
| 67 | reconFilter.coeff(i) * reconFilter.coeff(j) * reconFilter.coeff(k); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // locating the borders of the image -- ignoring BLANK pixels |
---|
[52] | 73 | // Only do this if flagBlankPix is true. Otherwise use the full range of x and y. |
---|
| 74 | // No trimming is done in the z-direction at this point. |
---|
[3] | 75 | int *xLim1 = new int[ydim]; |
---|
[52] | 76 | for(int i=0;i<ydim;i++) xLim1[i] = 0; |
---|
| 77 | int *xLim2 = new int[ydim]; |
---|
| 78 | for(int i=0;i<ydim;i++) xLim2[i] = xdim-1; |
---|
[3] | 79 | int *yLim1 = new int[xdim]; |
---|
[52] | 80 | for(int i=0;i<xdim;i++) yLim1[i] = 0; |
---|
[3] | 81 | int *yLim2 = new int[xdim]; |
---|
[52] | 82 | for(int i=0;i<xdim;i++) yLim2[i] = ydim-1; |
---|
[3] | 83 | |
---|
[52] | 84 | if(par.getFlagBlankPix()){ |
---|
| 85 | float avGapX = 0, avGapY = 0; |
---|
| 86 | for(int row=0;row<ydim;row++){ |
---|
| 87 | int ct1 = 0; |
---|
| 88 | int ct2 = xdim - 1; |
---|
| 89 | while((ct1<ct2)&&(input[row*xdim+ct1]==blankPixValue) ) ct1++; |
---|
| 90 | while((ct2>ct1)&&(input[row*xdim+ct2]==blankPixValue) ) ct2--; |
---|
| 91 | xLim1[row] = ct1; |
---|
| 92 | xLim2[row] = ct2; |
---|
| 93 | avGapX += ct2 - ct1 + 1; |
---|
| 94 | } |
---|
| 95 | avGapX /= float(ydim); |
---|
| 96 | |
---|
| 97 | for(int col=0;col<xdim;col++){ |
---|
| 98 | int ct1=0; |
---|
| 99 | int ct2=ydim-1; |
---|
| 100 | while((ct1<ct2)&&(input[col+xdim*ct1]==blankPixValue) ) ct1++; |
---|
| 101 | while((ct2>ct1)&&(input[col+xdim*ct2]==blankPixValue) ) ct2--; |
---|
| 102 | yLim1[col] = ct1; |
---|
| 103 | yLim2[col] = ct2; |
---|
| 104 | avGapY += ct2 - ct1 + 1; |
---|
| 105 | } |
---|
[96] | 106 | avGapY /= float(xdim); |
---|
[52] | 107 | |
---|
| 108 | mindim = int(avGapX); |
---|
| 109 | if(avGapY < avGapX) mindim = int(avGapY); |
---|
| 110 | numScales = reconFilter.getNumScales(mindim); |
---|
[3] | 111 | } |
---|
| 112 | |
---|
| 113 | float threshold; |
---|
| 114 | int iteration=0; |
---|
| 115 | newsigma = 1.e9; |
---|
| 116 | for(int i=0;i<size;i++) output[i] = 0; |
---|
| 117 | do{ |
---|
| 118 | if(par.isVerbose()) std::cout << "Iteration #"<<setw(2)<<++iteration<<": "; |
---|
| 119 | // first, get the value of oldsigma, set it to the previous newsigma value |
---|
| 120 | oldsigma = newsigma; |
---|
| 121 | // we are transforming the residual array (input array first time around) |
---|
| 122 | for(int i=0;i<size;i++) coeffs[i] = input[i] - output[i]; |
---|
| 123 | |
---|
| 124 | int spacing = 1; |
---|
| 125 | for(int scale = 1; scale<=numScales; scale++){ |
---|
| 126 | |
---|
| 127 | if(par.isVerbose()){ |
---|
| 128 | std::cout << "Scale "; |
---|
| 129 | std::cout << setw(2)<<scale<<" / "<<setw(2)<<numScales |
---|
| 130 | << "\b\b\b\b\b\b\b\b\b\b\b\b\b"<<std::flush; |
---|
| 131 | } |
---|
| 132 | |
---|
[52] | 133 | int pos = -1; |
---|
[3] | 134 | for(int zpos = 0; zpos<zdim; zpos++){ |
---|
| 135 | for(int ypos = 0; ypos<ydim; ypos++){ |
---|
| 136 | for(int xpos = 0; xpos<xdim; xpos++){ |
---|
| 137 | // loops over each pixel in the image |
---|
[52] | 138 | pos++; |
---|
[3] | 139 | |
---|
| 140 | wavelet[pos] = coeffs[pos]; |
---|
| 141 | |
---|
| 142 | if(!isGood[pos] ) wavelet[pos] = 0.; |
---|
| 143 | else{ |
---|
| 144 | |
---|
[52] | 145 | int filterpos = -1; |
---|
[3] | 146 | for(int zoffset=-filterHW; zoffset<=filterHW; zoffset++){ |
---|
| 147 | int z = zpos + spacing*zoffset; |
---|
| 148 | if(z<0) z = -z; // boundary conditions are |
---|
| 149 | if(z>=zdim) z = 2*(zdim-1) - z; // reflection. |
---|
[95] | 150 | |
---|
| 151 | int oldchan = z * spatialSize; |
---|
| 152 | |
---|
[3] | 153 | for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){ |
---|
| 154 | int y = ypos + spacing*yoffset; |
---|
[95] | 155 | |
---|
| 156 | // Boundary conditions -- assume reflection at boundaries. |
---|
| 157 | // Use limits as calculated above |
---|
| 158 | if(yLim1[xpos]!=yLim2[xpos]){ |
---|
| 159 | // if these are equal we will get into an infinite loop here |
---|
| 160 | while((y<yLim1[xpos])||(y>yLim2[xpos])){ |
---|
| 161 | if(y<yLim1[xpos]) y = 2*yLim1[xpos] - y; |
---|
| 162 | else if(y>yLim2[xpos]) y = 2*yLim2[xpos] - y; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | int oldrow = y * xdim; |
---|
[3] | 166 | |
---|
| 167 | for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){ |
---|
| 168 | int x = xpos + spacing*xoffset; |
---|
| 169 | |
---|
[52] | 170 | // Boundary conditions -- assume reflection at boundaries. |
---|
| 171 | // Use limits as calculated above |
---|
[3] | 172 | if(xLim1[ypos]!=xLim2[ypos]){ |
---|
| 173 | // if these are equal we will get into an infinite loop here |
---|
| 174 | while((x<xLim1[ypos])||(x>xLim2[ypos])){ |
---|
| 175 | if(x<xLim1[ypos]) x = 2*xLim1[ypos] - x; |
---|
| 176 | else if(x>xLim2[ypos]) x = 2*xLim2[ypos] - x; |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
[95] | 180 | // int oldpos = z*spatialSize + y*xdim + x; |
---|
| 181 | int oldpos = oldchan + oldrow + x; |
---|
| 182 | |
---|
| 183 | filterpos++; |
---|
| 184 | |
---|
[3] | 185 | if(isGood[oldpos]) |
---|
| 186 | wavelet[pos] -= filter[filterpos]*coeffs[oldpos]; |
---|
| 187 | |
---|
| 188 | } //-> end of xoffset loop |
---|
| 189 | } //-> end of yoffset loop |
---|
| 190 | } //-> end of zoffset loop |
---|
| 191 | } //-> end of else{ ( from if(!isGood[pos]) ) |
---|
| 192 | |
---|
| 193 | } //-> end of xpos loop |
---|
| 194 | } //-> end of ypos loop |
---|
| 195 | } //-> end of zpos loop |
---|
| 196 | |
---|
| 197 | // Need to do this after we've done *all* the convolving |
---|
| 198 | for(int pos=0;pos<size;pos++) coeffs[pos] = coeffs[pos] - wavelet[pos]; |
---|
| 199 | |
---|
| 200 | // Have found wavelet coeffs for this scale -- now threshold |
---|
| 201 | if(scale>=par.getMinScale()){ |
---|
| 202 | array = new float[size]; |
---|
| 203 | goodSize=0; |
---|
| 204 | for(int pos=0;pos<size;pos++) if(isGood[pos]) array[goodSize++] = wavelet[pos]; |
---|
| 205 | findMedianStats(array,goodSize,mean,sigma); |
---|
| 206 | delete [] array; |
---|
| 207 | |
---|
| 208 | threshold = mean + par.getAtrousCut() * originalSigma * sigmaFactors[scale]; |
---|
| 209 | for(int pos=0;pos<size;pos++){ |
---|
[86] | 210 | if(!isGood[pos]){ |
---|
| 211 | output[pos] = blankPixValue; |
---|
| 212 | // this preserves the Blank pixel values in the output. |
---|
| 213 | } |
---|
| 214 | else if( fabs(wavelet[pos]) > threshold ){ |
---|
| 215 | output[pos] += wavelet[pos]; |
---|
| 216 | // only add to the output if the wavelet coefficient is significant |
---|
| 217 | } |
---|
[3] | 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
[86] | 221 | spacing *= 2; // double the scale of the filter. |
---|
[3] | 222 | |
---|
| 223 | } //-> end of scale loop |
---|
| 224 | |
---|
| 225 | for(int pos=0;pos<size;pos++) if(isGood[pos]) output[pos] += coeffs[pos]; |
---|
| 226 | |
---|
| 227 | array = new float[size]; |
---|
| 228 | goodSize=0; |
---|
| 229 | for(int i=0;i<size;i++) if(isGood[i]) array[goodSize++] = input[i] - output[i]; |
---|
[96] | 230 | findMedianStats(array,goodSize,mean,newsigma); |
---|
| 231 | newsigma /= correctionFactor; // correct from MADFM to sigma estimator. |
---|
[3] | 232 | delete [] array; |
---|
| 233 | |
---|
| 234 | if(par.isVerbose()) std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; |
---|
| 235 | |
---|
| 236 | } while( (iteration==1) || |
---|
| 237 | (fabsf(oldsigma-newsigma)/newsigma > reconTolerance) ); |
---|
| 238 | |
---|
| 239 | if(par.isVerbose()) std::cout << "Completed "<<iteration<<" iterations. "; |
---|
| 240 | |
---|
[52] | 241 | delete [] xLim1,xLim2,yLim1,yLim2; |
---|
[3] | 242 | delete [] coeffs; |
---|
| 243 | delete [] wavelet; |
---|
| 244 | delete [] isGood; |
---|
| 245 | delete [] filter; |
---|
| 246 | delete [] sigmaFactors; |
---|
| 247 | } |
---|