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 | // ----------------------------------------------------------------------- |
---|
28 | #include <iostream> |
---|
29 | #include <iomanip> |
---|
30 | #include <vector> |
---|
31 | #include <duchamp/Cubes/cubeUtils.hh> |
---|
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> |
---|
37 | |
---|
38 | using namespace PixelInfo; |
---|
39 | using namespace Statistics; |
---|
40 | |
---|
41 | namespace duchamp |
---|
42 | { |
---|
43 | |
---|
44 | void growObject(Detection &object, Cube &cube) |
---|
45 | { |
---|
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. |
---|
58 | |
---|
59 | if(cube.pars().getFlagGrowth()){ |
---|
60 | |
---|
61 | bool doGrowing; |
---|
62 | if(cube.pars().getFlagUserGrowthThreshold()) |
---|
63 | doGrowing = cube.pars().getGrowthThreshold()<cube.pars().getThreshold(); |
---|
64 | else |
---|
65 | doGrowing = cube.pars().getGrowthCut()<cube.pars().getCut(); |
---|
66 | |
---|
67 | if(doGrowing){ |
---|
68 | |
---|
69 | std::vector <bool> isInObj(cube.getSize(),false); |
---|
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 | |
---|
75 | std::vector<Voxel> voxlist = object.getPixelSet(); |
---|
76 | int origSize = voxlist.size(); |
---|
77 | long zero = 0; |
---|
78 | |
---|
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(); |
---|
86 | isInObj[pos] = true; |
---|
87 | } |
---|
88 | |
---|
89 | StatsContainer<float> growthStats(cube.stats()); |
---|
90 | |
---|
91 | if(cube.pars().getFlagUserGrowthThreshold()) |
---|
92 | growthStats.setThreshold(cube.pars().getGrowthThreshold()); |
---|
93 | else |
---|
94 | growthStats.setThresholdSNR(cube.pars().getGrowthCut()); |
---|
95 | |
---|
96 | growthStats.setUseFDR(false); |
---|
97 | |
---|
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 |
---|
100 | |
---|
101 | long xpt=voxlist[i].getX(); |
---|
102 | long ypt=voxlist[i].getY(); |
---|
103 | long zpt=voxlist[i].getZ(); |
---|
104 | |
---|
105 | int xmin = std::max(xpt - threshS, zero); |
---|
106 | int xmax = std::min(xpt + threshS, cube.getDimX()-1); |
---|
107 | int ymin = std::max(ypt - threshS, zero); |
---|
108 | int ymax = std::min(ypt + threshS, cube.getDimY()-1); |
---|
109 | int zmin = std::max(zpt - threshV, zero); |
---|
110 | int zmax = std::min(zpt + threshV, cube.getDimZ()-1); |
---|
111 | |
---|
112 | //loop over surrounding pixels. |
---|
113 | for(int x=xmin; x<=xmax; x++){ |
---|
114 | for(int y=ymin; y<=ymax; y++){ |
---|
115 | for(int z=zmin; z<=zmax; z++){ |
---|
116 | |
---|
117 | if((x!=xpt)||(y!=ypt)||(z!=zpt)){ |
---|
118 | // ignore when the current object pixel |
---|
119 | |
---|
120 | long pos = x + y * cube.getDimX() + z * cube.getDimX() * cube.getDimY(); |
---|
121 | |
---|
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? |
---|
125 | (flagAdj || hypot(x-xpt,y-ypt)<threshS) && // pixel not too far away spatially? |
---|
126 | (fabs(z-zpt)<=threshV) ){ //pixel not too far away spectrally? |
---|
127 | |
---|
128 | float flux; |
---|
129 | if(cube.isRecon()) flux = cube.getReconValue(x,y,z); |
---|
130 | else flux = cube.getPixValue(x,y,z); |
---|
131 | |
---|
132 | Voxel pixnew(x,y,z,flux); |
---|
133 | |
---|
134 | if( growthStats.isDetection(flux) ){ |
---|
135 | isInObj[pos] = true; |
---|
136 | voxlist.push_back(pixnew); |
---|
137 | } // end of if |
---|
138 | |
---|
139 | } // end of if clause regarding position OK |
---|
140 | |
---|
141 | } // end of if clause regarding position not same |
---|
142 | |
---|
143 | } // end of z loop |
---|
144 | } // end of y loop |
---|
145 | } // end of x loop |
---|
146 | |
---|
147 | } // end of pix loop |
---|
148 | |
---|
149 | |
---|
150 | // Add in new pixels to the Detection |
---|
151 | for(size_t i=origSize; i<voxlist.size(); i++){ |
---|
152 | object.addPixel(voxlist[i]); |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | object.calcFluxes(cube.getArray(), cube.getDimArray()); |
---|
157 | |
---|
158 | isInObj.clear(); |
---|
159 | |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|