casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageDecomposer.h
Go to the documentation of this file.
1 //# ImageDecomposer.h: decompose images into components
2 //# Copyright (C) 1994,1995,1996,1997,1998,1999,2000,2001,2002
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id: ImageDecomposer.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $
27 
28 #ifndef IMAGES_IMAGEDECOMPOSER_H
29 #define IMAGES_IMAGEDECOMPOSER_H
30 
31 #include <casa/iostream.h>
32 #include <casa/math.h>
33 #include <casa/aips.h>
34 #include <casa/Arrays/Vector.h>
35 #include <casa/Arrays/Matrix.h>
36 #include <casa/Containers/Block.h>
41 
42 
43 namespace casa { //# NAMESPACE CASA - BEGIN
44 
45 // <summary>
46 // A tool to separate a complex image into individual components.
47 // </summary>
48 //
49 // <use visibility=export>
50 //
51 // <reviewed reviewer="" date="" tests="tImageDecomposer.cc">
52 // </reviewed>
53 //
54 // <prerequisite>
55 // <li> <linkto class=casacore::CoordinateSystem>CoordinateSystem</linkto>
56 // <li> <linkto class=casacore::ImageInterface>ImageInterface</linkto>
57 // </prerequisite>
58 
59 // <etymology>
60 // It takes an image, and separates it into components.
61 // </etymology>
62 
63 // <synopsis>
64 // ImageDecomposer is an image decomposition tool that performs several tasks,
65 // with the end result being that a strongly blended image is separated into
66 // components - both in the sense that it determines the parameters for each
67 // component (assuming a Gaussian model) and that it physically assigns each
68 // pixel in the image to an individual object. The products of these two
69 // operations are called the component list and the component map,
70 // respectively. The fitting process (which determines the component list) and
71 // the pixel-decomposition process (which determines the component map) are
72 // designed to work cooperatively to increase the efficiency and accuracy of
73 // both, though each can operate without the other if necessary.
74 //
75 // The algorithm between the decomposition is based on the function clfind
76 // described in Williams et al 1994, which uses a contouring procedure whereby
77 // a closed contour designates a separate component. The program first
78 // separates the image into clearly distint 'regions' of blended emission, then
79 // contours each region to determine the areas constituting each component and
80 // passes this information on to the fitter, which determines the component
81 // list.
82 //
83 // The software is compatible with 2 and 3 dimensional images, but is not
84 // yet structured for higher dimensions.
85 // </synopsis>
86 
87 // <example>
88 // <srcblock>
89 // casacore::TempImage<casacore::Double> image;
90 // //(populate the image with data: see dImageDecomposer.cc)
91 // ImageDecomposer<casacore::Double> id(image);
92 // id.setDeblendOptions(0.3, 8);
93 // id.setFitOptions(0.4);
94 // id.decomposeImage();
95 // id.display();
96 // id.printComponents();
97 // </srcblock>
98 // </example>
99 //
100 // <motivation>
101 // </motivation>
102 //
103 // <note>
104 // </note>
105 //
106 // <todo asof="2002/07/23">
107 // <li> Generalize dimensionality
108 // <li> Use casacore::Lattice iterators in place of casacore::IPosition loops wherever possible
109 // <li> Speed up fitting by not sending every region pixel to the fitter
110 // <li> Send the completed componentmap to the user as an AIPS++ (casacore::Int?) Image
111 // <li> Return a ComponentList instead of a Matrix
112 // <li> Enable custom contouring at user level
113 // <li> Add progress meter
114 // <li> Numerous other improvements to make are documented in the code
115 // </todo>
116 
117 
118 
119 template <class T> class ImageDecomposer {
120 
121 public:
122 
123 // 'Special' flag values for pixels in the component map. An indeterminate
124 // pixel lies directly between two components and cannot be immediately
125 // assigned. A masked pixel is not inside the targeted region of the
126 // sub-componentmap and is not used in decomposition or fitting.
129  MASKED = -2
130  };
131 
132  ImageDecomposer() = delete;
133 
135 
136  ImageDecomposer(const ImageDecomposer<T>& other);
137 
138  ImageDecomposer<T> &operator=(const ImageDecomposer<T> &other) = delete;
139 
141 
142 // Tell the decomposer what image to decompose ("target image").
143 // Also resets the internal component map.
144 // void setImage (casacore::ImageInterface<T>& image);
145 
146 // Tells the program whether or not to use the contour-based deblender. If not,
147 // the program will instead perform a single thresholding followed by a
148 // local maximum scan before fitting.
149  void setDeblend(casacore::Bool deblendIt=true);
150 
151 // Specifies deblending options:
152 // <ul>
153 // <li> thresholdVal: noise cutoff level, used to distinguish source pixels
154 // from background pixels. Also, regions which are not blended above this
155 // value will be fit separately.
156 // <li> nContour: number of total contours to use in deblending regions.
157 // <li> minRange: the minimum number of contours necessary to distinguish
158 // an object as a separate component.
159 // <li> nAxis: paramater used to define whether or not two adjacent blocks
160 // of pixels are contiguous - see identifyRegions for details.
161 // </ul>
162 // See decomposeImage for more information on the deblending process.
163  void setDeblendOptions(T thresholdVal=0.1, casacore::uInt nContour=11,
164  casacore::Int minRange=2, casacore::Int nAxis=2);
165 
166 // Tells the program whether or not to perform fitting. If not, the component
167 // list will be dstermined by estimation from the values of the first and
168 // second order moments of each component.
169  void setFit(casacore::Bool fitIt=true);
170 
171 // Specifies fitting options:
172 // <ul>
173 // <li> maximumRMS: The maximum RMS residual value (after fitting) allowed to
174 // identify a fit as successful.
175 // <li> maxRetries: the maximum number of times the fit will be restarted in
176 // order to try to reach a successful result (convergent with
177 // RMS < maximumRMS). The default value of -1 tells the program
178 // to calculate a reasonable value automatically based on the complexity
179 // of the image.
180 // <li> maxIter: maximum number of iterations to spend on each fit.
181 // <li> convCriteria: criterion to establish convergence: see NonLinearFitLM.
182 // </ul>
183 // Additional information on these parameters can be found in FitGaussian.
184  void setFitOptions(T maximumRMS=0.1, casacore::Int maxRetries=-1, casacore::uInt maxIter=256,
185  T convCriteria=0.0001);
186 
187 
188 // The primary method of this class - executes the instructions stated in the
189 // options above by deblending and/or fitting to the image to generate
190 // the component map and/or component list.
191  void decomposeImage();
192 
193 
194 // Returns the number of regions found in the image. A 'region' as defined
195 // in this code is a subset of the image of contiguous pixels whose values
196 // are greater than the threshold value specified in decomposeImage. A region
197 // may contain one or more components.
198  casacore::uInt numRegions() const;
199 
200 // Returns the number of components found in the image. A 'component' as
201 // defined in this code is a source that can be described as a single Gaussian.
202 // This can only be determined after deblending.
204 
205 // Returns the shape of the component map.
206  casacore::IPosition shape() const;
207 
208 // Returns the length of a specific axis.
209  casacore::Int shape(casacore::uInt axis) const;
210 
211 // Returns true if the image has been thresholded (split up into regions.)
212  casacore::Bool isDerived() const;
213 
214 // Returns true if the image has been decomposed (split up into components.)
215  casacore::Bool isDecomposed() const;
216 
217 // Returns the component parameters as a Matrix. (Ideally, this should be
218 // a ComponentList.)
220 
221 // Currently does nothing; in the future should return the component map
222 // in a way that it can be seen by the user in AIPS++, preferably as a
223 // colorized image.
224  void componentMap() const;
225 
226 // Command-line text output functions.
227 // <group>
228  void display() const;
229  void displayContourMap(const casacore::Vector<T>& clevels) const;
230  void printComponents() const;
231 // </group>
232 // Boxes each region in the componentmap:
233 // blc is set to the lowest coordinate value in each region;
234 // trc is set to one above the highest coordinate value in each region.
236 private:
237  casacore::ImageInterface<T> *itsImagePtr;// Points to the target image.
238  casacore::Lattice<casacore::Int> *itsMapPtr; // The actual component map.
239  casacore::IPosition itsShape; // Component map shape
240  casacore::uInt itsDim; // Component map number of dimensions
241  casacore::uInt itsNRegions; // Number of distinct regions in component map
242  casacore::uInt itsNComponents; // Number of components that have been fitted
243  casacore::Matrix<T> itsList; // The component list (Gaussian parameters for
244  // each component.)
247  casacore::uInt itsNContour; // Decomposition options
248  casacore::Int itsMinRange; // IMPR: maybe use a struct?
250 
253  casacore::Int itsMaxRetries; // Fitting options
256 
257 
258 
259  void copyOptions(const ImageDecomposer<T>& other);
260 
261 // Makes sure a pair of IPositions is in the correct format for blc/trc, and
262 // corrects them if they are not.
264 
265 // Used as an N-dimensional interator. This should probably be replaced by
266 // LatticeIterators...?
267 // <group>
269  void decrement(casacore::IPosition& pos) const;
270 // </group>
271 
272 // Returns the component to which the specified cell belongs
273 // <group>
276  casacore::Int getCell(const casacore::IPosition& coord) const;
277 // </group>
278 
279 // Assigns the specified cell to the specified component
280 // <group>
283  void setCell(const casacore::IPosition& coord, casacore::Int sval);
284 // </group>
285 
286 // Semi-automatic way to set contour levels: at the given increment counting
287 // between mincon and maxcon.
288  casacore::Vector<T> autoContour(T minCon, T maxCon, T inc) const;
289 
290 // Linearly spaces contours between minvalue and just below the
291 // maximum value in the target region of the target image, and returns
292 // the contour values as a Vector.
293  casacore::Vector<T> autoContour(casacore::Int nContours=11, T minValue=0) const;
294 
295 // Nonlinear spacing option for contouring; spaces contours according to the
296 // function given. The domain of the function is 0 <-> ncontours-1; the
297 // range is automatically calibrated to be minvalue <-> maxvalue. The function
298 // should be nondecreasing in the domain such that each contour is greater
299 // than the last.
301  casacore::Int nContours = 11, T minValue = 0) const;
302 
303 
304 //Eliminates any regions whose corresponding values in killRegion are true
305 // by setting all pixel values in the componentmap set to that region to
306 // zero. Zero-oriented; there is an offset of one between the index in
307 // killRegion and the actual region in the componentmap.
308  void destroyRegions(const casacore::Vector<casacore::Bool>& killRegion);
309 
310 // Eliminates regions with no cells by replacing them with higher-numbered
311 // regions.
312  void renumberRegions();
313 
314 // Overlays a smaller map onto an empty region of a larger map,
315 // and adds submap component list to main component list.
316 // The user should exercise caution with this function and synthesize submaps
317 // only into regions of the main map that are truly empty (0), as no blending
318 // is assumed between different maps.
319  void synthesize(const ImageDecomposer<T>& subdecomposer, casacore::IPosition blc);
320 
321 // Set all elements in the component map to zero and clear the component list.
322  void zero();
323 
324 // Set all nonmasked elements in the component map to zero and clear the
325 // component list.
326  void clear();
327 
328 
329 
330 // Finds the greatest value inside the specified rectangular area of the
331 // target image.
332 // <group>
334  void findAreaGlobalMax(T& maxval, casacore::IPosition& maxvalpos,
340  casacore::Int naxis) const;
341 // </group>
342 
343 // Finds all local maxima inside the specified rectangular area of the
344 // target image.
345 // <group>
349 // </group>
350 
351 // Finds the maximum value of the target image in each region of the
352 // componentmap.
353 // <group>
356  casacore::Block<casacore::IPosition>& maxvalpos) const;
357 // </group>
358 
359 
360 // Finds all local maxima of the target image inside the specifed region
361 // of the componentmap.
362 // <group>
365  casacore::Int nregion, casacore::Int naxis) const;
366 // </group>
367 
368 
369 //Compares specified pixel to adjacent pixels to determine if it is
370 //greatest in local pixel block.
371 //2D:
372 //naxis = 1: compare to 4 adjacent pixels (axes only)
373 //naxis = 2: compare to 8 adjacent pixels (axes and diagonals)
374 //3D:
375 //naxis = 1: compare to 6 adjacent pixels (axes only)
376 //naxis = 2: compare to 18 adjacent pixels (axes and 2-axis diagonals)
377 //naxis = 3: compare to 26 adjacent pixels (axes and 2/3-axis diagonals)
378 // <group>
382 // </group>
383 
384 // Finds a rough estimate of the width of each component by scanning to find
385 // the full width at quarter maximum.
386 // Requires the location of each component.
387 // This function is mostly obsolete, and is only used when the contour
388 // deblender is off (since the component map is necessary to determine the
389 // moments).
391  const casacore::Block<casacore::IPosition>& maxvalpos) const;
392 
393 // Calculates the 0th-2nd order moments of a region.
395 
396 
397 // Performs a single threshold scan on the image. In other words,
398 // identifies all contigous blocks of pixels in the target image above the
399 // threshold value thrval, assigning each unique block to an integer,
400 // starting at one. All pixels with target image values below thrval are set
401 // to zero.
402  casacore::uInt identifyRegions(T thrval, casacore::Int naxis=2);
403 
404 // Performs the contour decomposition on a blended image to generate a
405 // component map that can detect components blended above any threshold(s),
406 // by performing threshold scans at each contour level and recognizing
407 // as individual any components that are distinct above any such level.
408  void deblendRegions(const casacore::Vector<T>& contours, casacore::Int minRange=1, casacore::Int naxis=2);
409 
410 // Retrieves the target image's value at the given location.
411 // <group>
412  T getImageVal(casacore::IPosition coord) const;
415 // </group>
416 
417 // Retrieves the number of the highest contour with a value less then the
418 // target image's value at the given location.
419 // <group>
423  casacore::Int getContourVal(T val, const casacore::Vector<T>& clevels) const;
424 // </group>
425 
426 // Fits multiple gaussians to a single region. First performs a local
427 // maximum scan to estimate the number of components in the region.
429 
430 // Fits gaussians to an image; multiple gaussians per region in the component
431 // map. The regions are fit sequentially and independently, so this function
432 // can be used on the main image. If the map is not yet thresholded, will fit
433 // to the entire image as if it were a single composite object, which will be
434 // very slow.
435  void fitRegions();
436 
437 // Fits gaussians to an image; one gaussian per region in the pmap.
438 // This function is intended to be used only by ImageDecomposer on its
439 // intermediary subimages; using it at higher level will execute a full
440 // gaussian fit on the main image and will be extremely slow. Every
441 // nonflagged object pixel in the image is used in fitting.
442 
443 // If the deblended flag is true, the function will treat each region as
444 // an individual component and will fit that many gaussians to the image
445  void fitComponents();
446 
447 // Estimate the component parameters based on moments calculated using
448 // the component map.
450 
451 // Fits the specified number of 3D gaussians to the data, and returns
452 // solution in image (world) coordinates. Essentially just an interface
453 // for FitGaussian.
454 // <group>
455  casacore::Matrix<T> fitGauss(const casacore::Matrix<T>& positions, const casacore::Vector<T>& dataValues,
456  const casacore::Matrix<T>& initestimate) const;
457 
458 // </group>
459 
460 };
461 
462 } //# NAMESPACE CASA - END
463 
464 #ifndef CASACORE_NO_AUTO_TEMPLATES
465 #include <imageanalysis/ImageAnalysis/ImageDecomposer.tcc>
466 #endif //# CASACORE_NO_AUTO_TEMPLATES
467 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
casacore::Lattice< casacore::Int > * itsMapPtr
A 1-D Specialization of the Array class.
casacore::Int getCell(casacore::Int x, casacore::Int y) const
Returns the component to which the specified cell belongs.
int Int
Definition: aipstype.h:50
void displayContourMap(const casacore::Vector< T > &clevels) const
casacore::Vector< T > autoContour(T minCon, T maxCon, T inc) const
Semi-automatic way to set contour levels: at the given increment counting between mincon and maxcon...
T getImageVal(casacore::IPosition coord) const
Retrieves the target image&#39;s value at the given location.
void printComponents() const
void deblendRegions(const casacore::Vector< T > &contours, casacore::Int minRange=1, casacore::Int naxis=2)
Performs the contour decomposition on a blended image to generate a component map that can detect com...
void zero()
Set all elements in the component map to zero and clear the component list.
ImageDecomposer< T > & operator=(const ImageDecomposer< T > &other)=delete
casacore::uInt identifyRegions(T thrval, casacore::Int naxis=2)
Performs a single threshold scan on the image.
casacore::Matrix< T > fitRegion(casacore::Int region)
Fits multiple gaussians to a single region.
casacore::Vector< T > findRegionLocalMax(casacore::Int nregion, casacore::Int naxis) const
Finds all local maxima of the target image inside the specifed region of the componentmap.
casacore::uInt numComponents() const
Returns the number of components found in the image.
void clear()
Set all nonmasked elements in the component map to zero and clear the component list.
casacore::Matrix< T > estimateComponents()
Estimate the component parameters based on moments calculated using the component map...
casacore::Matrix< T > fitGauss(const casacore::Matrix< T > &positions, const casacore::Vector< T > &dataValues, const casacore::Matrix< T > &initestimate) const
Fits the specified number of 3D gaussians to the data, and returns solution in image (world) coordina...
casacore::uInt itsNRegions
casacore::ImageInterface< T > * itsImagePtr
A 2-D Specialization of the Array class.
casacore::Vector< T > findAllRegionGlobalMax() const
Finds the maximum value of the target image in each region of the componentmap.
void setFit(casacore::Bool fitIt=true)
Tells the program whether or not to perform fitting.
casacore::uInt itsMaxIter
void estimateComponentWidths(casacore::Matrix< T > &width, const casacore::Block< casacore::IPosition > &maxvalpos) const
Finds a rough estimate of the width of each component by scanning to find the full width at quarter m...
casacore::Int itsMinRange
void setDeblendOptions(T thresholdVal=0.1, casacore::uInt nContour=11, casacore::Int minRange=2, casacore::Int nAxis=2)
Specifies deblending options:
A tool to separate a complex image into individual components.
casacore::Bool increment(casacore::IPosition &pos, const casacore::IPosition &shape) const
Used as an N-dimensional interator.
A base class for astronomical images.
void componentMap() const
Currently does nothing; in the future should return the component map in a way that it can be seen by...
void correctBlcTrc(casacore::IPosition &blc, casacore::IPosition &trc) const
Makes sure a pair of IPositions is in the correct format for blc/trc, and corrects them if they are n...
casacore::Array< T > calculateMoments(casacore::Int region) const
Calculates the 0th-2nd order moments of a region.
casacore::Bool itsDeblendIt
each component.)
casacore::Int getContourVal(casacore::IPosition coord, const casacore::Vector< T > &clevels) const
Retrieves the number of the highest contour with a value less then the target image&#39;s value at the gi...
void destroyRegions(const casacore::Vector< casacore::Bool > &killRegion)
Eliminates any regions whose corresponding values in killRegion are true by setting all pixel values ...
void boundRegions(casacore::Block< casacore::IPosition > &blc, casacore::Block< casacore::IPosition > &trc)
Boxes each region in the componentmap: blc is set to the lowest coordinate value in each region; trc ...
casacore::uInt numRegions() const
Returns the number of regions found in the image.
casacore::IPosition itsShape
void setCell(casacore::Int x, casacore::Int y, casacore::Int sval)
Assigns the specified cell to the specified component.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
casacore::Int itsMaxRetries
void fitRegions()
Fits gaussians to an image; multiple gaussians per region in the component map.
casacore::Bool isDerived() const
Returns true if the image has been thresholded (split up into regions.)
void decrement(casacore::IPosition &pos) const
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
T findAreaGlobalMax(casacore::IPosition blc, casacore::IPosition trc) const
Finds the greatest value inside the specified rectangular area of the target image.
void setDeblend(casacore::Bool deblendIt=true)
Tell the decomposer what image to decompose (&quot;target image&quot;).
casacore::Matrix< T > componentList() const
Returns the component parameters as a Matrix.
casacore::Matrix< T > itsList
casacore::Bool isLocalMax(const casacore::IPosition &pos, casacore::Int naxis) const
Compares specified pixel to adjacent pixels to determine if it is greatest in local pixel block...
casacore::Vector< T > findAreaLocalMax(casacore::IPosition blc, casacore::IPosition trc, casacore::Int naxis) const
Finds all local maxima inside the specified rectangular area of the target image. ...
void decomposeImage()
The primary method of this class - executes the instructions stated in the options above by deblendin...
void copyOptions(const ImageDecomposer< T > &other)
casacore::Bool itsFitIt
void synthesize(const ImageDecomposer< T > &subdecomposer, casacore::IPosition blc)
Overlays a smaller map onto an empty region of a larger map, and adds submap component list to main c...
casacore::Bool isDecomposed() const
Returns true if the image has been decomposed (split up into components.)
void fitComponents()
Fits gaussians to an image; one gaussian per region in the pmap.
void display() const
Command-line text output functions.
void setFitOptions(T maximumRMS=0.1, casacore::Int maxRetries=-1, casacore::uInt maxIter=256, T convCriteria=0.0001)
Specifies fitting options:
casacore::IPosition shape() const
Returns the shape of the component map.
void renumberRegions()
Eliminates regions with no cells by replacing them with higher-numbered regions.
casacore::uInt itsNComponents
casacore::uInt itsNContour
componentValues
&#39;Special&#39; flag values for pixels in the component map.
unsigned int uInt
Definition: aipstype.h:51