source: branches/pixel-map-branch/src/Cubes/trimImage.cc @ 246

Last change on this file since 246 was 246, checked in by Matthew Whiting, 17 years ago

Summary of changes:

  • Rather than storing centres, the Objects now keep the xSum, ySum etc and return xSum/float(numPix) as the result for xCentre.
  • To do this, we also keep track of the number of pixels in each object. Each time a pixel, scan or object is added, we increment the number by the correct amount.
  • These parameters are also calculated as we go, rather than all at once.
  • Detection::calcParams() changed to Detection::calcFluxes(), so that it just works out the flux-related parameters. Things like xCentre etc are done in the Objects.
  • areClose() improved to work at the Scan level, rather than looking at each pixel. It only compares Scans within the appropriate y-range.
  • Other functions simplified include logDetectionList() and trimImage().
  • Scan::join() and Scan::isCommon() changed to Scan::unite() and Scan::intersect().
File size: 6.6 KB
Line 
1#include <iostream>
2#include <param.hh>
3#include <Cubes/cubes.hh>
4#include <PixelMap/Object3D.hh>
5
6void Cube::trimCube()
7{
8  /**
9   *  If the blankPix flag has been set, this routine trims excess blank
10   *    pixels from the edges of the spatial image.
11   *   It uses as its template the first channel, assuming that its non-BLANK
12   *    size is representative of the rest of the channels.
13   *   The edges are moved in until the first non-BLANK pixel is encountered.
14   *   All other arrays are similarly edited, and the amount of trimming is
15   *    recorded.
16   */
17
18  if(this->par.getFlagBlankPix()) {
19
20    long xdim = this->axisDim[0];
21    long ydim = this->axisDim[1];
22    long zdim = this->axisDim[2];
23    const long ZCHAN = 0;
24
25    int left,right,top,bottom;
26
27    for(int z = 0; z < zdim; z++){
28
29      for(int row=0;row<ydim;row++){
30        left=0;
31        right=0;
32        while((left<xdim)&&
33              (this->par.isBlank(this->array[row*xdim+left+ZCHAN*xdim*ydim])) ) {
34          left++;
35        }
36        while((right<xdim)&&
37              (this->par.isBlank(this->array[row*xdim+(xdim-1-right)+ZCHAN*xdim*ydim]))){
38          right++;
39        }
40
41        if( ((z==0)&&(row==0)) || (left < this->par.getBorderLeft()) )
42          this->par.setBorderLeft(left);
43        if( ((z==0)&&(row==0)) || (right < this->par.getBorderRight()) )
44          this->par.setBorderRight(right);
45
46      }
47   
48      for(int col=0;col<xdim;col++){
49        bottom=0;
50        top=0;
51        while((bottom<ydim)&&
52              (this->par.isBlank(this->array[col+bottom*xdim+ZCHAN*xdim*ydim])) ) bottom++;
53
54        while((top<ydim)&&
55              (this->par.isBlank(this->array[col+(ydim-1-top)*xdim+ZCHAN*xdim*ydim])) ) top++;
56
57        if( ((z==0)&&(col==0)) || (bottom < this->par.getBorderBottom()) )
58          this->par.setBorderBottom(bottom);
59        if( ((z==0)&&(col==0)) || (top < this->par.getBorderTop()) )
60          this->par.setBorderTop(top);
61
62      }
63
64    }
65   
66    left = this->par.getBorderLeft();
67    right = this->par.getBorderRight();
68    top = this->par.getBorderTop();
69    bottom = this->par.getBorderBottom();
70
71    this->axisDim[0] = xdim - left - right;
72    this->axisDim[1] = ydim - bottom - top;
73    this->axisDim[2] = zdim;
74    this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2];
75
76    long oldpos,newpos;
77   
78    // Do the trimming, but only if we need to -- is there a border of Blanks?
79    if((left>0)||(right>0)||(bottom>0)||(top>0)) {
80
81      // Trim the array of pixel values
82      float *newarray  = new float[this->numPixels];
83      for(int x = 0; x < axisDim[0]; x++){
84        for(int y = 0; y < axisDim[1]; y++){
85          for(int z = 0; z < axisDim[2]; z++){
86            oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim;
87            newpos = x + y*axisDim[0] + z*axisDim[0]*axisDim[1];
88            newarray[newpos]  = this->array[oldpos];
89          }
90        }
91      }
92      delete [] this->array;
93      this->array = newarray;
94
95      // Trim the 2-D detection map
96      short *newdetect = new short[this->axisDim[0]*this->axisDim[1]];
97      for(int x = 0; x < axisDim[0]; x++){
98        for(int y = 0; y < axisDim[1]; y++){
99          oldpos = (x+left) + (y+bottom)*xdim;
100          newpos = x + y*axisDim[0];
101          newdetect[newpos] = this->detectMap[oldpos];
102        }
103      }
104      delete [] this->detectMap;
105      this->detectMap = newdetect;
106
107      if(this->par.getFlagATrous()){
108        // Trim the reconstructed array if we are going to do the
109        // reconstruction
110        float *newrecon  = new float[this->numPixels];
111        for(int x = 0; x < axisDim[0]; x++){
112          for(int y = 0; y < axisDim[1]; y++){
113            for(int z = 0; z < axisDim[2]; z++){
114              oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim;
115              newpos = x + y*axisDim[0] + z*axisDim[0]*axisDim[1];
116              newrecon[newpos] = this->recon[oldpos];     
117            }
118          }
119        }
120        delete [] this->recon;
121        this->recon = newrecon;
122      }
123
124      // Set the flag indicating trimming has taken place only if it has.
125      this->par.setFlagCubeTrimmed(true);
126
127    }
128
129  }
130
131}
132
133
134void Cube::unTrimCube()
135{
136  /**
137   *  If the cube has been trimmed by trimCube(), this task adds back the
138   *   BLANK pixels on the edges, so that it returns to its original size.
139   *   All arrays are similarly edited.
140   */
141
142  if(this->par.getFlagCubeTrimmed()){
143
144    long left = this->par.getBorderLeft();
145    long right = this->par.getBorderRight();
146    long top = this->par.getBorderTop();
147    long bottom = this->par.getBorderBottom();
148
149    long smallXDim = this->axisDim[0];
150    long smallYDim = this->axisDim[1];
151    long smallZDim = this->axisDim[2];
152
153    // first correct the dimension sizes
154    this->axisDim[0] = smallXDim + left + right;
155    this->axisDim[1] = smallYDim + bottom + top;
156    this->axisDim[2] = smallZDim;
157    this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2];
158
159    long pos,smlpos;
160    bool isDud;
161
162    // Correct the array of pixel values
163    float *newarray  = new float[this->numPixels];
164    for(int x = 0; x < this->axisDim[0]; x++){
165      for(int y = 0; y < this->axisDim[1]; y++){
166        isDud = (x<left) || (x>=smallXDim+left) ||
167          (y<bottom) || (y>=smallYDim+bottom);
168       
169        for(int z = 0; z < this->axisDim[2]; z++){
170          pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1];
171          smlpos = (x-left) + (y-bottom)*smallXDim + z * smallXDim * smallYDim;
172          if(isDud) newarray[pos] = this->par.getBlankPixVal();
173          else      newarray[pos] = this->array[smlpos];
174        }
175      }
176    }
177    delete [] this->array;
178    this->array = newarray;
179
180    if(this->reconExists){
181      // Correct the reconstructed array
182      float *newrecon   = new float[this->numPixels];
183      for(int x = 0; x < this->axisDim[0]; x++){
184        for(int y = 0; y < this->axisDim[1]; y++){
185          isDud = (x<left) || (x>=smallXDim+left) ||
186            (y<bottom) || (y>=smallYDim+bottom);
187         
188          for(int z = 0; z < this->axisDim[2]; z++){
189            pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1];
190            smlpos = (x-left) + (y-bottom)*smallXDim
191              + z * smallXDim * smallYDim;
192            if(isDud) newrecon[pos] = this->par.getBlankPixVal();
193            else      newrecon[pos] = this->recon[smlpos];
194          }
195        }
196      }
197      delete [] this->recon;
198      this->recon = newrecon;
199    }
200
201    // Correct the 2-D detection map
202    short *newdetect = new short[this->axisDim[0]*this->axisDim[1]];
203    for(int x = 0; x < this->axisDim[0]; x++){
204      for(int y = 0; y < this->axisDim[1]; y++){
205        pos = x + y*this->axisDim[0];
206        smlpos = (x-left) + (y-bottom)*smallXDim;
207        isDud = (x<left) || (x>=smallXDim+left) ||
208          (y<bottom) || (y>=smallYDim+bottom);
209        if(isDud) newdetect[pos]=0;
210        else newdetect[pos] = this->detectMap[smlpos];
211      }
212    }
213    delete [] this->detectMap;
214    this->detectMap = newdetect;   
215
216 
217    // Now update the positions for all the detections
218 
219    for(int i=0;i<this->objectList.size();i++){
220      this->objectList[i].pixels().addOffsets(left,bottom,0);
221      //      objectList[i].calcParams(this->array,this->axisDim);
222    }
223
224  }
225
226}
Note: See TracBrowser for help on using the repository browser.