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

Last change on this file since 1007 was 861, checked in by MatthewWhiting, 13 years ago

Fixing function prototypes

File size: 16.6 KB
RevLine 
[233]1#include <iostream>
2#include <math.h>
[393]3#include <duchamp/param.hh>
4#include <duchamp/ATrous/atrous.hh>
5#include <duchamp/Utils/utils.hh>
[861]6#include <duchamp/Devel/devel.hh>
[233]7
[641]8namespace duchamp{
[233]9
[641]10  /***********************************************************************/
11  /////  1-DIMENSIONAL TRANSFORM
12  /***********************************************************************/
[233]13
[641]14  // template <class T>
15  // void atrousTransform(long &length, T *&spectrum, T *&coeffs, T *&wavelet)
16  // {
17  //   int filterHW = filterwidth/2;
18  //   int numScales = getNumScales(length);
[233]19
[641]20  //   delete [] coeffs;
21  //   coeffs = new T[(numScales+1)*length];
22  //   delete [] wavelet;
23  //   wavelet = new T[(numScales+1)*length];
24  //   for(int i=0;i<length;i++)  coeffs[i] = wavelet[i] = spectrum[i];
[233]25
[641]26  //   int spacing = 1;
27  //   for(int scale = 0; scale<numScales; scale++){
[233]28
[641]29  //     for(int pos = 0; pos<length; pos++){
30  //       coeffs[(scale+1)*length+pos] = 0;
31  //       for(int offset=-filterHW; offset<=filterHW; offset++){
32  //    int x = pos + spacing*offset;
33  //    if(x<0) x = -x;                    // boundary conditions are
34  //    if(x>=length) x = 2*(length-1) - x;    //    reflection.
35  // //         if(x<0) x = x+length;                // boundary conditions are
36  // //         if(x>=length) x = x-length;            //    continuous.
[233]37
[641]38  //    coeffs[(scale+1)*length+pos] += filter1D[offset+filterHW] *
39  //      coeffs[scale*length+x];
40  //       }
41  //       wavelet[(scale+1)*length+pos] = coeffs[scale*length+pos] -
42  //    coeffs[(scale+1)*length+pos];
43  //     }
[233]44
[641]45  //     spacing *= 2;
[233]46
[641]47  //   }
[233]48
[641]49  // }
50
[233]51 
[861]52  void atrousTransform(unsigned long &length, int &numScales, float *spectrum, double *coeffs, double *wavelet, duchamp::Param &par)
[641]53  {
54    duchamp::Filter reconFilter = par.filter();
55    int filterHW = reconFilter.width()/2;
[233]56
[641]57    for(int i=0;i<length;i++)  coeffs[i] = wavelet[i] = spectrum[i];
[233]58
[641]59    int spacing = 1;
60    for(int scale = 0; scale<numScales; scale++){
[233]61
[641]62      for(int pos = 0; pos<length; pos++){
63        coeffs[(scale+1)*length+pos] = 0;
64        for(int offset=-filterHW; offset<=filterHW; offset++){
65          int x = pos + spacing*offset;
66          if(x<0) x = -x;                    // boundary conditions are
67          if(x>=length) x = 2*(length-1) - x;    //    reflection.
68          //    if(x<0) x = x+length;                // boundary conditions are
69          //    if(x>=length) x = x-length;            //    continuous.
[233]70
[641]71          //    coeffs[(scale+1)*length+pos] += filter1D[offset+filterHW] *
72          //      coeffs[scale*length+x];
73          coeffs[(scale+1)*length+pos] += reconFilter.coeff(offset+filterHW) *
74            coeffs[scale*length+x];
75        }
76        wavelet[(scale+1)*length+pos] = coeffs[scale*length+pos] -
77          coeffs[(scale+1)*length+pos];
[233]78      }
[641]79
80      spacing *= 2;
81
[233]82    }
83
84  }
85
[861]86  void atrousTransform(unsigned long &length, float *spectrum, float *coeffs, float *wavelet, duchamp::Param &par)
[641]87  {
88    duchamp::Filter reconFilter = par.filter();
89    int filterHW = reconFilter.width()/2;
90    int numScales = reconFilter.getNumScales(length);
[233]91
[641]92    delete [] coeffs;
93    coeffs = new float[(numScales+1)*length];
94    delete [] wavelet;
95    wavelet = new float[(numScales+1)*length];
96    for(int i=0;i<length;i++) coeffs[i] = wavelet[i] = spectrum[i];
[233]97
[641]98    int spacing = 1;
99    for(int scale = 0; scale<numScales; scale++){
[233]100
[641]101      for(int pos = 0; pos<length; pos++){
102        coeffs[(scale+1)*length+pos] = 0;
103        for(int offset=-filterHW; offset<=filterHW; offset++){
104          int x = pos + spacing*offset;
105          if(x<0) x = -x;                    // boundary conditions are
106          if(x>=length) x = 2*(length-1) - x;    //    reflection.
107          //    if(x<0) x = x+length;                // boundary conditions are
108          //    if(x>=length) x = x-length;            //    continuous.
[233]109
[641]110          //    coeffs[(scale+1)*length+pos] += filter1D[offset+filterHW] *
111          //      coeffs[scale*length+x];
112          coeffs[(scale+1)*length+pos] += reconFilter.coeff(offset+filterHW) *
113            coeffs[scale*length+x];
114        }
115        wavelet[(scale+1)*length+pos] = coeffs[scale*length+pos] -
116          coeffs[(scale+1)*length+pos];
117      }
[233]118
[641]119      spacing *= 2;
120
[233]121    }
122
123  }
124
125
126
127
[641]128  /***********************************************************************/
129  /////  2-DIMENSIONAL TRANSFORM
130  /***********************************************************************/
[233]131
[641]132  void atrousTransform2D(long &xdim, long &ydim, int &numScales, float *input, double *coeffs, double *wavelet, duchamp::Param &par)
133  {
134    duchamp::Filter reconFilter = par.filter();
135    float blankPixValue = par.getBlankPixVal();
136    int filterHW = reconFilter.width()/2;
[233]137
[641]138    double *filter = new double[reconFilter.width()*reconFilter.width()];
139    for(int i=0;i<reconFilter.width();i++){
140      for(int j=0;j<reconFilter.width();j++){
141        filter[i*reconFilter.width()+j] = reconFilter.coeff(i) * reconFilter.coeff(j);
142      }
[233]143    }
144
[641]145    long size = xdim * ydim;
146    float *oldcoeffs = new float[size];
[233]147
[641]148    // locating the borders of the image -- ignoring BLANK pixels
149    //   int xLim1=0, yLim1=0, xLim2=xdim-1, yLim2=ydim-1;
150    //   for(int row=0;row<ydim;row++){
151    //     while((xLim1<xLim2)&&(input[row*xdim+xLim1]==blankPixValue)) xLim1++;
152    //     while((xLim2>xLim1)&&(input[row*xdim+xLim1]==blankPixValue)) xLim2--;
153    //   }
154    //   for(int col=0;col<xdim;col++){
155    //     while((yLim1<yLim2)&&(input[col+xdim*yLim1]==blankPixValue)) yLim1++;
156    //     while((yLim2>yLim1)&&(input[col+xdim*yLim1]==blankPixValue)) yLim2--;
157    //   }
158    //   std::cerr << "X Limits: "<<xLim1<<" "<<xLim2<<std::endl;
159    //   std::cerr << "Y Limits: "<<yLim1<<" "<<yLim2<<std::endl;
[233]160 
[641]161    int *xLim1 = new int[ydim];
162    int *yLim1 = new int[xdim];
163    int *xLim2 = new int[ydim];
164    int *yLim2 = new int[xdim];
165    for(int row=0;row<ydim;row++){
166      int ct1 = 0;
167      int ct2 = xdim - 1;
168      while((ct1<ct2)&&(input[row*xdim+ct1]==blankPixValue) ) ct1++;
169      while((ct2>ct1)&&(input[row*xdim+ct2]==blankPixValue) ) ct2--;
170      xLim1[row] = ct1;
171      xLim2[row] = ct2;
172      std::cerr<<row<<":"<<xLim1[row]<<","<<xLim2[row]<<" ";
173    }
174    std::cerr<<std::endl;
[233]175
[641]176    for(int col=0;col<xdim;col++){
177      int ct1=0;
178      int ct2=ydim-1;
179      while((ct1<ct2)&&(input[col+xdim*ct1]==blankPixValue) ) ct1++;
180      while((ct2>ct1)&&(input[col+xdim*ct2]==blankPixValue) ) ct2--;
181      yLim1[col] = ct1;
182      yLim2[col] = ct2;
183    }
[233]184
185
[641]186    bool *isGood = new bool[size];
187    for(int pos=0;pos<size;pos++) //isGood[pos] = (!flagBlank) || (input[pos]!=blankPixValue);
188      isGood[pos] = !par.isBlank(input[pos]);
[233]189
[641]190    for(int i=0;i<size;i++)  coeffs[i] = wavelet[i] = input[i];
[233]191
[641]192    int spacing = 1;
193    for(int scale = 0; scale<numScales; scale++){
[233]194
[641]195      for(int i=0;i<size;i++) oldcoeffs[i] = coeffs[i];
[233]196
[641]197      //std::cerr << numScales<<" "<<scale<<" "<<spacing<<" "<<reconFilter.width()*spacing<<std::endl;
198      for(int ypos = 0; ypos<ydim; ypos++){
199        for(int xpos = 0; xpos<xdim; xpos++){
200          // loops over each pixel in the image
201          int pos = ypos*xdim + xpos;
202          coeffs[pos] = 0;
[233]203
[641]204          //    if((par.getFlagBlankPix())&&(oldcoeffs[pos] == blankPixValue) )
205          if(par.isBlank(oldcoeffs[pos]) )
206            coeffs[pos] = oldcoeffs[pos];
207          else{
208            for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
209              int y = ypos + spacing*yoffset;
210              int newy;
211              //          if(y<0) y = -y;                 // boundary conditions are
212              //          if(y>=ydim) y = 2*(ydim-1) - y; //    reflection.
213              //          while((y<yLim1)||(y>yLim2)){
214              //            if(y<yLim1) y = 2*yLim1 - y;      // boundary conditions are
215              //            if(y>yLim2) y = 2*yLim2 - y;      //    reflection.
216              //          }
[233]217              // boundary conditions are reflection.
218         
[641]219              for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
220                int x = xpos + spacing*xoffset;
221                int newx;
222                //if(x<0) x = -x;                 // boundary conditions are
223                // if(x>=xdim) x = 2*(xdim-1) - x; //    reflection.
224                //while((x<xLim1)||(x>xLim2)){
225                //            if(x<xLim1) x = 2*xLim1 - x;      // boundary conditions are
226                //            if(x>xLim2) x = 2*xLim2 - x;      //    reflection.
227                //          }
228                // boundary conditions are reflection.
229                if(y<yLim1[xpos]) newy = 2*yLim1[xpos] - y;     
230                else if(y>yLim2[xpos]) newy = 2*yLim2[xpos] - y;     
231                else newy = y;
232                if(x<xLim1[ypos]) newx = 2*xLim1[ypos] - x;
233                else if(x>xLim2[ypos]) newx = 2*xLim2[ypos] - x;     
234                else newx=x;     
[233]235             
[641]236                x = newx;
237                y = newy;
[233]238
[641]239                int filterpos = (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
240                int oldpos = y*xdim + x;
[233]241
[641]242                if(// (x>=0)&&(x<xdim)&&(y>=0)&&(y<ydim)&&
243                   (isGood[pos]))
244                  coeffs[pos] += filter[filterpos] * oldcoeffs[oldpos];
245              }
[233]246            }
[641]247     
[233]248          }
[641]249          wavelet[(scale+1)*size+pos] = oldcoeffs[pos] - coeffs[pos];
[233]250
251
[641]252        }
[233]253      }
254 
[641]255      spacing *= 2;
[233]256
[641]257    }
[233]258
[641]259    delete [] filter;
260    delete [] oldcoeffs;
[233]261
[641]262  }
[233]263
[641]264  // void atrousTransform2D(long &xdim, long &ydim, int &numScales, float *input, double *coeffs, double *wavelet)
265  // {
266  //   Filter reconFilter = par.filter();
267  //   int filterHW = reconFilter.width()/2;
[233]268
[641]269  //   double *filter = new double[reconFilter.width()*reconFilter.width()];
270  //   for(int i=0;i<reconFilter.width();i++){
271  //     for(int j=0;j<reconFilter.width();j++){
272  //       filter[i*reconFilter.width()+j] = reconFilter.coeff(i) * reconFilter.coeff(j);
273  //     }
274  //   }
[233]275
[641]276  //   long size = xdim * ydim;
277  //   float *oldcoeffs = new float[size];
[233]278
[641]279  //   for(int i=0;i<size;i++)  coeffs[i] = wavelet[i] = input[i];
[233]280
281 
[641]282  //   int spacing = 1;
283  //   for(int scale = 0; scale<numScales; scale++){
[233]284
[641]285  //     for(int i=0;i<size;i++) oldcoeffs[i] = coeffs[i];
[233]286
[641]287  //     std::cerr << numScales<<" "<<scale<<" "<<spacing<<std::endl;
288  //     for(int ypos = 0; ypos<ydim; ypos++){
289  //       for(int xpos = 0; xpos<xdim; xpos++){
290  //    // loops over each pixel in the image
291  //    int pos = ypos*xdim + xpos;
292  //    coeffs[pos] = 0;
[233]293
[641]294  //    for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
295  //      int y = ypos + spacing*yoffset;
296  //      if(y<0) y = -y;                 // boundary conditions are
297  //      if(y>=ydim) y = 2*(ydim-1) - y; //    reflection.
[233]298         
[641]299  //      for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
300  //        int x = xpos + spacing*xoffset;
301  //        if(x<0) x = -x;                 // boundary conditions are
302  //        if(x>=xdim) x = 2*(xdim-1) - x; //    reflection.
[233]303
[641]304  //        int filterpos = (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
305  //        int oldpos = y*xdim + x;
306  //        coeffs[pos] += filter[filterpos] * oldcoeffs[oldpos];
307  //      }
308  //    }
[233]309       
[641]310  //    wavelet[(scale+1)*size+pos] = oldcoeffs[pos] - coeffs[pos];
[233]311
[641]312  //       }
313  //     }
[233]314 
[641]315  //     spacing *= 2;
[233]316
[641]317  //   }
[233]318
[641]319  //   delete [] filter;
320  //   delete [] oldcoeffs;
[233]321
[641]322  // }
[233]323
[641]324  /***********************************************************************/
325  /////  3-DIMENSIONAL TRANSFORM
326  /***********************************************************************/
[233]327
[641]328  void atrousTransform3D(long &xdim, long &ydim, long &zdim, int &numScales, float *&input, float *&coeffs, float *&wavelet, duchamp::Param &par)
329  {
330    duchamp::Filter reconFilter = par.filter();
331    float blankPixValue = par.getBlankPixVal();
332    int filterHW = reconFilter.width()/2;
[233]333
[641]334    long size = xdim * ydim * zdim;
335    float *oldcoeffs = new float[size];
[233]336
[641]337    std::cerr << "%";
338    int fsize = reconFilter.width()*reconFilter.width()*reconFilter.width();
339    std::cerr << "%";
340    double *filter = new double[fsize];
341    for(int i=0;i<reconFilter.width();i++){
342      for(int j=0;j<reconFilter.width();j++){
343        for(int k=0;k<reconFilter.width();k++){
344          filter[i +j*reconFilter.width() + k*reconFilter.width()*reconFilter.width()] =
345            reconFilter.coeff(i) * reconFilter.coeff(j) * reconFilter.coeff(k);
346        }
[233]347      }
348    }
[641]349    // locating the borders of the image -- ignoring BLANK pixels
350    // HAVE NOT DONE THIS FOR Z --> ASSUMING NO TRIMMING IN SPECTRAL DIRECTION
351    int xLim1 = 0, yLim1 = 0, xLim2 = xdim-1, yLim2 = ydim-1;
352    for(int col=0;col<xdim;col++){
353      while((yLim1<yLim2)&&(input[col+xdim*yLim1]==blankPixValue) ) yLim1++;
354      while((yLim2>yLim1)&&(input[col+xdim*yLim1]==blankPixValue) ) yLim2--;
355    }
356    for(int row=0;row<ydim;row++){
357      while((xLim1<xLim2)&&(input[row*xdim+xLim1]==blankPixValue) ) xLim1++;
358      while((xLim2>xLim1)&&(input[row*xdim+xLim1]==blankPixValue) ) xLim2--;
359    }
[233]360
[641]361    for(int i=0;i<size;i++)  coeffs[i] = wavelet[i] = input[i];
[233]362
[641]363    int spacing = 1;
364    std::cerr<<xdim<<"x"<<ydim<<"x"<<zdim<<"x"<<numScales;
365    for(int scale = 0; scale<numScales; scale++){
366      std::cerr << ".";
367      for(int i=0;i<size;i++) oldcoeffs[i] = coeffs[i];
[233]368
[641]369      for(int zpos = 0; zpos<zdim; zpos++){
370        for(int ypos = 0; ypos<ydim; ypos++){
371          for(int xpos = 0; xpos<xdim; xpos++){
372            // loops over each pixel in the image
373            int pos = zpos*xdim*ydim + ypos*xdim + xpos;
374            coeffs[pos] = 0;
[233]375
[641]376            if(par.isBlank(oldcoeffs[pos]) )
377              coeffs[pos] = oldcoeffs[pos];
378            else{
379              for(int zoffset=-filterHW; zoffset<=filterHW; zoffset++){
380                int z = zpos + spacing*zoffset;
381                if(z<0) z = -z;                 // boundary conditions are
382                if(z>=zdim) z = 2*(zdim-1) - z; //    reflection.
[233]383         
[641]384                for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
385                  int y = ypos + spacing*yoffset;
386                  //if(y<0) y = -y;                 // boundary conditions are
387                  // if(y>=ydim) y = 2*(ydim-1) - y; //    reflection.
388                  if(y<yLim1) y = 2*yLim1 - y;      // boundary conditions are
389                  if(y>yLim2) y = 2*yLim2 - y;      //    reflection.
[233]390         
[641]391                  for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
392                    int x = xpos + spacing*xoffset;
393                    //if(x<0) x = -x;                 // boundary conditions are
394                    //if(x>=xdim) x = 2*(xdim-1) - x; //    reflection.
395                    if(x<xLim1) x = 2*xLim1 - x;      // boundary conditions are
396                    if(x>xLim2) x = 2*xLim2 - x;      //    reflection.
[233]397
[641]398                    int filterpos = (zoffset+filterHW)*reconFilter.width()*reconFilter.width() +
399                      (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
400                    int oldpos = z*xdim*ydim + y*xdim + x;
[233]401
[641]402                    if(!par.isBlank(oldcoeffs[oldpos]))
403                      coeffs[pos] += filter[filterpos] * oldcoeffs[oldpos];
[233]404
[641]405                  }
[233]406                }
407              }
[641]408            }           
[233]409 
[641]410            wavelet[(scale+1)*size+pos] = oldcoeffs[pos] - coeffs[pos];
[233]411
[641]412          }
[233]413        }
414      }
415 
[641]416      spacing *= 2;
[233]417
[641]418    }
419    std::cerr << "|";
[233]420
[641]421    delete [] filter;
422    delete [] oldcoeffs;
[233]423
[641]424  }
[233]425
426
[641]427  // void atrousTransform3D(long &xdim, long &ydim, long &zdim, int &numScales, float *input, float *coeffs, float *wavelet)
428  // {
429  //   extern Filter reconFilter;
430  //   int filterHW = reconFilter.width()/2;
[233]431
[641]432  //   long size = xdim * ydim * zdim;
433  //   float *oldcoeffs = new float[size];
[233]434
[641]435  //   std::cerr << "%";
436  //   int fsize = reconFilter.width()*reconFilter.width()*reconFilter.width();
437  //   std::cerr << "%";
438  //   double *filter = new double[fsize];
439  //   for(int i=0;i<reconFilter.width();i++){
440  //     for(int j=0;j<reconFilter.width();j++){
441  //       for(int k=0;k<reconFilter.width();k++){
442  //       filter[i +j*reconFilter.width() + k*reconFilter.width()*reconFilter.width()] =
443  //    reconFilter.coeff(i) * reconFilter.coeff(j) * reconFilter.coeff(k);
444  //       }
445  //     }
446  //   }
[233]447
[641]448  //   for(int i=0;i<size;i++)  coeffs[i] = wavelet[i] = input[i];
[233]449
[641]450  //   int spacing = 1;
451  //   std::cerr<<xdim<<"x"<<ydim<<"x"<<zdim<<"x"<<numScales;
452  //   for(int scale = 0; scale<numScales; scale++){
453  //     std::cerr << ".";
454  //     for(int i=0;i<size;i++) oldcoeffs[i] = coeffs[i];
[233]455
[641]456  //     for(int zpos = 0; zpos<zdim; zpos++){
457  //       for(int ypos = 0; ypos<ydim; ypos++){
458  //    for(int xpos = 0; xpos<xdim; xpos++){
459  //      // loops over each pixel in the image
460  //      int pos = zpos*xdim*ydim + ypos*xdim + xpos;
461  //      coeffs[pos] = 0;
[233]462
[641]463  //      for(int zoffset=-filterHW; zoffset<=filterHW; zoffset++){
464  //        int z = zpos + spacing*zoffset;
465  //        if(z<0) z = -z;                 // boundary conditions are
466  //        if(z>=zdim) z = 2*(zdim-1) - z; //    reflection.
[233]467         
[641]468  //        for(int yoffset=-filterHW; yoffset<=filterHW; yoffset++){
469  //          int y = ypos + spacing*yoffset;
470  //          if(y<0) y = -y;                 // boundary conditions are
471  //          if(y>=ydim) y = 2*(ydim-1) - y; //    reflection.
[233]472         
[641]473  //          for(int xoffset=-filterHW; xoffset<=filterHW; xoffset++){
474  //            int x = xpos + spacing*xoffset;
475  //            if(x<0) x = -x;                 // boundary conditions are
476  //            if(x>=xdim) x = 2*(xdim-1) - x; //    reflection.
[233]477
[641]478  //            int filterpos = (zoffset+filterHW)*reconFilter.width()*reconFilter.width() +
479  //              (yoffset+filterHW)*reconFilter.width() + (xoffset+filterHW);
480  //            int oldpos = z*xdim*ydim + y*xdim + x;
[233]481
[641]482  //            coeffs[pos] += filter[filterpos] * oldcoeffs[oldpos];
[233]483
[641]484  //          }
485  //        }
486  //      }
[233]487               
[641]488  //      wavelet[(scale+1)*size+pos] = oldcoeffs[pos] - coeffs[pos];
[233]489
[641]490  //    }
491  //       }
492  //     }
[233]493 
[641]494  //     spacing *= 2;
[233]495
[641]496  //   }
497  //   std::cerr << "|";
[233]498
[641]499  //   delete [] filter;
500  //   delete [] oldcoeffs;
[233]501
[641]502  // }
[233]503
504
[641]505}
Note: See TracBrowser for help on using the repository browser.