source: trunk/ATrous/atrous_2d_reconstruct.cc @ 3

Last change on this file since 3 was 3, checked in by Matthew Whiting, 18 years ago

This is the first full import of all working code to
the Duchamp repository.
Made three directories at top level:

branches/ tags/ trunk/

and trunk/ has the full set of code:
ATrous/ Cubes/ Detection/ InputComplete? InputExample? README Utils/ docs/ mainDuchamp.cc param.cc param.hh

File size: 7.6 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <math.h>
4#include <ATrous/atrous.hh>
5//#include <Cubes/cubes.hh>
6#include <Utils/utils.hh>
7
8using std::endl;
9using std::setw;
10
11void atrous2DReconstruct(long &xdim, long &ydim, float *&input,float *&output, Param &par)
12{
13  extern Filter reconFilter;
14  bool flagBlank=par.getFlagBlankPix();
15  float blankPixValue = par.getBlankPixVal();
16  long size = xdim * ydim;
17  long mindim = xdim;
18  if (ydim<mindim) mindim = ydim;
19  int numScales = reconFilter.getNumScales(mindim);
20  /*
21  if(numScales>maxNumScales2D){
22    std::cerr<<"Error in atrous2DReconstruct:: numScales ("<<numScales<<") > "<<maxNumScales2D<<"\n";
23    std::cerr<<"Don't have correction factors for this many scales...\n";
24    std::cerr<<"XDIM = "<<xdim<<", YDIM = "<<ydim<<endl;
25    std::cerr<<"Exiting...\n";
26    exit(1);
27  }
28  */
29  double *sigmaFactors = new double[numScales+1];
30//   for(int i=0;i<=numScales;i++){
31//     if(i<=maxNumScales2D) sigmaFactors[i] = sigmaFactors2D[i];
32//     else sigmaFactors[i] = sigmaFactors[i-1] / 2.;
33//   }
34  for(int i=0;i<=numScales;i++){
35    if(i<=reconFilter.maxFactor(2)) sigmaFactors[i] = reconFilter.sigmaFactor(2,i);
36    else sigmaFactors[i] = sigmaFactors[i-1] / 2.;
37  }
38
39  float mean,sigma,originalSigma,originalMean,oldsigma,newsigma;
40  bool *isGood = new bool[size];
41  for(int pos=0;pos<size;pos++) //isGood[pos] = (!flagBlank) || (input[pos]!=blankPixValue);
42    isGood[pos] = !par.isBlank(input[pos]);
43 
44  float *array = new float[size];
45  int goodSize=0;
46  for(int i=0;i<size;i++) if(isGood[i]) array[goodSize++] = input[i];
47  findMedianStats(array,goodSize,originalMean,originalSigma);
48  delete [] array;
49 
50  float *coeffs    = new float[size];
51  float *wavelet   = new float[size];
52
53  for(int pos=0;pos<size;pos++) output[pos]=0.;
54
55  /***********************************************************************/
56  /////  2-DIMENSIONAL TRANSFORM
57//   int filterHW = filterwidth/2;
58//   double *filter = new double[filterwidth*filterwidth];
59//   for(int i=0;i<filterwidth;i++){
60//     for(int j=0;j<filterwidth;j++){
61//       filter[i*filterwidth+j] = filter1D[i] * filter1D[j];
62//     }
63//   }
64  int filterHW = reconFilter.width()/2;
65  double *filter = new double[reconFilter.width()*reconFilter.width()];
66  for(int i=0;i<reconFilter.width();i++){
67    for(int j=0;j<reconFilter.width();j++){
68      filter[i*reconFilter.width()+j] = reconFilter.coeff(i) * reconFilter.coeff(j);
69    }
70  }
71
72  int *xLim1 = new int[ydim];
73  int *yLim1 = new int[xdim];
74  int *xLim2 = new int[ydim];
75  int *yLim2 = new int[xdim];
76  float avGapX = 0, avGapY = 0;
77  for(int row=0;row<ydim;row++){
78    int ct1 = 0;
79    int ct2 = xdim - 1;
80    while((ct1<ct2)&&(input[row*xdim+ct1]==blankPixValue) ) ct1++;
81    while((ct2>ct1)&&(input[row*xdim+ct2]==blankPixValue) ) ct2--;
82    xLim1[row] = ct1;
83    xLim2[row] = ct2;
84    avGapX += ct2 - ct1;
85  }
86  avGapX /= float(ydim);
87
88  for(int col=0;col<xdim;col++){
89    int ct1=0;
90    int ct2=ydim-1;
91    while((ct1<ct2)&&(input[col+xdim*ct1]==blankPixValue) ) ct1++;
92    while((ct2>ct1)&&(input[col+xdim*ct2]==blankPixValue) ) ct2--;
93    yLim1[col] = ct1;
94    yLim2[col] = ct2;
95    avGapY += ct2 - ct1;
96  }
97  avGapY /= float(ydim);
98 
99  mindim = int(avGapX);
100  if(avGapY < avGapX) mindim = int(avGapY);
101  numScales = reconFilter.getNumScales(mindim);
102 
103
104  /***********************************************************************/
105  /***********************************************************************/
106
107  float threshold;
108  int iteration=0;
109  newsigma = 1.e9;
110  for(int i=0;i<size;i++) output[i] = 0;
111  do{
112    if(par.isVerbose()) std::cout << "Iteration #"<<++iteration<<":             ";
113    // first, get the value of oldsigma and set it to the previous newsigma value
114    oldsigma = newsigma;
115    // we are transforming the residual array
116    for(int i=0;i<size;i++)  coeffs[i] = input[i] - output[i]; 
117
118    int spacing = 1;
119    for(int scale = 1; scale<numScales; scale++){
120
121      if(par.isVerbose()) {
122        std::cout << "\b\b\b\b\b\b\b\b\b\b\b\bScale ";
123        std::cout << setw(2)<<scale<<" /"<<setw(2)<<numScales<<std::flush;
124      }
125
126      for(int ypos = 0; ypos<ydim; ypos++){
127        for(int xpos = 0; xpos<xdim; xpos++){
128          // loops over each pixel in the image
129          int pos = ypos*xdim + xpos;
130         
131          wavelet[pos] = coeffs[pos];
132
133          if(!isGood[pos]) wavelet[pos] = 0.;
134          else{
135
136            for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
137              int y = ypos + spacing*yoffset;
138              int newy;
139              //          if(y<0) y = -y;                 // boundary conditions are
140              //          if(y>=ydim) y = 2*(ydim-1) - y; //    reflection.
141              //          while((y<yLim1)||(y>yLim2)){
142              //            if(y<yLim1) y = 2*yLim1 - y;      // boundary conditions are
143              //            if(y>yLim2) y = 2*yLim2 - y;      //    reflection.
144              //          }
145              // boundary conditions are reflection.
146         
147              for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
148                int x = xpos + spacing*xoffset;
149                int newx;
150                //if(x<0) x = -x;                 // boundary conditions are
151                // if(x>=xdim) x = 2*(xdim-1) - x; //    reflection.
152                //while((x<xLim1)||(x>xLim2)){
153                //            if(x<xLim1) x = 2*xLim1 - x;      // boundary conditions are
154                //            if(x>xLim2) x = 2*xLim2 - x;      //    reflection.
155                //          }
156                // boundary conditions are reflection.
157                while((y<yLim1[xpos])||(y>yLim2[xpos])){
158                  if(y<yLim1[xpos]) y = 2*yLim1[xpos] - y;     
159                  else if(y>yLim2[xpos]) y = 2*yLim2[xpos] - y;     
160                }
161                while((x<xLim1[ypos])||(x>xLim2[ypos])){
162                  if(x<xLim1[ypos]) x = 2*xLim1[ypos] - x;     
163                  else if(x>xLim2[ypos]) x = 2*xLim2[ypos] - x;     
164                }
165
166//              if(y<yLim1[xpos]) newy = 2*yLim1[xpos] - y;     
167//              else if(y>yLim2[xpos]) newy = 2*yLim2[xpos] - y;     
168//              else newy = y;
169//              if(x<xLim1[ypos]) newx = 2*xLim1[ypos] - x;
170//              else if(x>xLim2[ypos]) newx = 2*xLim2[ypos] - x;     
171//              else newx=x;     
172             
173//              x = newx;
174//              y = newy;
175
176                int filterpos = (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
177                int oldpos = y*xdim + x;
178
179                if(isGood[pos])
180                  wavelet[pos] -= filter[filterpos] * coeffs[oldpos];
181
182              } //-> end of xoffset loop
183            } //-> end of yoffset loop
184          } //-> end of else{ ( from if(!isGood[pos])  )
185       
186        } //-> end of xpos loop
187      } //-> end of ypos loop
188
189      // Need to do this after we've done *all* the convolving
190      for(int pos=0;pos<size;pos++) coeffs[pos] = coeffs[pos] - wavelet[pos];
191
192      // Have found wavelet coeffs for this scale -- now threshold   
193      if(scale>=par.getMinScale()){
194        array = new float[size];
195        goodSize=0;
196        for(int pos=0;pos<size;pos++) if(isGood[pos]) array[goodSize++] = wavelet[pos];
197        findMedianStats(array,goodSize,mean,sigma);
198        delete [] array;
199
200        threshold = mean + par.getAtrousCut() * originalSigma * sigmaFactors[scale];
201        for(int pos=0;pos<size;pos++){
202          if(!isGood[pos]) output[pos] = blankPixValue; // preserve the Blank pixel values in the output.
203          else if( fabs(wavelet[pos]) > threshold ) output[pos] += wavelet[pos];
204        }
205      }
206      spacing *= 2;
207
208    } // END OF LOOP OVER SCALES
209
210    for(int pos=0;pos<size;pos++) if(isGood[pos]) output[pos] += coeffs[pos];
211
212    array = new float[size];
213    goodSize=0;
214    for(int i=0;i<size;i++) if(isGood[i]) array[goodSize++] = input[i] - output[i];
215    findMedianStats(array,goodSize,mean,newsigma);
216    delete [] array;
217   
218    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";
219
220  } while( (iteration==1) ||
221           (fabsf(oldsigma-newsigma)/newsigma > reconTolerance) );
222
223  if(par.isVerbose()) std::cout << "Completed "<<iteration<<" iterations. ";
224
225  delete [] coeffs;
226  delete [] wavelet;
227  delete [] isGood;
228  delete [] filter;
229  delete [] sigmaFactors;
230}
231   
232   
Note: See TracBrowser for help on using the repository browser.