[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // atrous_3d_reconstruct.cc: 3-dimensional wavelet reconstruction. |
---|
| 3 | // ----------------------------------------------------------------------- |
---|
| 4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 5 | // |
---|
| 6 | // This program is free software; you can redistribute it and/or modify it |
---|
| 7 | // under the terms of the GNU General Public License as published by the |
---|
| 8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 9 | // option) any later version. |
---|
| 10 | // |
---|
| 11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 14 | // for more details. |
---|
| 15 | // |
---|
| 16 | // You should have received a copy of the GNU General Public License |
---|
| 17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | // |
---|
| 20 | // Correspondence concerning Duchamp may be directed to: |
---|
| 21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 22 | // Postal address: Dr. Matthew Whiting |
---|
| 23 | // Australia Telescope National Facility, CSIRO |
---|
| 24 | // PO Box 76 |
---|
| 25 | // Epping NSW 1710 |
---|
| 26 | // AUSTRALIA |
---|
| 27 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
| 29 | #include <iomanip> |
---|
| 30 | #include <math.h> |
---|
[393] | 31 | #include <duchamp/duchamp.hh> |
---|
| 32 | #include <duchamp/param.hh> |
---|
| 33 | #include <duchamp/ATrous/atrous.hh> |
---|
| 34 | #include <duchamp/ATrous/filter.hh> |
---|
| 35 | #include <duchamp/Utils/utils.hh> |
---|
| 36 | #include <duchamp/Utils/feedback.hh> |
---|
| 37 | #include <duchamp/Utils/Statistics.hh> |
---|
[190] | 38 | using Statistics::madfmToSigma; |
---|
[3] | 39 | |
---|
| 40 | using std::endl; |
---|
| 41 | using std::setw; |
---|
| 42 | |
---|
[378] | 43 | namespace duchamp |
---|
[3] | 44 | { |
---|
[86] | 45 | |
---|
[378] | 46 | void atrous3DReconstruct(long &xdim, long &ydim, long &zdim, float *&input, |
---|
| 47 | float *&output, Param &par) |
---|
| 48 | { |
---|
[528] | 49 | /// A routine that uses the a trous wavelet method to reconstruct a |
---|
| 50 | /// 3-dimensional image cube. |
---|
| 51 | /// The Param object "par" contains all necessary info about the filter and |
---|
| 52 | /// reconstruction parameters. |
---|
| 53 | /// |
---|
| 54 | /// If there are no non-BLANK pixels (and we are testing for |
---|
| 55 | /// BLANKs), the reconstruction cannot be done, so we return the |
---|
| 56 | /// input array as the output array and give a warning message. |
---|
| 57 | /// |
---|
| 58 | /// \param xdim The length of the x-axis. |
---|
| 59 | /// \param ydim The length of the y-axis. |
---|
| 60 | /// \param zdim The length of the z-axis. |
---|
| 61 | /// \param input The input spectrum. |
---|
| 62 | /// \param output The returned reconstructed spectrum. This array needs to be declared beforehand. |
---|
| 63 | /// \param par The Param set. |
---|
[52] | 64 | |
---|
[378] | 65 | long size = xdim * ydim * zdim; |
---|
| 66 | long spatialSize = xdim * ydim; |
---|
| 67 | long mindim = xdim; |
---|
| 68 | if (ydim<mindim) mindim = ydim; |
---|
| 69 | if (zdim<mindim) mindim = zdim; |
---|
| 70 | int numScales = par.filter().getNumScales(mindim); |
---|
[3] | 71 | |
---|
[378] | 72 | double *sigmaFactors = new double[numScales+1]; |
---|
| 73 | for(int i=0;i<=numScales;i++){ |
---|
| 74 | if(i<=par.filter().maxFactor(3)) |
---|
| 75 | sigmaFactors[i] = par.filter().sigmaFactor(3,i); |
---|
| 76 | else sigmaFactors[i] = sigmaFactors[i-1] / sqrt(8.); |
---|
| 77 | } |
---|
[52] | 78 | |
---|
[378] | 79 | float mean,sigma,originalSigma,originalMean,oldsigma,newsigma; |
---|
| 80 | bool *isGood = new bool[size]; |
---|
| 81 | int goodSize=0; |
---|
| 82 | for(int pos=0;pos<size;pos++){ |
---|
| 83 | isGood[pos] = !par.isBlank(input[pos]); |
---|
| 84 | if(isGood[pos]) goodSize++; |
---|
| 85 | } |
---|
[3] | 86 | |
---|
[378] | 87 | if(goodSize == 0){ |
---|
| 88 | // There are no good pixels -- everything is BLANK for some reason. |
---|
| 89 | // Return the input array as the output, and give a warning message. |
---|
[3] | 90 | |
---|
[378] | 91 | for(int pos=0;pos<xdim; pos++) output[pos] = input[pos]; |
---|
[3] | 92 | |
---|
[378] | 93 | duchampWarning("3D Reconstruction", |
---|
| 94 | "There are no good pixels to be reconstructed -- all are BLANK.\nPerhaps you need to try this with flagTrim=false.\nReturning input array.\n"); |
---|
| 95 | } |
---|
| 96 | else{ |
---|
| 97 | // Otherwise, all is good, and we continue. |
---|
[231] | 98 | |
---|
| 99 | |
---|
[650] | 100 | findMedianStats(input,goodSize,isGood,originalMean,originalSigma); |
---|
[378] | 101 | originalSigma = madfmToSigma(originalSigma); |
---|
[231] | 102 | |
---|
[378] | 103 | float *coeffs = new float[size]; |
---|
| 104 | float *wavelet = new float[size]; |
---|
[650] | 105 | float *residual = new float[size]; |
---|
[231] | 106 | |
---|
[378] | 107 | for(int pos=0;pos<size;pos++) output[pos]=0.; |
---|
| 108 | |
---|
| 109 | // Define the 3-D (separable) filter, using info from par.filter() |
---|
| 110 | int filterwidth = par.filter().width(); |
---|
| 111 | int filterHW = filterwidth/2; |
---|
| 112 | int fsize = filterwidth*filterwidth*filterwidth; |
---|
| 113 | double *filter = new double[fsize]; |
---|
| 114 | for(int i=0;i<filterwidth;i++){ |
---|
| 115 | for(int j=0;j<filterwidth;j++){ |
---|
| 116 | for(int k=0;k<filterwidth;k++){ |
---|
| 117 | filter[i +j*filterwidth + k*filterwidth*filterwidth] = |
---|
| 118 | par.filter().coeff(i) * par.filter().coeff(j) * par.filter().coeff(k); |
---|
| 119 | } |
---|
[231] | 120 | } |
---|
[3] | 121 | } |
---|
| 122 | |
---|
[378] | 123 | // Locating the borders of the image -- ignoring BLANK pixels |
---|
| 124 | // Only do this if flagBlankPix is true. |
---|
| 125 | // Otherwise use the full range of x and y. |
---|
| 126 | // No trimming is done in the z-direction at this point. |
---|
| 127 | int *xLim1 = new int[ydim]; |
---|
| 128 | for(int i=0;i<ydim;i++) xLim1[i] = 0; |
---|
| 129 | int *xLim2 = new int[ydim]; |
---|
| 130 | for(int i=0;i<ydim;i++) xLim2[i] = xdim-1; |
---|
| 131 | int *yLim1 = new int[xdim]; |
---|
| 132 | for(int i=0;i<xdim;i++) yLim1[i] = 0; |
---|
| 133 | int *yLim2 = new int[xdim]; |
---|
| 134 | for(int i=0;i<xdim;i++) yLim2[i] = ydim-1; |
---|
[3] | 135 | |
---|
[378] | 136 | if(par.getFlagBlankPix()){ |
---|
| 137 | float avGapX = 0, avGapY = 0; |
---|
| 138 | for(int row=0;row<ydim;row++){ |
---|
| 139 | int ct1 = 0; |
---|
| 140 | int ct2 = xdim - 1; |
---|
| 141 | while((ct1<ct2)&&(par.isBlank(input[row*xdim+ct1]))) ct1++; |
---|
| 142 | while((ct2>ct1)&&(par.isBlank(input[row*xdim+ct2]))) ct2--; |
---|
| 143 | xLim1[row] = ct1; |
---|
| 144 | xLim2[row] = ct2; |
---|
| 145 | avGapX += ct2 - ct1 + 1; |
---|
| 146 | } |
---|
| 147 | avGapX /= float(ydim); |
---|
[52] | 148 | |
---|
[378] | 149 | for(int col=0;col<xdim;col++){ |
---|
| 150 | int ct1=0; |
---|
| 151 | int ct2=ydim-1; |
---|
| 152 | while((ct1<ct2)&&(par.isBlank(input[col+xdim*ct1]))) ct1++; |
---|
| 153 | while((ct2>ct1)&&(par.isBlank(input[col+xdim*ct2]))) ct2--; |
---|
| 154 | yLim1[col] = ct1; |
---|
| 155 | yLim2[col] = ct2; |
---|
| 156 | avGapY += ct2 - ct1 + 1; |
---|
| 157 | } |
---|
| 158 | avGapY /= float(xdim); |
---|
| 159 | |
---|
| 160 | mindim = int(avGapX); |
---|
| 161 | if(avGapY < avGapX) mindim = int(avGapY); |
---|
| 162 | numScales = par.filter().getNumScales(mindim); |
---|
[231] | 163 | } |
---|
[3] | 164 | |
---|
[378] | 165 | float threshold; |
---|
| 166 | int iteration=0; |
---|
| 167 | newsigma = 1.e9; |
---|
| 168 | for(int i=0;i<size;i++) output[i] = 0; |
---|
| 169 | do{ |
---|
| 170 | if(par.isVerbose()) std::cout << "Iteration #"<<setw(2)<<++iteration<<": "; |
---|
| 171 | // first, get the value of oldsigma, set it to the previous newsigma value |
---|
| 172 | oldsigma = newsigma; |
---|
| 173 | // we are transforming the residual array (input array first time around) |
---|
| 174 | for(int i=0;i<size;i++) coeffs[i] = input[i] - output[i]; |
---|
[3] | 175 | |
---|
[378] | 176 | int spacing = 1; |
---|
| 177 | for(int scale = 1; scale<=numScales; scale++){ |
---|
[3] | 178 | |
---|
[378] | 179 | if(par.isVerbose()){ |
---|
| 180 | std::cout << "Scale "; |
---|
| 181 | std::cout << setw(2)<<scale<<" / "<<setw(2)<<numScales; |
---|
| 182 | printBackSpace(13); |
---|
| 183 | std::cout << std::flush; |
---|
| 184 | } |
---|
[3] | 185 | |
---|
[378] | 186 | int pos = -1; |
---|
| 187 | for(int zpos = 0; zpos<zdim; zpos++){ |
---|
| 188 | for(int ypos = 0; ypos<ydim; ypos++){ |
---|
| 189 | for(int xpos = 0; xpos<xdim; xpos++){ |
---|
| 190 | // loops over each pixel in the image |
---|
| 191 | pos++; |
---|
[3] | 192 | |
---|
[378] | 193 | wavelet[pos] = coeffs[pos]; |
---|
[3] | 194 | |
---|
[378] | 195 | if(!isGood[pos] ) wavelet[pos] = 0.; |
---|
| 196 | else{ |
---|
[3] | 197 | |
---|
[378] | 198 | int filterpos = -1; |
---|
| 199 | for(int zoffset=-filterHW; zoffset<=filterHW; zoffset++){ |
---|
| 200 | int z = zpos + spacing*zoffset; |
---|
| 201 | if(z<0) z = -z; // boundary conditions are |
---|
| 202 | if(z>=zdim) z = 2*(zdim-1) - z; // reflection. |
---|
[103] | 203 | |
---|
[378] | 204 | int oldchan = z * spatialSize; |
---|
[103] | 205 | |
---|
[378] | 206 | for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){ |
---|
| 207 | int y = ypos + spacing*yoffset; |
---|
[103] | 208 | |
---|
[231] | 209 | // Boundary conditions -- assume reflection at boundaries. |
---|
| 210 | // Use limits as calculated above |
---|
[378] | 211 | if(yLim1[xpos]!=yLim2[xpos]){ |
---|
[231] | 212 | // if these are equal we will get into an infinite loop |
---|
[378] | 213 | while((y<yLim1[xpos])||(y>yLim2[xpos])){ |
---|
| 214 | if(y<yLim1[xpos]) y = 2*yLim1[xpos] - y; |
---|
| 215 | else if(y>yLim2[xpos]) y = 2*yLim2[xpos] - y; |
---|
[231] | 216 | } |
---|
| 217 | } |
---|
[378] | 218 | int oldrow = y * xdim; |
---|
| 219 | |
---|
| 220 | for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){ |
---|
| 221 | int x = xpos + spacing*xoffset; |
---|
[103] | 222 | |
---|
[378] | 223 | // Boundary conditions -- assume reflection at boundaries. |
---|
| 224 | // Use limits as calculated above |
---|
| 225 | if(xLim1[ypos]!=xLim2[ypos]){ |
---|
| 226 | // if these are equal we will get into an infinite loop |
---|
| 227 | while((x<xLim1[ypos])||(x>xLim2[ypos])){ |
---|
| 228 | if(x<xLim1[ypos]) x = 2*xLim1[ypos] - x; |
---|
| 229 | else if(x>xLim2[ypos]) x = 2*xLim2[ypos] - x; |
---|
| 230 | } |
---|
| 231 | } |
---|
[231] | 232 | |
---|
[378] | 233 | int oldpos = oldchan + oldrow + x; |
---|
| 234 | |
---|
| 235 | filterpos++; |
---|
[103] | 236 | |
---|
[378] | 237 | if(isGood[oldpos]) |
---|
| 238 | wavelet[pos] -= filter[filterpos]*coeffs[oldpos]; |
---|
[3] | 239 | |
---|
[378] | 240 | } //-> end of xoffset loop |
---|
| 241 | } //-> end of yoffset loop |
---|
| 242 | } //-> end of zoffset loop |
---|
| 243 | } //-> end of else{ ( from if(!isGood[pos]) ) |
---|
[3] | 244 | |
---|
[378] | 245 | } //-> end of xpos loop |
---|
| 246 | } //-> end of ypos loop |
---|
| 247 | } //-> end of zpos loop |
---|
[3] | 248 | |
---|
[378] | 249 | // Need to do this after we've done *all* the convolving |
---|
| 250 | for(int pos=0;pos<size;pos++) coeffs[pos] = coeffs[pos] - wavelet[pos]; |
---|
[3] | 251 | |
---|
[378] | 252 | // Have found wavelet coeffs for this scale -- now threshold |
---|
| 253 | if(scale>=par.getMinScale()){ |
---|
[650] | 254 | findMedianStats(wavelet,goodSize,isGood,mean,sigma); |
---|
[3] | 255 | |
---|
[378] | 256 | threshold = mean + |
---|
| 257 | par.getAtrousCut()*originalSigma*sigmaFactors[scale]; |
---|
| 258 | for(int pos=0;pos<size;pos++){ |
---|
| 259 | if(!isGood[pos]){ |
---|
| 260 | output[pos] = input[pos]; |
---|
| 261 | // this preserves the Blank pixel values in the output. |
---|
| 262 | } |
---|
| 263 | else if( fabs(wavelet[pos]) > threshold ){ |
---|
| 264 | output[pos] += wavelet[pos]; |
---|
| 265 | // only add to the output if the wavelet coefficient is significant |
---|
| 266 | } |
---|
[231] | 267 | } |
---|
[86] | 268 | } |
---|
[3] | 269 | |
---|
[378] | 270 | spacing *= 2; // double the scale of the filter. |
---|
[3] | 271 | |
---|
[378] | 272 | } //-> end of scale loop |
---|
[3] | 273 | |
---|
[378] | 274 | for(int pos=0;pos<size;pos++) if(isGood[pos]) output[pos] += coeffs[pos]; |
---|
[3] | 275 | |
---|
[650] | 276 | for(int i=0;i<size;i++) residual[i] = input[i] - output[i]; |
---|
| 277 | findMedianStats(residual,goodSize,isGood,mean,newsigma); |
---|
[378] | 278 | newsigma = madfmToSigma(newsigma); |
---|
[3] | 279 | |
---|
[378] | 280 | if(par.isVerbose()) printBackSpace(15); |
---|
[3] | 281 | |
---|
[378] | 282 | } while( (iteration==1) || |
---|
| 283 | (fabs(oldsigma-newsigma)/newsigma > reconTolerance) ); |
---|
[3] | 284 | |
---|
[378] | 285 | if(par.isVerbose()) std::cout << "Completed "<<iteration<<" iterations. "; |
---|
[3] | 286 | |
---|
[378] | 287 | delete [] xLim1; |
---|
| 288 | delete [] xLim2; |
---|
| 289 | delete [] yLim1; |
---|
| 290 | delete [] yLim2; |
---|
| 291 | delete [] filter; |
---|
[650] | 292 | delete [] residual; |
---|
[378] | 293 | delete [] coeffs; |
---|
| 294 | delete [] wavelet; |
---|
[231] | 295 | |
---|
[378] | 296 | } |
---|
| 297 | |
---|
| 298 | delete [] isGood; |
---|
| 299 | delete [] sigmaFactors; |
---|
[231] | 300 | } |
---|
| 301 | |
---|
[3] | 302 | } |
---|