source: branches/pixelmap-refactor-branch/src/Detection/growObject.cc @ 1441

Last change on this file since 1441 was 541, checked in by MatthewWhiting, 15 years ago

Changing all calls of uint to unsigned int, as there are sometimes compilers that don't know about that typedef. Also added an include call for stdlib.h to fitsHeader.cc so that it knows about calloc.

File size: 5.3 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    ///  @details
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    if(cube.pars().getFlagGrowth()){
63
64      bool doGrowing;
65      if(cube.pars().getFlagUserGrowthThreshold())
66        doGrowing = cube.pars().getGrowthThreshold()<cube.pars().getThreshold();
67      else
68        doGrowing = cube.pars().getGrowthCut()<cube.pars().getCut();
69
70      if(doGrowing){
71
72        vector <bool> isInObj(cube.getSize(),false);
73        bool flagAdj = cube.pars().getFlagAdjacent();
74        int threshS = int(cube.pars().getThreshS());
75        if(flagAdj) threshS = 1;
76        int threshV = int(cube.pars().getThreshV());
77
78        vector<Voxel> voxlist = object.getPixelSet();
79        int origSize = voxlist.size();
80        long zero = 0;
81
82        for(unsigned int i=0;i<voxlist.size();i++) {
83          long pos = voxlist[i].getX() + voxlist[i].getY()*cube.getDimX() +
84            voxlist[i].getZ()*cube.getDimX()*cube.getDimY();
85          isInObj[pos] = true;
86        }
87 
88        StatsContainer<float> growthStats(cube.stats());
89
90        if(cube.pars().getFlagUserGrowthThreshold())
91          growthStats.setThreshold(cube.pars().getGrowthThreshold());
92        else
93          growthStats.setThresholdSNR(cube.pars().getGrowthCut());
94
95        growthStats.setUseFDR(false);
96
97        //    for(int pix=0; pix<object.getSize(); pix++){ // for each pixel in the object
98        for(unsigned int pix=0; pix<voxlist.size(); pix++){ // for each pixel in the object
99
100          int xmin = std::max(voxlist[pix].getX() - threshS, zero);
101          int xmax = std::min(voxlist[pix].getX() + threshS, cube.getDimX()-1);
102          int ymin = std::max(voxlist[pix].getY() - threshS, zero);
103          int ymax = std::min(voxlist[pix].getY() + threshS, cube.getDimY()-1);
104          int zmin = std::max(voxlist[pix].getZ() - threshV, zero);
105          int zmax = std::min(voxlist[pix].getZ() + threshV, cube.getDimZ()-1);
106
107          //loop over surrounding pixels.
108          for(int x=xmin; x<=xmax; x++){
109            for(int y=ymin; y<=ymax; y++){
110              for(int z=zmin; z<=zmax; z++){
111
112                if((x!=voxlist[pix].getX())||(y!=voxlist[pix].getY())||(z!=voxlist[pix].getZ())){
113                  // ignore when the current object pixel
114
115                  long pos = x + y * cube.getDimX() + z * cube.getDimX() * cube.getDimY();
116
117                  if(!isInObj[pos] && // pixel not already in object?
118                     !cube.isBlank(x,y,z)   &&   // pixel not BLANK?
119                     !cube.pars().isInMW(z)       &&   // pixel not MW?
120                     (flagAdj || hypot(x-voxlist[pix].getX(),y-voxlist[pix].getY())<threshS)   ){ // pixel not too far away?
121           
122                    float flux;
123                    if(cube.isRecon()) flux = cube.getReconValue(x,y,z);
124                    else               flux = cube.getPixValue(x,y,z);
125
126                    Voxel pixnew(x,y,z,flux);
127                   
128                    if(  growthStats.isDetection(flux) ){
129                      isInObj[pos] = true;
130                      voxlist.push_back(pixnew);
131                    } // end of if
132
133                  } // end of if clause regarding position OK
134
135                } // end of if clause regarding position not same
136
137              } // end of z loop
138            } // end of y loop
139          } // end of x loop
140     
141        } // end of pix loop
142
143
144        // Add in new pixels to the Detection
145        for(unsigned int i=origSize; i<voxlist.size(); i++){
146          object.addPixel(voxlist[i]);
147        }
148     
149     
150        object.calcFluxes(cube.getArray(), cube.getDimArray());
151
152        isInObj.clear();
153
154      }
155
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.