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

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

Fixing bugs in the growing function, and changing the default growth depth

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