1 | #include <iostream> |
---|
2 | #include <Cubes/cubes.hh> |
---|
3 | |
---|
4 | void Cube::trimCube() |
---|
5 | { |
---|
6 | /** |
---|
7 | * Cube::trimCube() |
---|
8 | * |
---|
9 | * If the blankPix flag has been set, this routine trims excess blank pixels |
---|
10 | * from the edges of the spatial image. |
---|
11 | * It uses as its template the first channel, assuming that its non-BLANK size |
---|
12 | * 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 recorded. |
---|
15 | * |
---|
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 | for(int z = 0; z < zdim; z++){ |
---|
26 | |
---|
27 | for(int row=0;row<ydim;row++){ |
---|
28 | int borderLeft=0; |
---|
29 | int borderRight=0; |
---|
30 | while((borderLeft<xdim)&& |
---|
31 | (this->par.isBlank(this->array[row*xdim+borderLeft+ZCHAN*xdim*ydim])) ) { |
---|
32 | borderLeft++; |
---|
33 | } |
---|
34 | while((borderRight<xdim)&& |
---|
35 | (this->par.isBlank(this->array[row*xdim+(xdim-1-borderRight)+ZCHAN*xdim*ydim]))){ |
---|
36 | borderRight++; |
---|
37 | } |
---|
38 | |
---|
39 | if( ((z==0)&&(row==0)) || (borderLeft < this->par.getBorderLeft()) ) |
---|
40 | this->par.setBorderLeft(borderLeft); |
---|
41 | if( ((z==0)&&(row==0)) || (borderRight < this->par.getBorderRight()) ) |
---|
42 | this->par.setBorderRight(borderRight); |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | for(int col=0;col<xdim;col++){ |
---|
47 | int borderBottom=0; |
---|
48 | int borderTop=0; |
---|
49 | while((borderBottom<ydim)&& |
---|
50 | (this->par.isBlank(this->array[col+borderBottom*xdim+ZCHAN*xdim*ydim])) ) borderBottom++; |
---|
51 | |
---|
52 | while((borderTop<ydim)&& |
---|
53 | (this->par.isBlank(this->array[col+(ydim-1-borderTop)*xdim+ZCHAN*xdim*ydim])) ) borderTop++; |
---|
54 | |
---|
55 | if( ((z==0)&&(col==0)) || (borderBottom < this->par.getBorderBottom()) ) |
---|
56 | this->par.setBorderBottom(borderBottom); |
---|
57 | if( ((z==0)&&(col==0)) || (borderTop < this->par.getBorderTop()) ) |
---|
58 | this->par.setBorderTop(borderTop); |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | this->axisDim[0] = xdim - this->par.getBorderLeft() - this->par.getBorderRight(); |
---|
65 | this->axisDim[1] = ydim - this->par.getBorderBottom() - this->par.getBorderTop(); |
---|
66 | this->axisDim[2] = zdim; |
---|
67 | this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2]; |
---|
68 | |
---|
69 | long oldpos,newpos; |
---|
70 | |
---|
71 | // Do the trimming, but only if we need to -- is there a border of Blanks? |
---|
72 | if((this->par.getBorderLeft()>0)||(this->par.getBorderRight()>0)|| |
---|
73 | (this->par.getBorderBottom()>0)||(this->par.getBorderTop()>0)) { |
---|
74 | |
---|
75 | // Trim the array of pixel values |
---|
76 | float *newarray = new float[this->numPixels]; |
---|
77 | for(int x = 0; x < axisDim[0]; x++){ |
---|
78 | for(int y = 0; y < axisDim[1]; y++){ |
---|
79 | for(int z = 0; z < axisDim[2]; z++){ |
---|
80 | oldpos = (x+this->par.getBorderLeft()) + (y+this->par.getBorderBottom())*xdim + z*xdim*ydim; |
---|
81 | newpos = x + y*axisDim[0] + z*axisDim[0]*axisDim[1]; |
---|
82 | newarray[newpos] = this->array[oldpos]; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | delete [] this->array; |
---|
87 | this->array = newarray; |
---|
88 | |
---|
89 | // Trim the 2-D detection map |
---|
90 | short *newdetect = new short[this->axisDim[0]*this->axisDim[1]]; |
---|
91 | for(int x = 0; x < axisDim[0]; x++){ |
---|
92 | for(int y = 0; y < axisDim[1]; y++){ |
---|
93 | oldpos = (x+this->par.getBorderLeft()) + (y+this->par.getBorderBottom())*xdim; |
---|
94 | newpos = x + y*axisDim[0]; |
---|
95 | newdetect[newpos] = this->detectMap[oldpos]; |
---|
96 | } |
---|
97 | } |
---|
98 | delete [] this->detectMap; |
---|
99 | this->detectMap = newdetect; |
---|
100 | |
---|
101 | if(this->par.getFlagATrous()){ |
---|
102 | // Trim the reconstructed array if we are going to do the reconstruction |
---|
103 | float *newrecon = new float[this->numPixels]; |
---|
104 | for(int x = 0; x < axisDim[0]; x++){ |
---|
105 | for(int y = 0; y < axisDim[1]; y++){ |
---|
106 | for(int z = 0; z < axisDim[2]; z++){ |
---|
107 | oldpos = (x+this->par.getBorderLeft()) + (y+this->par.getBorderBottom())*xdim + z*xdim*ydim; |
---|
108 | newpos = x + y*axisDim[0] + z*axisDim[0]*axisDim[1]; |
---|
109 | newrecon[newpos] = this->recon[oldpos]; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | delete [] this->recon; |
---|
114 | this->recon = newrecon; |
---|
115 | } |
---|
116 | |
---|
117 | // Set the flag indicating trimming has taken place only if it has. |
---|
118 | this->par.setFlagCubeTrimmed(true); |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | void Cube::unTrimCube() |
---|
128 | { |
---|
129 | /** |
---|
130 | * Cube::unTrimCube() |
---|
131 | * |
---|
132 | * If the cube has been trimmed by trimCube(), this task adds back the BLANK pixels on |
---|
133 | * the edges, so that it returns to its original size. |
---|
134 | * All arrays are similarly edited. |
---|
135 | */ |
---|
136 | |
---|
137 | if(this->par.getFlagCubeTrimmed()){ |
---|
138 | |
---|
139 | long smallXDim = this->axisDim[0]; |
---|
140 | long smallYDim = this->axisDim[1]; |
---|
141 | long smallZDim = this->axisDim[2]; |
---|
142 | |
---|
143 | // first correct the dimension sizes |
---|
144 | this->axisDim[0] = smallXDim + this->par.getBorderLeft() + this->par.getBorderRight(); |
---|
145 | this->axisDim[1] = smallYDim + this->par.getBorderBottom() + this->par.getBorderTop(); |
---|
146 | this->axisDim[2] = smallZDim; |
---|
147 | this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2]; |
---|
148 | |
---|
149 | long pos,smlpos; |
---|
150 | bool isDud; |
---|
151 | |
---|
152 | // Correct the array of pixel values |
---|
153 | float *newarray = new float[this->numPixels]; |
---|
154 | for(int x = 0; x < this->axisDim[0]; x++){ |
---|
155 | for(int y = 0; y < this->axisDim[1]; y++){ |
---|
156 | isDud = (x<this->par.getBorderLeft()) || (x>=smallXDim+this->par.getBorderLeft()) || |
---|
157 | (y<this->par.getBorderBottom()) || (y>=smallYDim+this->par.getBorderBottom()); |
---|
158 | |
---|
159 | for(int z = 0; z < this->axisDim[2]; z++){ |
---|
160 | pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1]; |
---|
161 | smlpos = (x-this->par.getBorderLeft()) + (y-this->par.getBorderBottom())*smallXDim |
---|
162 | + z * smallXDim * smallYDim; |
---|
163 | if(isDud) newarray[pos] = this->par.getBlankPixVal(); |
---|
164 | else newarray[pos] = this->array[smlpos]; |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | delete [] this->array; |
---|
169 | this->array = newarray; |
---|
170 | |
---|
171 | if(this->reconExists){ |
---|
172 | // Correct the reconstructed array |
---|
173 | float *newrecon = new float[this->numPixels]; |
---|
174 | for(int x = 0; x < this->axisDim[0]; x++){ |
---|
175 | for(int y = 0; y < this->axisDim[1]; y++){ |
---|
176 | isDud = (x<this->par.getBorderLeft()) || (x>=smallXDim+this->par.getBorderLeft()) || |
---|
177 | (y<this->par.getBorderBottom()) || (y>=smallYDim+this->par.getBorderBottom()); |
---|
178 | |
---|
179 | for(int z = 0; z < this->axisDim[2]; z++){ |
---|
180 | pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1]; |
---|
181 | smlpos = (x-this->par.getBorderLeft()) + (y-this->par.getBorderBottom())*smallXDim |
---|
182 | + z * smallXDim * smallYDim; |
---|
183 | if(isDud) newrecon[pos] = this->par.getBlankPixVal(); |
---|
184 | else newrecon[pos] = this->recon[smlpos]; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | delete [] this->recon; |
---|
189 | this->recon = newrecon; |
---|
190 | } |
---|
191 | |
---|
192 | // Correct the 2-D detection map |
---|
193 | short *newdetect = new short[this->axisDim[0]*this->axisDim[1]]; |
---|
194 | for(int x = 0; x < this->axisDim[0]; x++){ |
---|
195 | for(int y = 0; y < this->axisDim[1]; y++){ |
---|
196 | pos = x + y*this->axisDim[0]; |
---|
197 | smlpos = (x-this->par.getBorderLeft()) + (y-this->par.getBorderBottom())*smallXDim; |
---|
198 | isDud = (x<this->par.getBorderLeft()) || (x>=smallXDim+this->par.getBorderLeft()) || |
---|
199 | (y<this->par.getBorderBottom()) || (y>=smallYDim+this->par.getBorderBottom()); |
---|
200 | if(isDud) newdetect[pos]=0; |
---|
201 | else newdetect[pos] = this->detectMap[smlpos]; |
---|
202 | } |
---|
203 | } |
---|
204 | delete [] this->detectMap; |
---|
205 | this->detectMap = newdetect; |
---|
206 | |
---|
207 | |
---|
208 | // Now update the positions for all the detections |
---|
209 | |
---|
210 | for(int i=0;i<this->objectList.size();i++){ |
---|
211 | for(int pix=0;pix<objectList[i].getSize();pix++){ |
---|
212 | long x = objectList[i].getX(pix); |
---|
213 | long y = objectList[i].getY(pix); |
---|
214 | objectList[i].setX(pix,x+this->par.getBorderLeft()); |
---|
215 | objectList[i].setY(pix,y+this->par.getBorderBottom()); |
---|
216 | } |
---|
217 | objectList[i].calcParams(); |
---|
218 | } |
---|
219 | |
---|
220 | } |
---|
221 | |
---|
222 | } |
---|