source: trunk/src/Detection/growObject.cc @ 525

Last change on this file since 525 was 504, checked in by MatthewWhiting, 16 years ago

Improving the way the different growth thresholds are dealt with in the growing function.

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