source: trunk/src/Detection/ObjectGrower.cc @ 1242

Last change on this file since 1242 was 1242, checked in by MatthewWhiting, 11 years ago

Tickets #193 & #195 - Implementing channel flagging. Still a lot of commented-out code, plus the MW code is still present (although not used anywhere). Also making use of the new plotting classes.

File size: 9.8 KB
Line 
1// -----------------------------------------------------------------------
2// ObjectGrower.cc: Implementation of the object growing functions
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
29#include <iostream>
30#include <vector>
31#include <duchamp/duchamp.hh>
32#include <duchamp/Detection/ObjectGrower.hh>
33#include <duchamp/Detection/detection.hh>
34#include <duchamp/Cubes/cubes.hh>
35#include <duchamp/Utils/Statistics.hh>
36#include <duchamp/PixelMap/Voxel.hh>
37
38
39namespace duchamp {
40
41  ObjectGrower::ObjectGrower() {
42  }
43
44  ObjectGrower::ObjectGrower(ObjectGrower &o)
45  {
46    this->operator=(o);
47  }
48
49  ObjectGrower& ObjectGrower::operator=(const ObjectGrower &o)
50  {
51    if(this == &o) return *this;
52    this->itsFlagArray = o.itsFlagArray;
53    this->itsArrayDim = o.itsArrayDim;
54    this->itsGrowthStats = o.itsGrowthStats;
55    this->itsSpatialThresh = o.itsSpatialThresh;
56    this->itsVelocityThresh = o.itsVelocityThresh;
57    this->itsFluxArray = o.itsFluxArray;
58    return *this;
59  }
60
61  void ObjectGrower::define( Cube *theCube )
62  {
63    /// @details This copies all necessary information from the Cube
64    /// and its parameters & statistics. It also defines the array of
65    /// pixel flags, which involves looking at each object to assign
66    /// them as detected, all blank & "milky-way" pixels to assign
67    /// them appropriately, and all others to "available". It is only
68    /// the latter that will be considered in the growing function.
69    /// @param theCube A pointer to a duchamp::Cube
70
71    this->itsGrowthStats = Statistics::StatsContainer<float>(theCube->stats());
72    if(theCube->pars().getFlagUserGrowthThreshold())
73      this->itsGrowthStats.setThreshold(theCube->pars().getGrowthThreshold());
74    else
75      this->itsGrowthStats.setThresholdSNR(theCube->pars().getGrowthCut());   
76    this->itsGrowthStats.setUseFDR(false);
77
78    if(theCube->isRecon()) this->itsFluxArray = theCube->getRecon();
79    else this->itsFluxArray = theCube->getArray();
80
81    this->itsArrayDim = std::vector<size_t>(3);
82    this->itsArrayDim[0]=theCube->getDimX();
83    this->itsArrayDim[1]=theCube->getDimY();
84    this->itsArrayDim[2]=theCube->getDimZ();
85    size_t spatsize=this->itsArrayDim[0]*this->itsArrayDim[1];
86    size_t fullsize=spatsize*this->itsArrayDim[2];
87
88    if(theCube->pars().getFlagAdjacent())
89      this->itsSpatialThresh = 1;
90    else
91      this->itsSpatialThresh = int(theCube->pars().getThreshS());
92    this->itsVelocityThresh = int(theCube->pars().getThreshV());
93
94    this->itsFlagArray = std::vector<STATE>(fullsize,AVAILABLE);
95
96    for(size_t o=0;o<theCube->getNumObj();o++){
97      std::vector<Voxel> voxlist = theCube->getObject(o).getPixelSet();
98      for(size_t i=0; i<voxlist.size(); i++){
99        size_t pos = voxlist[i].getX() + voxlist[i].getY()*this->itsArrayDim[0] + voxlist[i].getZ()*spatsize;
100        this->itsFlagArray[pos] = DETECTED;
101      }
102    }
103
104    // if(theCube->pars().getFlagMW()){
105    //   int minz=std::max(0,theCube->pars().getMinMW());
106    //   int maxz=std::min(int(theCube->getDimZ())-1,theCube->pars().getMaxMW());
107    //   if(minz<maxz){
108    //  for(size_t i=minz*spatsize;i<(maxz+1)*spatsize;i++) this->itsFlagArray[i]=MW;
109    //   }
110    // }
111
112    std::vector<int> flaggedChans=theCube->pars().getFlaggedChannels();
113    for(size_t iz=0; iz<flaggedChans.size();iz++) {
114        int z=flaggedChans[iz];
115        for(size_t i=0;i<spatsize;i++) this->itsFlagArray[i+z*spatsize]=MW;
116    }
117
118    for(size_t i=0;i<fullsize;i++)
119      if(theCube->isBlank(i)) this->itsFlagArray[i]=BLANK;
120
121  }
122
123
124  void ObjectGrower::updateDetectMap(short *map)
125  {
126
127    int numNondegDim=0;
128    for(int i=0;i<3;i++) if(this->itsArrayDim[i]>1) numNondegDim++;
129
130    if(numNondegDim>1){
131      size_t spatsize=this->itsArrayDim[0]*this->itsArrayDim[1];
132      for(size_t xy=0;xy<spatsize;xy++){
133        short ct=0;
134        for(size_t z=0;z<this->itsArrayDim[2];z++){
135          if(this->itsFlagArray[xy+z*spatsize] == DETECTED) ct++;
136        }
137        map[xy]=ct;
138      }
139    }
140    else{
141      for(size_t z=0;z<this->itsArrayDim[2];z++){
142        map[z] = (this->itsFlagArray[z] == DETECTED) ? 1 : 0;
143      }
144    }
145
146  }
147
148
149  void ObjectGrower::grow(Detection *theObject)
150  {
151    /// @details This function grows the provided object out to the
152    /// secondary threshold provided in itsGrowthStats. For each pixel
153    /// in an object, all surrounding pixels are considered and, if
154    /// their flag is AVAILABLE, their flux is examined. If it's above
155    /// the threshold, that pixel is added to the list to be looked at
156    /// and their flag is changed to DETECTED.
157    /// @param theObject The duchamp::Detection object to be grown. It
158    /// is returned with new pixels in place. Only the basic
159    /// parameters that belong to PixelInfo::Object3D are
160    /// recalculated.
161
162    size_t spatsize=this->itsArrayDim[0]*this->itsArrayDim[1];
163    long zero = 0;
164    std::vector<Voxel> voxlist = theObject->getPixelSet();
165    size_t origSize = voxlist.size();
166    long xpt,ypt,zpt;
167    long xmin,xmax,ymin,ymax,zmin,zmax,x,y,z;
168    size_t pos;
169    for(size_t i=0; i<voxlist.size(); i++){
170     
171      // std::vector<Voxel> newvox = this->growFromPixel(voxlist[i]);
172      // for(size_t v=0;v<newvox.size();v++) voxlist.push_back(newvox[v]);
173
174      xpt=voxlist[i].getX();
175      ypt=voxlist[i].getY();
176      zpt=voxlist[i].getZ();
177     
178      xmin = size_t(std::max(xpt - this->itsSpatialThresh, zero));
179      xmax = size_t(std::min(xpt + this->itsSpatialThresh, long(this->itsArrayDim[0])-1));
180      ymin = size_t(std::max(ypt - this->itsSpatialThresh, zero));
181      ymax = size_t(std::min(ypt + this->itsSpatialThresh, long(this->itsArrayDim[1])-1));
182      zmin = size_t(std::max(zpt - this->itsVelocityThresh, zero));
183      zmax = size_t(std::min(zpt + this->itsVelocityThresh, long(this->itsArrayDim[2])-1));
184     
185      //loop over surrounding pixels.
186      for(x=xmin; x<=xmax; x++){
187        for(y=ymin; y<=ymax; y++){
188          for(z=zmin; z<=zmax; z++){
189
190            pos=x+y*this->itsArrayDim[0]+z*spatsize;
191            if( ((x!=xpt) || (y!=ypt) || (z!=zpt))
192                && this->itsFlagArray[pos]==AVAILABLE ) {
193
194              if(this->itsGrowthStats.isDetection(this->itsFluxArray[pos])){
195                this->itsFlagArray[pos]=DETECTED;
196                voxlist.push_back(Voxel(x,y,z));
197              }
198            }
199
200          } //end of z loop
201        } // end of y loop
202      } // end of x loop
203
204    } // end of i loop (voxels)
205
206    // Add in new pixels to the Detection
207    for(size_t i=origSize; i<voxlist.size(); i++){
208      theObject->addPixel(voxlist[i]);
209    }
210   
211
212  }
213
214
215  std::vector<Voxel> ObjectGrower::growFromPixel(Voxel &vox)
216  {
217
218    std::vector<Voxel> newVoxels;
219    // std::cerr << vox << "\n";
220    long xpt=vox.getX();
221    long ypt=vox.getY();
222    long zpt=vox.getZ();
223    size_t spatsize=this->itsArrayDim[0]*this->itsArrayDim[1];
224    long zero = 0;
225    // std::cerr << "--> " << xpt << " " << ypt << " " << zpt << "\n";
226
227    int xmin = std::max(xpt - this->itsSpatialThresh, zero);
228    int xmax = std::min(xpt + this->itsSpatialThresh, long(this->itsArrayDim[0])-1);
229    int ymin = std::max(ypt - this->itsSpatialThresh, zero);
230    int ymax = std::min(ypt + this->itsSpatialThresh, long(this->itsArrayDim[1])-1);
231    int zmin = std::max(zpt - this->itsVelocityThresh, zero);
232    int zmax = std::min(zpt + this->itsVelocityThresh, long(this->itsArrayDim[2])-1);
233     
234    // std::cerr << xmin << " " << xmax << "  " << ymin << " " << ymax << "  " << zmin << " " << zmax << "\n";
235    //loop over surrounding pixels.
236    size_t pos;
237    Voxel nvox;
238    std::vector<Voxel> morevox;
239    for(int x=xmin; x<=xmax; x++){
240      for(int y=ymin; y<=ymax; y++){
241        for(int z=zmin; z<=zmax; z++){
242
243          pos=x+y*this->itsArrayDim[0]+z*spatsize;
244          if( ((x!=xpt) || (y!=ypt) || (z!=zpt))
245              && this->itsFlagArray[pos]==AVAILABLE ) {
246
247            if(this->itsGrowthStats.isDetection(this->itsFluxArray[pos])){
248              this->itsFlagArray[pos]=DETECTED;
249              nvox.setXYZF(x,y,z,this->itsFluxArray[pos]);
250              newVoxels.push_back(nvox);
251              // std::cerr << x << " " << y << " " << z << " " << this->itsFluxArray[pos] << "  =  " << nvox << "\n";
252              // morevox = this->growFromPixel(nvox);
253              // if(morevox.size()>0)
254              //        for(size_t v=0;v<morevox.size();v++) newVoxels.push_back(morevox[v]);
255             
256            }
257          }
258
259        } //end of z loop
260      } // end of y loop
261    } // end of x loop
262
263    std::vector<Voxel>::iterator v,v2;
264    for(v=newVoxels.begin();v<newVoxels.end();v++) {
265      std::vector<Voxel> morevox = this->growFromPixel(*v);
266      for(v2=morevox.begin();v2<morevox.end();v2++)
267        newVoxels.push_back(*v2);
268    }
269
270    return newVoxels;
271
272  }
273
274  // void ObjectGrower::resetDetectionFlags()
275  // {
276  //   for(size_t i=0;i<itsFlagArray.size();i++)
277  //     if(itsFlagArray[i]==DETECTED) itsFlagArray[i] = AVAILABLE;
278  // }
279
280  // void ObjectGrower::growInwardsFromEdge()
281  // {
282   
283
284
285  // }
286
287}
Note: See TracBrowser for help on using the repository browser.