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

Last change on this file since 913 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
Line 
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// -----------------------------------------------------------------------
28#include <iostream>
29#include <iomanip>
30#include <math.h>
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>
38using Statistics::madfmToSigma;
39
40namespace duchamp
41{
42
43  void atrous2DReconstruct(size_t &xdim, size_t &ydim, float *&input, float *&output, Param &par)
44  {
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.
59
60    size_t size = xdim * ydim;
61    unsigned long mindim = xdim;
62    if (ydim<mindim) mindim = ydim;
63    unsigned int numScales = par.filter().getNumScales(mindim);
64    double *sigmaFactors = new double[numScales+1];
65    for(size_t i=0;i<=numScales;i++){
66      if(i<=par.filter().maxFactor(2))
67        sigmaFactors[i] = par.filter().sigmaFactor(2,i);
68      else sigmaFactors[i] = sigmaFactors[i-1] / 2.;
69    }
70
71    float mean,originalSigma,oldsigma,newsigma;
72    size_t goodSize=0;
73    bool *isGood = new bool[size];
74    for(size_t pos=0;pos<size;pos++){
75      isGood[pos] = !par.isBlank(input[pos]);
76      if(isGood[pos]) goodSize++;
77    }
78
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.
82
83      for(size_t pos=0;pos<size; pos++) output[pos] = input[pos];
84
85      DUCHAMPWARN("2D Reconstruction","There are no good pixels to be reconstructed -- all are BLANK. Returning input array.");
86    }
87    else{
88      // Otherwise, all is good, and we continue.
89
90      //      findMedianStats(input,goodSize,isGood,originalMean,originalSigma);
91      // originalSigma = madfmToSigma(originalSigma);
92      if(par.getFlagRobustStats())
93        originalSigma = madfmToSigma(findMADFM(input,isGood,size));
94      else
95        originalSigma = findStddev<float>(input,isGood,size);
96 
97      float *coeffs    = new float[size];
98      float *wavelet   = new float[size];
99      // float *residual  = new float[size];
100
101      for(size_t pos=0;pos<size;pos++) output[pos]=0.;
102
103      unsigned int filterwidth = par.filter().width();
104      int filterHW = filterwidth/2;
105      double *filter = new double[filterwidth*filterwidth];
106      for(size_t i=0;i<filterwidth;i++){
107        for(size_t j=0;j<filterwidth;j++){
108          filter[i*filterwidth+j] = par.filter().coeff(i) * par.filter().coeff(j);
109        }
110      }
111
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;
120
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);
133   
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);
144   
145      //        // if(avGapX < mindim) mindim = int(avGapX);
146      //        // if(avGapY < mindim) mindim = int(avGapY);
147      //        // numScales = par.filter().getNumScales(mindim);
148      // }
149
150      float threshold;
151      int iteration=0;
152      newsigma = 1.e9;
153      for(size_t i=0;i<size;i++) output[i] = 0;
154      do{
155        if(par.isVerbose()) {
156          std::cout << "Iteration #"<<std::setw(2)<<++iteration<<":";
157          printBackSpace(13);
158        }
159
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
164        for(size_t i=0;i<size;i++)  coeffs[i] = input[i] - output[i]; 
165
166        int spacing = 1;
167        for(unsigned int scale = 1; scale<numScales; scale++){
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
176          for(unsigned long ypos = 0; ypos<ydim; ypos++){
177            for(unsigned long xpos = 0; xpos<xdim; xpos++){
178              // loops over each pixel in the image
179              size_t pos = ypos*xdim + xpos;
180         
181              wavelet[pos] = coeffs[pos];
182
183              if(!isGood[pos]) wavelet[pos] = 0.;
184              else{
185
186                size_t filterpos = 0;
187                for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
188                  long y = long(ypos) + spacing*yoffset;
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                  }
194                  // Boundary conditions -- assume reflection at boundaries.
195                  // Use limits as calculated above
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;     
201                  //            }
202                  //          }
203                  size_t oldrow = y * xdim;
204         
205                  for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
206                    long x = long(xpos) + spacing*xoffset;
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                    }
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                    //          }
221
222                    size_t oldpos = oldrow + x;
223
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.;
229
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
235                    filterpos++;
236
237                  } //-> end of xoffset loop
238                } //-> end of yoffset loop
239              } //-> end of else{ ( from if(!isGood[pos])  )
240       
241            } //-> end of xpos loop
242          } //-> end of ypos loop
243
244          // Need to do this after we've done *all* the convolving
245          for(size_t pos=0;pos<size;pos++) coeffs[pos] = coeffs[pos] - wavelet[pos];
246
247          // Have found wavelet coeffs for this scale -- now threshold   
248          if(scale>=par.getMinScale()){
249            //      findMedianStats(wavelet,goodSize,isGood,mean,sigma);
250            if(par.getFlagRobustStats())
251              mean = findMedian<float>(wavelet,isGood,size);
252            else
253              mean= findMean<float>(wavelet,isGood,size);
254
255            threshold = mean +
256              par.getAtrousCut() * originalSigma * sigmaFactors[scale];
257            for(size_t pos=0;pos<size;pos++){
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            }
263          }
264          spacing *= 2;
265
266        } // END OF LOOP OVER SCALES
267
268        for(size_t pos=0;pos<size;pos++) if(isGood[pos]) output[pos] += coeffs[pos];
269
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
277          newsigma = findStddevDiff<float>(input,output,isGood,size);
278
279        if(par.isVerbose()) printBackSpace(15);
280
281      } while( (iteration==1) ||
282               (fabs(oldsigma-newsigma)/newsigma > reconTolerance) );
283
284      if(par.isVerbose()) std::cout << "Completed "<<iteration<<" iterations. ";
285
286      // delete [] xLim1;
287      // delete [] xLim2;
288      // delete [] yLim1;
289      // delete [] yLim2;
290      delete [] filter;
291      delete [] coeffs;
292      delete [] wavelet;
293      // delete [] residual;
294
295    }
296
297    delete [] isGood;
298    delete [] sigmaFactors;
299  }
300   
301}
Note: See TracBrowser for help on using the repository browser.