source: trunk/src/ATrous/atrous_2d_reconstruct.cc @ 1007

Last change on this file since 1007 was 913, checked in by MatthewWhiting, 12 years ago

A large swathe of changes aimed at improving warning/error/exception handling. Now make use of macros and streams. Also, there is now a distinction between DUCHAMPERROR and DUCHAMPTHROW.

File size: 10.5 KB
RevLine 
[299]1// -----------------------------------------------------------------------
2// atrous_2d_reconstruct.cc: 2-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]38using Statistics::madfmToSigma;
[3]39
[378]40namespace duchamp
[3]41{
[86]42
[887]43  void atrous2DReconstruct(size_t &xdim, size_t &ydim, float *&input, float *&output, Param &par)
[378]44  {
[528]45    ///  A routine that uses the a trous wavelet method to reconstruct a
46    ///   2-dimensional image.
47    ///
48    ///  If there are no non-BLANK pixels (and we are testing for
49    ///  BLANKs), the reconstruction cannot be done, so we return the
50    ///  input array as the output array and give a warning message.
51    ///
52    ///  \param xdim The length of the x-axis of the image.
53    ///  \param ydim The length of the y-axis of the image.
54    ///  \param input The input spectrum.
55    ///  \param output The returned reconstructed spectrum. This array
56    ///  needs to be declared beforehand.
57    ///  \param par The Param set:contains all necessary info about the
58    ///  filter and reconstruction parameters.
[3]59
[846]60    size_t size = xdim * ydim;
61    unsigned long mindim = xdim;
[378]62    if (ydim<mindim) mindim = ydim;
[884]63    unsigned int numScales = par.filter().getNumScales(mindim);
[378]64    double *sigmaFactors = new double[numScales+1];
[846]65    for(size_t i=0;i<=numScales;i++){
[378]66      if(i<=par.filter().maxFactor(2))
67        sigmaFactors[i] = par.filter().sigmaFactor(2,i);
68      else sigmaFactors[i] = sigmaFactors[i-1] / 2.;
69    }
[231]70
[894]71    float mean,originalSigma,oldsigma,newsigma;
[846]72    size_t goodSize=0;
[378]73    bool *isGood = new bool[size];
[846]74    for(size_t pos=0;pos<size;pos++){
[378]75      isGood[pos] = !par.isBlank(input[pos]);
76      if(isGood[pos]) goodSize++;
77    }
[231]78
[378]79    if(goodSize == 0){
80      // There are no good pixels -- everything is BLANK for some reason.
81      // Return the input array as the output, and give a warning message.
[231]82
[846]83      for(size_t pos=0;pos<size; pos++) output[pos] = input[pos];
[378]84
[913]85      DUCHAMPWARN("2D Reconstruction","There are no good pixels to be reconstructed -- all are BLANK. Returning input array.");
[378]86    }
87    else{
88      // Otherwise, all is good, and we continue.
[231]89
[849]90      //      findMedianStats(input,goodSize,isGood,originalMean,originalSigma);
91      // originalSigma = madfmToSigma(originalSigma);
92      if(par.getFlagRobustStats())
93        originalSigma = madfmToSigma(findMADFM(input,isGood,size));
94      else
[889]95        originalSigma = findStddev<float>(input,isGood,size);
[3]96 
[378]97      float *coeffs    = new float[size];
98      float *wavelet   = new float[size];
[849]99      // float *residual  = new float[size];
[3]100
[846]101      for(size_t pos=0;pos<size;pos++) output[pos]=0.;
[3]102
[846]103      unsigned int filterwidth = par.filter().width();
[378]104      int filterHW = filterwidth/2;
105      double *filter = new double[filterwidth*filterwidth];
[846]106      for(size_t i=0;i<filterwidth;i++){
107        for(size_t j=0;j<filterwidth;j++){
[378]108          filter[i*filterwidth+j] = par.filter().coeff(i) * par.filter().coeff(j);
109        }
[231]110      }
[3]111
[908]112      // long *xLim1 = new long[ydim];
113      // for(size_t i=0;i<ydim;i++) xLim1[i] = 0;
114      // long *yLim1 = new long[xdim];
115      // for(size_t i=0;i<xdim;i++) yLim1[i] = 0;
116      // long *xLim2 = new long[ydim];
117      // for(size_t i=0;i<ydim;i++) xLim2[i] = xdim-1;
118      // long *yLim2 = new long[xdim];
119      // for(size_t i=0;i<xdim;i++) yLim2[i] = ydim-1;
[3]120
[908]121      // if(par.getFlagBlankPix()){
122      //        float avGapX = 0, avGapY = 0;
123      //        for(size_t row=0;row<ydim;row++){
124      //          size_t ct1 = 0;
125      //          size_t ct2 = xdim - 1;
126      //          while((ct1<ct2)&&(par.isBlank(input[row*xdim+ct1]))) ct1++;
127      //          while((ct2>ct1)&&(par.isBlank(input[row*xdim+ct2]))) ct2--;
128      //          xLim1[row] = ct1;
129      //          xLim2[row] = ct2;
130      //          avGapX += ct2 - ct1;
131      //        }
132      //        avGapX /= float(ydim);
[103]133   
[908]134      //        for(size_t col=0;col<xdim;col++){
135      //          size_t ct1=0;
136      //          size_t ct2=ydim-1;
137      //          while((ct1<ct2)&&(par.isBlank(input[col+xdim*ct1]))) ct1++;
138      //          while((ct2>ct1)&&(par.isBlank(input[col+xdim*ct2]))) ct2--;
139      //          yLim1[col] = ct1;
140      //          yLim2[col] = ct2;
141      //          avGapY += ct2 - ct1;
142      //        }
143      //        avGapY /= float(xdim);
[103]144   
[908]145      //        // if(avGapX < mindim) mindim = int(avGapX);
146      //        // if(avGapY < mindim) mindim = int(avGapY);
147      //        // numScales = par.filter().getNumScales(mindim);
148      // }
[3]149
[378]150      float threshold;
151      int iteration=0;
152      newsigma = 1.e9;
[846]153      for(size_t i=0;i<size;i++) output[i] = 0;
[378]154      do{
155        if(par.isVerbose()) {
156          std::cout << "Iteration #"<<std::setw(2)<<++iteration<<":";
[231]157          printBackSpace(13);
158        }
159
[378]160        // first, get the value of oldsigma and set it to the previous
161        //   newsigma value
162        oldsigma = newsigma;
163        // we are transforming the residual array
[846]164        for(size_t i=0;i<size;i++)  coeffs[i] = input[i] - output[i]; 
[378]165
166        int spacing = 1;
[846]167        for(unsigned int scale = 1; scale<numScales; scale++){
[378]168
169          if(par.isVerbose()){
170            std::cout << "Scale ";
171            std::cout << std::setw(2)<<scale<<" / "<<std::setw(2)<<numScales;
172            printBackSpace(13);
173            std::cout <<std::flush;
174          }
175
[846]176          for(unsigned long ypos = 0; ypos<ydim; ypos++){
177            for(unsigned long xpos = 0; xpos<xdim; xpos++){
[378]178              // loops over each pixel in the image
[846]179              size_t pos = ypos*xdim + xpos;
[3]180         
[378]181              wavelet[pos] = coeffs[pos];
[3]182
[378]183              if(!isGood[pos]) wavelet[pos] = 0.;
184              else{
[3]185
[908]186                size_t filterpos = 0;
[378]187                for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
[846]188                  long y = long(ypos) + spacing*yoffset;
[908]189                  while((y<0)||(y>=long(ydim))){
190                    // boundary conditions are reflection.
191                    if(y<0) y = 0 - y;
192                    else if(y>=long(ydim)) y = 2*(ydim-1) - y;
193                  }
[231]194                  // Boundary conditions -- assume reflection at boundaries.
195                  // Use limits as calculated above
[378]196                  //          if(yLim1[xpos]!=yLim2[xpos]){
197                  //            // if these are equal we will get into an infinite loop here
198                  //            while((y<yLim1[xpos])||(y>yLim2[xpos])){
199                  //              if(y<yLim1[xpos]) y = 2*yLim1[xpos] - y;     
200                  //              else if(y>yLim2[xpos]) y = 2*yLim2[xpos] - y;     
[231]201                  //            }
[378]202                  //          }
[846]203                  size_t oldrow = y * xdim;
[378]204         
205                  for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
[846]206                    long x = long(xpos) + spacing*xoffset;
[908]207                    while((x<0)||(x>=long(xdim))){
208                      // boundary conditions are reflection.
209                      if(x<0) x = 0 - x;
210                      else if(x>=long(xdim)) x = 2*(xdim-1) - x;
211                    }
[378]212                    // Boundary conditions -- assume reflection at boundaries.
213                    // Use limits as calculated above
214                    //          if(xLim1[ypos]!=xLim2[ypos]){
215                    //            // if these are equal we will get into an infinite loop here
216                    //            while((x<xLim1[ypos])||(x>xLim2[ypos])){
217                    //              if(x<xLim1[ypos]) x = 2*xLim1[ypos] - x;     
218                    //              else if(x>xLim2[ypos]) x = 2*xLim2[ypos] - x;     
219                    //            }
220                    //          }
[3]221
[846]222                    size_t oldpos = oldrow + x;
[3]223
[908]224                    // float oldCoeff;
225                    // if((y>=yLim1[xpos])&&(y<=yLim2[xpos])&&
226                    //    (x>=xLim1[ypos])&&(x<=xLim2[ypos])  )
227                    //   oldCoeff = coeffs[oldpos];
228                    // else oldCoeff = 0.;
[3]229
[908]230                    // if(isGood[pos]) wavelet[pos] -= filter[filterpos] * oldCoeff;
231                    // //                 wavelet[pos] -= filter[filterpos] * coeffs[oldpos];
232                    if(isGood[pos])
233                      wavelet[pos] -= filter[filterpos] * coeffs[oldpos];
234
[378]235                    filterpos++;
[3]236
[378]237                  } //-> end of xoffset loop
238                } //-> end of yoffset loop
239              } //-> end of else{ ( from if(!isGood[pos])  )
[3]240       
[378]241            } //-> end of xpos loop
242          } //-> end of ypos loop
[3]243
[378]244          // Need to do this after we've done *all* the convolving
[846]245          for(size_t pos=0;pos<size;pos++) coeffs[pos] = coeffs[pos] - wavelet[pos];
[3]246
[378]247          // Have found wavelet coeffs for this scale -- now threshold   
248          if(scale>=par.getMinScale()){
[849]249            //      findMedianStats(wavelet,goodSize,isGood,mean,sigma);
250            if(par.getFlagRobustStats())
[889]251              mean = findMedian<float>(wavelet,isGood,size);
[849]252            else
[889]253              mean= findMean<float>(wavelet,isGood,size);
[3]254
[378]255            threshold = mean +
256              par.getAtrousCut() * originalSigma * sigmaFactors[scale];
[846]257            for(size_t pos=0;pos<size;pos++){
[378]258              if(!isGood[pos]) output[pos] = input[pos];
259              // preserve the Blank pixel values in the output.
260              else if( fabs(wavelet[pos]) > threshold )
261                output[pos] += wavelet[pos];
262            }
[231]263          }
[378]264          spacing *= 2;
[3]265
[378]266        } // END OF LOOP OVER SCALES
[3]267
[846]268        for(size_t pos=0;pos<size;pos++) if(isGood[pos]) output[pos] += coeffs[pos];
[3]269
[849]270        // for(size_t i=0;i<size;i++) residual[i] = input[i] - output[i];
271        // findMedianStats(residual,goodSize,isGood,mean,newsigma);
272        // findMedianStatsDiff(input,output,size,isGood,mean,newsigma);
273        // newsigma = madfmToSigma(newsigma);
274        if(par.getFlagRobustStats())
275          newsigma = madfmToSigma(findMADFMDiff(input,output,isGood,size));
276        else
[889]277          newsigma = findStddevDiff<float>(input,output,isGood,size);
[849]278
[378]279        if(par.isVerbose()) printBackSpace(15);
[3]280
[378]281      } while( (iteration==1) ||
282               (fabs(oldsigma-newsigma)/newsigma > reconTolerance) );
[3]283
[378]284      if(par.isVerbose()) std::cout << "Completed "<<iteration<<" iterations. ";
[3]285
[908]286      // delete [] xLim1;
287      // delete [] xLim2;
288      // delete [] yLim1;
289      // delete [] yLim2;
[378]290      delete [] filter;
291      delete [] coeffs;
292      delete [] wavelet;
[849]293      // delete [] residual;
[231]294
[378]295    }
296
297    delete [] isGood;
298    delete [] sigmaFactors;
[231]299  }
[378]300   
[3]301}
Note: See TracBrowser for help on using the repository browser.