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