[300] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // growObject.cc: Grow a detected object down to a lower flux threshold. |
---|
| 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> |
---|
| 29 | #include <iomanip> |
---|
| 30 | #include <vector> |
---|
[463] | 31 | #include <duchamp/Cubes/cubeUtils.hh> |
---|
[393] | 32 | #include <duchamp/Cubes/cubes.hh> |
---|
| 33 | #include <duchamp/Utils/utils.hh> |
---|
| 34 | #include <duchamp/Utils/Statistics.hh> |
---|
| 35 | #include <duchamp/PixelMap/Voxel.hh> |
---|
| 36 | #include <duchamp/Detection/detection.hh> |
---|
[3] | 37 | |
---|
[258] | 38 | using namespace PixelInfo; |
---|
[274] | 39 | using namespace Statistics; |
---|
[258] | 40 | |
---|
[378] | 41 | namespace duchamp |
---|
[3] | 42 | { |
---|
| 43 | |
---|
[378] | 44 | void growObject(Detection &object, Cube &cube) |
---|
| 45 | { |
---|
[528] | 46 | /// @details |
---|
| 47 | /// A function to grow an object (given by the Detection) by |
---|
| 48 | /// including neighbouring voxels out to some lower threshold than |
---|
| 49 | /// what was previously used in the detection. |
---|
| 50 | /// |
---|
| 51 | /// Each pixel has each of its neighbours examined, and if one of |
---|
| 52 | /// them is not in the object but above the growth threshold, it |
---|
| 53 | /// is added to the object. |
---|
| 54 | /// |
---|
| 55 | /// \param object Object to be grown. |
---|
| 56 | /// \param cube Necessary to see both the Param list, containing |
---|
| 57 | /// the growth threshold, and the actual array of pixel fluxes. |
---|
[3] | 58 | |
---|
[504] | 59 | if(cube.pars().getFlagGrowth()){ |
---|
[378] | 60 | |
---|
[504] | 61 | bool doGrowing; |
---|
| 62 | if(cube.pars().getFlagUserGrowthThreshold()) |
---|
[539] | 63 | doGrowing = cube.pars().getGrowthThreshold()<cube.pars().getThreshold(); |
---|
[504] | 64 | else |
---|
| 65 | doGrowing = cube.pars().getGrowthCut()<cube.pars().getCut(); |
---|
[190] | 66 | |
---|
[504] | 67 | if(doGrowing){ |
---|
[458] | 68 | |
---|
[630] | 69 | std::vector <bool> isInObj(cube.getSize(),false); |
---|
[504] | 70 | bool flagAdj = cube.pars().getFlagAdjacent(); |
---|
| 71 | int threshS = int(cube.pars().getThreshS()); |
---|
| 72 | if(flagAdj) threshS = 1; |
---|
| 73 | int threshV = int(cube.pars().getThreshV()); |
---|
| 74 | |
---|
[630] | 75 | std::vector<Voxel> voxlist = object.getPixelSet(); |
---|
[504] | 76 | int origSize = voxlist.size(); |
---|
| 77 | long zero = 0; |
---|
| 78 | |
---|
[631] | 79 | // std::vector<Voxel>::iterator vox; |
---|
| 80 | // for(vox=voxlist.begin();vox<voxlist.end();vox++){ |
---|
| 81 | // long pos = vox->getX() + vox->getY()*cube.getDimX() + |
---|
| 82 | // vox->getZ()*cube.getDimX()*cube.getDimY(); |
---|
| 83 | for(size_t i=0; i<voxlist.size(); i++){ |
---|
| 84 | long pos = voxlist[i].getX() + voxlist[i].getY()*cube.getDimX() + |
---|
| 85 | voxlist[i].getZ()*cube.getDimX()*cube.getDimY(); |
---|
[504] | 86 | isInObj[pos] = true; |
---|
| 87 | } |
---|
[190] | 88 | |
---|
[504] | 89 | StatsContainer<float> growthStats(cube.stats()); |
---|
[3] | 90 | |
---|
[504] | 91 | if(cube.pars().getFlagUserGrowthThreshold()) |
---|
| 92 | growthStats.setThreshold(cube.pars().getGrowthThreshold()); |
---|
| 93 | else |
---|
| 94 | growthStats.setThresholdSNR(cube.pars().getGrowthCut()); |
---|
[461] | 95 | |
---|
[504] | 96 | growthStats.setUseFDR(false); |
---|
[3] | 97 | |
---|
[631] | 98 | for(size_t i=0; i<voxlist.size(); i++){ |
---|
| 99 | // for(vox=voxlist.begin();vox<voxlist.end();vox++){ // for each pixel in the object |
---|
[3] | 100 | |
---|
[631] | 101 | long xpt=voxlist[i].getX(); |
---|
| 102 | long ypt=voxlist[i].getY(); |
---|
| 103 | long zpt=voxlist[i].getZ(); |
---|
[458] | 104 | |
---|
[884] | 105 | size_t xmin = std::max(xpt - threshS, zero); |
---|
| 106 | size_t xmax = std::min(xpt + threshS, long(cube.getDimX()-1)); |
---|
| 107 | size_t ymin = std::max(ypt - threshS, zero); |
---|
| 108 | size_t ymax = std::min(ypt + threshS, long(cube.getDimY()-1)); |
---|
| 109 | size_t zmin = std::max(zpt - threshV, zero); |
---|
| 110 | size_t zmax = std::min(zpt + threshV, long(cube.getDimZ()-1)); |
---|
[631] | 111 | |
---|
[504] | 112 | //loop over surrounding pixels. |
---|
[884] | 113 | for(size_t x=xmin; x<=xmax; x++){ |
---|
| 114 | for(size_t y=ymin; y<=ymax; y++){ |
---|
| 115 | for(size_t z=zmin; z<=zmax; z++){ |
---|
[458] | 116 | |
---|
[631] | 117 | if((x!=xpt)||(y!=ypt)||(z!=zpt)){ |
---|
[504] | 118 | // ignore when the current object pixel |
---|
[458] | 119 | |
---|
[884] | 120 | size_t pos = x + y * cube.getDimX() + z * cube.getDimX() * cube.getDimY(); |
---|
[458] | 121 | |
---|
[504] | 122 | if(!isInObj[pos] && // pixel not already in object? |
---|
| 123 | !cube.isBlank(x,y,z) && // pixel not BLANK? |
---|
| 124 | !cube.pars().isInMW(z) && // pixel not MW? |
---|
[689] | 125 | (flagAdj || hypot(x-xpt,y-ypt)<threshS) && // pixel not too far away spatially? |
---|
[690] | 126 | (fabs(z-zpt)<=threshV) ){ //pixel not too far away spectrally? |
---|
[3] | 127 | |
---|
[504] | 128 | float flux; |
---|
| 129 | if(cube.isRecon()) flux = cube.getReconValue(x,y,z); |
---|
| 130 | else flux = cube.getPixValue(x,y,z); |
---|
[190] | 131 | |
---|
[504] | 132 | Voxel pixnew(x,y,z,flux); |
---|
[539] | 133 | |
---|
[504] | 134 | if( growthStats.isDetection(flux) ){ |
---|
| 135 | isInObj[pos] = true; |
---|
| 136 | voxlist.push_back(pixnew); |
---|
| 137 | } // end of if |
---|
[86] | 138 | |
---|
[504] | 139 | } // end of if clause regarding position OK |
---|
[86] | 140 | |
---|
[504] | 141 | } // end of if clause regarding position not same |
---|
[86] | 142 | |
---|
[504] | 143 | } // end of z loop |
---|
| 144 | } // end of y loop |
---|
| 145 | } // end of x loop |
---|
[86] | 146 | |
---|
[504] | 147 | } // end of pix loop |
---|
[86] | 148 | |
---|
| 149 | |
---|
[504] | 150 | // Add in new pixels to the Detection |
---|
[623] | 151 | for(size_t i=origSize; i<voxlist.size(); i++){ |
---|
[504] | 152 | object.addPixel(voxlist[i]); |
---|
| 153 | } |
---|
[458] | 154 | |
---|
| 155 | |
---|
[504] | 156 | object.calcFluxes(cube.getArray(), cube.getDimArray()); |
---|
[86] | 157 | |
---|
[504] | 158 | isInObj.clear(); |
---|
[458] | 159 | |
---|
[504] | 160 | } |
---|
| 161 | |
---|
[458] | 162 | } |
---|
[378] | 163 | } |
---|
[86] | 164 | } |
---|