[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // trimImage.cc: Trim a Cube of BLANKs around the spatial edge. |
---|
| 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 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
[393] | 29 | #include <duchamp/param.hh> |
---|
| 30 | #include <duchamp/Cubes/cubes.hh> |
---|
| 31 | #include <duchamp/PixelMap/Object3D.hh> |
---|
[3] | 32 | |
---|
[258] | 33 | using namespace PixelInfo; |
---|
| 34 | |
---|
[378] | 35 | namespace duchamp |
---|
[3] | 36 | { |
---|
[86] | 37 | |
---|
[378] | 38 | void Cube::trimCube() |
---|
| 39 | { |
---|
| 40 | /** |
---|
| 41 | * If the blankPix flag has been set, this routine trims excess blank |
---|
| 42 | * pixels from the edges of the spatial image. |
---|
| 43 | * It uses as its template the first channel, assuming that its non-BLANK |
---|
| 44 | * size is representative of the rest of the channels. |
---|
| 45 | * The edges are moved in until the first non-BLANK pixel is encountered. |
---|
| 46 | * All other arrays are similarly edited, and the amount of trimming is |
---|
| 47 | * recorded. |
---|
| 48 | */ |
---|
[3] | 49 | |
---|
[378] | 50 | // if(this->par.getFlagBlankPix()) { |
---|
| 51 | if(this->par.getFlagTrim()) { |
---|
[3] | 52 | |
---|
[378] | 53 | long xdim = this->axisDim[0]; |
---|
| 54 | long ydim = this->axisDim[1]; |
---|
| 55 | long zdim = this->axisDim[2]; |
---|
| 56 | const long ZCHAN = 0; |
---|
[258] | 57 | |
---|
[378] | 58 | int left,right,top,bottom; |
---|
[3] | 59 | |
---|
[378] | 60 | for(int z = 0; z < zdim; z++){ |
---|
[3] | 61 | |
---|
[378] | 62 | for(int row=0;row<ydim;row++){ |
---|
| 63 | left=0; |
---|
| 64 | right=0; |
---|
| 65 | while((left<xdim)&& |
---|
| 66 | (this->par.isBlank(this->array[row*xdim+left+ZCHAN*xdim*ydim])) ) { |
---|
| 67 | left++; |
---|
| 68 | } |
---|
| 69 | while((right<xdim)&& |
---|
| 70 | (this->par.isBlank(this->array[row*xdim+(xdim-1-right)+ZCHAN*xdim*ydim]))){ |
---|
| 71 | right++; |
---|
| 72 | } |
---|
[3] | 73 | |
---|
[378] | 74 | if( ((z==0)&&(row==0)) || (left < this->par.getBorderLeft()) ) |
---|
| 75 | this->par.setBorderLeft(left); |
---|
| 76 | if( ((z==0)&&(row==0)) || (right < this->par.getBorderRight()) ) |
---|
| 77 | this->par.setBorderRight(right); |
---|
| 78 | |
---|
| 79 | } |
---|
[3] | 80 | |
---|
[378] | 81 | for(int col=0;col<xdim;col++){ |
---|
| 82 | bottom=0; |
---|
| 83 | top=0; |
---|
| 84 | while((bottom<ydim)&& |
---|
| 85 | (this->par.isBlank(this->array[col+bottom*xdim+ZCHAN*xdim*ydim])) ) bottom++; |
---|
[3] | 86 | |
---|
[378] | 87 | while((top<ydim)&& |
---|
| 88 | (this->par.isBlank(this->array[col+(ydim-1-top)*xdim+ZCHAN*xdim*ydim])) ) top++; |
---|
[3] | 89 | |
---|
[378] | 90 | if( ((z==0)&&(col==0)) || (bottom < this->par.getBorderBottom()) ) |
---|
| 91 | this->par.setBorderBottom(bottom); |
---|
| 92 | if( ((z==0)&&(col==0)) || (top < this->par.getBorderTop()) ) |
---|
| 93 | this->par.setBorderTop(top); |
---|
[3] | 94 | |
---|
[378] | 95 | } |
---|
| 96 | |
---|
[3] | 97 | } |
---|
| 98 | |
---|
[378] | 99 | left = this->par.getBorderLeft(); |
---|
| 100 | right = this->par.getBorderRight(); |
---|
| 101 | top = this->par.getBorderTop(); |
---|
| 102 | bottom = this->par.getBorderBottom(); |
---|
[258] | 103 | |
---|
[378] | 104 | this->axisDim[0] = xdim - left - right; |
---|
| 105 | this->axisDim[1] = ydim - bottom - top; |
---|
| 106 | this->axisDim[2] = zdim; |
---|
| 107 | this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2]; |
---|
[3] | 108 | |
---|
[378] | 109 | long oldpos,newpos; |
---|
[3] | 110 | |
---|
[378] | 111 | // Do the trimming, but only if we need to -- is there a border of Blanks? |
---|
| 112 | if((left>0)||(right>0)||(bottom>0)||(top>0)) { |
---|
[3] | 113 | |
---|
[378] | 114 | // Trim the array of pixel values |
---|
[256] | 115 | float *newarray = new float[this->numPixels]; |
---|
| 116 | for(int x = 0; x < axisDim[0]; x++){ |
---|
| 117 | for(int y = 0; y < axisDim[1]; y++){ |
---|
| 118 | for(int z = 0; z < axisDim[2]; z++){ |
---|
[285] | 119 | oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim; |
---|
| 120 | newpos = x + y*this->axisDim[0] + |
---|
| 121 | z*this->axisDim[0]*this->axisDim[1]; |
---|
[378] | 122 | newarray[newpos] = this->array[oldpos]; |
---|
[256] | 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
[378] | 126 | delete [] this->array; |
---|
| 127 | this->array = newarray; |
---|
[256] | 128 | |
---|
[378] | 129 | // Trim the array of baseline values |
---|
| 130 | if(this->par.getFlagBaseline()){ |
---|
| 131 | float *newarray = new float[this->numPixels]; |
---|
| 132 | for(int x = 0; x < axisDim[0]; x++){ |
---|
| 133 | for(int y = 0; y < axisDim[1]; y++){ |
---|
| 134 | for(int z = 0; z < axisDim[2]; z++){ |
---|
| 135 | oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim; |
---|
| 136 | newpos = x + y*this->axisDim[0] + |
---|
| 137 | z*this->axisDim[0]*this->axisDim[1]; |
---|
| 138 | newarray[newpos] = this->baseline[oldpos]; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | delete [] this->baseline; |
---|
| 143 | this->baseline = newarray; |
---|
[3] | 144 | } |
---|
| 145 | |
---|
[378] | 146 | // Trim the 2-D detection map |
---|
| 147 | short *newdetect = new short[this->axisDim[0]*this->axisDim[1]]; |
---|
[3] | 148 | for(int x = 0; x < axisDim[0]; x++){ |
---|
| 149 | for(int y = 0; y < axisDim[1]; y++){ |
---|
[378] | 150 | oldpos = (x+left) + (y+bottom)*xdim; |
---|
| 151 | newpos = x + y*this->axisDim[0]; |
---|
| 152 | newdetect[newpos] = this->detectMap[oldpos]; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | delete [] this->detectMap; |
---|
| 156 | this->detectMap = newdetect; |
---|
| 157 | |
---|
| 158 | if(this->par.getFlagATrous() || this->par.getFlagSmooth()){ |
---|
| 159 | // Trim the reconstructed array if we are going to do the |
---|
| 160 | // reconstruction or smooth the array |
---|
| 161 | float *newrecon = new float[this->numPixels]; |
---|
| 162 | for(int x = 0; x < axisDim[0]; x++){ |
---|
| 163 | for(int y = 0; y < axisDim[1]; y++){ |
---|
| 164 | for(int z = 0; z < axisDim[2]; z++){ |
---|
| 165 | oldpos = (x+left) + (y+bottom)*xdim + z*xdim*ydim; |
---|
| 166 | newpos = x + y*this->axisDim[0] + |
---|
| 167 | z*this->axisDim[0]*this->axisDim[1]; |
---|
| 168 | newrecon[newpos] = this->recon[oldpos]; |
---|
| 169 | } |
---|
[3] | 170 | } |
---|
| 171 | } |
---|
[378] | 172 | delete [] this->recon; |
---|
| 173 | this->recon = newrecon; |
---|
[3] | 174 | } |
---|
[378] | 175 | |
---|
| 176 | // Set the flag indicating trimming has taken place only if it has. |
---|
| 177 | this->par.setFlagCubeTrimmed(true); |
---|
| 178 | |
---|
[3] | 179 | } |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
[378] | 186 | void Cube::unTrimCube() |
---|
| 187 | { |
---|
| 188 | /** |
---|
| 189 | * If the cube has been trimmed by trimCube(), this task adds back the |
---|
| 190 | * BLANK pixels on the edges, so that it returns to its original size. |
---|
| 191 | * All arrays are similarly edited. |
---|
| 192 | */ |
---|
[3] | 193 | |
---|
[378] | 194 | if(this->par.getFlagCubeTrimmed()){ |
---|
[3] | 195 | |
---|
[378] | 196 | long left = this->par.getBorderLeft(); |
---|
| 197 | long right = this->par.getBorderRight(); |
---|
| 198 | long top = this->par.getBorderTop(); |
---|
| 199 | long bottom = this->par.getBorderBottom(); |
---|
[3] | 200 | |
---|
[378] | 201 | long smallXDim = this->axisDim[0]; |
---|
| 202 | long smallYDim = this->axisDim[1]; |
---|
| 203 | long smallZDim = this->axisDim[2]; |
---|
[258] | 204 | |
---|
[378] | 205 | // first correct the dimension sizes |
---|
| 206 | this->axisDim[0] = smallXDim + left + right; |
---|
| 207 | this->axisDim[1] = smallYDim + bottom + top; |
---|
| 208 | this->axisDim[2] = smallZDim; |
---|
| 209 | this->numPixels = this->axisDim[0]*this->axisDim[1]*this->axisDim[2]; |
---|
[3] | 210 | |
---|
[378] | 211 | long pos,smlpos; |
---|
| 212 | bool isDud; |
---|
[3] | 213 | |
---|
[378] | 214 | // Correct the array of pixel values |
---|
| 215 | float *newarray = new float[this->numPixels]; |
---|
[3] | 216 | for(int x = 0; x < this->axisDim[0]; x++){ |
---|
| 217 | for(int y = 0; y < this->axisDim[1]; y++){ |
---|
[258] | 218 | isDud = (x<left) || (x>=smallXDim+left) || |
---|
| 219 | (y<bottom) || (y>=smallYDim+bottom); |
---|
[378] | 220 | |
---|
[3] | 221 | for(int z = 0; z < this->axisDim[2]; z++){ |
---|
| 222 | pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1]; |
---|
[378] | 223 | smlpos = (x-left) + (y-bottom)*smallXDim + z * smallXDim * smallYDim; |
---|
| 224 | if(isDud) newarray[pos] = this->par.getBlankPixVal(); |
---|
| 225 | else newarray[pos] = this->array[smlpos]; |
---|
[3] | 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
[378] | 229 | delete [] this->array; |
---|
| 230 | this->array = newarray; |
---|
[3] | 231 | |
---|
[378] | 232 | if(this->reconExists){ |
---|
| 233 | // Correct the reconstructed/smoothed array |
---|
| 234 | float *newrecon = new float[this->numPixels]; |
---|
| 235 | for(int x = 0; x < this->axisDim[0]; x++){ |
---|
| 236 | for(int y = 0; y < this->axisDim[1]; y++){ |
---|
| 237 | isDud = (x<left) || (x>=smallXDim+left) || |
---|
| 238 | (y<bottom) || (y>=smallYDim+bottom); |
---|
| 239 | |
---|
| 240 | for(int z = 0; z < this->axisDim[2]; z++){ |
---|
| 241 | pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1]; |
---|
| 242 | smlpos = (x-left) + (y-bottom)*smallXDim |
---|
| 243 | + z * smallXDim * smallYDim; |
---|
| 244 | if(isDud) newrecon[pos] = this->par.getBlankPixVal(); |
---|
| 245 | else newrecon[pos] = this->recon[smlpos]; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | delete [] this->recon; |
---|
| 250 | this->recon = newrecon; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | // Correct the array of baseline values |
---|
| 254 | if(this->par.getFlagBaseline()){ |
---|
| 255 | float *newbase = new float[this->numPixels]; |
---|
| 256 | for(int x = 0; x < this->axisDim[0]; x++){ |
---|
| 257 | for(int y = 0; y < this->axisDim[1]; y++){ |
---|
| 258 | isDud = (x<left) || (x>=smallXDim+left) || |
---|
| 259 | (y<bottom) || (y>=smallYDim+bottom); |
---|
| 260 | |
---|
| 261 | for(int z = 0; z < this->axisDim[2]; z++){ |
---|
| 262 | pos = x + y*this->axisDim[0] + z*this->axisDim[0]*this->axisDim[1]; |
---|
| 263 | smlpos = (x-left) + (y-bottom)*smallXDim + z*smallXDim*smallYDim; |
---|
| 264 | if(isDud) newbase[pos] = this->par.getBlankPixVal(); |
---|
| 265 | else newbase[pos] = this->baseline[smlpos]; |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | delete [] this->baseline; |
---|
| 270 | this->baseline = newbase; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | // Correct the 2-D detection map |
---|
| 274 | short *newdetect = new short[this->axisDim[0]*this->axisDim[1]]; |
---|
[269] | 275 | for(int x = 0; x < this->axisDim[0]; x++){ |
---|
| 276 | for(int y = 0; y < this->axisDim[1]; y++){ |
---|
[378] | 277 | pos = x + y*this->axisDim[0]; |
---|
| 278 | smlpos = (x-left) + (y-bottom)*smallXDim; |
---|
[269] | 279 | isDud = (x<left) || (x>=smallXDim+left) || |
---|
| 280 | (y<bottom) || (y>=smallYDim+bottom); |
---|
[378] | 281 | if(isDud) newdetect[pos]=0; |
---|
| 282 | else newdetect[pos] = this->detectMap[smlpos]; |
---|
[269] | 283 | } |
---|
| 284 | } |
---|
[378] | 285 | delete [] this->detectMap; |
---|
| 286 | this->detectMap = newdetect; |
---|
[269] | 287 | |
---|
[378] | 288 | |
---|
| 289 | // Now update the positions for all the detections |
---|
| 290 | |
---|
| 291 | for(int i=0;i<this->objectList->size();i++){ |
---|
| 292 | this->objectList->at(i).pixels().addOffsets(left,bottom,0); |
---|
| 293 | // objectList[i].calcParams(this->array,this->axisDim); |
---|
[3] | 294 | } |
---|
| 295 | |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | } |
---|