source: tags/release-1.1/src/Detection/lutz_detect.cc @ 1391

Last change on this file since 1391 was 301, checked in by Matthew Whiting, 17 years ago

Mostly adding the distribution text to the start of files, with a few additional comments added too.

File size: 8.9 KB
Line 
1// -----------------------------------------------------------------------
2// lutz_detect.cc: Search a 2D Image for objects.
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 <Cubes/cubes.hh>
29#include <PixelMap/Voxel.hh>
30#include <PixelMap/Object2D.hh>
31#include <vector>
32
33using namespace PixelInfo;
34
35/** Enumeration to describe status of a pixel or a detected object */
36enum STATUS { NONOBJECT, ///< Pixel not above the threshold.
37              OBJECT,    ///< Pixel above the threshold.
38              COMPLETE,  ///< Object is complete
39              INCOMPLETE ///< Object not yet complete
40};
41
42/** Simple enumeration to enable obvious reference to current or prior
43    row. */
44enum ROW { PRIOR=0, CURRENT};
45
46/** A couple of null values: the default starting value for markers,
47    and one used for debugging. */
48enum NULLS { NULLSTART=-1, ///< Default start/end value, obviously
49                           ///   outside valid range.
50             NULLMARKER=45 ///< ASCII 45 = '-', which eases printing
51                           ///   for debugging purposes
52};
53
54//---------------------------
55/**
56 * A simple class local to lutz_detect.cc to help manage detected
57 * objects.
58 *
59 * Keeps a track of a detection, as well as the start and finish
60 * locations of the detection on the current row.
61 */
62class FoundObject{
63public:
64  /** Basic constructor, setting the start & end to NULL values. */
65  FoundObject(){start=NULLSTART; end=NULLSTART;};
66  int start; ///< Pixel on the current row where the detection starts.
67  int end;   ///< Pixel on the current row where the detection finishes.
68  Object2D info; ///< Collection of detected pixels.
69};
70//---------------------------
71
72std::vector<Object2D> Image::lutz_detect()
73{
74/**
75 *  A detection algorithm for 2-dimensional images based on that of
76 *  Lutz (1980).
77 * 
78 *  The image is raster-scanned, and searched row-by-row. Objects
79 *  detected in each row are compared to objects in subsequent rows,
80 *  and combined if they are connected (in an 8-fold sense).
81 *
82 *  Note that "detected" here means according to the
83 *  Image::isDetection(long,long) function.
84 *
85 *  Upon return, the detected objects are stored in the
86 *  Image::objectList vector.
87 *
88 */
89
90  // Allocate necessary arrays.
91  std::vector<Object2D> outputlist;
92  STATUS *status  = new STATUS[2];
93  Object2D *store = new Object2D[this->axisDim[0]+1];
94  char *marker    = new char[this->axisDim[0]+1];
95  for(int i=0; i<(this->axisDim[0]+1); i++) marker[i] = NULLMARKER;
96  std::vector<FoundObject> oS;
97  std::vector<STATUS>      psS;
98
99  Pixel pix;
100
101  for(int posY=0;posY<(this->axisDim[1]+1);posY++){
102    // Loop over each row -- consider rows one at a time
103   
104    status[PRIOR] = COMPLETE;
105    status[CURRENT] = NONOBJECT;
106
107    for(int posX=0;posX<(this->axisDim[0]+1);posX++){
108      // Now the loop for a given row, looking at each column individually.
109
110      char currentMarker = marker[posX];
111      marker[posX] = NULLMARKER;
112
113      bool isObject;
114      if((posX<this->axisDim[0])&&(posY<this->axisDim[1])){
115        // if we are in the original image
116        isObject = this->isDetection(posX,posY);
117      }
118      else isObject = false;
119      // else we're in the padding row/col and isObject=FALSE;
120
121      //
122      // ------------------------------ START SEGMENT ------------------------
123      // If the current pixel is object and the previous pixel is not, then
124      // start a new segment.
125      // If the pixel touches an object on the prior row, the marker is either
126      // an S or an s, depending on whether the object has started yet.
127      // If it doesn't touch a prior object, this is the start of a completly
128      // new object on this row.
129      //
130      if ( (isObject) && (status[CURRENT] != OBJECT) ){
131
132        status[CURRENT] = OBJECT;
133        if(status[PRIOR] == OBJECT){
134         
135          if(oS.back().start==NULLSTART){
136            marker[posX] = 'S';
137            oS.back().start = posX;
138          }
139          else  marker[posX] = 's';
140        }
141        else{
142          psS.push_back(status[PRIOR]);  //PUSH PS onto PSSTACK;
143          marker[posX] = 'S';
144          oS.resize(oS.size()+1);        //PUSH OBSTACK;
145          oS.back().start = posX;
146
147          status[PRIOR] = COMPLETE;
148        }
149      }
150
151      //
152      // ------------------------------ PROCESS MARKER -----------------------
153      // If the current marker is not blank, then we need to deal with it.
154      // Four cases:
155      //   S --> start of object on prior row. Push priorStatus onto PSSTACK
156      //         and set priorStatus to OBJECT
157      //   s --> start of a secondary segment of object on prior row.
158      //         If current object joined, pop PSSTACK and join the objects.
159      //         Set priorStatus to OBJECT.
160      //   f --> end of a secondary segment of object on prior row.
161      //         Set priorStatus to INCOMPLETE.
162      //   F --> end of object on prior row. If no more of the object is to
163      //          come (priorStatus=COMPLETE), then finish it and output data.
164      //         Add to list, but only if it has more than the minimum number
165      //           of pixels.
166      //
167      if(currentMarker != NULLMARKER){
168
169        if(currentMarker == 'S'){
170          psS.push_back(status[PRIOR]);      // PUSH PS onto PSSTACK
171          if(status[CURRENT] == NONOBJECT){
172            psS.push_back(COMPLETE);         // PUSH COMPLETE ONTO PSSTACK;
173            oS.resize(oS.size()+1);          // PUSH OBSTACK;
174            oS.back().info = store[posX];
175          }
176          else oS.back().info = oS.back().info + store[posX];
177         
178          status[PRIOR] = OBJECT;
179        }
180
181        /*---------*/
182        if(currentMarker == 's'){
183
184          if( (status[CURRENT] == OBJECT) && (status[PRIOR] == COMPLETE) ){
185            status[PRIOR] = psS.back();
186            psS.pop_back();                   //POP PSSTACK ONTO PS
187
188//          oS.at(oS.size()-2).info.addAnObject( oS.back().info );
189//          if(oS.at(oS.size()-2).start == NULLSTART)
190//            oS.at(oS.size()-2).start = oS.back().start;
191//          else marker[oS.back().start] = 's';
192
193            oS[oS.size()-2].info = oS[oS.size()-2].info + oS.back().info;
194            if(oS[oS.size()-2].start == NULLSTART)
195              oS[oS.size()-2].start = oS.back().start;
196            else marker[oS.back().start] = 's';
197
198            oS.pop_back();
199          }
200
201          status[PRIOR] = OBJECT;
202        }
203
204        /*---------*/
205        if(currentMarker == 'f') status[PRIOR] = INCOMPLETE;
206
207        /*---------*/
208        if(currentMarker == 'F') {
209
210          status[PRIOR] = psS.back();
211          psS.pop_back();                    //POP PSSTACK ONTO PS
212
213          if( (status[CURRENT] == NONOBJECT) && (status[PRIOR] == COMPLETE) ){
214
215            if(oS.back().start == NULLSTART){
216              // The object is completed. If it is big enough, add to
217              // the end of the output list.         
218              if(oS.back().info.getSize() >= this->minSize){
219        //oS.back().info.calcParams(); // work out midpoints, fluxes etc
220                outputlist.push_back(oS.back().info);
221              }
222            }
223            else{
224              marker[ oS.back().end ] = 'F';
225              store[ oS.back().start ] = oS.back().info;
226            }
227
228            oS.pop_back();
229
230            status[PRIOR] = psS.back();
231            psS.pop_back();
232          }
233        }
234
235      } // end of PROCESSMARKER section ( if(currentMarker!=NULLMARKER) )
236
237      if (isObject){
238        oS.back().info.addPixel(posX,posY);
239      }
240      else{
241        //
242        // ----------------------------- END SEGMENT -------------------------
243        // If the current pixel is background and the previous pixel was an
244        // object, then finish the segment.
245        // If the prior status is COMPLETE, it's the end of the final segment
246        // of the object section.
247        // If not, it's end of the segment, but not necessarily the section.
248        //
249        if ( status[CURRENT] == OBJECT) {
250
251          status[CURRENT] = NONOBJECT;
252
253          if(status[PRIOR] != COMPLETE){
254            marker[posX] = 'f';
255            oS.back().end = posX;
256          }
257          else{
258            status[PRIOR] = psS.back();
259            psS.pop_back();                   //POP PSSTACK onto PS;
260            marker[posX] = 'F';
261            store[ oS.back().start ] = oS.back().info;
262            oS.pop_back();
263          }
264        }
265       
266      } //-> end of END SEGMENT else{ clause
267
268    }//-> end of loop over posX
269
270  }//-> end of loop over posY
271
272  // clean up and remove declared arrays
273  delete [] marker;
274  delete [] store;
275  delete [] status;
276
277  return outputlist;
278
279}
Note: See TracBrowser for help on using the repository browser.